1fb527f03a9977233377a1796c0c2c47e656993c
[iotcloud.git] / version2 / src / C / IoTString.h
1 #ifndef IOTSTRING_H
2 #define IOTSTRING_H
3
4 #include "array.h"
5 #include <string.h>
6 /**
7  * IoTString wraps the underlying char string.
8  * @author Brian Demsky <bdemsky@uci.edu>
9  * @version 1.0
10  */
11
12 inline unsigned int hashCharArray(Array<char> *array) {
13         uint len = array->length();
14         unsigned int hash = 0;
15         for (uint i = 0; i < len; i++) {
16                 hash = 31 * hash + array->get(i);
17         }
18         return hash;
19 }
20
21 class IoTString {
22 private:
23         Array<char> *array;
24         IoTString() {}
25         unsigned int hashvalue;
26         /**
27          * Builds an IoTString object around the char array.  This
28          * constructor makes a copy, so the caller is free to modify the char array.
29          */
30
31 public:
32         IoTString(Array<char> *_array) :
33                 array(new Array<char>(_array)),
34                 hashvalue(hashCharArray(array)) {
35         }
36
37         IoTString(const char *_array) {
38                 int32_t len = strlen(_array);
39                 array = new Array<char>(len);
40                 memcpy(array->internalArray(), _array, len);
41                 hashvalue = hashCharArray(array);
42         }
43
44         IoTString(IoTString *string) :
45                 array(new Array<char>(string->array)),
46                 hashvalue(hashCharArray(array)) {
47         }
48
49         ~IoTString() {
50                 delete array;
51         }
52
53         char get(uint i) {return array->get(i);}
54
55         /**
56          * Internal method to grab a reference to our char array.  Caller
57          * must not modify it.
58          */
59
60         Array<char> *internalBytes() { return array; }
61
62         /**
63          * Returns a copy of the underlying char string.
64          */
65
66         Array<char> *getBytes() { return new Array<char>(array); }
67
68         /**
69          * Returns the length in chars of the IoTString.
70          */
71
72         bool equals(IoTString *str) {
73                 uint strlength = str->array->length();
74                 uint thislength = array->length();
75                 if (strlength != thislength)
76                         return false;
77
78                 int result = memcmp(str->array->internalArray(), array->internalArray(), strlength);
79                 return result == 0;
80         }
81
82         unsigned int hashValue() { return hashvalue;}
83         int length() { return array->length(); }
84         friend IoTString *IoTString_shallow(Array<char> *_array);
85 };
86
87 inline IoTString *IoTString_shallow(Array<char> *_array) {
88         IoTString *str = new IoTString();
89         str->array = _array;
90         return str;
91 }
92
93 inline unsigned int hashString(IoTString *a) {
94         return a->hashValue();
95 }
96
97 inline bool StringEquals(IoTString *a, IoTString *b) {
98         return a->equals(b);
99 }
100 #endif