StandardChars.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. // This class is instantiated as a global const instance
  198. // C++ requires that a default constructor be provided in that case
  199. // See http://stackoverflow.com/questions/7411515/why-does-c-require-a-user-provided-default-constructor-to-default-construct-a
  200. TrivialCaseMapper() {}
  201. static const TrivialCaseMapper Instance;
  202. };
  203. template <>
  204. class StandardChars<char16> : public Chars<char16>
  205. {
  206. private:
  207. static const int numDigitPairs;
  208. static const Char* const digitStr;
  209. static const int numWhitespacePairs;
  210. static const Char* const whitespaceStr;
  211. static const int numWordPairs;
  212. static const Char* const wordStr;
  213. static const int numWordIUPairs;
  214. static const Char* const wordIUStr;
  215. static const int numNewlinePairs;
  216. static const Char* const newlineStr;
  217. ArenaAllocator* allocator;
  218. typedef CaseMapper<TrivialCaseMapper> UnicodeDataCaseMapper;
  219. const UnicodeDataCaseMapper unicodeDataCaseMapper;
  220. typedef CaseMapper<UnicodeDataCaseMapper> CaseFoldingCaseMapper;
  221. const CaseFoldingCaseMapper caseFoldingCaseMapper;
  222. CharSet<Char>* fullSet;
  223. CharSet<Char>* emptySet;
  224. CharSet<Char>* wordSet;
  225. CharSet<Char>* nonWordSet;
  226. CharSet<Char>* wordIUSet;
  227. CharSet<Char>* nonWordIUSet;
  228. CharSet<Char>* newlineSet;
  229. CharSet<Char>* whitespaceSet;
  230. CharSet<Char>* surrogateUpperRange;
  231. public:
  232. StandardChars(ArenaAllocator* allocator);
  233. inline bool IsWord(Char c) const
  234. {
  235. return CTU(c) < ASCIIChars::NumChars && ASCIIChars::IsWord(ASCIIChars::UTC(CTU(c)));
  236. }
  237. inline bool IsNewline(Char c) const
  238. {
  239. return CTU(c) < ASCIIChars::NumChars ? ASCIIChars::IsNewline(ASCIIChars::UTC(CTU(c))) : (CTU(c) & 0xfffe) == 0x2028;
  240. }
  241. inline bool IsWhitespaceOrNewline(Char c) const
  242. {
  243. if (CTU(c) < ASCIIChars::NumChars)
  244. return ASCIIChars::IsWhitespace(ASCIIChars::UTC(CTU(c)));
  245. else
  246. return CTU(c) == 0x1680 || (CTU(c) >= 0x2000 && CTU(c) <= 0x200a) ||
  247. CTU(c) == 0x2028 || CTU(c) == 0x2029 || CTU(c) == 0x202f || CTU(c) == 0x205f ||
  248. CTU(c) == 0x3000 || CTU(c) == 0xfeff;
  249. }
  250. inline bool IsLetter(Char c) const
  251. {
  252. return CTU(c) < ASCIIChars::NumChars && ASCIIChars::IsLetter(ASCIIChars::UTC(CTU(c)));
  253. }
  254. inline bool IsDigit(Char c) const
  255. {
  256. return CTU(c) < ASCIIChars::NumChars && ASCIIChars::IsDigit(ASCIIChars::UTC(CTU(c)));
  257. }
  258. inline bool IsOctal(Char c) const
  259. {
  260. return CTU(c) < ASCIIChars::NumChars && ASCIIChars::IsOctal(ASCIIChars::UTC(CTU(c)));
  261. }
  262. inline bool IsHex(Char c) const
  263. {
  264. return CTU(c) < ASCIIChars::NumChars && ASCIIChars::IsHex(ASCIIChars::UTC(CTU(c)));
  265. }
  266. inline uint DigitValue(Char c) const
  267. {
  268. return CTU(c) < ASCIIChars::NumChars ? ASCIIChars::DigitValue(ASCIIChars::UTC(CTU(c))) : 0;
  269. }
  270. void SetDigits(ArenaAllocator* setAllocator, CharSet<Char> &set);
  271. void SetNonDigits(ArenaAllocator* setAllocator, CharSet<Char> &set);
  272. void SetWhitespace(ArenaAllocator* setAllocator, CharSet<Char> &set);
  273. void SetNonWhitespace(ArenaAllocator* setAllocator, CharSet<Char> &set);
  274. void SetWordChars(ArenaAllocator* setAllocator, CharSet<Char> &set);
  275. void SetNonWordChars(ArenaAllocator* setAllocator, CharSet<Char> &set);
  276. void SetWordIUChars(ArenaAllocator* setAllocator, CharSet<Char> &set);
  277. void SetNonWordIUChars(ArenaAllocator* setAllocator, CharSet<Char> &set);
  278. void SetNewline(ArenaAllocator* setAllocator, CharSet<Char> &set);
  279. void SetNonNewline(ArenaAllocator* setAllocator, CharSet<Char> &set);
  280. void SetFullSet(ArenaAllocator* setAllocator, CharSet<Char> &set);
  281. CharSet<Char>* GetFullSet();
  282. CharSet<Char>* GetEmptySet();
  283. CharSet<Char>* GetWordSet();
  284. CharSet<Char>* GetNonWordSet();
  285. CharSet<Char>* GetNewlineSet();
  286. CharSet<Char>* GetWhitespaceSet();
  287. CharSet<Char>* GetSurrogateUpperRange();
  288. inline Char ToCanonical(CaseInsensitive::MappingSource mappingSource, Char c) const
  289. {
  290. if (mappingSource == CaseInsensitive::MappingSource::UnicodeData)
  291. {
  292. return unicodeDataCaseMapper.ToCanonical(c);
  293. }
  294. else
  295. {
  296. Assert(mappingSource == CaseInsensitive::MappingSource::CaseFolding);
  297. return caseFoldingCaseMapper.ToCanonical(c);
  298. }
  299. }
  300. CompileAssert(CaseInsensitive::EquivClassSize == 4);
  301. inline bool ToEquivs(CaseInsensitive::MappingSource mappingSource, Char c, __out_ecount(4) Char* equivs) const
  302. {
  303. if (mappingSource == CaseInsensitive::MappingSource::UnicodeData)
  304. {
  305. return unicodeDataCaseMapper.ToEquivs(c, equivs);
  306. }
  307. else
  308. {
  309. Assert(mappingSource == CaseInsensitive::MappingSource::CaseFolding);
  310. return caseFoldingCaseMapper.ToEquivs(c, equivs);
  311. }
  312. }
  313. inline bool IsTrivialString(CaseInsensitive::MappingSource mappingSource, const Char* str, CharCount strLen) const
  314. {
  315. if (mappingSource == CaseInsensitive::MappingSource::UnicodeData)
  316. {
  317. return unicodeDataCaseMapper.IsTrivialString(str, strLen);
  318. }
  319. else
  320. {
  321. Assert(mappingSource == CaseInsensitive::MappingSource::CaseFolding);
  322. return caseFoldingCaseMapper.IsTrivialString(str, strLen);
  323. }
  324. }
  325. };
  326. typedef UnifiedRegex::StandardChars<uint8> UTF8StandardChars;
  327. typedef UnifiedRegex::StandardChars<char16> UnicodeStandardChars;
  328. }