StringPrototype.cpp 14 KB

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