StringUtilities.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "../../Include/RmlUi/Core/StringUtilities.h"
  30. #include <ctype.h>
  31. #include <stdio.h>
  32. namespace Rml {
  33. namespace Core {
  34. // Expands character-delimited list of values in a single string to a whitespace-trimmed list of values.
  35. void StringUtilities::ExpandString(StringList& string_list, const String& string, const char delimiter)
  36. {
  37. char quote = 0;
  38. bool last_char_delimiter = true;
  39. const char* ptr = string.c_str();
  40. const char* start_ptr = nullptr;
  41. const char* end_ptr = ptr;
  42. size_t num_delimiter_values = std::count(string.begin(), string.end(), delimiter);
  43. if (num_delimiter_values == 0)
  44. {
  45. string_list.push_back(StripWhitespace(string));
  46. return;
  47. }
  48. string_list.reserve(string_list.size() + num_delimiter_values + 1);
  49. while (*ptr)
  50. {
  51. // Switch into quote mode if the last char was a delimeter ( excluding whitespace )
  52. // and we're not already in quote mode
  53. if (last_char_delimiter && !quote && (*ptr == '"' || *ptr == '\''))
  54. {
  55. quote = *ptr;
  56. }
  57. // Switch out of quote mode if we encounter a quote that hasn't been escaped
  58. else if (*ptr == quote && *(ptr-1) != '\\')
  59. {
  60. quote = 0;
  61. }
  62. // If we encouter a delimiter while not in quote mode, add the item to the list
  63. else if (*ptr == delimiter && !quote)
  64. {
  65. if (start_ptr)
  66. string_list.emplace_back(start_ptr, end_ptr + 1);
  67. else
  68. string_list.emplace_back();
  69. last_char_delimiter = true;
  70. start_ptr = nullptr;
  71. }
  72. // Otherwise if its not white space or we're in quote mode, advance the pointers
  73. else if (!isspace(*ptr) || quote)
  74. {
  75. if (!start_ptr)
  76. start_ptr = ptr;
  77. end_ptr = ptr;
  78. last_char_delimiter = false;
  79. }
  80. ptr++;
  81. }
  82. // If there's data pending, add it.
  83. if (start_ptr)
  84. string_list.emplace_back(start_ptr, end_ptr + 1);
  85. }
  86. void StringUtilities::ExpandString(StringList& string_list, const String& string, const char delimiter, char quote_character, char unquote_character)
  87. {
  88. int quote_mode_depth = 0;
  89. const char* ptr = string.c_str();
  90. const char* start_ptr = nullptr;
  91. const char* end_ptr = ptr;
  92. while (*ptr)
  93. {
  94. // Increment the quote depth for each quote character encountered
  95. if (*ptr == quote_character)
  96. {
  97. ++quote_mode_depth;
  98. }
  99. // And decrement it for every unquote character
  100. else if (*ptr == unquote_character)
  101. {
  102. --quote_mode_depth;
  103. }
  104. // If we encouter a delimiter while not in quote mode, add the item to the list
  105. if (*ptr == delimiter && quote_mode_depth == 0)
  106. {
  107. if (start_ptr)
  108. string_list.emplace_back(start_ptr, end_ptr + 1);
  109. else
  110. string_list.emplace_back();
  111. start_ptr = nullptr;
  112. }
  113. // Otherwise if its not white space or we're in quote mode, advance the pointers
  114. else if (!isspace(*ptr) || quote_mode_depth > 0)
  115. {
  116. if (!start_ptr)
  117. start_ptr = ptr;
  118. end_ptr = ptr;
  119. }
  120. ptr++;
  121. }
  122. // If there's data pending, add it.
  123. if (start_ptr)
  124. string_list.emplace_back(start_ptr, end_ptr + 1);
  125. }
  126. // Joins a list of string values into a single string separated by a character delimiter.
  127. void StringUtilities::JoinString(String& string, const StringList& string_list, const char delimiter)
  128. {
  129. for (size_t i = 0; i < string_list.size(); i++)
  130. {
  131. string += string_list[i];
  132. if (delimiter != '\0' && i < string_list.size() - 1)
  133. string += delimiter;
  134. }
  135. }
  136. // Defines, helper functions for the UTF8 / UCS2 conversion functions.
  137. #define _NXT 0x80
  138. #define _SEQ2 0xc0
  139. #define _SEQ3 0xe0
  140. #define _SEQ4 0xf0
  141. #define _SEQ5 0xf8
  142. #define _SEQ6 0xfc
  143. #define _BOM 0xfeff
  144. static int __wchar_forbidden(unsigned int sym)
  145. {
  146. // Surrogate pairs
  147. if (sym >= 0xd800 && sym <= 0xdfff)
  148. return -1;
  149. return 0;
  150. }
  151. static int __utf8_forbidden(unsigned char octet)
  152. {
  153. switch (octet)
  154. {
  155. case 0xc0:
  156. case 0xc1:
  157. case 0xf5:
  158. case 0xff:
  159. return -1;
  160. default:
  161. return 0;
  162. }
  163. }
  164. // Converts a character array in UTF-8 encoding to a vector of words.
  165. bool StringUtilities::UTF8toUCS2(const String& input, WString& output)
  166. {
  167. if (input.empty())
  168. return true;
  169. output.reserve(input.size());
  170. unsigned char* p = (unsigned char*) input.c_str();
  171. unsigned char* lim = p + input.size();
  172. // Skip the UTF-8 byte order marker if it exists.
  173. if (input.substr(0, 3) == "\xEF\xBB\xBF")
  174. p += 3;
  175. int num_bytes;
  176. for (; p < lim; p += num_bytes)
  177. {
  178. if (__utf8_forbidden(*p) != 0)
  179. return false;
  180. // Get number of bytes for one wide character.
  181. word high;
  182. num_bytes = 1;
  183. if ((*p & 0x80) == 0)
  184. {
  185. high = (wchar_t)*p;
  186. }
  187. else if ((*p & 0xe0) == _SEQ2)
  188. {
  189. num_bytes = 2;
  190. high = (wchar_t)(*p & 0x1f);
  191. }
  192. else if ((*p & 0xf0) == _SEQ3)
  193. {
  194. num_bytes = 3;
  195. high = (wchar_t)(*p & 0x0f);
  196. }
  197. else if ((*p & 0xf8) == _SEQ4)
  198. {
  199. num_bytes = 4;
  200. high = (wchar_t)(*p & 0x07);
  201. }
  202. else if ((*p & 0xfc) == _SEQ5)
  203. {
  204. num_bytes = 5;
  205. high = (wchar_t)(*p & 0x03);
  206. }
  207. else if ((*p & 0xfe) == _SEQ6)
  208. {
  209. num_bytes = 6;
  210. high = (wchar_t)(*p & 0x01);
  211. }
  212. else
  213. {
  214. return false;
  215. }
  216. // Does the sequence header tell us the truth about length?
  217. if (lim - p <= num_bytes - 1)
  218. {
  219. return false;
  220. }
  221. // Validate the sequence. All symbols must have higher bits set to 10xxxxxx.
  222. if (num_bytes > 1)
  223. {
  224. int i;
  225. for (i = 1; i < num_bytes; i++)
  226. {
  227. if ((p[i] & 0xc0) != _NXT)
  228. break;
  229. }
  230. if (i != num_bytes)
  231. {
  232. return false;
  233. }
  234. }
  235. // Make up a single UCS-4 (32-bit) character from the required number of UTF-8 tokens. The first byte has
  236. // been determined earlier, the second and subsequent bytes contribute the first six of their bits into the
  237. // final character code.
  238. unsigned int ucs4_char = 0;
  239. int num_bits = 0;
  240. for (int i = 1; i < num_bytes; i++)
  241. {
  242. ucs4_char |= (word)(p[num_bytes - i] & 0x3f) << num_bits;
  243. num_bits += 6;
  244. }
  245. ucs4_char |= high << num_bits;
  246. // Check for surrogate pairs.
  247. if (__wchar_forbidden(ucs4_char) != 0)
  248. {
  249. return false;
  250. }
  251. // Only add the character to the output if it exists in the Basic Multilingual Plane (ie, fits in a single
  252. // word).
  253. if (ucs4_char <= 0xffff)
  254. output.push_back((word) ucs4_char);
  255. }
  256. return true;
  257. }
  258. // Converts an array of words in UCS-2 encoding into a character array in UTF-8 encoding.
  259. bool StringUtilities::UCS2toUTF8(const WString& input, String& output)
  260. {
  261. unsigned char *oc;
  262. size_t n;
  263. output.reserve(input.size());
  264. const word* w = input.data();
  265. const word* wlim = w + input.size();
  266. //Log::Message(LC_CORE, Log::LT_ALWAYS, "UCS2TOUTF8 size: %d", input_size);
  267. for (; w < wlim; w++)
  268. {
  269. if (__wchar_forbidden(*w) != 0)
  270. return false;
  271. if (*w == _BOM)
  272. continue;
  273. //if (*w < 0)
  274. // return false;
  275. if (*w <= 0x007f)
  276. n = 1;
  277. else if (*w <= 0x07ff)
  278. n = 2;
  279. else //if (*w <= 0x0000ffff)
  280. n = 3;
  281. /*else if (*w <= 0x001fffff)
  282. n = 4;
  283. else if (*w <= 0x03ffffff)
  284. n = 5;
  285. else // if (*w <= 0x7fffffff)
  286. n = 6;*/
  287. // Convert to little endian.
  288. word ch = (*w >> 8) & 0x00FF;
  289. ch |= (*w << 8) & 0xFF00;
  290. // word ch = EMPConvertEndian(*w, RMLUI_ENDIAN_BIG);
  291. oc = (unsigned char *)&ch;
  292. switch (n)
  293. {
  294. case 1:
  295. output += oc[1];
  296. break;
  297. case 2:
  298. output += (_SEQ2 | (oc[1] >> 6) | ((oc[0] & 0x07) << 2));
  299. output += (_NXT | (oc[1] & 0x3f));
  300. break;
  301. case 3:
  302. output += (_SEQ3 | ((oc[0] & 0xf0) >> 4));
  303. output += (_NXT | (oc[1] >> 6) | ((oc[0] & 0x0f) << 2));
  304. output += (_NXT | (oc[1] & 0x3f));
  305. break;
  306. case 4:
  307. break;
  308. case 5:
  309. break;
  310. case 6:
  311. break;
  312. }
  313. //Log::Message(LC_CORE, Log::LT_ALWAYS, "Converting...%c(%d) %d -> %d", *w, *w, w - input, output.size());
  314. }
  315. return true;
  316. }
  317. // Strip whitespace characters from the beginning and end of a string.
  318. String StringUtilities::StripWhitespace(const String& string)
  319. {
  320. const char* start = string.c_str();
  321. const char* end = start + string.size();
  322. while (start < end && IsWhitespace(*start))
  323. start++;
  324. while (end > start && IsWhitespace(*(end - 1)))
  325. end--;
  326. if (start < end)
  327. return String(start, end);
  328. return String();
  329. }
  330. // Operators for STL containers using strings.
  331. bool StringUtilities::StringComparei::operator()(const String& lhs, const String& rhs) const
  332. {
  333. return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
  334. }
  335. }
  336. }