-
Notifications
You must be signed in to change notification settings - Fork 0
/
csString.c
69 lines (61 loc) · 1.58 KB
/
csString.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "csString.h"
#include <string.h>
#include <stdlib.h>
// forward declaration of method implementations
uint32_t stringHash(csStringRef self);
bool stringEquals(csStringRef self, csAdtRef other);
int stringSize(csStringRef self);
char * stringCStr(csStringRef self);
// declare the method structure for csString
csStringMethods strMethods = {
stringHash,
stringEquals,
stringCStr,
stringSize,
stringCStr,
};
// declare csString constructor function
csStringRef newCSString(char *cStr){
csStringRef aString = (csStringRef)malloc(sizeof(csString));
if(aString == (csStringRef)NULL){
return (csStringRef)NULL;
}
aString->methods = &strMethods;
aString->size = strlen(cStr);
aString->contents = cStr;
return aString;
}
// stringHash()
// 32 bit fnv-1a hash
// assumes that the string like object
// has the char * in a field called contents
// has the string length in a field called count
#define HASH_SEED 2166136261U
#define HASH_PRIME 16777619U
uint32_t stringHash(csStringRef self)
{
uint32_t result = HASH_SEED;
int i;
for (i = 0; i < self->size; ++i) {
result = result ^ self->contents[i];
result = result * HASH_PRIME;
}
return result;
}
bool stringEquals(csStringRef self, csAdtRef other){
csStringRef strRef;
if((csAdtMethodsRef)self->methods != other->methods){
return false;
}
strRef = (csStringRef)other;
if(self->size != strRef->size){
return false;
}
return strcmp(self->contents,strRef->contents)==0;
}
int stringSize(csStringRef self){
return self->size;
}
char * stringCStr(csStringRef self){
return self->contents;
}