StringPrototype.cpp 14 KB

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