xpathkw.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. {
  2. This file is part of the Free Component Library
  3. A perfect hash for XPath keywords
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. const
  11. XPathKeywords: array [TXPathKeyword] of PWideChar = (
  12. '',
  13. #08'ancestor',
  14. #16'ancestor-or-self',
  15. #09'attribute',
  16. #05'child',
  17. #10'descendant',
  18. #18'descendant-or-self',
  19. #09'following',
  20. #17'following-sibling',
  21. #09'namespace',
  22. #06'parent',
  23. #09'preceding',
  24. #17'preceding-sibling',
  25. #04'self',
  26. #07'comment',
  27. #04'text',
  28. #22'processing-instruction',
  29. #04'node',
  30. #03'and',
  31. #02'or',
  32. #03'div',
  33. #03'mod',
  34. #04'last',
  35. #08'position',
  36. #05'count',
  37. #02'id',
  38. #10'local-name',
  39. #13'namespace-uri',
  40. #04'name',
  41. #06'string',
  42. #06'concat',
  43. #11'starts-with',
  44. #08'contains',
  45. #16'substring-before',
  46. #15'substring-after',
  47. #09'substring',
  48. #13'string-length',
  49. #15'normalize-space',
  50. #09'translate',
  51. #07'boolean',
  52. #03'not',
  53. #04'true',
  54. #05'false',
  55. #04'lang',
  56. #06'number',
  57. #03'sum',
  58. #05'floor',
  59. #07'ceiling',
  60. #05'round'
  61. );
  62. { The following code is not very maintainable because it was hand-ported from
  63. C code generated by gperf. Unless a tool like gperf is ported or modified to
  64. generate Pascal, modifying it will be painful.
  65. The good side is that one shouldn't ever need to modify it. }
  66. MaxHash = 55;
  67. KeywordIndex: array[0..MaxHash-1] of TXPathKeyword = (
  68. xkNone, xkNone,
  69. xkId,
  70. xkNone, xkNone, xkNone,
  71. xkString,
  72. xkSum,
  73. xkParent,
  74. xkSubstring,
  75. xkNone,
  76. xkComment,
  77. xkName,
  78. xkStringLength,
  79. xkNumber,
  80. xkSubstringAfter,
  81. xkSubstringBefore,
  82. xkNamespace,
  83. xkFloor,
  84. xkNormalizeSpace,
  85. xkSelf,
  86. xkNamespaceUri,
  87. xkPreceding,
  88. xkOr,
  89. xkPosition,
  90. xkText,
  91. xkProcessingInstruction,
  92. xkConcat,
  93. xkLast,
  94. xkContains,
  95. xkPrecedingSibling,
  96. xkAncestor,
  97. xkFalse,
  98. xkLocalName,
  99. xkCount,
  100. xkLang,
  101. xkFollowing,
  102. xkDescendant,
  103. xkNode,
  104. xkAncestorOrSelf,
  105. xkBoolean,
  106. xkNot,
  107. xkStartsWith,
  108. xkAnd,
  109. xkFollowingSibling,
  110. xkDescendantOrSelf,
  111. xkChild,
  112. xkTrue,
  113. xkCeiling,
  114. xkMod,
  115. xkDiv,
  116. xkRound,
  117. xkNone,
  118. xkAttribute,
  119. xkTranslate
  120. );
  121. AssoValues: array[97..122] of Byte = (
  122. 10, 31, 0, 13, 30, 11, 55, 55, 0, 41,
  123. 55, 10, 16, 4, 21, 2, 55, 17, 0, 14,
  124. 34, 29, 34, 55, 7, 55
  125. );
  126. function LookupXPathKeyword(p: PWideChar; Len: Integer): TXPathKeyword;
  127. var
  128. hash: Integer;
  129. p1: PWideChar;
  130. begin
  131. result := xkNone;
  132. hash := Len;
  133. if Len >= 1 then
  134. begin
  135. if (p^ >= 'a') and (p^ <= 'y') then
  136. Inc(hash, AssoValues[ord(p^)])
  137. else
  138. Exit;
  139. if Len > 2 then
  140. if (p[2] >= 'a') and (p[2] <= 'y') then
  141. Inc(hash, AssoValues[ord(p[2])+1])
  142. else
  143. Exit;
  144. end;
  145. if (hash >= 0) and (hash <= MaxHash) then
  146. begin
  147. p1 := XPathKeywords[KeywordIndex[hash]];
  148. if (ord(p1^) = Len) and
  149. CompareMem(p, p1+1, Len*sizeof(WideChar)) then
  150. Result := KeywordIndex[hash];
  151. end;
  152. end;