DeprecatedString.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteBuffer.h>
  7. #include <AK/DeprecatedFlyString.h>
  8. #include <AK/DeprecatedString.h>
  9. #include <AK/Format.h>
  10. #include <AK/Function.h>
  11. #include <AK/StdLibExtras.h>
  12. #include <AK/StringView.h>
  13. #include <AK/Utf8View.h>
  14. #include <AK/Vector.h>
  15. namespace AK {
  16. bool DeprecatedString::operator==(DeprecatedFlyString const& fly_string) const
  17. {
  18. return m_impl == fly_string.impl() || view() == fly_string.view();
  19. }
  20. bool DeprecatedString::operator==(DeprecatedString const& other) const
  21. {
  22. return m_impl == other.impl() || view() == other.view();
  23. }
  24. bool DeprecatedString::operator==(StringView other) const
  25. {
  26. return view() == other;
  27. }
  28. bool DeprecatedString::operator<(DeprecatedString const& other) const
  29. {
  30. return view() < other.view();
  31. }
  32. bool DeprecatedString::operator>(DeprecatedString const& other) const
  33. {
  34. return view() > other.view();
  35. }
  36. bool DeprecatedString::copy_characters_to_buffer(char* buffer, size_t buffer_size) const
  37. {
  38. // We must fit at least the NUL-terminator.
  39. VERIFY(buffer_size > 0);
  40. size_t characters_to_copy = min(length(), buffer_size - 1);
  41. __builtin_memcpy(buffer, characters(), characters_to_copy);
  42. buffer[characters_to_copy] = 0;
  43. return characters_to_copy == length();
  44. }
  45. DeprecatedString DeprecatedString::isolated_copy() const
  46. {
  47. if (!m_impl)
  48. return {};
  49. if (!m_impl->length())
  50. return empty();
  51. char* buffer;
  52. auto impl = StringImpl::create_uninitialized(length(), buffer);
  53. memcpy(buffer, m_impl->characters(), m_impl->length());
  54. return DeprecatedString(move(*impl));
  55. }
  56. DeprecatedString DeprecatedString::substring(size_t start, size_t length) const
  57. {
  58. if (!length)
  59. return DeprecatedString::empty();
  60. VERIFY(m_impl);
  61. VERIFY(!Checked<size_t>::addition_would_overflow(start, length));
  62. VERIFY(start + length <= m_impl->length());
  63. return { characters() + start, length };
  64. }
  65. DeprecatedString DeprecatedString::substring(size_t start) const
  66. {
  67. VERIFY(m_impl);
  68. VERIFY(start <= length());
  69. return { characters() + start, length() - start };
  70. }
  71. StringView DeprecatedString::substring_view(size_t start, size_t length) const
  72. {
  73. VERIFY(m_impl);
  74. VERIFY(!Checked<size_t>::addition_would_overflow(start, length));
  75. VERIFY(start + length <= m_impl->length());
  76. return { characters() + start, length };
  77. }
  78. StringView DeprecatedString::substring_view(size_t start) const
  79. {
  80. VERIFY(m_impl);
  81. VERIFY(start <= length());
  82. return { characters() + start, length() - start };
  83. }
  84. Vector<DeprecatedString> DeprecatedString::split(char separator, SplitBehavior split_behavior) const
  85. {
  86. return split_limit(separator, 0, split_behavior);
  87. }
  88. Vector<DeprecatedString> DeprecatedString::split_limit(char separator, size_t limit, SplitBehavior split_behavior) const
  89. {
  90. if (is_empty())
  91. return {};
  92. Vector<DeprecatedString> v;
  93. size_t substart = 0;
  94. bool keep_empty = has_flag(split_behavior, SplitBehavior::KeepEmpty);
  95. bool keep_separator = has_flag(split_behavior, SplitBehavior::KeepTrailingSeparator);
  96. for (size_t i = 0; i < length() && (v.size() + 1) != limit; ++i) {
  97. char ch = characters()[i];
  98. if (ch == separator) {
  99. size_t sublen = i - substart;
  100. if (sublen != 0 || keep_empty)
  101. v.append(substring(substart, keep_separator ? sublen + 1 : sublen));
  102. substart = i + 1;
  103. }
  104. }
  105. size_t taillen = length() - substart;
  106. if (taillen != 0 || keep_empty)
  107. v.append(substring(substart, taillen));
  108. return v;
  109. }
  110. Vector<StringView> DeprecatedString::split_view(Function<bool(char)> separator, SplitBehavior split_behavior) const
  111. {
  112. if (is_empty())
  113. return {};
  114. Vector<StringView> v;
  115. size_t substart = 0;
  116. bool keep_empty = has_flag(split_behavior, SplitBehavior::KeepEmpty);
  117. bool keep_separator = has_flag(split_behavior, SplitBehavior::KeepTrailingSeparator);
  118. for (size_t i = 0; i < length(); ++i) {
  119. char ch = characters()[i];
  120. if (separator(ch)) {
  121. size_t sublen = i - substart;
  122. if (sublen != 0 || keep_empty)
  123. v.append(substring_view(substart, keep_separator ? sublen + 1 : sublen));
  124. substart = i + 1;
  125. }
  126. }
  127. size_t taillen = length() - substart;
  128. if (taillen != 0 || keep_empty)
  129. v.append(substring_view(substart, taillen));
  130. return v;
  131. }
  132. Vector<StringView> DeprecatedString::split_view(char const separator, SplitBehavior split_behavior) const
  133. {
  134. return split_view([separator](char ch) { return ch == separator; }, split_behavior);
  135. }
  136. ByteBuffer DeprecatedString::to_byte_buffer() const
  137. {
  138. if (!m_impl)
  139. return {};
  140. // FIXME: Handle OOM failure.
  141. return ByteBuffer::copy(bytes()).release_value_but_fixme_should_propagate_errors();
  142. }
  143. template<typename T>
  144. Optional<T> DeprecatedString::to_int(TrimWhitespace trim_whitespace) const
  145. {
  146. return StringUtils::convert_to_int<T>(view(), trim_whitespace);
  147. }
  148. template Optional<i8> DeprecatedString::to_int(TrimWhitespace) const;
  149. template Optional<i16> DeprecatedString::to_int(TrimWhitespace) const;
  150. template Optional<i32> DeprecatedString::to_int(TrimWhitespace) const;
  151. template Optional<long> DeprecatedString::to_int(TrimWhitespace) const;
  152. template Optional<long long> DeprecatedString::to_int(TrimWhitespace) const;
  153. template<typename T>
  154. Optional<T> DeprecatedString::to_uint(TrimWhitespace trim_whitespace) const
  155. {
  156. return StringUtils::convert_to_uint<T>(view(), trim_whitespace);
  157. }
  158. template Optional<u8> DeprecatedString::to_uint(TrimWhitespace) const;
  159. template Optional<u16> DeprecatedString::to_uint(TrimWhitespace) const;
  160. template Optional<u32> DeprecatedString::to_uint(TrimWhitespace) const;
  161. template Optional<unsigned long> DeprecatedString::to_uint(TrimWhitespace) const;
  162. template Optional<unsigned long long> DeprecatedString::to_uint(TrimWhitespace) const;
  163. #ifndef KERNEL
  164. Optional<double> DeprecatedString::to_double(TrimWhitespace trim_whitespace) const
  165. {
  166. return StringUtils::convert_to_floating_point<double>(*this, trim_whitespace);
  167. }
  168. Optional<float> DeprecatedString::to_float(TrimWhitespace trim_whitespace) const
  169. {
  170. return StringUtils::convert_to_floating_point<float>(*this, trim_whitespace);
  171. }
  172. #endif
  173. bool DeprecatedString::starts_with(StringView str, CaseSensitivity case_sensitivity) const
  174. {
  175. return StringUtils::starts_with(*this, str, case_sensitivity);
  176. }
  177. bool DeprecatedString::starts_with(char ch) const
  178. {
  179. if (is_empty())
  180. return false;
  181. return characters()[0] == ch;
  182. }
  183. bool DeprecatedString::ends_with(StringView str, CaseSensitivity case_sensitivity) const
  184. {
  185. return StringUtils::ends_with(*this, str, case_sensitivity);
  186. }
  187. bool DeprecatedString::ends_with(char ch) const
  188. {
  189. if (is_empty())
  190. return false;
  191. return characters()[length() - 1] == ch;
  192. }
  193. DeprecatedString DeprecatedString::repeated(char ch, size_t count)
  194. {
  195. if (!count)
  196. return empty();
  197. char* buffer;
  198. auto impl = StringImpl::create_uninitialized(count, buffer);
  199. memset(buffer, ch, count);
  200. return *impl;
  201. }
  202. DeprecatedString DeprecatedString::repeated(StringView string, size_t count)
  203. {
  204. if (!count || string.is_empty())
  205. return empty();
  206. char* buffer;
  207. auto impl = StringImpl::create_uninitialized(count * string.length(), buffer);
  208. for (size_t i = 0; i < count; i++)
  209. __builtin_memcpy(buffer + i * string.length(), string.characters_without_null_termination(), string.length());
  210. return *impl;
  211. }
  212. DeprecatedString DeprecatedString::bijective_base_from(size_t value, unsigned base, StringView map)
  213. {
  214. value++;
  215. if (map.is_null())
  216. map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"sv;
  217. VERIFY(base >= 2 && base <= map.length());
  218. // The '8 bits per byte' assumption may need to go?
  219. Array<char, round_up_to_power_of_two(sizeof(size_t) * 8 + 1, 2)> buffer;
  220. size_t i = 0;
  221. do {
  222. auto remainder = value % base;
  223. auto new_value = value / base;
  224. if (remainder == 0) {
  225. new_value--;
  226. remainder = map.length();
  227. }
  228. buffer[i++] = map[remainder - 1];
  229. value = new_value;
  230. } while (value > 0);
  231. for (size_t j = 0; j < i / 2; ++j)
  232. swap(buffer[j], buffer[i - j - 1]);
  233. return DeprecatedString { ReadonlyBytes(buffer.data(), i) };
  234. }
  235. DeprecatedString DeprecatedString::roman_number_from(size_t value)
  236. {
  237. if (value > 3999)
  238. return DeprecatedString::number(value);
  239. StringBuilder builder;
  240. while (value > 0) {
  241. if (value >= 1000) {
  242. builder.append('M');
  243. value -= 1000;
  244. } else if (value >= 900) {
  245. builder.append("CM"sv);
  246. value -= 900;
  247. } else if (value >= 500) {
  248. builder.append('D');
  249. value -= 500;
  250. } else if (value >= 400) {
  251. builder.append("CD"sv);
  252. value -= 400;
  253. } else if (value >= 100) {
  254. builder.append('C');
  255. value -= 100;
  256. } else if (value >= 90) {
  257. builder.append("XC"sv);
  258. value -= 90;
  259. } else if (value >= 50) {
  260. builder.append('L');
  261. value -= 50;
  262. } else if (value >= 40) {
  263. builder.append("XL"sv);
  264. value -= 40;
  265. } else if (value >= 10) {
  266. builder.append('X');
  267. value -= 10;
  268. } else if (value == 9) {
  269. builder.append("IX"sv);
  270. value -= 9;
  271. } else if (value >= 5 && value <= 8) {
  272. builder.append('V');
  273. value -= 5;
  274. } else if (value == 4) {
  275. builder.append("IV"sv);
  276. value -= 4;
  277. } else if (value <= 3) {
  278. builder.append('I');
  279. value -= 1;
  280. }
  281. }
  282. return builder.to_deprecated_string();
  283. }
  284. bool DeprecatedString::matches(StringView mask, Vector<MaskSpan>& mask_spans, CaseSensitivity case_sensitivity) const
  285. {
  286. return StringUtils::matches(*this, mask, case_sensitivity, &mask_spans);
  287. }
  288. bool DeprecatedString::matches(StringView mask, CaseSensitivity case_sensitivity) const
  289. {
  290. return StringUtils::matches(*this, mask, case_sensitivity);
  291. }
  292. bool DeprecatedString::contains(StringView needle, CaseSensitivity case_sensitivity) const
  293. {
  294. return StringUtils::contains(*this, needle, case_sensitivity);
  295. }
  296. bool DeprecatedString::contains(char needle, CaseSensitivity case_sensitivity) const
  297. {
  298. return StringUtils::contains(*this, StringView(&needle, 1), case_sensitivity);
  299. }
  300. bool DeprecatedString::equals_ignoring_ascii_case(StringView other) const
  301. {
  302. return StringUtils::equals_ignoring_ascii_case(view(), other);
  303. }
  304. DeprecatedString DeprecatedString::reverse() const
  305. {
  306. StringBuilder reversed_string(length());
  307. for (size_t i = length(); i-- > 0;) {
  308. reversed_string.append(characters()[i]);
  309. }
  310. return reversed_string.to_deprecated_string();
  311. }
  312. DeprecatedString escape_html_entities(StringView html)
  313. {
  314. StringBuilder builder;
  315. for (size_t i = 0; i < html.length(); ++i) {
  316. if (html[i] == '<')
  317. builder.append("&lt;"sv);
  318. else if (html[i] == '>')
  319. builder.append("&gt;"sv);
  320. else if (html[i] == '&')
  321. builder.append("&amp;"sv);
  322. else if (html[i] == '"')
  323. builder.append("&quot;"sv);
  324. else
  325. builder.append(html[i]);
  326. }
  327. return builder.to_deprecated_string();
  328. }
  329. DeprecatedString::DeprecatedString(DeprecatedFlyString const& string)
  330. : m_impl(string.impl())
  331. {
  332. }
  333. DeprecatedString DeprecatedString::to_lowercase() const
  334. {
  335. if (!m_impl)
  336. return {};
  337. return m_impl->to_lowercase();
  338. }
  339. DeprecatedString DeprecatedString::to_uppercase() const
  340. {
  341. if (!m_impl)
  342. return {};
  343. return m_impl->to_uppercase();
  344. }
  345. DeprecatedString DeprecatedString::to_snakecase() const
  346. {
  347. return StringUtils::to_snakecase(*this);
  348. }
  349. DeprecatedString DeprecatedString::to_titlecase() const
  350. {
  351. return StringUtils::to_titlecase(*this);
  352. }
  353. DeprecatedString DeprecatedString::invert_case() const
  354. {
  355. return StringUtils::invert_case(*this);
  356. }
  357. bool DeprecatedString::operator==(char const* cstring) const
  358. {
  359. return view() == cstring;
  360. }
  361. DeprecatedString DeprecatedString::vformatted(StringView fmtstr, TypeErasedFormatParams& params)
  362. {
  363. StringBuilder builder;
  364. MUST(vformat(builder, fmtstr, params));
  365. return builder.to_deprecated_string();
  366. }
  367. Vector<size_t> DeprecatedString::find_all(StringView needle) const
  368. {
  369. return StringUtils::find_all(*this, needle);
  370. }
  371. DeprecatedStringCodePointIterator DeprecatedString::code_points() const
  372. {
  373. return DeprecatedStringCodePointIterator(*this);
  374. }
  375. ErrorOr<DeprecatedString> DeprecatedString::from_utf8(ReadonlyBytes bytes)
  376. {
  377. if (!Utf8View(bytes).validate())
  378. return Error::from_string_literal("DeprecatedString::from_utf8: Input was not valid UTF-8");
  379. return DeprecatedString { StringImpl::create(bytes) };
  380. }
  381. }