StringUtilities.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  2. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  3. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  4. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  5. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  6. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  7. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  8. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  9. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  10. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  11. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  12. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  13. #include "..\..\Include\RmlUi\Core\StringUtilities.h"
  14. /*
  15. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  16. *
  17. * For the latest information, see http://github.com/mikke89/RmlUi
  18. *
  19. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  20. * Copyright (c) 2019 The RmlUi Team, and contributors
  21. *
  22. * Permission is hereby granted, free of charge, to any person obtaining a copy
  23. * of this software and associated documentation files (the "Software"), to deal
  24. * in the Software without restriction, including without limitation the rights
  25. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  26. * copies of the Software, and to permit persons to whom the Software is
  27. * furnished to do so, subject to the following conditions:
  28. *
  29. * The above copyright notice and this permission notice shall be included in
  30. * all copies or substantial portions of the Software.
  31. *
  32. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  33. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  34. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  35. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  36. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  37. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  38. * THE SOFTWARE.
  39. *
  40. */
  41. #include "precompiled.h"
  42. #include "../../Include/RmlUi/Core/StringUtilities.h"
  43. #include <ctype.h>
  44. #include <stdio.h>
  45. #include <stdarg.h>
  46. namespace Rml {
  47. namespace Core {
  48. static int FormatString(String& string, size_t max_size, const char* format, va_list argument_list)
  49. {
  50. const int INTERNAL_BUFFER_SIZE = 1024;
  51. static char buffer[INTERNAL_BUFFER_SIZE];
  52. char* buffer_ptr = buffer;
  53. if (max_size + 1 > INTERNAL_BUFFER_SIZE)
  54. buffer_ptr = new char[max_size + 1];
  55. int length = vsnprintf(buffer_ptr, max_size, format, argument_list);
  56. buffer_ptr[length >= 0 ? length : max_size] = '\0';
  57. #ifdef RMLUI_DEBUG
  58. if (length == -1)
  59. {
  60. Log::Message(Log::LT_WARNING, "FormatString: String truncated to %d bytes when processing %s", max_size, format);
  61. }
  62. #endif
  63. string = buffer_ptr;
  64. if (buffer_ptr != buffer)
  65. delete[] buffer_ptr;
  66. return length;
  67. }
  68. int FormatString(String& string, size_t max_size, const char* format, ...)
  69. {
  70. va_list argument_list;
  71. va_start(argument_list, format);
  72. int result = FormatString(string, (int)max_size, format, argument_list);
  73. va_end(argument_list);
  74. return result;
  75. }
  76. String CreateString(size_t max_size, const char* format, ...)
  77. {
  78. String result;
  79. result.reserve(max_size);
  80. va_list argument_list;
  81. va_start(argument_list, format);
  82. FormatString(result, max_size, format, argument_list);
  83. va_end(argument_list);
  84. return result;
  85. }
  86. String StringUtilities::ToLower(const String& string) {
  87. String str_lower = string;
  88. std::transform(str_lower.begin(), str_lower.end(), str_lower.begin(), ::tolower);
  89. return str_lower;
  90. }
  91. String StringUtilities::Replace(String subject, const String& search, const String& replace)
  92. {
  93. size_t pos = 0;
  94. while ((pos = subject.find(search, pos)) != String::npos) {
  95. subject.replace(pos, search.length(), replace);
  96. pos += replace.length();
  97. }
  98. return subject;
  99. }
  100. String StringUtilities::Replace(String subject, char search, char replace)
  101. {
  102. const size_t size = subject.size();
  103. for (size_t i = 0; i < size; i++)
  104. {
  105. if (subject[i] == search)
  106. subject[i] = replace;
  107. }
  108. return subject;
  109. }
  110. // Expands character-delimited list of values in a single string to a whitespace-trimmed list of values.
  111. void StringUtilities::ExpandString(StringList& string_list, const String& string, const char delimiter)
  112. {
  113. char quote = 0;
  114. bool last_char_delimiter = true;
  115. const char* ptr = string.c_str();
  116. const char* start_ptr = nullptr;
  117. const char* end_ptr = ptr;
  118. size_t num_delimiter_values = std::count(string.begin(), string.end(), delimiter);
  119. if (num_delimiter_values == 0)
  120. {
  121. string_list.push_back(StripWhitespace(string));
  122. return;
  123. }
  124. string_list.reserve(string_list.size() + num_delimiter_values + 1);
  125. while (*ptr)
  126. {
  127. // Switch into quote mode if the last char was a delimeter ( excluding whitespace )
  128. // and we're not already in quote mode
  129. if (last_char_delimiter && !quote && (*ptr == '"' || *ptr == '\''))
  130. {
  131. quote = *ptr;
  132. }
  133. // Switch out of quote mode if we encounter a quote that hasn't been escaped
  134. else if (*ptr == quote && *(ptr-1) != '\\')
  135. {
  136. quote = 0;
  137. }
  138. // If we encounter a delimiter while not in quote mode, add the item to the list
  139. else if (*ptr == delimiter && !quote)
  140. {
  141. if (start_ptr)
  142. string_list.emplace_back(start_ptr, end_ptr + 1);
  143. else
  144. string_list.emplace_back();
  145. last_char_delimiter = true;
  146. start_ptr = nullptr;
  147. }
  148. // Otherwise if its not white space or we're in quote mode, advance the pointers
  149. else if (!isspace(*ptr) || quote)
  150. {
  151. if (!start_ptr)
  152. start_ptr = ptr;
  153. end_ptr = ptr;
  154. last_char_delimiter = false;
  155. }
  156. ptr++;
  157. }
  158. // If there's data pending, add it.
  159. if (start_ptr)
  160. string_list.emplace_back(start_ptr, end_ptr + 1);
  161. }
  162. void StringUtilities::ExpandString(StringList& string_list, const String& string, const char delimiter, char quote_character, char unquote_character, bool ignore_repeated_delimiters)
  163. {
  164. int quote_mode_depth = 0;
  165. const char* ptr = string.c_str();
  166. const char* start_ptr = nullptr;
  167. const char* end_ptr = ptr;
  168. while (*ptr)
  169. {
  170. // Increment the quote depth for each quote character encountered
  171. if (*ptr == quote_character)
  172. {
  173. ++quote_mode_depth;
  174. }
  175. // And decrement it for every unquote character
  176. else if (*ptr == unquote_character)
  177. {
  178. --quote_mode_depth;
  179. }
  180. // If we encounter a delimiter while not in quote mode, add the item to the list
  181. if (*ptr == delimiter && quote_mode_depth == 0)
  182. {
  183. if (start_ptr)
  184. string_list.emplace_back(start_ptr, end_ptr + 1);
  185. else if(!ignore_repeated_delimiters)
  186. string_list.emplace_back();
  187. start_ptr = nullptr;
  188. }
  189. // Otherwise if its not white space or we're in quote mode, advance the pointers
  190. else if (!isspace(*ptr) || quote_mode_depth > 0)
  191. {
  192. if (!start_ptr)
  193. start_ptr = ptr;
  194. end_ptr = ptr;
  195. }
  196. ptr++;
  197. }
  198. // If there's data pending, add it.
  199. if (start_ptr)
  200. string_list.emplace_back(start_ptr, end_ptr + 1);
  201. }
  202. // Joins a list of string values into a single string separated by a character delimiter.
  203. void StringUtilities::JoinString(String& string, const StringList& string_list, const char delimiter)
  204. {
  205. for (size_t i = 0; i < string_list.size(); i++)
  206. {
  207. string += string_list[i];
  208. if (delimiter != '\0' && i < string_list.size() - 1)
  209. string += delimiter;
  210. }
  211. }
  212. // Strip whitespace characters from the beginning and end of a string.
  213. String StringUtilities::StripWhitespace(const String& string)
  214. {
  215. const char* start = string.c_str();
  216. const char* end = start + string.size();
  217. while (start < end && IsWhitespace(*start))
  218. start++;
  219. while (end > start && IsWhitespace(*(end - 1)))
  220. end--;
  221. if (start < end)
  222. return String(start, end);
  223. return String();
  224. }
  225. // Operators for STL containers using strings.
  226. bool StringUtilities::StringComparei::operator()(const String& lhs, const String& rhs) const
  227. {
  228. return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
  229. }
  230. Character StringUtilities::ToCharacter(const char* p)
  231. {
  232. if ((*p & (1 << 7)) == 0)
  233. return static_cast<Character>(*p);
  234. int num_bytes = 0;
  235. int code = 0;
  236. if ((*p & 0b1110'0000) == 0b1100'0000)
  237. {
  238. num_bytes = 2;
  239. code = (*p & 0b0001'1111);
  240. }
  241. else if ((*p & 0b1111'0000) == 0b1110'0000)
  242. {
  243. num_bytes = 3;
  244. code = (*p & 0b0000'1111);
  245. }
  246. else if ((*p & 0b1111'1000) == 0b1111'0000)
  247. {
  248. num_bytes = 4;
  249. code = (*p & 0b0000'0111);
  250. }
  251. else
  252. {
  253. // Invalid begin byte
  254. return Character::Null;
  255. }
  256. for (int i = 1; i < num_bytes; i++)
  257. {
  258. const char byte = *(p + i);
  259. if ((byte & 0b1100'0000) != 0b1000'0000)
  260. {
  261. // Invalid continuation byte
  262. ++p;
  263. return Character::Null;
  264. }
  265. code = ((code << 6) | (byte & 0b0011'1111));
  266. }
  267. return static_cast<Character>(code);
  268. }
  269. String StringUtilities::ToUTF8(Character character)
  270. {
  271. return ToUTF8(&character, 1);
  272. }
  273. String StringUtilities::ToUTF8(const Character* characters, int num_characters)
  274. {
  275. String result;
  276. result.reserve(num_characters);
  277. bool invalid_character = false;
  278. for (int i = 0; i < num_characters; i++)
  279. {
  280. char32_t c = (char32_t)characters[i];
  281. constexpr int l3 = 0b0000'0111;
  282. constexpr int l4 = 0b0000'1111;
  283. constexpr int l5 = 0b0001'1111;
  284. constexpr int l6 = 0b0011'1111;
  285. constexpr int h1 = 0b1000'0000;
  286. constexpr int h2 = 0b1100'0000;
  287. constexpr int h3 = 0b1110'0000;
  288. constexpr int h4 = 0b1111'0000;
  289. if (c < 0x80)
  290. result += (char)c;
  291. else if (c < 0x800)
  292. result += { char(((c >> 6) & l5) | h2), char((c & l6) | h1) };
  293. else if (c < 0x10000)
  294. result += { char(((c >> 12) & l4) | h3), char(((c >> 6) & l6) | h1), char((c & l6) | h1) };
  295. else if (c <= 0x10FFFF)
  296. result += { char(((c >> 18) & l3) | h4), char(((c >> 12) & l6) | h1), char(((c >> 6) & l6) | h1), char((c & l6) | h1) };
  297. else
  298. invalid_character = true;
  299. }
  300. if (invalid_character)
  301. Log::Message(Log::LT_WARNING, "One or more invalid code points encountered while encoding to UTF-8.");
  302. return result;
  303. }
  304. size_t StringUtilities::LengthUTF8(StringView string_view)
  305. {
  306. const char* const p_end = string_view.end();
  307. // Skip any continuation bytes at the beginning
  308. const char* p = string_view.begin();
  309. size_t num_continuation_bytes = 0;
  310. while (p != p_end)
  311. {
  312. if ((*p & 0b1100'0000) == 0b1000'0000)
  313. ++num_continuation_bytes;
  314. ++p;
  315. }
  316. return string_view.size() - num_continuation_bytes;
  317. }
  318. U16String StringUtilities::ToUTF16(const String& input)
  319. {
  320. U16String result;
  321. if (input.empty())
  322. return result;
  323. std::vector<Character> characters;
  324. characters.reserve(input.size());
  325. for (auto it = StringIteratorU8(input); it; ++it)
  326. characters.push_back(*it);
  327. result.reserve(input.size());
  328. bool valid_characters = true;
  329. for (Character character : characters)
  330. {
  331. char32_t c = (char32_t)character;
  332. if (c <= 0xD7FF || (c >= 0xE000 && c <= 0xFFFF))
  333. {
  334. // Single 16-bit code unit.
  335. result += (char16_t)c;
  336. }
  337. else if (c >= 0x10000 && c <= 0x10FFFF)
  338. {
  339. // Encode as two 16-bit code units.
  340. char32_t c_shift = c - 0x10000;
  341. char16_t w1 = (0xD800 | ((c_shift >> 10) & 0x3FF));
  342. char16_t w2 = (0xDC00 | (c_shift & 0x3FF));
  343. result += {w1, w2};
  344. }
  345. else
  346. {
  347. valid_characters = false;
  348. }
  349. }
  350. if (!valid_characters)
  351. Log::Message(Log::LT_WARNING, "Invalid characters encountered while converting UTF-8 string to UTF-16.");
  352. return result;
  353. }
  354. String StringUtilities::ToUTF8(const U16String& input)
  355. {
  356. std::vector<Character> characters;
  357. characters.reserve(input.size());
  358. bool valid_input = true;
  359. char16_t w1 = 0;
  360. for (char16_t w : input)
  361. {
  362. if (w <= 0xD7FF || w >= 0xE000)
  363. {
  364. // Single 16-bit code unit.
  365. characters.push_back((Character)(w));
  366. }
  367. else
  368. {
  369. // Two 16-bit code units.
  370. if (!w1 && w < 0xDC00)
  371. {
  372. w1 = w;
  373. }
  374. else if (w1 && w >= 0xDC00)
  375. {
  376. characters.push_back((Character)(((((char32_t)w1 & 0x3FF) << 10) | ((char32_t)(w) & 0x3FF)) + 0x10000u));
  377. w1 = 0;
  378. }
  379. else
  380. {
  381. valid_input = false;
  382. }
  383. }
  384. }
  385. String result;
  386. if (characters.size() > 0)
  387. result = StringUtilities::ToUTF8(characters.data(), (int)characters.size());
  388. if (!valid_input)
  389. Log::Message(Log::LT_WARNING, "Invalid characters encountered while converting UTF-16 string to UTF-8.");
  390. return result;
  391. }
  392. StringView::StringView(const char* p_begin, const char* p_end) : p_begin(p_begin), p_end(p_end)
  393. {
  394. RMLUI_ASSERT(p_end >= p_begin);
  395. }
  396. StringView::StringView(const String& string) : p_begin(string.data()), p_end(string.data() + string.size())
  397. {}
  398. StringView::StringView(const String& string, size_t offset) : p_begin(string.data()), p_end(string.data() + string.size())
  399. {}
  400. StringView::StringView(const String& string, size_t offset, size_t count) : p_begin(string.data()), p_end(string.data() + std::min(offset + count, string.size()))
  401. {}
  402. bool StringView::operator==(const StringView& other) const {
  403. return (p_end - p_begin) == (other.p_end - other.p_begin) && strcmp(p_begin, other.p_begin) == 0;
  404. }
  405. StringIteratorU8::StringIteratorU8(const char* p_begin, const char* p, const char* p_end) : view(p_begin, p_end), p(p)
  406. {}
  407. StringIteratorU8::StringIteratorU8(const String& string) : view(string), p(string.data())
  408. {}
  409. StringIteratorU8::StringIteratorU8(const String& string, size_t offset) : view(string), p(string.data() + offset)
  410. {}
  411. StringIteratorU8::StringIteratorU8(const String& string, size_t offset, size_t count) : view(string, 0, offset + count), p(string.data() + offset)
  412. {}
  413. StringIteratorU8& StringIteratorU8::operator++() {
  414. RMLUI_ASSERT(p != view.end());
  415. ++p;
  416. SeekForward();
  417. return *this;
  418. }
  419. StringIteratorU8& StringIteratorU8::operator--() {
  420. RMLUI_ASSERT(p - 1 != view.begin());
  421. --p;
  422. SeekBack();
  423. return *this;
  424. }
  425. inline void StringIteratorU8::SeekBack() {
  426. p = StringUtilities::SeekBackwardUTF8(p, view.end());
  427. }
  428. inline void StringIteratorU8::SeekForward() {
  429. p = StringUtilities::SeekForwardUTF8(p, view.end());
  430. }
  431. }
  432. }