diff --git a/boa_engine/src/builtins/string/mod.rs b/boa_engine/src/builtins/string/mod.rs index 3a66409c47d..a62a6c5ff2f 100644 --- a/boa_engine/src/builtins/string/mod.rs +++ b/boa_engine/src/builtins/string/mod.rs @@ -41,6 +41,7 @@ pub(crate) enum Placement { pub(crate) fn code_point_at(string: &JsString, position: usize) -> (u32, u8, bool) { let mut encoded = string.encode_utf16(); + let size = encoded.clone().count(); let first = encoded .nth(position) @@ -49,8 +50,6 @@ pub(crate) fn code_point_at(string: &JsString, position: usize) -> (u32, u8, boo return (u32::from(first), 1, false); } - let size = encoded.clone().count(); - if is_trailing_surrogate(first) || position + 1 == size { return (u32::from(first), 1, true); } @@ -688,7 +687,7 @@ impl String { // 4. If n < 0 or n is +∞, throw a RangeError exception. _ => context.throw_range_error( "repeat count must be a positive finite number \ - that doesn't overflow the maximum string length", + that doesn't overflow the maximum string length (2^32 - 1)", ), } } @@ -1768,7 +1767,7 @@ impl String { let substring_utf16: Vec = string .encode_utf16() .skip(int_start) - .take(int_start - int_end) + .take(int_end - int_start) .collect(); let substring = StdString::from_utf16_lossy(&substring_utf16); diff --git a/boa_engine/src/builtins/string/tests.rs b/boa_engine/src/builtins/string/tests.rs index c3118869f29..a8f3b2ea809 100644 --- a/boa_engine/src/builtins/string/tests.rs +++ b/boa_engine/src/builtins/string/tests.rs @@ -147,7 +147,8 @@ fn repeat_throws_when_count_is_negative() { } "# ), - "\"RangeError: repeat count cannot be a negative number\"" + "\"RangeError: repeat count must be a positive finite number \ + that doesn't overflow the maximum string length (2^32 - 1)\"" ); } @@ -166,7 +167,8 @@ fn repeat_throws_when_count_is_infinity() { } "# ), - "\"RangeError: repeat count cannot be infinity\"" + "\"RangeError: repeat count must be a positive finite number \ + that doesn't overflow the maximum string length (2^32 - 1)\"" ); } @@ -185,7 +187,8 @@ fn repeat_throws_when_count_overflows_max_length() { } "# ), - "\"RangeError: repeat count must not overflow maximum string length\"" + "\"RangeError: repeat count must be a positive finite number \ + that doesn't overflow the maximum string length (2^32 - 1)\"" ); }