1 /*
2 Copyright (c) by respective owners including Yahoo!, Microsoft, and
3 individual contributors. All rights reserved. Released under a BSD (revised)
4 license as described in the file LICENSE.
5 */
6 #include <iostream>
7 #ifndef WIN32
8 #include <strings.h>
9 #endif
10
11 #include "parse_primitives.h"
12 #include "hash.h"
13
tokenize(char delim,substring s,v_array<substring> & ret,bool allow_empty)14 void tokenize(char delim, substring s, v_array<substring>& ret, bool allow_empty)
15 {
16 ret.erase();
17 char *last = s.begin;
18 for (; s.begin != s.end; s.begin++) {
19 if (*s.begin == delim) {
20 if (allow_empty || (s.begin != last))
21 {
22 substring temp = {last, s.begin};
23 ret.push_back(temp);
24 }
25 last = s.begin+1;
26 }
27 }
28 if (allow_empty || (s.begin != last))
29 {
30 substring final = {last, s.begin};
31 ret.push_back(final);
32 }
33 }
34
hashstring(substring s,uint32_t h)35 size_t hashstring (substring s, uint32_t h)
36 {
37 //trim leading whitespace but not UTF-8
38 for(; s.begin < s.end && *(s.begin) <= 0x20 && (int)*(s.begin)>= 0; s.begin++);
39 //trim trailing white space but not UTF-8
40 for(; s.end > s.begin && *(s.end-1) <= 0x20 && (int)*(s.end-1) >=0; s.end--);
41
42 size_t ret = 0;
43 char *p = s.begin;
44 while (p != s.end)
45 if (*p >= '0' && *p <= '9')
46 ret = 10*ret + *(p++) - '0';
47 else
48 return uniform_hash((unsigned char *)s.begin, s.end - s.begin, h);
49
50 return ret + h;
51 }
52
hashall(substring s,uint32_t h)53 size_t hashall (substring s, uint32_t h)
54 { return uniform_hash((unsigned char *)s.begin, s.end - s.begin, h); }
55
getHasher(const std::string & s)56 hash_func_t getHasher(const std::string& s){
57 if (s=="strings")
58 return hashstring;
59 else if(s=="all")
60 return hashall;
61 else{
62 std::cerr << "Unknown hash function: " << s.c_str() << ". Exiting " << std::endl;
63 throw std::exception();
64 }
65 }
66