-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.h
186 lines (159 loc) · 3.12 KB
/
utils.h
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* This file contains functions that are usually in the C stdlib,
* but aren't that hard to implement here for the sake of speed.
*/
#ifndef OBJC_UTILITIES_H
#define OBJC_UTILITIES_H
/* A private allocator that uses pages for allocations. */
PRIVATE char *objc_string_allocator_alloc(size_t size);
/*
* Just as the regular strlen function, returns a number of non-zero characters.
*/
static inline unsigned int
objc_strlen(const char *str)
{
unsigned int counter;
if (str == NULL){
return 0;
}
counter = 0;
while (*str != '\0') {
++counter;
++str;
}
return counter;
}
/*
* Unlike the POSIX function, this one handles allocating the new string itself.
*/
static inline char *
objc_strcpy(const char *str)
{
unsigned int len;
char *result;
char *curr_char;
if (str == NULL){
return NULL;
}
len = objc_strlen(str);
result = objc_string_allocator_alloc(len + 1); /* +1 for zero-termination */
curr_char = result;
while (*str != '\0') {
*curr_char = *str;
++curr_char;
++str;
}
*curr_char = '\0';
return result;
}
/*
* Returns YES if the strings are equal.
*/
static inline BOOL
objc_strings_equal(const char *str1, const char *str2)
{
unsigned int index;
if (str1 == str2){
return YES;
}
if (str1 == NULL || str2 == NULL){
/* Just one of them NULL */
return NO;
}
index = 0;
while (YES) {
if (str1[index] == str2[index]){
if (str1[index] == '\0'){
/* Equal */
return YES;
}
++index;
continue;
}
return NO;
}
return NO;
}
/*
* Returns YES if str has prefix pr.
*/
static inline BOOL
objc_string_has_prefix(const char *str, const char *pr)
{
int prefix_len = objc_strlen(pr);
for (int i = 0; i < prefix_len; ++i){
if (pr[i] == '\0'){
return YES;
}
if (str[i] == '\0'){
return NO;
}
if (str[i] != pr[i]){
return NO;
}
}
return YES;
}
/*
* Hashes string str.
*/
static inline uint32_t
objc_hash_string(const char *str)
{
register uint32_t hash = 0;
register int32_t c;
while ((c = *str++)){
hash = c + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
/*
* Returns long long value of the string.
*/
static inline long long
objc_string_long_long_value(const char *str)
{
long long result = 0;
char c;
while (YES){
c = *str;
if (c < '0' || c > '9'){
/* Hit non-alfa char */
return result;
}
result *= 10;
result += (c - '0');
++str;
}
return result;
}
/*
* Returns YES if ptr1 == ptr2;
*/
static inline BOOL
objc_pointers_are_equal(const void *ptr1, const void *ptr2)
{
return ptr1 == ptr2;
}
/*
* Hashes a pointer;
*/
static inline unsigned int
objc_hash_pointer(const void *ptr)
{
/*
* Bit-rotate right 4, since the lowest few bits in an object pointer
* will always be 0, which is not so useful for a hash value.
*/
return (unsigned int)(((uintptr_t)ptr >> 4) |
(uintptr_t)((uintptr_t)ptr << (uintptr_t)((sizeof(id) * 8) - 4)));
}
/*
* Copies memory from source to destination.
*/
#define objc_copy_memory(dest, src, len) memcpy(dest, src, len)
/*
* Clears memory by writing zeroes everywhere.
*/
#define objc_memory_zero(mem, size) memset(mem, 0, size)
#endif /* !OBJC_UTILITIES_H */