diff options
author | Linus Groh <mail@linusgroh.de> | 2020-05-17 15:19:48 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-17 16:23:48 +0200 |
commit | 71ec72bddf72483de702afed6721504dcbd6a3d4 (patch) | |
tree | 6647a41497317728654877b7cf909af61ca04a57 /Libraries | |
parent | 73eef31f49f6022c5a219c4c19ecaed9ac77a833 (diff) | |
download | serenity-71ec72bddf72483de702afed6721504dcbd6a3d4.zip |
LibJS: Simplify various StringPrototype functions
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Runtime/StringPrototype.cpp | 93 |
1 files changed, 31 insertions, 62 deletions
diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index 0062a22263..bc54c5823d 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -113,9 +113,9 @@ Value StringPrototype::repeat(Interpreter& interpreter) return interpreter.throw_exception<RangeError>("repeat count must be a positive number"); if (count_value.is_infinity()) return interpreter.throw_exception<RangeError>("repeat count must be a finite number"); - auto count = count_value.to_i32(); + auto count = count_value.to_size_t(); StringBuilder builder; - for (i32 i = 0; i < count; ++i) + for (size_t i = 0; i < count; ++i) builder.append(string); return js_string(interpreter, builder.to_string()); } @@ -130,15 +130,14 @@ Value StringPrototype::starts_with(Interpreter& interpreter) auto search_string = interpreter.argument(0).to_string(interpreter); if (interpreter.exception()) return {}; - auto search_string_length = static_cast<i32>(search_string.length()); - i32 position = 0; + auto string_length = string.length(); + auto search_string_length = search_string.length(); + size_t start = 0; if (interpreter.argument_count() > 1) { auto number = interpreter.argument(1).to_number(); if (!number.is_nan()) - position = number.to_i32(); + start = min(number.to_size_t(), string_length); } - auto string_length = static_cast<i32>(string.length()); - auto start = min(max(position, 0), string_length); if (start + search_string_length > string_length) return Value(false); if (search_string_length == 0) @@ -151,10 +150,7 @@ Value StringPrototype::index_of(Interpreter& interpreter) auto string = string_from(interpreter); if (string.is_null()) return {}; - Value needle_value = js_undefined(); - if (interpreter.argument_count() >= 1) - needle_value = interpreter.argument(0); - auto needle = needle_value.to_string(interpreter); + auto needle = interpreter.argument(0).to_string(interpreter); if (interpreter.exception()) return {}; return Value((i32)string.index_of(needle).value_or(-1)); @@ -199,11 +195,7 @@ enum class PadPlacement { static Value pad_string(Interpreter& interpreter, const String& string, PadPlacement placement) { - auto max_length_value = interpreter.argument(0).to_number(); - if (max_length_value.is_nan() || max_length_value.is_undefined() || max_length_value.as_double() < 0) - return js_string(interpreter, string); - - auto max_length = static_cast<size_t>(max_length_value.to_i32()); + auto max_length = interpreter.argument(0).to_size_t(); if (max_length <= string.length()) return js_string(interpreter, string); @@ -212,9 +204,9 @@ static Value pad_string(Interpreter& interpreter, const String& string, PadPlace fill_string = interpreter.argument(1).to_string(interpreter); if (interpreter.exception()) return {}; + if (fill_string.is_empty()) + return js_string(interpreter, string); } - if (fill_string.is_empty()) - return js_string(interpreter, string); auto fill_length = max_length - string.length(); @@ -292,35 +284,22 @@ Value StringPrototype::substring(Interpreter& interpreter) if (interpreter.argument_count() == 0) return js_string(interpreter, string); - i32 string_length = static_cast<i32>(string.length()); - i32 index_start = interpreter.argument(0).to_number().to_i32(); - i32 index_end = string_length; - - if (index_start > string_length) - index_start = string_length; - else if (index_start < 0) - index_start = 0; + auto string_length = string.length(); + auto index_start = min(interpreter.argument(0).to_size_t(), string_length); + auto index_end = string_length; - if (interpreter.argument_count() >= 2) { - index_end = interpreter.argument(1).to_number().to_i32(); - - if (index_end > string_length) - index_end = string_length; - else if (index_end < 0) - index_end = 0; - } + if (interpreter.argument_count() >= 2) + index_end = min(interpreter.argument(1).to_size_t(), string_length); if (index_start == index_end) return js_string(interpreter, String("")); if (index_start > index_end) { - if (interpreter.argument_count() == 1) { + if (interpreter.argument_count() == 1) return js_string(interpreter, String("")); - } else { - i32 temp_index_start = index_start; - index_start = index_end; - index_end = temp_index_start; - } + auto temp_index_start = index_start; + index_start = index_end; + index_end = temp_index_start; } auto part_length = index_end - index_start; @@ -336,16 +315,12 @@ Value StringPrototype::includes(Interpreter& interpreter) auto search_string = interpreter.argument(0).to_string(interpreter); if (interpreter.exception()) return {}; - i32 position = 0; + size_t position = 0; if (interpreter.argument_count() >= 2) { - position = interpreter.argument(1).to_i32(); - - if (position >= static_cast<i32>(string.length())) + position = interpreter.argument(1).to_size_t(); + if (position >= string.length()) return Value(false); - - if (position < 0) - position = 0; } if (position == 0) @@ -365,9 +340,9 @@ Value StringPrototype::slice(Interpreter& interpreter) if (interpreter.argument_count() == 0) return js_string(interpreter, string); - i32 string_length = static_cast<i32>(string.length()); - i32 index_start = interpreter.argument(0).to_i32(); - i32 index_end = string_length; + auto string_length = static_cast<i32>(string.length()); + auto index_start = interpreter.argument(0).to_i32(); + auto index_end = string_length; auto negative_min_index = -(string_length - 1); if (index_start < negative_min_index) @@ -410,21 +385,15 @@ Value StringPrototype::last_index_of(Interpreter& interpreter) if (search_string.length() > string.length()) return Value(-1); - ssize_t max_index = string.length() - search_string.length(); - ssize_t from_index = max_index; - if (interpreter.argument_count() >= 2) { - from_index = static_cast<ssize_t>(interpreter.argument(1).to_i32()); - - if (from_index < 0) - from_index = 0; - if (from_index > max_index) - from_index = max_index; - } + auto max_index = string.length() - search_string.length(); + auto from_index = max_index; + if (interpreter.argument_count() >= 2) + from_index = min(interpreter.argument(1).to_size_t(), max_index); - for (ssize_t i = from_index; i >= 0; --i) { + for (i32 i = from_index; i >= 0; --i) { auto part_view = string.substring_view(i, search_string.length()); if (part_view == search_string) - return Value((i32)i); + return Value(i); } return Value(-1); |