StringPrototype.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/PrimitiveString.h>
  32. #include <LibJS/Runtime/StringObject.h>
  33. #include <LibJS/Runtime/StringPrototype.h>
  34. #include <LibJS/Runtime/Value.h>
  35. #include <string.h>
  36. namespace JS {
  37. StringPrototype::StringPrototype()
  38. : StringObject(js_string(interpreter(), String::empty()))
  39. {
  40. set_prototype(interpreter().object_prototype());
  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. }
  56. StringPrototype::~StringPrototype()
  57. {
  58. }
  59. Value StringPrototype::char_at(Interpreter& interpreter)
  60. {
  61. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  62. if (!this_object)
  63. return {};
  64. i32 index = 0;
  65. if (interpreter.argument_count())
  66. index = interpreter.argument(0).to_i32();
  67. ASSERT(this_object->is_string_object());
  68. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  69. if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
  70. return js_string(interpreter, String::empty());
  71. return js_string(interpreter, underlying_string.substring(index, 1));
  72. }
  73. Value StringPrototype::repeat(Interpreter& interpreter)
  74. {
  75. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  76. if (!this_object)
  77. return {};
  78. ASSERT(this_object->is_string_object());
  79. if (!interpreter.argument_count())
  80. return js_string(interpreter, String::empty());
  81. i32 count = 0;
  82. count = interpreter.argument(0).to_i32();
  83. if (count < 0) {
  84. // FIXME: throw RangeError
  85. return {};
  86. }
  87. auto* string_object = static_cast<StringObject*>(this_object);
  88. StringBuilder builder;
  89. for (i32 i = 0; i < count; ++i)
  90. builder.append(string_object->primitive_string()->string());
  91. return js_string(interpreter, builder.to_string());
  92. }
  93. Value StringPrototype::starts_with(Interpreter& interpreter)
  94. {
  95. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  96. if (!this_object)
  97. return {};
  98. if (!interpreter.argument_count())
  99. return Value(false);
  100. auto search_string = interpreter.argument(0).to_string();
  101. auto search_string_length = static_cast<i32>(search_string.length());
  102. i32 position = 0;
  103. if (interpreter.argument_count() > 1) {
  104. auto number = interpreter.argument(1).to_number();
  105. if (!number.is_nan())
  106. position = number.to_i32();
  107. }
  108. ASSERT(this_object->is_string_object());
  109. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  110. auto underlying_string_length = static_cast<i32>(underlying_string.length());
  111. auto start = min(max(position, 0), underlying_string_length);
  112. if (start + search_string_length > underlying_string_length)
  113. return Value(false);
  114. if (search_string_length == 0)
  115. return Value(true);
  116. return Value(underlying_string.substring(start, search_string_length) == search_string);
  117. }
  118. Value StringPrototype::index_of(Interpreter& interpreter)
  119. {
  120. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  121. if (!this_object)
  122. return {};
  123. if (!this_object->is_string_object())
  124. return interpreter.throw_exception<TypeError>("Not a String object");
  125. Value needle_value = js_undefined();
  126. if (interpreter.argument_count() >= 1)
  127. needle_value = interpreter.argument(0);
  128. auto needle = needle_value.to_string();
  129. auto haystack = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  130. // FIXME: We should have a helper in AK::String for this.
  131. auto* ptr = strstr(haystack.characters(), needle.characters());
  132. if (!ptr)
  133. return Value(-1);
  134. return Value((i32)(ptr - haystack.characters()));
  135. }
  136. static StringObject* string_object_from(Interpreter& interpreter)
  137. {
  138. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  139. if (!this_object)
  140. return nullptr;
  141. if (!this_object->is_string_object()) {
  142. interpreter.throw_exception<TypeError>("Not a String object");
  143. return nullptr;
  144. }
  145. return static_cast<StringObject*>(this_object);
  146. }
  147. Value StringPrototype::to_lowercase(Interpreter& interpreter)
  148. {
  149. auto* string_object = string_object_from(interpreter);
  150. if (!string_object)
  151. return {};
  152. return js_string(interpreter, string_object->primitive_string()->string().to_lowercase());
  153. }
  154. Value StringPrototype::to_uppercase(Interpreter& interpreter)
  155. {
  156. auto* string_object = string_object_from(interpreter);
  157. if (!string_object)
  158. return {};
  159. return js_string(interpreter, string_object->primitive_string()->string().to_uppercase());
  160. }
  161. Value StringPrototype::length_getter(Interpreter& interpreter)
  162. {
  163. auto* string_object = string_object_from(interpreter);
  164. if (!string_object)
  165. return {};
  166. return Value((i32)string_object->primitive_string()->string().length());
  167. }
  168. Value StringPrototype::to_string(Interpreter& interpreter)
  169. {
  170. auto* string_object = string_object_from(interpreter);
  171. if (!string_object)
  172. return {};
  173. return js_string(interpreter, string_object->primitive_string()->string());
  174. }
  175. enum class PadPlacement {
  176. Start,
  177. End,
  178. };
  179. static Value pad_string(Interpreter& interpreter, Object* object, PadPlacement placement)
  180. {
  181. auto string = object->to_string().as_string()->string();
  182. if (interpreter.argument(0).to_number().is_nan()
  183. || interpreter.argument(0).to_number().is_undefined()
  184. || interpreter.argument(0).to_number().to_i32() < 0) {
  185. return js_string(interpreter, string);
  186. }
  187. auto max_length = static_cast<size_t>(interpreter.argument(0).to_i32());
  188. if (max_length <= string.length())
  189. return js_string(interpreter, string);
  190. String fill_string = " ";
  191. if (!interpreter.argument(1).is_undefined())
  192. fill_string = interpreter.argument(1).to_string();
  193. if (fill_string.is_empty())
  194. return js_string(interpreter, string);
  195. auto fill_length = max_length - string.length();
  196. StringBuilder filler_builder;
  197. while (filler_builder.length() < fill_length)
  198. filler_builder.append(fill_string);
  199. auto filler = filler_builder.build().substring(0, fill_length);
  200. if (placement == PadPlacement::Start)
  201. return js_string(interpreter, String::format("%s%s", filler.characters(), string.characters()));
  202. return js_string(interpreter, String::format("%s%s", string.characters(), filler.characters()));
  203. }
  204. Value StringPrototype::pad_start(Interpreter& interpreter)
  205. {
  206. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  207. if (!this_object)
  208. return {};
  209. return pad_string(interpreter, this_object, PadPlacement::Start);
  210. }
  211. Value StringPrototype::pad_end(Interpreter& interpreter)
  212. {
  213. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  214. if (!this_object)
  215. return {};
  216. return pad_string(interpreter, this_object, PadPlacement::End);
  217. }
  218. enum class TrimMode {
  219. Left,
  220. Right,
  221. Both
  222. };
  223. static Value trim_string(Interpreter& interpreter, const Object& object, TrimMode mode)
  224. {
  225. auto& string = object.to_string().as_string()->string();
  226. size_t substring_start = 0;
  227. size_t substring_length = string.length();
  228. auto is_white_space_character = [](char character) -> bool {
  229. return character == 0x9 || character == 0xa || character == 0xb || character == 0xc || character == 0xd || character == 0x20;
  230. };
  231. if (mode == TrimMode::Left || mode == TrimMode::Both) {
  232. for (size_t i = 0; i < string.length(); ++i) {
  233. if (!is_white_space_character(string[i])) {
  234. substring_start = i;
  235. substring_length -= substring_start;
  236. break;
  237. }
  238. }
  239. }
  240. if (substring_length == 0)
  241. return js_string(interpreter, String(""));
  242. if (mode == TrimMode::Right || mode == TrimMode::Both) {
  243. size_t count = 0;
  244. for (size_t i = string.length() - 1; i > 0; --i) {
  245. if (!is_white_space_character(string[i])) {
  246. substring_length -= count;
  247. break;
  248. }
  249. count++;
  250. }
  251. }
  252. return js_string(interpreter, string.substring(substring_start, substring_length));
  253. }
  254. Value StringPrototype::trim(Interpreter& interpreter)
  255. {
  256. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  257. if (!this_object)
  258. return {};
  259. return trim_string(interpreter, *this_object, TrimMode::Both);
  260. }
  261. Value StringPrototype::trim_start(Interpreter& interpreter)
  262. {
  263. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  264. if (!this_object)
  265. return {};
  266. return trim_string(interpreter, *this_object, TrimMode::Left);
  267. }
  268. Value StringPrototype::trim_end(Interpreter& interpreter)
  269. {
  270. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  271. if (!this_object)
  272. return {};
  273. return trim_string(interpreter, *this_object, TrimMode::Right);
  274. }
  275. Value StringPrototype::concat(Interpreter& interpreter)
  276. {
  277. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  278. if (!this_object)
  279. return {};
  280. auto& string = this_object->to_string().as_string()->string();
  281. StringBuilder builder;
  282. builder.append(string);
  283. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  284. auto string_argument = interpreter.argument(i).to_string();
  285. builder.append(string_argument);
  286. }
  287. return js_string(interpreter, builder.to_string());
  288. }
  289. }