StringPrototype.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. put_native_function("slice", slice, 2, attr);
  59. }
  60. StringPrototype::~StringPrototype()
  61. {
  62. }
  63. Value StringPrototype::char_at(Interpreter& interpreter)
  64. {
  65. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  66. if (!this_object)
  67. return {};
  68. i32 index = 0;
  69. if (interpreter.argument_count())
  70. index = interpreter.argument(0).to_i32();
  71. ASSERT(this_object->is_string_object());
  72. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string().string();
  73. if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
  74. return js_string(interpreter, String::empty());
  75. return js_string(interpreter, underlying_string.substring(index, 1));
  76. }
  77. Value StringPrototype::repeat(Interpreter& interpreter)
  78. {
  79. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  80. if (!this_object)
  81. return {};
  82. ASSERT(this_object->is_string_object());
  83. if (!interpreter.argument_count())
  84. return js_string(interpreter, String::empty());
  85. if (interpreter.argument(0).to_double() < 0)
  86. return interpreter.throw_exception<RangeError>("repeat count must be a positive number");
  87. if (interpreter.argument(0).to_number().is_infinity())
  88. return interpreter.throw_exception<RangeError>("repeat count must be a finite number");
  89. auto count = interpreter.argument(0).to_i32();
  90. auto& string_object = static_cast<const StringObject&>(*this_object);
  91. StringBuilder builder;
  92. for (i32 i = 0; i < count; ++i)
  93. builder.append(string_object.primitive_string().string());
  94. return js_string(interpreter, builder.to_string());
  95. }
  96. Value StringPrototype::starts_with(Interpreter& interpreter)
  97. {
  98. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  99. if (!this_object)
  100. return {};
  101. if (!interpreter.argument_count())
  102. return Value(false);
  103. auto search_string = interpreter.argument(0).to_string();
  104. auto search_string_length = static_cast<i32>(search_string.length());
  105. i32 position = 0;
  106. if (interpreter.argument_count() > 1) {
  107. auto number = interpreter.argument(1).to_number();
  108. if (!number.is_nan())
  109. position = number.to_i32();
  110. }
  111. ASSERT(this_object->is_string_object());
  112. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string().string();
  113. auto underlying_string_length = static_cast<i32>(underlying_string.length());
  114. auto start = min(max(position, 0), underlying_string_length);
  115. if (start + search_string_length > underlying_string_length)
  116. return Value(false);
  117. if (search_string_length == 0)
  118. return Value(true);
  119. return Value(underlying_string.substring(start, search_string_length) == search_string);
  120. }
  121. Value StringPrototype::index_of(Interpreter& interpreter)
  122. {
  123. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  124. if (!this_object)
  125. return {};
  126. if (!this_object->is_string_object())
  127. return interpreter.throw_exception<TypeError>("Not a String object");
  128. Value needle_value = js_undefined();
  129. if (interpreter.argument_count() >= 1)
  130. needle_value = interpreter.argument(0);
  131. auto needle = needle_value.to_string();
  132. auto haystack = static_cast<const StringObject*>(this_object)->primitive_string().string();
  133. return Value((i32)haystack.index_of(needle).value_or(-1));
  134. }
  135. static StringObject* string_object_from(Interpreter& interpreter)
  136. {
  137. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  138. if (!this_object)
  139. return nullptr;
  140. if (!this_object->is_string_object()) {
  141. interpreter.throw_exception<TypeError>("Not a String object");
  142. return nullptr;
  143. }
  144. return static_cast<StringObject*>(this_object);
  145. }
  146. Value StringPrototype::to_lowercase(Interpreter& interpreter)
  147. {
  148. auto* string_object = string_object_from(interpreter);
  149. if (!string_object)
  150. return {};
  151. return js_string(interpreter, string_object->primitive_string().string().to_lowercase());
  152. }
  153. Value StringPrototype::to_uppercase(Interpreter& interpreter)
  154. {
  155. auto* string_object = string_object_from(interpreter);
  156. if (!string_object)
  157. return {};
  158. return js_string(interpreter, string_object->primitive_string().string().to_uppercase());
  159. }
  160. Value StringPrototype::length_getter(Interpreter& interpreter)
  161. {
  162. auto* string_object = string_object_from(interpreter);
  163. if (!string_object)
  164. return {};
  165. return Value((i32)string_object->primitive_string().string().length());
  166. }
  167. Value StringPrototype::to_string(Interpreter& interpreter)
  168. {
  169. auto* string_object = string_object_from(interpreter);
  170. if (!string_object)
  171. return {};
  172. return js_string(interpreter, string_object->primitive_string().string());
  173. }
  174. enum class PadPlacement {
  175. Start,
  176. End,
  177. };
  178. static Value pad_string(Interpreter& interpreter, Object* object, PadPlacement placement)
  179. {
  180. auto string = object->to_string().as_string().string();
  181. if (interpreter.argument(0).to_number().is_nan()
  182. || interpreter.argument(0).to_number().is_undefined()
  183. || interpreter.argument(0).to_number().to_i32() < 0) {
  184. return js_string(interpreter, string);
  185. }
  186. auto max_length = static_cast<size_t>(interpreter.argument(0).to_i32());
  187. if (max_length <= string.length())
  188. return js_string(interpreter, string);
  189. String fill_string = " ";
  190. if (!interpreter.argument(1).is_undefined())
  191. fill_string = interpreter.argument(1).to_string();
  192. if (fill_string.is_empty())
  193. return js_string(interpreter, string);
  194. auto fill_length = max_length - string.length();
  195. StringBuilder filler_builder;
  196. while (filler_builder.length() < fill_length)
  197. filler_builder.append(fill_string);
  198. auto filler = filler_builder.build().substring(0, fill_length);
  199. if (placement == PadPlacement::Start)
  200. return js_string(interpreter, String::format("%s%s", filler.characters(), string.characters()));
  201. return js_string(interpreter, String::format("%s%s", string.characters(), filler.characters()));
  202. }
  203. Value StringPrototype::pad_start(Interpreter& interpreter)
  204. {
  205. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  206. if (!this_object)
  207. return {};
  208. return pad_string(interpreter, this_object, PadPlacement::Start);
  209. }
  210. Value StringPrototype::pad_end(Interpreter& interpreter)
  211. {
  212. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  213. if (!this_object)
  214. return {};
  215. return pad_string(interpreter, this_object, PadPlacement::End);
  216. }
  217. enum class TrimMode {
  218. Left,
  219. Right,
  220. Both
  221. };
  222. static Value trim_string(Interpreter& interpreter, const Object& object, TrimMode mode)
  223. {
  224. auto& string = object.to_string().as_string().string();
  225. size_t substring_start = 0;
  226. size_t substring_length = string.length();
  227. auto is_white_space_character = [](char character) -> bool {
  228. return character == 0x9 || character == 0xa || character == 0xb || character == 0xc || character == 0xd || character == 0x20;
  229. };
  230. if (mode == TrimMode::Left || mode == TrimMode::Both) {
  231. for (size_t i = 0; i < string.length(); ++i) {
  232. if (!is_white_space_character(string[i])) {
  233. substring_start = i;
  234. substring_length -= substring_start;
  235. break;
  236. }
  237. }
  238. }
  239. if (substring_length == 0)
  240. return js_string(interpreter, String(""));
  241. if (mode == TrimMode::Right || mode == TrimMode::Both) {
  242. size_t count = 0;
  243. for (size_t i = string.length() - 1; i > 0; --i) {
  244. if (!is_white_space_character(string[i])) {
  245. substring_length -= count;
  246. break;
  247. }
  248. count++;
  249. }
  250. }
  251. return js_string(interpreter, string.substring(substring_start, substring_length));
  252. }
  253. Value StringPrototype::trim(Interpreter& interpreter)
  254. {
  255. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  256. if (!this_object)
  257. return {};
  258. return trim_string(interpreter, *this_object, TrimMode::Both);
  259. }
  260. Value StringPrototype::trim_start(Interpreter& interpreter)
  261. {
  262. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  263. if (!this_object)
  264. return {};
  265. return trim_string(interpreter, *this_object, TrimMode::Left);
  266. }
  267. Value StringPrototype::trim_end(Interpreter& interpreter)
  268. {
  269. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  270. if (!this_object)
  271. return {};
  272. return trim_string(interpreter, *this_object, TrimMode::Right);
  273. }
  274. Value StringPrototype::concat(Interpreter& interpreter)
  275. {
  276. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  277. if (!this_object)
  278. return {};
  279. auto& string = this_object->to_string().as_string().string();
  280. StringBuilder builder;
  281. builder.append(string);
  282. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  283. auto string_argument = interpreter.argument(i).to_string();
  284. builder.append(string_argument);
  285. }
  286. return js_string(interpreter, builder.to_string());
  287. }
  288. Value StringPrototype::substring(Interpreter& interpreter)
  289. {
  290. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  291. if (!this_object)
  292. return {};
  293. auto& string = this_object->to_string().as_string().string();
  294. if (interpreter.argument_count() == 0)
  295. return js_string(interpreter, string);
  296. i32 string_length = static_cast<i32>(string.length());
  297. i32 index_start = interpreter.argument(0).to_number().to_i32();
  298. i32 index_end = string_length;
  299. if (index_start > string_length)
  300. index_start = string_length;
  301. else if (index_start < 0)
  302. index_start = 0;
  303. if (interpreter.argument_count() >= 2) {
  304. index_end = interpreter.argument(1).to_number().to_i32();
  305. if (index_end > string_length)
  306. index_end = string_length;
  307. else if (index_end < 0)
  308. index_end = 0;
  309. }
  310. if (index_start == index_end)
  311. return js_string(interpreter, String(""));
  312. if (index_start > index_end) {
  313. if (interpreter.argument_count() == 1) {
  314. return js_string(interpreter, String(""));
  315. } else {
  316. i32 temp_index_start = index_start;
  317. index_start = index_end;
  318. index_end = temp_index_start;
  319. }
  320. }
  321. auto part_length = index_end - index_start;
  322. auto string_part = string.substring(index_start, part_length);
  323. return js_string(interpreter, string_part);
  324. }
  325. Value StringPrototype::includes(Interpreter& interpreter)
  326. {
  327. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  328. if (!this_object)
  329. return {};
  330. auto& string = this_object->to_string().as_string().string();
  331. auto search_string = interpreter.argument(0).to_string();
  332. i32 position = 0;
  333. if (interpreter.argument_count() >= 2) {
  334. position = interpreter.argument(1).to_i32();
  335. if (position >= static_cast<i32>(string.length()))
  336. return Value(false);
  337. if (position < 0)
  338. position = 0;
  339. }
  340. if (position == 0)
  341. return Value(string.contains(search_string));
  342. auto substring_length = string.length() - position;
  343. auto substring_search = string.substring(position, substring_length);
  344. return Value(substring_search.contains(search_string));
  345. }
  346. Value StringPrototype::slice(Interpreter& interpreter)
  347. {
  348. auto* string_object = string_object_from(interpreter);
  349. if (!string_object)
  350. return {};
  351. auto& string = string_object->primitive_string().string();
  352. if (interpreter.argument_count() == 0)
  353. return js_string(interpreter, string);
  354. i32 string_length = static_cast<i32>(string.length());
  355. i32 index_start = interpreter.argument(0).to_i32();
  356. i32 index_end = string_length;
  357. auto negative_min_index = -(string_length - 1);
  358. if (index_start < negative_min_index)
  359. index_start = 0;
  360. else if (index_start < 0)
  361. index_start = string_length + index_start;
  362. if (interpreter.argument_count() >= 2) {
  363. index_end = interpreter.argument(1).to_i32();
  364. if (index_end < negative_min_index)
  365. return js_string(interpreter, String::empty());
  366. if (index_end > string_length)
  367. index_end = string_length;
  368. else if (index_end < 0)
  369. index_end = string_length + index_end;
  370. }
  371. if (index_start >= index_end)
  372. return js_string(interpreter, String::empty());
  373. auto part_length = index_end - index_start;
  374. auto string_part = string.substring(index_start, part_length);
  375. return js_string(interpreter, string_part);
  376. }
  377. }