StringPrototype.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibJS/Heap/Heap.h>
  29. #include <LibJS/Interpreter.h>
  30. #include <LibJS/Runtime/Error.h>
  31. #include <LibJS/Runtime/GlobalObject.h>
  32. #include <LibJS/Runtime/PrimitiveString.h>
  33. #include <LibJS/Runtime/StringObject.h>
  34. #include <LibJS/Runtime/StringPrototype.h>
  35. #include <LibJS/Runtime/Value.h>
  36. #include <string.h>
  37. namespace JS {
  38. StringPrototype::StringPrototype()
  39. : StringObject(*js_string(interpreter(), String::empty()), *interpreter().global_object().object_prototype())
  40. {
  41. u8 attr = Attribute::Writable | Attribute::Configurable;
  42. put_native_property("length", length_getter, nullptr, 0);
  43. put_native_function("charAt", char_at, 1, attr);
  44. put_native_function("repeat", repeat, 1, attr);
  45. put_native_function("startsWith", starts_with, 1, attr);
  46. put_native_function("indexOf", index_of, 1, attr);
  47. put_native_function("toLowerCase", to_lowercase, 0, attr);
  48. put_native_function("toUpperCase", to_uppercase, 0, attr);
  49. put_native_function("toString", to_string, 0, attr);
  50. put_native_function("padStart", pad_start, 1, attr);
  51. put_native_function("padEnd", pad_end, 1, attr);
  52. put_native_function("trim", trim, 0, attr);
  53. put_native_function("trimStart", trim_start, 0, attr);
  54. put_native_function("trimEnd", trim_end, 0, attr);
  55. put_native_function("concat", concat, 1, attr);
  56. put_native_function("substring", substring, 2, attr);
  57. put_native_function("includes", includes, 1, attr);
  58. }
  59. StringPrototype::~StringPrototype()
  60. {
  61. }
  62. Value StringPrototype::char_at(Interpreter& interpreter)
  63. {
  64. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  65. if (!this_object)
  66. return {};
  67. i32 index = 0;
  68. if (interpreter.argument_count())
  69. index = interpreter.argument(0).to_i32();
  70. ASSERT(this_object->is_string_object());
  71. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string().string();
  72. if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
  73. return js_string(interpreter, String::empty());
  74. return js_string(interpreter, underlying_string.substring(index, 1));
  75. }
  76. Value StringPrototype::repeat(Interpreter& interpreter)
  77. {
  78. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  79. if (!this_object)
  80. return {};
  81. ASSERT(this_object->is_string_object());
  82. if (!interpreter.argument_count())
  83. return js_string(interpreter, String::empty());
  84. if (interpreter.argument(0).to_double() < 0)
  85. return interpreter.throw_exception<RangeError>("repeat count must be a positive number");
  86. if (interpreter.argument(0).to_number().is_infinity())
  87. return interpreter.throw_exception<RangeError>("repeat count must be a finite number");
  88. auto count = interpreter.argument(0).to_i32();
  89. auto& string_object = static_cast<const StringObject&>(*this_object);
  90. StringBuilder builder;
  91. for (i32 i = 0; i < count; ++i)
  92. builder.append(string_object.primitive_string().string());
  93. return js_string(interpreter, builder.to_string());
  94. }
  95. Value StringPrototype::starts_with(Interpreter& interpreter)
  96. {
  97. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  98. if (!this_object)
  99. return {};
  100. if (!interpreter.argument_count())
  101. return Value(false);
  102. auto search_string = interpreter.argument(0).to_string();
  103. auto search_string_length = static_cast<i32>(search_string.length());
  104. i32 position = 0;
  105. if (interpreter.argument_count() > 1) {
  106. auto number = interpreter.argument(1).to_number();
  107. if (!number.is_nan())
  108. position = number.to_i32();
  109. }
  110. ASSERT(this_object->is_string_object());
  111. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string().string();
  112. auto underlying_string_length = static_cast<i32>(underlying_string.length());
  113. auto start = min(max(position, 0), underlying_string_length);
  114. if (start + search_string_length > underlying_string_length)
  115. return Value(false);
  116. if (search_string_length == 0)
  117. return Value(true);
  118. return Value(underlying_string.substring(start, search_string_length) == search_string);
  119. }
  120. Value StringPrototype::index_of(Interpreter& interpreter)
  121. {
  122. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  123. if (!this_object)
  124. return {};
  125. if (!this_object->is_string_object())
  126. return interpreter.throw_exception<TypeError>("Not a String object");
  127. Value needle_value = js_undefined();
  128. if (interpreter.argument_count() >= 1)
  129. needle_value = interpreter.argument(0);
  130. auto needle = needle_value.to_string();
  131. auto haystack = static_cast<const StringObject*>(this_object)->primitive_string().string();
  132. return Value((i32)haystack.index_of(needle).value_or(-1));
  133. }
  134. static StringObject* string_object_from(Interpreter& interpreter)
  135. {
  136. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  137. if (!this_object)
  138. return nullptr;
  139. if (!this_object->is_string_object()) {
  140. interpreter.throw_exception<TypeError>("Not a String object");
  141. return nullptr;
  142. }
  143. return static_cast<StringObject*>(this_object);
  144. }
  145. Value StringPrototype::to_lowercase(Interpreter& interpreter)
  146. {
  147. auto* string_object = string_object_from(interpreter);
  148. if (!string_object)
  149. return {};
  150. return js_string(interpreter, string_object->primitive_string().string().to_lowercase());
  151. }
  152. Value StringPrototype::to_uppercase(Interpreter& interpreter)
  153. {
  154. auto* string_object = string_object_from(interpreter);
  155. if (!string_object)
  156. return {};
  157. return js_string(interpreter, string_object->primitive_string().string().to_uppercase());
  158. }
  159. Value StringPrototype::length_getter(Interpreter& interpreter)
  160. {
  161. auto* string_object = string_object_from(interpreter);
  162. if (!string_object)
  163. return {};
  164. return Value((i32)string_object->primitive_string().string().length());
  165. }
  166. Value StringPrototype::to_string(Interpreter& interpreter)
  167. {
  168. auto* string_object = string_object_from(interpreter);
  169. if (!string_object)
  170. return {};
  171. return js_string(interpreter, string_object->primitive_string().string());
  172. }
  173. enum class PadPlacement {
  174. Start,
  175. End,
  176. };
  177. static Value pad_string(Interpreter& interpreter, Object* object, PadPlacement placement)
  178. {
  179. auto string = object->to_string().as_string().string();
  180. if (interpreter.argument(0).to_number().is_nan()
  181. || interpreter.argument(0).to_number().is_undefined()
  182. || interpreter.argument(0).to_number().to_i32() < 0) {
  183. return js_string(interpreter, string);
  184. }
  185. auto max_length = static_cast<size_t>(interpreter.argument(0).to_i32());
  186. if (max_length <= string.length())
  187. return js_string(interpreter, string);
  188. String fill_string = " ";
  189. if (!interpreter.argument(1).is_undefined())
  190. fill_string = interpreter.argument(1).to_string();
  191. if (fill_string.is_empty())
  192. return js_string(interpreter, string);
  193. auto fill_length = max_length - string.length();
  194. StringBuilder filler_builder;
  195. while (filler_builder.length() < fill_length)
  196. filler_builder.append(fill_string);
  197. auto filler = filler_builder.build().substring(0, fill_length);
  198. if (placement == PadPlacement::Start)
  199. return js_string(interpreter, String::format("%s%s", filler.characters(), string.characters()));
  200. return js_string(interpreter, String::format("%s%s", string.characters(), filler.characters()));
  201. }
  202. Value StringPrototype::pad_start(Interpreter& interpreter)
  203. {
  204. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  205. if (!this_object)
  206. return {};
  207. return pad_string(interpreter, this_object, PadPlacement::Start);
  208. }
  209. Value StringPrototype::pad_end(Interpreter& interpreter)
  210. {
  211. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  212. if (!this_object)
  213. return {};
  214. return pad_string(interpreter, this_object, PadPlacement::End);
  215. }
  216. enum class TrimMode {
  217. Left,
  218. Right,
  219. Both
  220. };
  221. static Value trim_string(Interpreter& interpreter, const Object& object, TrimMode mode)
  222. {
  223. auto& string = object.to_string().as_string().string();
  224. size_t substring_start = 0;
  225. size_t substring_length = string.length();
  226. auto is_white_space_character = [](char character) -> bool {
  227. return character == 0x9 || character == 0xa || character == 0xb || character == 0xc || character == 0xd || character == 0x20;
  228. };
  229. if (mode == TrimMode::Left || mode == TrimMode::Both) {
  230. for (size_t i = 0; i < string.length(); ++i) {
  231. if (!is_white_space_character(string[i])) {
  232. substring_start = i;
  233. substring_length -= substring_start;
  234. break;
  235. }
  236. }
  237. }
  238. if (substring_length == 0)
  239. return js_string(interpreter, String(""));
  240. if (mode == TrimMode::Right || mode == TrimMode::Both) {
  241. size_t count = 0;
  242. for (size_t i = string.length() - 1; i > 0; --i) {
  243. if (!is_white_space_character(string[i])) {
  244. substring_length -= count;
  245. break;
  246. }
  247. count++;
  248. }
  249. }
  250. return js_string(interpreter, string.substring(substring_start, substring_length));
  251. }
  252. Value StringPrototype::trim(Interpreter& interpreter)
  253. {
  254. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  255. if (!this_object)
  256. return {};
  257. return trim_string(interpreter, *this_object, TrimMode::Both);
  258. }
  259. Value StringPrototype::trim_start(Interpreter& interpreter)
  260. {
  261. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  262. if (!this_object)
  263. return {};
  264. return trim_string(interpreter, *this_object, TrimMode::Left);
  265. }
  266. Value StringPrototype::trim_end(Interpreter& interpreter)
  267. {
  268. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  269. if (!this_object)
  270. return {};
  271. return trim_string(interpreter, *this_object, TrimMode::Right);
  272. }
  273. Value StringPrototype::concat(Interpreter& interpreter)
  274. {
  275. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  276. if (!this_object)
  277. return {};
  278. auto& string = this_object->to_string().as_string().string();
  279. StringBuilder builder;
  280. builder.append(string);
  281. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  282. auto string_argument = interpreter.argument(i).to_string();
  283. builder.append(string_argument);
  284. }
  285. return js_string(interpreter, builder.to_string());
  286. }
  287. Value StringPrototype::substring(Interpreter& interpreter)
  288. {
  289. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  290. if (!this_object)
  291. return {};
  292. auto& string = this_object->to_string().as_string().string();
  293. if (interpreter.argument_count() == 0)
  294. return js_string(interpreter, string);
  295. i32 string_length = static_cast<i32>(string.length());
  296. i32 index_start = interpreter.argument(0).to_number().to_i32();
  297. i32 index_end = string_length;
  298. if (index_start > string_length)
  299. index_start = string_length;
  300. else if (index_start < 0)
  301. index_start = 0;
  302. if (interpreter.argument_count() >= 2) {
  303. index_end = interpreter.argument(1).to_number().to_i32();
  304. if (index_end > string_length)
  305. index_end = string_length;
  306. else if (index_end < 0)
  307. index_end = 0;
  308. }
  309. if (index_start == index_end)
  310. return js_string(interpreter, String(""));
  311. if (index_start > index_end) {
  312. if (interpreter.argument_count() == 1) {
  313. return js_string(interpreter, String(""));
  314. } else {
  315. i32 temp_index_start = index_start;
  316. index_start = index_end;
  317. index_end = temp_index_start;
  318. }
  319. }
  320. auto part_length = index_end - index_start;
  321. auto string_part = string.substring(index_start, part_length);
  322. return js_string(interpreter, string_part);
  323. }
  324. Value StringPrototype::includes(Interpreter& interpreter)
  325. {
  326. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  327. if (!this_object)
  328. return {};
  329. auto& string = this_object->to_string().as_string().string();
  330. auto search_string = interpreter.argument(0).to_string();
  331. i32 position = 0;
  332. if (interpreter.argument_count() >= 2) {
  333. position = interpreter.argument(1).to_i32();
  334. if (position >= static_cast<i32>(string.length()))
  335. return Value(false);
  336. if (position < 0)
  337. position = 0;
  338. }
  339. if (position == 0)
  340. return Value(string.contains(search_string));
  341. auto substring_length = string.length() - position;
  342. auto substring_search = string.substring(position, substring_length);
  343. return Value(substring_search.contains(search_string));
  344. }
  345. }