-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.c
More file actions
63 lines (55 loc) · 1.33 KB
/
slice.c
File metadata and controls
63 lines (55 loc) · 1.33 KB
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
#include <ctype.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "slice2.h"
Slice* slice_construct(char const *const s, size_t const l) {
Slice* sl = malloc(sizeof(Slice));
sl->start = s;
sl->len = l;
return sl;
}
Slice slice_construct2(char const *const start, char const *const end) {
Slice sl = {start, (size_t)(end - start)};
return sl;
}
int slice_eq(const Slice* sl, char const *p) {
for(size_t i = 0; i < sl->len; i++) {
if(p[i] != sl->start[i])
return 0;
}
return p[sl->len] == 0;
}
int slice_eq2(const Slice* s1, const Slice* s2) {
if(s1->len != s2->len)
return 0;
for(size_t i = 0; i < s1->len; i++) {
if(s1->start[i] != s2->start[i])
return 0;
}
return 1;
}
int isIdentifier(const Slice* s1) {
if(s1->len == 0)
return 0;
if(!isalpha(s1->start[0]))
return 0;
for(size_t i = 1; i < s1->len; i++)
if(!isalnum(s1->start[i]))
return 0;
return 1;
}
void print(const Slice* s1) {
for (size_t i = 0; i < s1->len; i++) {
printf("%c", s1->start[i]);
}
}
size_t sliceHash(const Slice* key) {
size_t out = 5381;
for (size_t i = 0; i < key->len; i++) {
char const c = key->start[i];
out = out * 33 + c;
}
return out;
}