Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*----------------------------------------------------------------------------*-
- ========================================
- y_stringhash - Fast string comparisons
- ========================================
- Description:
- Allows you to hash strings at compile time to use them in a switch.
- Legal:
- Version: MPL 1.1
- The contents of this file are subject to the Mozilla Public License Version
- 1.1 (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.mozilla.org/MPL/
- Software distributed under the License is distributed on an "AS IS" basis,
- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- for the specific language governing rights and limitations under the
- License.
- The Original Code is the SA:MP compiler time string hashing include.
- The Initial Developer of the Original Code is Alex "Y_Less" Cole.
- Portions created by the Initial Developer are Copyright (C) 2008
- the Initial Developer. All Rights Reserved.
- Special Thanks to:
- SA:MP Team past, present and future
- Version:
- 1.0
- Changelog:
- 19/08/10:
- First version.
- Functions:
- stock:
- Hash - Generate a string hash at run time.
- Definitions:
- _H - Generate a string hash at compile time.
- _I - Generate a case insensitive string hash at compile time.
- -*----------------------------------------------------------------------------*/
- // Case sensitive.
- // The bit that actually does the work.
- #define _DOHASH(%0,%1,%2) (_HASH%1(%2)*33+%0)
- // Space.
- #define _HASH(%0) _DOHASH(32,%0)
- // Numbers.
- #define _HASH0(%0) _DOHASH(48,%0)
- #define _HASH1(%0) _DOHASH(49,%0)
- #define _HASH2(%0) _DOHASH(50,%0)
- #define _HASH3(%0) _DOHASH(51,%0)
- #define _HASH4(%0) _DOHASH(52,%0)
- #define _HASH5(%0) _DOHASH(53,%0)
- #define _HASH6(%0) _DOHASH(54,%0)
- #define _HASH7(%0) _DOHASH(55,%0)
- #define _HASH8(%0) _DOHASH(56,%0)
- #define _HASH9(%0) _DOHASH(57,%0)
- // Upper case letters.
- #define _HASHA(%0) _DOHASH(65,%0)
- #define _HASHB(%0) _DOHASH(66,%0)
- #define _HASHC(%0) _DOHASH(67,%0)
- #define _HASHD(%0) _DOHASH(68,%0)
- #define _HASHE(%0) _DOHASH(69,%0)
- #define _HASHF(%0) _DOHASH(70,%0)
- #define _HASHG(%0) _DOHASH(71,%0)
- #define _HASHH(%0) _DOHASH(72,%0)
- #define _HASHI(%0) _DOHASH(73,%0)
- #define _HASHJ(%0) _DOHASH(74,%0)
- #define _HASHK(%0) _DOHASH(75,%0)
- #define _HASHL(%0) _DOHASH(76,%0)
- #define _HASHM(%0) _DOHASH(77,%0)
- #define _HASHN(%0) _DOHASH(78,%0)
- #define _HASHO(%0) _DOHASH(79,%0)
- #define _HASHP(%0) _DOHASH(80,%0)
- #define _HASHQ(%0) _DOHASH(81,%0)
- #define _HASHR(%0) _DOHASH(82,%0)
- #define _HASHS(%0) _DOHASH(83,%0)
- #define _HASHT(%0) _DOHASH(84,%0)
- #define _HASHU(%0) _DOHASH(85,%0)
- #define _HASHV(%0) _DOHASH(86,%0)
- #define _HASHW(%0) _DOHASH(87,%0)
- #define _HASHX(%0) _DOHASH(88,%0)
- #define _HASHY(%0) _DOHASH(89,%0)
- #define _HASHZ(%0) _DOHASH(90,%0)
- // Underscore.
- #define _HASH_(%0) _DOHASH(95,%0)
- // Lower case letters.
- #define _HASHa(%0) _DOHASH(97,%0)
- #define _HASHb(%0) _DOHASH(98,%0)
- #define _HASHc(%0) _DOHASH(99,%0)
- #define _HASHd(%0) _DOHASH(100,%0)
- #define _HASHe(%0) _DOHASH(101,%0)
- #define _HASHf(%0) _DOHASH(102,%0)
- #define _HASHg(%0) _DOHASH(103,%0)
- #define _HASHh(%0) _DOHASH(104,%0)
- #define _HASHi(%0) _DOHASH(105,%0)
- #define _HASHj(%0) _DOHASH(106,%0)
- #define _HASHk(%0) _DOHASH(107,%0)
- #define _HASHl(%0) _DOHASH(108,%0)
- #define _HASHm(%0) _DOHASH(109,%0)
- #define _HASHn(%0) _DOHASH(110,%0)
- #define _HASHo(%0) _DOHASH(111,%0)
- #define _HASHp(%0) _DOHASH(112,%0)
- #define _HASHq(%0) _DOHASH(113,%0)
- #define _HASHr(%0) _DOHASH(114,%0)
- #define _HASHs(%0) _DOHASH(115,%0)
- #define _HASHt(%0) _DOHASH(116,%0)
- #define _HASHu(%0) _DOHASH(117,%0)
- #define _HASHv(%0) _DOHASH(118,%0)
- #define _HASHw(%0) _DOHASH(119,%0)
- #define _HASHx(%0) _DOHASH(120,%0)
- #define _HASHy(%0) _DOHASH(121,%0)
- #define _HASHz(%0) _DOHASH(122,%0)
- // String end.
- #define _HASH@(%0) -1
- // Internal call.
- #define _REHASH(%0,%1) _HASH%0(%1)
- // Signify the end with two "@" symbols.
- #define HASH(%0) _REHASH(%0,@,@)
- #define _H(%0) _REHASH(%0,@,@)
- // Case insensitive
- // The bit that actually does the work.
- #define _DOHASI(%0,%1,%2) (_HASI%1(%2)*33+%0)
- // Space.
- #define _HASI(%0) _DOHASI(32,%0)
- // Numbers.
- #define _HASI0(%0) _DOHASI(48,%0)
- #define _HASI1(%0) _DOHASI(49,%0)
- #define _HASI2(%0) _DOHASI(50,%0)
- #define _HASI3(%0) _DOHASI(51,%0)
- #define _HASI4(%0) _DOHASI(52,%0)
- #define _HASI5(%0) _DOHASI(53,%0)
- #define _HASI6(%0) _DOHASI(54,%0)
- #define _HASI7(%0) _DOHASI(55,%0)
- #define _HASI8(%0) _DOHASI(56,%0)
- #define _HASI9(%0) _DOHASI(57,%0)
- // Upper case letters.
- #define _HASIA(%0) _DOHASI(65,%0)
- #define _HASIB(%0) _DOHASI(66,%0)
- #define _HASIC(%0) _DOHASI(67,%0)
- #define _HASID(%0) _DOHASI(68,%0)
- #define _HASIE(%0) _DOHASI(69,%0)
- #define _HASIF(%0) _DOHASI(70,%0)
- #define _HASIG(%0) _DOHASI(71,%0)
- #define _HASIH(%0) _DOHASI(72,%0)
- #define _HASII(%0) _DOHASI(73,%0)
- #define _HASIJ(%0) _DOHASI(74,%0)
- #define _HASIK(%0) _DOHASI(75,%0)
- #define _HASIL(%0) _DOHASI(76,%0)
- #define _HASIM(%0) _DOHASI(77,%0)
- #define _HASIN(%0) _DOHASI(78,%0)
- #define _HASIO(%0) _DOHASI(79,%0)
- #define _HASIP(%0) _DOHASI(80,%0)
- #define _HASIQ(%0) _DOHASI(81,%0)
- #define _HASIR(%0) _DOHASI(82,%0)
- #define _HASIS(%0) _DOHASI(83,%0)
- #define _HASIT(%0) _DOHASI(84,%0)
- #define _HASIU(%0) _DOHASI(85,%0)
- #define _HASIV(%0) _DOHASI(86,%0)
- #define _HASIW(%0) _DOHASI(87,%0)
- #define _HASIX(%0) _DOHASI(88,%0)
- #define _HASIY(%0) _DOHASI(89,%0)
- #define _HASIZ(%0) _DOHASI(90,%0)
- // Underscore.
- #define _HASI_(%0) _DOHASI(95,%0)
- // Lower case letters.
- #define _HASIa(%0) _DOHASI(65,%0)
- #define _HASIb(%0) _DOHASI(66,%0)
- #define _HASIc(%0) _DOHASI(67,%0)
- #define _HASId(%0) _DOHASI(68,%0)
- #define _HASIe(%0) _DOHASI(69,%0)
- #define _HASIf(%0) _DOHASI(70,%0)
- #define _HASIg(%0) _DOHASI(71,%0)
- #define _HASIh(%0) _DOHASI(72,%0)
- #define _HASIi(%0) _DOHASI(73,%0)
- #define _HASIj(%0) _DOHASI(74,%0)
- #define _HASIk(%0) _DOHASI(75,%0)
- #define _HASIl(%0) _DOHASI(76,%0)
- #define _HASIm(%0) _DOHASI(77,%0)
- #define _HASIn(%0) _DOHASI(78,%0)
- #define _HASIo(%0) _DOHASI(79,%0)
- #define _HASIp(%0) _DOHASI(80,%0)
- #define _HASIq(%0) _DOHASI(81,%0)
- #define _HASIr(%0) _DOHASI(82,%0)
- #define _HASIs(%0) _DOHASI(83,%0)
- #define _HASIt(%0) _DOHASI(84,%0)
- #define _HASIu(%0) _DOHASI(85,%0)
- #define _HASIv(%0) _DOHASI(86,%0)
- #define _HASIw(%0) _DOHASI(87,%0)
- #define _HASIx(%0) _DOHASI(88,%0)
- #define _HASIy(%0) _DOHASI(89,%0)
- #define _HASIz(%0) _DOHASI(90,%0)
- // String end.
- #define _HASI@(%0) -1
- // Internal call.
- #define _REHASI(%0,%1) _HASI%0(%1)
- // Signify the end with two "@" symbols.
- #define HASI(%0) _REHASI(%0,@,@)
- #define _I(%0) _REHASI(%0,@,@)
- /*----------------------------------------------------------------------------*-
- Function:
- Hash
- Params:
- str - The string to hash.
- sensitive - Wether the hash is case sensitive or not.
- Return:
- The reverse Bernstein hash of the string.
- Notes:
- Based on Bernstein hash, but backwards to match the macros. The only
- characters which can be used in the compile time version of this code are:
- a-z, A-Z, 0-9, _ and space.
- native Hash(str[]);
- -*----------------------------------------------------------------------------*/
- stock YHash(str[], bool:sensitive = true)
- {
- if (sensitive)
- {
- new
- len = strlen(str),
- hash = -1;
- while (len--)
- {
- hash = hash * 33 + str[len];
- }
- return hash;
- }
- else
- {
- new
- len = strlen(str),
- hash = -1;
- while (len--)
- {
- new
- ch = str[len];
- if (ch >= 'a' && ch <= 'z')
- {
- ch &= ~0x20;
- }
- hash = hash * 33 + ch;
- }
- return hash;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment