StandardChars.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #pragma once
  6. namespace UnifiedRegex
  7. {
  8. template <typename C>
  9. class StandardChars {};
  10. class ASCIIChars : public Chars<char>
  11. {
  12. private:
  13. enum CharClass : uint8
  14. {
  15. Word = 1 << 0,
  16. Newline = 1 << 1,
  17. Whitespace = 1 << 2,
  18. Letter = 1 << 3,
  19. Digit = 1 << 4,
  20. Octal = 1 << 5,
  21. Hex = 1 << 6
  22. };
  23. static const uint8 classes[NumChars];
  24. static const uint8 values[NumChars];
  25. public:
  26. __inline static bool IsWord(Char c)
  27. {
  28. return (classes[CTU(c)] & Word) != 0;
  29. }
  30. __inline static bool IsNewline(Char c)
  31. {
  32. return (classes[CTU(c)] & Newline) != 0;
  33. }
  34. __inline static bool IsWhitespace(Char c)
  35. {
  36. return (classes[CTU(c)] & Whitespace) != 0;
  37. }
  38. __inline static bool IsLetter(Char c)
  39. {
  40. return (classes[CTU(c)] & Letter) != 0;
  41. }
  42. __inline static bool IsDigit(Char c)
  43. {
  44. return (classes[CTU(c)] & Digit) != 0;
  45. }
  46. __inline static bool IsOctal(Char c)
  47. {
  48. return (classes[CTU(c)] & Octal) != 0;
  49. }
  50. __inline static bool IsHex(Char c)
  51. {
  52. return (classes[CTU(c)] & Hex) != 0;
  53. }
  54. __inline static uint DigitValue(Char c)
  55. {
  56. return values[CTU(c)];
  57. }
  58. };
  59. template <>
  60. class StandardChars<uint8> : Chars<uint8>
  61. {
  62. public:
  63. inline StandardChars(ArenaAllocator* allocator) {}
  64. __inline bool IsWord(Char c) const
  65. {
  66. return ASCIIChars::IsWord(ASCIIChars::UTC(CTU(c)));
  67. }
  68. __inline bool IsNewline(Char c) const
  69. {
  70. return ASCIIChars::IsNewline(ASCIIChars::UTC(CTU(c)));
  71. }
  72. __inline bool IsWhitespaceOrNewline(Char c) const
  73. {
  74. return ASCIIChars::IsWhitespace(ASCIIChars::UTC(CTU(c)));
  75. }
  76. __inline bool IsLetter(Char c) const
  77. {
  78. return ASCIIChars::IsLetter(ASCIIChars::UTC(CTU(c)));
  79. }
  80. __inline bool IsDigit(Char c) const
  81. {
  82. return ASCIIChars::IsDigit(ASCIIChars::UTC(CTU(c)));
  83. }
  84. __inline bool IsOctal(Char c) const
  85. {
  86. return ASCIIChars::IsOctal(ASCIIChars::UTC(CTU(c)));
  87. }
  88. __inline bool IsHex(Char c) const
  89. {
  90. return ASCIIChars::IsHex(ASCIIChars::UTC(CTU(c)));
  91. }
  92. __inline uint DigitValue(Char c) const
  93. {
  94. return ASCIIChars::DigitValue(ASCIIChars::UTC(CTU(c)));
  95. }
  96. };
  97. template <typename FallbackCaseMapper>
  98. class CaseMapper
  99. {
  100. public:
  101. CaseMapper(ArenaAllocator *allocator, CaseInsensitive::MappingSource mappingSource, const FallbackCaseMapper *fallbackMapper) :
  102. toEquivs((uint64) -1),
  103. fallbackMapper(fallbackMapper)
  104. {
  105. CompileAssert(sizeof(char16) == 2);
  106. CompileAssert(sizeof(uint) > sizeof(char16));
  107. const uint maxUChar = Chars<char16>::MaxUChar;
  108. uint l = 0;
  109. uint h = maxUChar;
  110. uint tblidx = 0;
  111. do {
  112. uint acth;
  113. char16 equivl[CaseInsensitive::EquivClassSize];
  114. bool isNonTrivial = CaseInsensitive::RangeToEquivClassOnlyInSource(mappingSource, tblidx, l, h, acth, equivl);
  115. if (isNonTrivial)
  116. {
  117. __assume(acth <= maxUChar); // property of algorithm: acth never greater than h
  118. do
  119. {
  120. uint64 r = 0;
  121. CompileAssert(sizeof(r) >= sizeof(char16) * CaseInsensitive::EquivClassSize);
  122. for (int i = CaseInsensitive::EquivClassSize - 1; i >= 0; i--)
  123. {
  124. __assume(equivl[i] <= maxUChar); // property of algorithm: never map outside of range
  125. r <<= 16;
  126. r |= Chars<char16>::CTU(equivl[i]++);
  127. }
  128. toEquivs.Set(allocator, Chars<char16>::UTC(l++), r);
  129. }
  130. while (l <= acth);
  131. }
  132. else
  133. {
  134. l = acth + 1;
  135. }
  136. }
  137. while (l <= h);
  138. }
  139. inline char16 ToCanonical(char16 c) const
  140. {
  141. uint64 r = toEquivs.Get(c);
  142. return r == EQUIV_MISSING ? fallbackMapper->ToCanonical(c) : Chars<char16>::UTC(r & 0xffff);
  143. }
  144. CompileAssert(CaseInsensitive::EquivClassSize == 4);
  145. inline bool ToEquivs(char16 c, __out_ecount(4) char16* equivs) const
  146. {
  147. uint64 r = toEquivs.Get(c);
  148. if (r == EQUIV_MISSING)
  149. {
  150. return fallbackMapper->ToEquivs(c, equivs);
  151. }
  152. else
  153. {
  154. for (int i = 0; i < CaseInsensitive::EquivClassSize; i++)
  155. {
  156. equivs[i] = Chars<char16>::UTC(r & 0xffff);
  157. r >>= 16;
  158. }
  159. return true;
  160. }
  161. }
  162. inline bool IsTrivialString(const char16* str, CharCount strLen) const
  163. {
  164. for (CharCount i = 0; i < strLen; i++)
  165. {
  166. if (toEquivs.Get(str[i]) != EQUIV_MISSING)
  167. return false;
  168. }
  169. return fallbackMapper->IsTrivialString(str, strLen);
  170. }
  171. private:
  172. // Map character to:
  173. // - -1 if trivial equivalence class
  174. // - otherwise to four 16-bit fields: <equiv 4><equiv 3><equiv 2><equiv 1>
  175. const static uint64 EQUIV_MISSING = static_cast<uint64>(-1);
  176. CharMap<char16, uint64> toEquivs;
  177. const FallbackCaseMapper *fallbackMapper;
  178. };
  179. class TrivialCaseMapper
  180. {
  181. public:
  182. inline char16 ToCanonical(char16 c) const
  183. {
  184. return c;
  185. }
  186. CompileAssert(CaseInsensitive::EquivClassSize == 4);
  187. inline bool ToEquivs(char16 c, __out_ecount(4) char16* equivs) const
  188. {
  189. for (int i = 0; i < CaseInsensitive::EquivClassSize; i++)
  190. equivs[i] = c;
  191. return false;
  192. }
  193. inline bool IsTrivialString(const char16* str, CharCount strLen) const
  194. {
  195. return true;
  196. }
  197. static const TrivialCaseMapper Instance;
  198. };
  199. template <>
  200. class StandardChars<char16> : public Chars<char16>
  201. {
  202. private:
  203. static const int numDigitPairs;
  204. static const Char* const digitStr;
  205. static const int numWhitespacePairs;
  206. static const Char* const whitespaceStr;
  207. static const int numWordPairs;
  208. static const Char* const wordStr;
  209. static const int numNewlinePairs;
  210. static const Char* const newlineStr;
  211. ArenaAllocator* allocator;
  212. typedef CaseMapper<TrivialCaseMapper> UnicodeDataCaseMapper;
  213. const UnicodeDataCaseMapper unicodeDataCaseMapper;
  214. typedef CaseMapper<UnicodeDataCaseMapper> CaseFoldingCaseMapper;
  215. const CaseFoldingCaseMapper caseFoldingCaseMapper;
  216. CharSet<Char>* fullSet;
  217. CharSet<Char>* emptySet;
  218. CharSet<Char>* wordSet;
  219. CharSet<Char>* nonWordSet;
  220. CharSet<Char>* newlineSet;
  221. CharSet<Char>* whitespaceSet;
  222. CharSet<Char>* surrogateUpperRange;
  223. public:
  224. StandardChars(ArenaAllocator* allocator);
  225. __inline bool IsWord(Char c) const
  226. {
  227. return CTU(c) < ASCIIChars::NumChars && ASCIIChars::IsWord(ASCIIChars::UTC(CTU(c)));
  228. }
  229. __inline bool IsNewline(Char c) const
  230. {
  231. return CTU(c) < ASCIIChars::NumChars ? ASCIIChars::IsNewline(ASCIIChars::UTC(CTU(c))) : (CTU(c) & 0xfffe) == 0x2028;
  232. }
  233. __inline bool IsWhitespaceOrNewline(Char c) const
  234. {
  235. if (CTU(c) < ASCIIChars::NumChars)
  236. return ASCIIChars::IsWhitespace(ASCIIChars::UTC(CTU(c)));
  237. else
  238. return CTU(c) == 0x1680 || CTU(c) == 0x180e || (CTU(c) >= 0x2000 && CTU(c) <= 0x200a) ||
  239. CTU(c) == 0x2028 || CTU(c) == 0x2029 || CTU(c) == 0x202f || CTU(c) == 0x205f ||
  240. CTU(c) == 0x3000 || CTU(c) == 0xfeff;
  241. }
  242. __inline bool IsLetter(Char c) const
  243. {
  244. return CTU(c) < ASCIIChars::NumChars && ASCIIChars::IsLetter(ASCIIChars::UTC(CTU(c)));
  245. }
  246. __inline bool IsDigit(Char c) const
  247. {
  248. return CTU(c) < ASCIIChars::NumChars && ASCIIChars::IsDigit(ASCIIChars::UTC(CTU(c)));
  249. }
  250. __inline bool IsOctal(Char c) const
  251. {
  252. return CTU(c) < ASCIIChars::NumChars && ASCIIChars::IsOctal(ASCIIChars::UTC(CTU(c)));
  253. }
  254. __inline bool IsHex(Char c) const
  255. {
  256. return CTU(c) < ASCIIChars::NumChars && ASCIIChars::IsHex(ASCIIChars::UTC(CTU(c)));
  257. }
  258. __inline uint DigitValue(Char c) const
  259. {
  260. return CTU(c) < ASCIIChars::NumChars ? ASCIIChars::DigitValue(ASCIIChars::UTC(CTU(c))) : 0;
  261. }
  262. void SetDigits(ArenaAllocator* setAllocator, CharSet<Char> &set);
  263. void SetNonDigits(ArenaAllocator* setAllocator, CharSet<Char> &set);
  264. void SetWhitespace(ArenaAllocator* setAllocator, CharSet<Char> &set);
  265. void SetNonWhitespace(ArenaAllocator* setAllocator, CharSet<Char> &set);
  266. void SetWordChars(ArenaAllocator* setAllocator, CharSet<Char> &set);
  267. void SetNonWordChars(ArenaAllocator* setAllocator, CharSet<Char> &set);
  268. void SetNewline(ArenaAllocator* setAllocator, CharSet<Char> &set);
  269. void SetNonNewline(ArenaAllocator* setAllocator, CharSet<Char> &set);
  270. CharSet<Char>* GetFullSet();
  271. CharSet<Char>* GetEmptySet();
  272. CharSet<Char>* GetWordSet();
  273. CharSet<Char>* GetNonWordSet();
  274. CharSet<Char>* GetNewlineSet();
  275. CharSet<Char>* GetWhitespaceSet();
  276. CharSet<Char>* GetSurrogateUpperRange();
  277. inline Char ToCanonical(CaseInsensitive::MappingSource mappingSource, Char c) const
  278. {
  279. if (mappingSource == CaseInsensitive::MappingSource::UnicodeData)
  280. {
  281. return unicodeDataCaseMapper.ToCanonical(c);
  282. }
  283. else
  284. {
  285. Assert(mappingSource == CaseInsensitive::MappingSource::CaseFolding);
  286. return caseFoldingCaseMapper.ToCanonical(c);
  287. }
  288. }
  289. CompileAssert(CaseInsensitive::EquivClassSize == 4);
  290. inline bool ToEquivs(CaseInsensitive::MappingSource mappingSource, Char c, __out_ecount(4) Char* equivs) const
  291. {
  292. if (mappingSource == CaseInsensitive::MappingSource::UnicodeData)
  293. {
  294. return unicodeDataCaseMapper.ToEquivs(c, equivs);
  295. }
  296. else
  297. {
  298. Assert(mappingSource == CaseInsensitive::MappingSource::CaseFolding);
  299. return caseFoldingCaseMapper.ToEquivs(c, equivs);
  300. }
  301. }
  302. inline bool IsTrivialString(CaseInsensitive::MappingSource mappingSource, const Char* str, CharCount strLen) const
  303. {
  304. if (mappingSource == CaseInsensitive::MappingSource::UnicodeData)
  305. {
  306. return unicodeDataCaseMapper.IsTrivialString(str, strLen);
  307. }
  308. else
  309. {
  310. Assert(mappingSource == CaseInsensitive::MappingSource::CaseFolding);
  311. return caseFoldingCaseMapper.IsTrivialString(str, strLen);
  312. }
  313. }
  314. };
  315. typedef UnifiedRegex::StandardChars<uint8> UTF8StandardChars;
  316. typedef UnifiedRegex::StandardChars<char16> UnicodeStandardChars;
  317. }