StringUtilities.cpp 10 KB

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