-
Notifications
You must be signed in to change notification settings - Fork 4
/
cowl_string.c
160 lines (131 loc) 路 4.81 KB
/
cowl_string.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
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
/**
* @author Ivano Bilenchi
*
* @copyright Copyright (c) 2019 SisInf Lab, Polytechnic University of Bari
* @copyright <http://sisinflab.poliba.it/swottools>
* @copyright SPDX-License-Identifier: EPL-2.0
*
* @file
*/
#include "cowl_string_private.h"
#include "cowl_hash_utils.h"
#include "cowl_macros.h"
#include <stdio.h>
UHASH_IMPL(CowlStringTable, cowl_string_hash, cowl_string_equals)
static UHash(CowlStringTable) *str_tbl = NULL;
void cowl_string_api_init(void) {
str_tbl = uhset_alloc(CowlStringTable);
}
void cowl_string_api_deinit(void) {
uhash_free(CowlStringTable, str_tbl);
}
cowl_struct(CowlString)* cowl_string_alloc(CowlRawString raw_string) {
CowlString init = cowl_string_init(raw_string);
cowl_struct(CowlString) *string = malloc(sizeof(*string));
memcpy(string, &init, sizeof(*string));
return string;
}
cowl_struct(CowlString) cowl_string_init(CowlRawString raw_string) {
cowl_uint_t hash = cowl_hash_2(COWL_HASH_INIT_STRING, raw_string.length,
cowl_raw_string_hash(raw_string));
CowlString init = {
.super = COWL_HASH_OBJECT_INIT(hash),
.raw_string = raw_string
};
return init;
}
static void cowl_string_free(CowlString *string) {
if (!string) return;
cowl_raw_string_deinit(string->raw_string);
free((void *)string);
}
CowlString* cowl_string_get_intern(CowlString *string, bool copy) {
if (!(string && string->raw_string.length)) return cowl_string_get_empty();
uhash_ret_t ret;
uhash_uint_t idx = uhash_put(CowlStringTable, str_tbl, string, &ret);
if (ret == UHASH_INSERTED) {
if (copy) {
string = cowl_string_copy(string);
uhash_key(str_tbl, idx) = string;
}
} else {
string = uhash_key(str_tbl, idx);
cowl_object_retain(string);
}
return string;
}
CowlString* cowl_string_copy(CowlString *string) {
cowl_uint_t hash = cowl_object_hash_get(string);
cowl_struct(CowlString) *copy = malloc(sizeof(*string));
copy->super = COWL_HASH_OBJECT_INIT(hash);
copy->raw_string = cowl_raw_string_copy(string->raw_string);
return copy;
}
void cowl_string_split_two(CowlRawString string, cowl_uint_t lhs_length, CowlString **out) {
if (lhs_length < string.length) {
out[0] = cowl_string_get(string.cstring, lhs_length, true);
out[1] = cowl_string_get(string.cstring + lhs_length, string.length - lhs_length, true);
} else {
out[0] = cowl_string_get(string.cstring, string.length, true);
out[1] = cowl_string_get_empty();
}
}
CowlString* cowl_string_get(char const *cstring, cowl_uint_t length, bool copy) {
CowlRawString raw_string = cowl_raw_string_init(cstring, length, copy);
return cowl_string_alloc(raw_string);
}
CowlString* cowl_string_get_empty(void) {
static CowlString *empty = NULL;
if (!empty) empty = cowl_string_from_static("");
return cowl_string_retain(empty);
}
CowlString* cowl_string_retain(CowlString *string) {
return cowl_object_retain(string);
}
void cowl_string_release(CowlString *string) {
if (string && !cowl_object_release(string)) {
// If the string was interned, it must also be removed from the hash set.
uhash_uint_t k = uhash_get(CowlStringTable, str_tbl, string);
if (k != UHASH_INDEX_MISSING && uhash_key(str_tbl, k) == string) {
uhash_delete(CowlStringTable, str_tbl, k);
}
cowl_string_free(string);
}
}
char const* cowl_string_release_copying_cstring(CowlString *string) {
if (!string) return NULL;
char const *cstring;
if (cowl_object_release(string)) {
cstring = strndup(string->raw_string.cstring, string->raw_string.length);
} else {
cstring = string->raw_string.cstring;
((cowl_struct(CowlString) *)string)->raw_string.cstring = NULL;
cowl_string_free(string);
}
return cstring;
}
char const* cowl_string_get_cstring(CowlString *string) {
return string->raw_string.cstring;
}
cowl_uint_t cowl_string_get_length(CowlString *string) {
return string->raw_string.length;
}
bool cowl_string_equals(CowlString *lhs, CowlString *rhs) {
return lhs->super.hash == rhs->super.hash &&
cowl_raw_string_equals(lhs->raw_string, rhs->raw_string);
}
cowl_uint_t cowl_string_hash(CowlString *string) {
return cowl_object_hash_get(string);
}
CowlString* cowl_string_with_format(char const *format, ...) {
va_list args;
va_start(args, format);
CowlRawString raw_string = cowl_raw_string_with_format_list(format, args);
va_end(args);
return cowl_string_alloc(raw_string);
}
CowlString* cowl_string_concat(CowlString *lhs, CowlString *rhs) {
CowlRawString comp[] = { lhs->raw_string, rhs->raw_string };
CowlRawString cat = cowl_raw_string_concat(cowl_array_size(comp), comp);
return cowl_string_alloc(cat);
}