StandardChars.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "ParserPch.h"
  6. namespace UnifiedRegex
  7. {
  8. // ----------------------------------------------------------------------
  9. // ASCIIChars
  10. // ----------------------------------------------------------------------
  11. /*
  12. To get these two tables run:
  13. ch.exe ascii.js
  14. where ascii.js is:
  15. ----------------------------------------------------------------------
  16. function echo(s) { WScript.Echo(s); }
  17. var NumChars = 1 << 8;
  18. var Word = 1 << 0;
  19. var Newline = 1 << 1;
  20. var Whitespace = 1 << 2;
  21. var Letter = 1 << 3;
  22. var Digit = 1 << 4;
  23. var Octal = 1 << 5;
  24. var Hex = 1 << 6;
  25. var classes = [];
  26. var values = [];
  27. function cc(s) {
  28. return s.charCodeAt(0);
  29. }
  30. var c;
  31. for (c = 0; c < NumChars; c++)
  32. {
  33. classes[c] = 0;
  34. values[c] = 0;
  35. }
  36. for (c = cc('0'); c <= cc('7'); c++)
  37. {
  38. classes[c] |= Word | Octal | Digit | Hex;
  39. values[c] = c - cc('0');
  40. }
  41. for (c = cc('8'); c <= cc('9'); c++)
  42. {
  43. classes[c] |= Word | Digit | Hex;
  44. values[c] = c - cc('0');
  45. }
  46. for (c = cc('a'); c <= cc('f'); c++)
  47. {
  48. classes[c] |= Word | Hex | Letter;
  49. values[c] = 10 + c - cc('a');
  50. }
  51. for (c = cc('g'); c <= cc('z'); c++)
  52. classes[c] |= Word | Letter;
  53. for (c = cc('A'); c <= cc('F'); c++)
  54. {
  55. classes[c] |= Word | Hex | Letter;
  56. values[c] = 10 + c - cc('A');
  57. }
  58. for (c = cc('G'); c <= cc('Z'); c++)
  59. classes[c] |= Word | Letter;
  60. classes[cc('_')] |= Word;
  61. classes[cc('\n')] |= Newline;
  62. classes[cc('\r')] |= Newline;
  63. for (c = cc('\t'); c <= cc('\r'); c++)
  64. classes[c] |= Whitespace;
  65. classes[cc(' ')] |= Whitespace;
  66. classes[cc('\x85')] |= Whitespace;
  67. classes[cc('\xa0')] |= Whitespace;
  68. hex = "0123456789abcdef";
  69. function toHex(n) {
  70. return "0x" + hex[n >> 4] + hex[n & 0xf];
  71. }
  72. function dump(a) {
  73. for (c = 0; c < NumChars; c++) {
  74. if (c % 16 == 0)
  75. str = " ";
  76. else
  77. str += ", ";
  78. str += toHex(a[c]);
  79. if (c % 16 == 15)
  80. {
  81. if (c < NumChars - 1)
  82. str += ",";
  83. echo(str);
  84. }
  85. }
  86. }
  87. echo(" const uint8 ASCIIChars::classes[] = {");
  88. dump(classes);
  89. echo(" };");
  90. echo(" const uint8 ASCIIChars::values[] = {");
  91. dump(values);
  92. echo(" };");
  93. ----------------------------------------------------------------------
  94. */
  95. const uint8 ASCIIChars::classes[] = {
  96. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x06, 0x04, 0x04, 0x06, 0x00, 0x00,
  97. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  98. 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x51, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  100. 0x00, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  101. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x00, 0x00, 0x00, 0x00, 0x01,
  102. 0x00, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  103. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  111. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  112. };
  113. const uint8 ASCIIChars::values[] = {
  114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  117. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  120. 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  121. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  123. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  128. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  129. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  130. };
  131. // ----------------------------------------------------------------------
  132. // TrivialCaseMapper
  133. // ----------------------------------------------------------------------
  134. const TrivialCaseMapper TrivialCaseMapper::Instance;
  135. // ----------------------------------------------------------------------
  136. // StandardChars<wchar_t>
  137. // ----------------------------------------------------------------------
  138. /*
  139. To get the whitespaces string, run:
  140. gawk -f spaces.gawk http://www.unicode.org/Public/UNIDATA/UnicodeData.txt
  141. where spaces.gawk is
  142. ----------------------------------------------------------------------
  143. BEGIN {
  144. FS = ";";
  145. start = -1;
  146. last = -1;
  147. str = "";
  148. }
  149. {
  150. code = strtonum("0x" $1);
  151. if ($3 == "Zs" || code == 0x0009 || code == 0x000B || code == 0x000C || code == 0x0020 || code == 0x00A0 || code == 0xFEFF || code == 0x000A || code == 0x000D || code == 0x2028 || code == 0x2029)
  152. {
  153. if (start < 0)
  154. start = code;
  155. else if (code > last + 1) {
  156. str = sprintf("%s\\x%04x\\x%04x", str, start, last);
  157. start = code;
  158. }
  159. last = code;
  160. }
  161. }
  162. END {
  163. str = sprintf("%s\\x%04x\\x%04x", str, start, last);
  164. print str;
  165. }----------------------------------------------------------------------
  166. */
  167. const int StandardChars<wchar_t>::numDigitPairs = 1;
  168. const wchar_t* const StandardChars<wchar_t>::digitStr = L"09";
  169. const int StandardChars<wchar_t>::numWhitespacePairs = 11;
  170. const wchar_t* const StandardChars<wchar_t>::whitespaceStr = L"\x0009\x000d\x0020\x0020\x00a0\x00a0\x1680\x1680\x180e\x180e\x2000\x200a\x2028\x2029\x202f\x202f\x205f\x205f\x3000\x3000\xfeff\xfeff";
  171. const int StandardChars<wchar_t>::numWordPairs = 4;
  172. const wchar_t* const StandardChars<wchar_t>::wordStr = L"09AZ__az";
  173. const int StandardChars<wchar_t>::numNewlinePairs = 3;
  174. const wchar_t* const StandardChars<wchar_t>::newlineStr = L"\x000a\x000a\x000d\x000d\x2028\x2029";
  175. StandardChars<wchar_t>::StandardChars(ArenaAllocator* allocator)
  176. : allocator(allocator)
  177. , unicodeDataCaseMapper(allocator, CaseInsensitive::MappingSource::UnicodeData, &TrivialCaseMapper::Instance)
  178. , caseFoldingCaseMapper(allocator, CaseInsensitive::MappingSource::CaseFolding, &unicodeDataCaseMapper)
  179. , fullSet(0)
  180. , emptySet(0)
  181. , wordSet(0)
  182. , nonWordSet(0)
  183. , newlineSet(0)
  184. , whitespaceSet(0)
  185. {
  186. }
  187. void StandardChars<wchar_t>::SetDigits(ArenaAllocator* setAllocator, CharSet<Char> &set)
  188. {
  189. set.SetRanges(setAllocator, numDigitPairs, digitStr);
  190. }
  191. void StandardChars<wchar_t>::SetNonDigits(ArenaAllocator* setAllocator, CharSet<Char> &set)
  192. {
  193. set.SetNotRanges(setAllocator, numDigitPairs, digitStr);
  194. }
  195. void StandardChars<wchar_t>::SetWhitespace(ArenaAllocator* setAllocator, CharSet<Char> &set)
  196. {
  197. set.SetRanges(setAllocator, numWhitespacePairs, whitespaceStr);
  198. }
  199. void StandardChars<wchar_t>::SetNonWhitespace(ArenaAllocator* setAllocator, CharSet<Char> &set)
  200. {
  201. set.SetNotRanges(setAllocator, numWhitespacePairs, whitespaceStr);
  202. }
  203. void StandardChars<wchar_t>::SetWordChars(ArenaAllocator* setAllocator, CharSet<Char> &set)
  204. {
  205. set.SetRanges(setAllocator, numWordPairs, wordStr);
  206. }
  207. void StandardChars<wchar_t>::SetNonWordChars(ArenaAllocator* setAllocator, CharSet<Char> &set)
  208. {
  209. set.SetNotRanges(setAllocator, numWordPairs, wordStr);
  210. }
  211. void StandardChars<wchar_t>::SetNewline(ArenaAllocator* setAllocator, CharSet<Char> &set)
  212. {
  213. set.SetRanges(setAllocator, numNewlinePairs, newlineStr);
  214. }
  215. void StandardChars<wchar_t>::SetNonNewline(ArenaAllocator* setAllocator, CharSet<Char> &set)
  216. {
  217. set.SetNotRanges(setAllocator, numNewlinePairs, newlineStr);
  218. }
  219. CharSet<wchar_t>* StandardChars<wchar_t>::GetFullSet()
  220. {
  221. if (fullSet == 0)
  222. {
  223. fullSet = Anew(allocator, UnicodeCharSet);
  224. fullSet->SetRange(allocator, MinChar, MaxChar);
  225. }
  226. return fullSet;
  227. }
  228. CharSet<wchar_t>* StandardChars<wchar_t>::GetEmptySet()
  229. {
  230. if (emptySet == 0)
  231. {
  232. emptySet = Anew(allocator, UnicodeCharSet);
  233. // leave empty
  234. }
  235. return emptySet;
  236. }
  237. CharSet<wchar_t>* StandardChars<wchar_t>::GetWordSet()
  238. {
  239. if (wordSet == 0)
  240. {
  241. wordSet = Anew(allocator, UnicodeCharSet);
  242. wordSet->SetRanges(allocator, numWordPairs, wordStr);
  243. }
  244. return wordSet;
  245. }
  246. CharSet<wchar_t>* StandardChars<wchar_t>::GetNonWordSet()
  247. {
  248. if (nonWordSet == 0)
  249. {
  250. nonWordSet = Anew(allocator, UnicodeCharSet);
  251. nonWordSet->SetNotRanges(allocator, numWordPairs, wordStr);
  252. }
  253. return nonWordSet;
  254. }
  255. CharSet<wchar_t>* StandardChars<wchar_t>::GetNewlineSet()
  256. {
  257. if (newlineSet == 0)
  258. {
  259. newlineSet = Anew(allocator, UnicodeCharSet);
  260. newlineSet->SetRanges(allocator, numNewlinePairs, newlineStr);
  261. }
  262. return newlineSet;
  263. }
  264. CharSet<wchar_t>* StandardChars<wchar_t>::GetWhitespaceSet()
  265. {
  266. if (whitespaceSet == 0)
  267. {
  268. whitespaceSet = Anew(allocator, UnicodeCharSet);
  269. whitespaceSet->SetRanges(allocator, numWhitespacePairs, whitespaceStr);
  270. }
  271. return whitespaceSet;
  272. }
  273. CharSet<wchar_t>* StandardChars<wchar_t>::GetSurrogateUpperRange()
  274. {
  275. if (surrogateUpperRange == 0)
  276. {
  277. surrogateUpperRange = Anew(allocator, UnicodeCharSet);
  278. surrogateUpperRange->SetRange(allocator, (wchar_t)0xDC00u, (wchar_t)0xDFFFu);
  279. }
  280. return surrogateUpperRange;
  281. }
  282. }