StringPrototype.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. }
  57. StringPrototype::~StringPrototype()
  58. {
  59. }
  60. Value StringPrototype::char_at(Interpreter& interpreter)
  61. {
  62. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  63. if (!this_object)
  64. return {};
  65. i32 index = 0;
  66. if (interpreter.argument_count())
  67. index = interpreter.argument(0).to_i32();
  68. ASSERT(this_object->is_string_object());
  69. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  70. if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
  71. return js_string(interpreter, String::empty());
  72. return js_string(interpreter, underlying_string.substring(index, 1));
  73. }
  74. Value StringPrototype::repeat(Interpreter& interpreter)
  75. {
  76. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  77. if (!this_object)
  78. return {};
  79. ASSERT(this_object->is_string_object());
  80. if (!interpreter.argument_count())
  81. return js_string(interpreter, String::empty());
  82. i32 count = 0;
  83. count = interpreter.argument(0).to_i32();
  84. if (count < 0) {
  85. // FIXME: throw RangeError
  86. return {};
  87. }
  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. // FIXME: We should have a helper in AK::String for this.
  132. auto* ptr = strstr(haystack.characters(), needle.characters());
  133. if (!ptr)
  134. return Value(-1);
  135. return Value((i32)(ptr - haystack.characters()));
  136. }
  137. static StringObject* string_object_from(Interpreter& interpreter)
  138. {
  139. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  140. if (!this_object)
  141. return nullptr;
  142. if (!this_object->is_string_object()) {
  143. interpreter.throw_exception<TypeError>("Not a String object");
  144. return nullptr;
  145. }
  146. return static_cast<StringObject*>(this_object);
  147. }
  148. Value StringPrototype::to_lowercase(Interpreter& interpreter)
  149. {
  150. auto* string_object = string_object_from(interpreter);
  151. if (!string_object)
  152. return {};
  153. return js_string(interpreter, string_object->primitive_string()->string().to_lowercase());
  154. }
  155. Value StringPrototype::to_uppercase(Interpreter& interpreter)
  156. {
  157. auto* string_object = string_object_from(interpreter);
  158. if (!string_object)
  159. return {};
  160. return js_string(interpreter, string_object->primitive_string()->string().to_uppercase());
  161. }
  162. Value StringPrototype::length_getter(Interpreter& interpreter)
  163. {
  164. auto* string_object = string_object_from(interpreter);
  165. if (!string_object)
  166. return {};
  167. return Value((i32)string_object->primitive_string()->string().length());
  168. }
  169. Value StringPrototype::to_string(Interpreter& interpreter)
  170. {
  171. auto* string_object = string_object_from(interpreter);
  172. if (!string_object)
  173. return {};
  174. return js_string(interpreter, string_object->primitive_string()->string());
  175. }
  176. enum class PadPlacement {
  177. Start,
  178. End,
  179. };
  180. static Value pad_string(Interpreter& interpreter, Object* object, PadPlacement placement)
  181. {
  182. auto string = object->to_string().as_string()->string();
  183. if (interpreter.argument(0).to_number().is_nan()
  184. || interpreter.argument(0).to_number().is_undefined()
  185. || interpreter.argument(0).to_number().to_i32() < 0) {
  186. return js_string(interpreter, string);
  187. }
  188. auto max_length = static_cast<size_t>(interpreter.argument(0).to_i32());
  189. if (max_length <= string.length())
  190. return js_string(interpreter, string);
  191. String fill_string = " ";
  192. if (!interpreter.argument(1).is_undefined())
  193. fill_string = interpreter.argument(1).to_string();
  194. if (fill_string.is_empty())
  195. return js_string(interpreter, string);
  196. auto fill_length = max_length - string.length();
  197. StringBuilder filler_builder;
  198. while (filler_builder.length() < fill_length)
  199. filler_builder.append(fill_string);
  200. auto filler = filler_builder.build().substring(0, fill_length);
  201. if (placement == PadPlacement::Start)
  202. return js_string(interpreter, String::format("%s%s", filler.characters(), string.characters()));
  203. return js_string(interpreter, String::format("%s%s", string.characters(), filler.characters()));
  204. }
  205. Value StringPrototype::pad_start(Interpreter& interpreter)
  206. {
  207. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  208. if (!this_object)
  209. return {};
  210. return pad_string(interpreter, this_object, PadPlacement::Start);
  211. }
  212. Value StringPrototype::pad_end(Interpreter& interpreter)
  213. {
  214. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  215. if (!this_object)
  216. return {};
  217. return pad_string(interpreter, this_object, PadPlacement::End);
  218. }
  219. enum class TrimMode {
  220. Left,
  221. Right,
  222. Both
  223. };
  224. static Value trim_string(Interpreter& interpreter, const Object& object, TrimMode mode)
  225. {
  226. auto& string = object.to_string().as_string()->string();
  227. size_t substring_start = 0;
  228. size_t substring_length = string.length();
  229. auto is_white_space_character = [](char character) -> bool {
  230. return character == 0x9 || character == 0xa || character == 0xb || character == 0xc || character == 0xd || character == 0x20;
  231. };
  232. if (mode == TrimMode::Left || mode == TrimMode::Both) {
  233. for (size_t i = 0; i < string.length(); ++i) {
  234. if (!is_white_space_character(string[i])) {
  235. substring_start = i;
  236. substring_length -= substring_start;
  237. break;
  238. }
  239. }
  240. }
  241. if (substring_length == 0)
  242. return js_string(interpreter, String(""));
  243. if (mode == TrimMode::Right || mode == TrimMode::Both) {
  244. size_t count = 0;
  245. for (size_t i = string.length() - 1; i > 0; --i) {
  246. if (!is_white_space_character(string[i])) {
  247. substring_length -= count;
  248. break;
  249. }
  250. count++;
  251. }
  252. }
  253. return js_string(interpreter, string.substring(substring_start, substring_length));
  254. }
  255. Value StringPrototype::trim(Interpreter& interpreter)
  256. {
  257. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  258. if (!this_object)
  259. return {};
  260. return trim_string(interpreter, *this_object, TrimMode::Both);
  261. }
  262. Value StringPrototype::trim_start(Interpreter& interpreter)
  263. {
  264. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  265. if (!this_object)
  266. return {};
  267. return trim_string(interpreter, *this_object, TrimMode::Left);
  268. }
  269. Value StringPrototype::trim_end(Interpreter& interpreter)
  270. {
  271. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  272. if (!this_object)
  273. return {};
  274. return trim_string(interpreter, *this_object, TrimMode::Right);
  275. }
  276. Value StringPrototype::concat(Interpreter& interpreter)
  277. {
  278. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  279. if (!this_object)
  280. return {};
  281. auto& string = this_object->to_string().as_string()->string();
  282. StringBuilder builder;
  283. builder.append(string);
  284. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  285. auto string_argument = interpreter.argument(i).to_string();
  286. builder.append(string_argument);
  287. }
  288. return js_string(interpreter, builder.to_string());
  289. }
  290. Value StringPrototype::substring(Interpreter& interpreter)
  291. {
  292. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  293. if (!this_object)
  294. return {};
  295. auto& string = this_object->to_string().as_string()->string();
  296. if (interpreter.argument_count() == 0)
  297. return js_string(interpreter, string);
  298. i32 string_length = static_cast<i32>(string.length());
  299. i32 index_start = interpreter.argument(0).to_number().to_i32();
  300. i32 index_end = string_length;
  301. if (index_start > string_length)
  302. index_start = string_length;
  303. else if (index_start < 0)
  304. index_start = 0;
  305. if (interpreter.argument_count() >= 2) {
  306. index_end = interpreter.argument(1).to_number().to_i32();
  307. if (index_end > string_length)
  308. index_end = string_length;
  309. else if (index_end < 0)
  310. index_end = 0;
  311. }
  312. if (index_start == index_end)
  313. return js_string(interpreter, String(""));
  314. if (index_start > index_end) {
  315. if (interpreter.argument_count() == 1) {
  316. return js_string(interpreter, String(""));
  317. } else {
  318. i32 temp_index_start = index_start;
  319. index_start = index_end;
  320. index_end = temp_index_start;
  321. }
  322. }
  323. auto part_length = index_end - index_start;
  324. auto string_part = string.substring(index_start, part_length);
  325. return js_string(interpreter, string_part);
  326. }
  327. }