Y_Less

y_stringhash 1.0

Aug 19th, 2010
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.58 KB | None | 0 0
  1. /*----------------------------------------------------------------------------*-
  2.                     ========================================
  3.                      y_stringhash - Fast string comparisons
  4.                     ========================================
  5. Description:
  6.     Allows you to hash strings at compile time to use them in a switch.
  7. Legal:
  8.     Version: MPL 1.1
  9.    
  10.     The contents of this file are subject to the Mozilla Public License Version
  11.     1.1 (the "License"); you may not use this file except in compliance with
  12.     the License. You may obtain a copy of the License at
  13.     http://www.mozilla.org/MPL/
  14.    
  15.     Software distributed under the License is distributed on an "AS IS" basis,
  16.     WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  17.     for the specific language governing rights and limitations under the
  18.     License.
  19.    
  20.     The Original Code is the SA:MP compiler time string hashing include.
  21.    
  22.     The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  23.     Portions created by the Initial Developer are Copyright (C) 2008
  24.     the Initial Developer. All Rights Reserved.
  25.    
  26.     Special Thanks to:
  27.    
  28.     SA:MP Team past, present and future
  29. Version:
  30.     1.0
  31. Changelog:
  32.     19/08/10:
  33.         First version.
  34. Functions:
  35.     stock:
  36.         Hash - Generate a string hash at run time.
  37. Definitions:
  38.     _H - Generate a string hash at compile time.
  39.     _I - Generate a case insensitive string hash at compile time.
  40. -*----------------------------------------------------------------------------*/
  41.  
  42. // Case sensitive.
  43. // The bit that actually does the work.
  44. #define _DOHASH(%0,%1,%2) (_HASH%1(%2)*33+%0)
  45.  
  46. // Space.
  47. #define _HASH(%0) _DOHASH(32,%0)
  48.  
  49. // Numbers.
  50. #define _HASH0(%0) _DOHASH(48,%0)
  51. #define _HASH1(%0) _DOHASH(49,%0)
  52. #define _HASH2(%0) _DOHASH(50,%0)
  53. #define _HASH3(%0) _DOHASH(51,%0)
  54. #define _HASH4(%0) _DOHASH(52,%0)
  55. #define _HASH5(%0) _DOHASH(53,%0)
  56. #define _HASH6(%0) _DOHASH(54,%0)
  57. #define _HASH7(%0) _DOHASH(55,%0)
  58. #define _HASH8(%0) _DOHASH(56,%0)
  59. #define _HASH9(%0) _DOHASH(57,%0)
  60.  
  61. // Upper case letters.
  62. #define _HASHA(%0) _DOHASH(65,%0)
  63. #define _HASHB(%0) _DOHASH(66,%0)
  64. #define _HASHC(%0) _DOHASH(67,%0)
  65. #define _HASHD(%0) _DOHASH(68,%0)
  66. #define _HASHE(%0) _DOHASH(69,%0)
  67. #define _HASHF(%0) _DOHASH(70,%0)
  68. #define _HASHG(%0) _DOHASH(71,%0)
  69. #define _HASHH(%0) _DOHASH(72,%0)
  70. #define _HASHI(%0) _DOHASH(73,%0)
  71. #define _HASHJ(%0) _DOHASH(74,%0)
  72. #define _HASHK(%0) _DOHASH(75,%0)
  73. #define _HASHL(%0) _DOHASH(76,%0)
  74. #define _HASHM(%0) _DOHASH(77,%0)
  75. #define _HASHN(%0) _DOHASH(78,%0)
  76. #define _HASHO(%0) _DOHASH(79,%0)
  77. #define _HASHP(%0) _DOHASH(80,%0)
  78. #define _HASHQ(%0) _DOHASH(81,%0)
  79. #define _HASHR(%0) _DOHASH(82,%0)
  80. #define _HASHS(%0) _DOHASH(83,%0)
  81. #define _HASHT(%0) _DOHASH(84,%0)
  82. #define _HASHU(%0) _DOHASH(85,%0)
  83. #define _HASHV(%0) _DOHASH(86,%0)
  84. #define _HASHW(%0) _DOHASH(87,%0)
  85. #define _HASHX(%0) _DOHASH(88,%0)
  86. #define _HASHY(%0) _DOHASH(89,%0)
  87. #define _HASHZ(%0) _DOHASH(90,%0)
  88.  
  89. // Underscore.
  90. #define _HASH_(%0) _DOHASH(95,%0)
  91.  
  92. // Lower case letters.
  93. #define _HASHa(%0) _DOHASH(97,%0)
  94. #define _HASHb(%0) _DOHASH(98,%0)
  95. #define _HASHc(%0) _DOHASH(99,%0)
  96. #define _HASHd(%0) _DOHASH(100,%0)
  97. #define _HASHe(%0) _DOHASH(101,%0)
  98. #define _HASHf(%0) _DOHASH(102,%0)
  99. #define _HASHg(%0) _DOHASH(103,%0)
  100. #define _HASHh(%0) _DOHASH(104,%0)
  101. #define _HASHi(%0) _DOHASH(105,%0)
  102. #define _HASHj(%0) _DOHASH(106,%0)
  103. #define _HASHk(%0) _DOHASH(107,%0)
  104. #define _HASHl(%0) _DOHASH(108,%0)
  105. #define _HASHm(%0) _DOHASH(109,%0)
  106. #define _HASHn(%0) _DOHASH(110,%0)
  107. #define _HASHo(%0) _DOHASH(111,%0)
  108. #define _HASHp(%0) _DOHASH(112,%0)
  109. #define _HASHq(%0) _DOHASH(113,%0)
  110. #define _HASHr(%0) _DOHASH(114,%0)
  111. #define _HASHs(%0) _DOHASH(115,%0)
  112. #define _HASHt(%0) _DOHASH(116,%0)
  113. #define _HASHu(%0) _DOHASH(117,%0)
  114. #define _HASHv(%0) _DOHASH(118,%0)
  115. #define _HASHw(%0) _DOHASH(119,%0)
  116. #define _HASHx(%0) _DOHASH(120,%0)
  117. #define _HASHy(%0) _DOHASH(121,%0)
  118. #define _HASHz(%0) _DOHASH(122,%0)
  119.  
  120. // String end.
  121. #define _HASH@(%0) -1
  122.  
  123. // Internal call.
  124. #define _REHASH(%0,%1) _HASH%0(%1)
  125.  
  126. // Signify the end with two "@" symbols.
  127. #define HASH(%0) _REHASH(%0,@,@)
  128.  
  129. #define _H(%0) _REHASH(%0,@,@)
  130.  
  131. // Case insensitive
  132. // The bit that actually does the work.
  133. #define _DOHASI(%0,%1,%2) (_HASI%1(%2)*33+%0)
  134.  
  135. // Space.
  136. #define _HASI(%0) _DOHASI(32,%0)
  137.  
  138. // Numbers.
  139. #define _HASI0(%0) _DOHASI(48,%0)
  140. #define _HASI1(%0) _DOHASI(49,%0)
  141. #define _HASI2(%0) _DOHASI(50,%0)
  142. #define _HASI3(%0) _DOHASI(51,%0)
  143. #define _HASI4(%0) _DOHASI(52,%0)
  144. #define _HASI5(%0) _DOHASI(53,%0)
  145. #define _HASI6(%0) _DOHASI(54,%0)
  146. #define _HASI7(%0) _DOHASI(55,%0)
  147. #define _HASI8(%0) _DOHASI(56,%0)
  148. #define _HASI9(%0) _DOHASI(57,%0)
  149.  
  150. // Upper case letters.
  151. #define _HASIA(%0) _DOHASI(65,%0)
  152. #define _HASIB(%0) _DOHASI(66,%0)
  153. #define _HASIC(%0) _DOHASI(67,%0)
  154. #define _HASID(%0) _DOHASI(68,%0)
  155. #define _HASIE(%0) _DOHASI(69,%0)
  156. #define _HASIF(%0) _DOHASI(70,%0)
  157. #define _HASIG(%0) _DOHASI(71,%0)
  158. #define _HASIH(%0) _DOHASI(72,%0)
  159. #define _HASII(%0) _DOHASI(73,%0)
  160. #define _HASIJ(%0) _DOHASI(74,%0)
  161. #define _HASIK(%0) _DOHASI(75,%0)
  162. #define _HASIL(%0) _DOHASI(76,%0)
  163. #define _HASIM(%0) _DOHASI(77,%0)
  164. #define _HASIN(%0) _DOHASI(78,%0)
  165. #define _HASIO(%0) _DOHASI(79,%0)
  166. #define _HASIP(%0) _DOHASI(80,%0)
  167. #define _HASIQ(%0) _DOHASI(81,%0)
  168. #define _HASIR(%0) _DOHASI(82,%0)
  169. #define _HASIS(%0) _DOHASI(83,%0)
  170. #define _HASIT(%0) _DOHASI(84,%0)
  171. #define _HASIU(%0) _DOHASI(85,%0)
  172. #define _HASIV(%0) _DOHASI(86,%0)
  173. #define _HASIW(%0) _DOHASI(87,%0)
  174. #define _HASIX(%0) _DOHASI(88,%0)
  175. #define _HASIY(%0) _DOHASI(89,%0)
  176. #define _HASIZ(%0) _DOHASI(90,%0)
  177.  
  178. // Underscore.
  179. #define _HASI_(%0) _DOHASI(95,%0)
  180.  
  181. // Lower case letters.
  182. #define _HASIa(%0) _DOHASI(65,%0)
  183. #define _HASIb(%0) _DOHASI(66,%0)
  184. #define _HASIc(%0) _DOHASI(67,%0)
  185. #define _HASId(%0) _DOHASI(68,%0)
  186. #define _HASIe(%0) _DOHASI(69,%0)
  187. #define _HASIf(%0) _DOHASI(70,%0)
  188. #define _HASIg(%0) _DOHASI(71,%0)
  189. #define _HASIh(%0) _DOHASI(72,%0)
  190. #define _HASIi(%0) _DOHASI(73,%0)
  191. #define _HASIj(%0) _DOHASI(74,%0)
  192. #define _HASIk(%0) _DOHASI(75,%0)
  193. #define _HASIl(%0) _DOHASI(76,%0)
  194. #define _HASIm(%0) _DOHASI(77,%0)
  195. #define _HASIn(%0) _DOHASI(78,%0)
  196. #define _HASIo(%0) _DOHASI(79,%0)
  197. #define _HASIp(%0) _DOHASI(80,%0)
  198. #define _HASIq(%0) _DOHASI(81,%0)
  199. #define _HASIr(%0) _DOHASI(82,%0)
  200. #define _HASIs(%0) _DOHASI(83,%0)
  201. #define _HASIt(%0) _DOHASI(84,%0)
  202. #define _HASIu(%0) _DOHASI(85,%0)
  203. #define _HASIv(%0) _DOHASI(86,%0)
  204. #define _HASIw(%0) _DOHASI(87,%0)
  205. #define _HASIx(%0) _DOHASI(88,%0)
  206. #define _HASIy(%0) _DOHASI(89,%0)
  207. #define _HASIz(%0) _DOHASI(90,%0)
  208.  
  209. // String end.
  210. #define _HASI@(%0) -1
  211.  
  212. // Internal call.
  213. #define _REHASI(%0,%1) _HASI%0(%1)
  214.  
  215. // Signify the end with two "@" symbols.
  216. #define HASI(%0) _REHASI(%0,@,@)
  217.  
  218. #define _I(%0) _REHASI(%0,@,@)
  219.  
  220. /*----------------------------------------------------------------------------*-
  221. Function:
  222.     Hash
  223. Params:
  224.     str - The string to hash.
  225.     sensitive - Wether the hash is case sensitive or not.
  226. Return:
  227.     The reverse Bernstein hash of the string.
  228. Notes:
  229.     Based on Bernstein hash, but backwards to match the macros.  The only
  230.     characters which can be used in the compile time version of this code are:
  231.     a-z, A-Z, 0-9, _ and space.
  232.  
  233. native Hash(str[]);
  234.  
  235. -*----------------------------------------------------------------------------*/
  236.  
  237. stock YHash(str[], bool:sensitive = true)
  238. {
  239.     if (sensitive)
  240.     {
  241.         new
  242.             len = strlen(str),
  243.             hash = -1;
  244.         while (len--)
  245.         {
  246.             hash = hash * 33 + str[len];
  247.         }
  248.         return hash;
  249.     }
  250.     else
  251.     {
  252.         new
  253.             len = strlen(str),
  254.             hash = -1;
  255.         while (len--)
  256.         {
  257.             new
  258.                 ch = str[len];
  259.             if (ch >= 'a' && ch <= 'z')
  260.             {
  261.                 ch &= ~0x20;
  262.             }
  263.             hash = hash * 33 + ch;
  264.         }
  265.         return hash;
  266.     }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment