patternprops.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2011, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. * file name: patternprops.cpp
  9. * encoding: UTF-8
  10. * tab size: 8 (not used)
  11. * indentation:4
  12. *
  13. * created on: 2011mar13
  14. * created by: Markus W. Scherer
  15. */
  16. #include "unicode/utypes.h"
  17. #include "patternprops.h"
  18. U_NAMESPACE_BEGIN
  19. /*
  20. * One byte per Latin-1 character.
  21. * Bit 0 is set if either Pattern property is true,
  22. * bit 1 if Pattern_Syntax is true,
  23. * bit 2 if Pattern_White_Space is true.
  24. * That is, Pattern_Syntax is encoded as 3 and Pattern_White_Space as 5.
  25. */
  26. static const uint8_t latin1[256]={
  27. // WS: 9..D
  28. 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 0, 0,
  29. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  30. // WS: 20 Syntax: 21..2F
  31. 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  32. // Syntax: 3A..40
  33. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3,
  34. 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  35. // Syntax: 5B..5E
  36. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0,
  37. // Syntax: 60
  38. 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  39. // Syntax: 7B..7E
  40. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0,
  41. // WS: 85
  42. 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44. // Syntax: A1..A7, A9, AB, AC, AE
  45. 0, 3, 3, 3, 3, 3, 3, 3, 0, 3, 0, 3, 3, 0, 3, 0,
  46. // Syntax: B0, B1, B6, BB, BF
  47. 3, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 3,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. // Syntax: D7
  50. 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52. // Syntax: F7
  53. 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0
  54. };
  55. /*
  56. * One byte per 32 characters from U+2000..U+303F indexing into
  57. * a small table of 32-bit data words.
  58. * The first two data words are all-zeros and all-ones.
  59. */
  60. static const uint8_t index2000[130]={
  61. 2, 3, 4, 0, 0, 0, 0, 0, // 20xx
  62. 0, 0, 0, 0, 5, 1, 1, 1, // 21xx
  63. 1, 1, 1, 1, 1, 1, 1, 1, // 22xx
  64. 1, 1, 1, 1, 1, 1, 1, 1, // 23xx
  65. 1, 1, 1, 0, 0, 0, 0, 0, // 24xx
  66. 1, 1, 1, 1, 1, 1, 1, 1, // 25xx
  67. 1, 1, 1, 1, 1, 1, 1, 1, // 26xx
  68. 1, 1, 1, 6, 7, 1, 1, 1, // 27xx
  69. 1, 1, 1, 1, 1, 1, 1, 1, // 28xx
  70. 1, 1, 1, 1, 1, 1, 1, 1, // 29xx
  71. 1, 1, 1, 1, 1, 1, 1, 1, // 2Axx
  72. 1, 1, 1, 1, 1, 1, 1, 1, // 2Bxx
  73. 0, 0, 0, 0, 0, 0, 0, 0, // 2Cxx
  74. 0, 0, 0, 0, 0, 0, 0, 0, // 2Dxx
  75. 1, 1, 1, 1, 0, 0, 0, 0, // 2Exx
  76. 0, 0, 0, 0, 0, 0, 0, 0, // 2Fxx
  77. 8, 9 // 3000..303F
  78. };
  79. /*
  80. * One 32-bit integer per 32 characters. Ranges of all-false and all-true
  81. * are mapped to the first two values, other ranges map to appropriate bit patterns.
  82. */
  83. static const uint32_t syntax2000[]={
  84. 0,
  85. 0xffffffff,
  86. 0xffff0000, // 2: 2010..201F
  87. 0x7fff00ff, // 3: 2020..2027, 2030..203E
  88. 0x7feffffe, // 4: 2041..2053, 2055..205E
  89. 0xffff0000, // 5: 2190..219F
  90. 0x003fffff, // 6: 2760..2775
  91. 0xfff00000, // 7: 2794..279F
  92. 0xffffff0e, // 8: 3001..3003, 3008..301F
  93. 0x00010001 // 9: 3020, 3030
  94. };
  95. /*
  96. * Same as syntax2000, but with additional bits set for the
  97. * Pattern_White_Space characters 200E 200F 2028 2029.
  98. */
  99. static const uint32_t syntaxOrWhiteSpace2000[]={
  100. 0,
  101. 0xffffffff,
  102. 0xffffc000, // 2: 200E..201F
  103. 0x7fff03ff, // 3: 2020..2029, 2030..203E
  104. 0x7feffffe, // 4: 2041..2053, 2055..205E
  105. 0xffff0000, // 5: 2190..219F
  106. 0x003fffff, // 6: 2760..2775
  107. 0xfff00000, // 7: 2794..279F
  108. 0xffffff0e, // 8: 3001..3003, 3008..301F
  109. 0x00010001 // 9: 3020, 3030
  110. };
  111. UBool
  112. PatternProps::isSyntax(UChar32 c) {
  113. if(c<0) {
  114. return false;
  115. } else if(c<=0xff) {
  116. return (latin1[c] >> 1) & 1;
  117. } else if(c<0x2010) {
  118. return false;
  119. } else if(c<=0x3030) {
  120. uint32_t bits=syntax2000[index2000[(c-0x2000)>>5]];
  121. return (bits >> (c & 0x1f)) & 1;
  122. } else if(0xfd3e<=c && c<=0xfe46) {
  123. return c<=0xfd3f || 0xfe45<=c;
  124. } else {
  125. return false;
  126. }
  127. }
  128. UBool
  129. PatternProps::isSyntaxOrWhiteSpace(UChar32 c) {
  130. if(c<0) {
  131. return false;
  132. } else if(c<=0xff) {
  133. return latin1[c] & 1;
  134. } else if(c<0x200e) {
  135. return false;
  136. } else if(c<=0x3030) {
  137. uint32_t bits=syntaxOrWhiteSpace2000[index2000[(c-0x2000)>>5]];
  138. return (bits >> (c & 0x1f)) & 1;
  139. } else if(0xfd3e<=c && c<=0xfe46) {
  140. return c<=0xfd3f || 0xfe45<=c;
  141. } else {
  142. return false;
  143. }
  144. }
  145. UBool
  146. PatternProps::isWhiteSpace(UChar32 c) {
  147. if(c<0) {
  148. return false;
  149. } else if(c<=0xff) {
  150. return (latin1[c] >> 2) & 1;
  151. } else if(0x200e<=c && c<=0x2029) {
  152. return c<=0x200f || 0x2028<=c;
  153. } else {
  154. return false;
  155. }
  156. }
  157. const char16_t *
  158. PatternProps::skipWhiteSpace(const char16_t *s, int32_t length) {
  159. while(length>0 && isWhiteSpace(*s)) {
  160. ++s;
  161. --length;
  162. }
  163. return s;
  164. }
  165. int32_t
  166. PatternProps::skipWhiteSpace(const UnicodeString& s, int32_t start) {
  167. int32_t i = start;
  168. int32_t length = s.length();
  169. while(i<length && isWhiteSpace(s.charAt(i))) {
  170. ++i;
  171. }
  172. return i;
  173. }
  174. const char16_t *
  175. PatternProps::trimWhiteSpace(const char16_t *s, int32_t &length) {
  176. if(length<=0 || (!isWhiteSpace(s[0]) && !isWhiteSpace(s[length-1]))) {
  177. return s;
  178. }
  179. int32_t start=0;
  180. int32_t limit=length;
  181. while(start<limit && isWhiteSpace(s[start])) {
  182. ++start;
  183. }
  184. if(start<limit) {
  185. // There is non-white space at start; we will not move limit below that,
  186. // so we need not test start<limit in the loop.
  187. while(isWhiteSpace(s[limit-1])) {
  188. --limit;
  189. }
  190. }
  191. length=limit-start;
  192. return s+start;
  193. }
  194. UBool
  195. PatternProps::isIdentifier(const char16_t *s, int32_t length) {
  196. if(length<=0) {
  197. return false;
  198. }
  199. const char16_t *limit=s+length;
  200. do {
  201. if(isSyntaxOrWhiteSpace(*s++)) {
  202. return false;
  203. }
  204. } while(s<limit);
  205. return true;
  206. }
  207. const char16_t *
  208. PatternProps::skipIdentifier(const char16_t *s, int32_t length) {
  209. while(length>0 && !isSyntaxOrWhiteSpace(*s)) {
  210. ++s;
  211. --length;
  212. }
  213. return s;
  214. }
  215. U_NAMESPACE_END