diff options
| author | Tyge Løvset <[email protected]> | 2022-04-24 13:31:11 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-04-24 13:31:11 +0200 |
| commit | 6d8406e66c029d4c672bb7531aabec7eb1d079d8 (patch) | |
| tree | 4bd1e513a04000d3e4140857fcae032f05abeba0 /include | |
| parent | 2b74b8a880d48232892ca84ac29efacd66905cd4 (diff) | |
| download | STC-modified-6d8406e66c029d4c672bb7531aabec7eb1d079d8.tar.gz STC-modified-6d8406e66c029d4c672bb7531aabec7eb1d079d8.zip | |
Created VERSION 3.5. See News section in docs for changes.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/ccommon.h | 19 | ||||
| -rw-r--r-- | include/stc/cstr.h | 74 | ||||
| -rw-r--r-- | include/stc/csview.h | 17 | ||||
| -rw-r--r-- | include/stc/template.h | 5 |
4 files changed, 60 insertions, 55 deletions
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 56dd501e..1c19bde7 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -112,27 +112,24 @@ typedef const char* crawstr; #define _c_ROTL(x, k) (x << (k) | x >> (8*sizeof(x) - (k)))
-STC_INLINE uint64_t c_strhash(const char *s) {
- int c; uint64_t h = *s++;
- if (h) while ((c = *s++)) h = (h << 10) - h + c;
- return _c_ROTL(h, 26) ^ h;
-}
-
STC_INLINE uint64_t c_default_hash(const void* key, size_t len) {
- uint64_t u8, h = 1;
+ uint64_t u8, h = 1; size_t n = len >> 3;
uint32_t u4;
const uint8_t *x = (const uint8_t*) key;
- for (size_t n = len >> 3; n--; )
- memcpy(&u8, x, 8), x += 8, h += u8*0xc6a4a7935bd1e99d;
+ while (n--)
+ memcpy(&u8, x, 8), x += 8, h += (h << 10) ^ (u8*0xc6a4a7935bd1e99d);
switch (len &= 7) {
- case 0: return h;
- case 4: memcpy(&u4, x, 4); return h + u4*0xc6a4a7935bd1e99d;
+ case 0: return h;
+ case 4: memcpy(&u4, x, 4); return h + u4*0xc6a4a7935bd1e99d;
}
h += *x++;
while (--len) h = (h << 10) - h + *x++;
return _c_ROTL(h, 26) ^ h;
}
+STC_INLINE uint64_t c_strhash(const char *s)
+ { return c_default_hash(s, strlen(s)); }
+
STC_INLINE char* c_strnstrn(const char *s, const char *needle, size_t slen, const size_t nlen) {
if (!nlen) return (char *)s;
if (nlen > slen) return NULL;
diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 9c6568f7..b669b6d5 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -87,12 +87,17 @@ STC_API void cstr_erase_n(cstr* self, size_t pos, size_t n); STC_API cstr cstr_from_fmt(const char* fmt, ...);
STC_API int cstr_printf(cstr* self, const char* fmt, ...);
STC_API void cstr_replace_all(cstr* self, const char* find, const char* repl);
+STC_API cstr cstr_from_replace_all_sv(csview sv, csview find, csview repl);
STC_INLINE cstr_rep_t cstr_rep(cstr* s) {
return cstr_is_long(s)
? c_make(cstr_rep_t){s->lon.data, cstr_l_size(s), cstr_l_cap(s)}
: c_make(cstr_rep_t){s->sml.data, cstr_s_size(s), cstr_s_cap};
}
+STC_INLINE csview cstr_sv(const cstr* s) {
+ return cstr_is_long(s) ? c_make(csview){s->lon.data, cstr_l_size(s)}
+ : c_make(csview){s->sml.data, cstr_s_size(s)};
+}
STC_INLINE cstr cstr_init(void)
{ return cstr_null; }
@@ -132,8 +137,8 @@ STC_INLINE cstr cstr_move(cstr* self) { }
STC_INLINE cstr cstr_clone(cstr s) {
- cstr_rep_t r = cstr_rep(&s);
- return cstr_from_n(r.data, r.size);
+ csview sv = cstr_sv(&s);
+ return cstr_from_n(sv.str, sv.size);
}
STC_INLINE void cstr_drop(cstr* self) {
@@ -169,17 +174,19 @@ STC_INLINE size_t cstr_length(cstr s) STC_INLINE size_t cstr_capacity(cstr s)
{ return cstr_is_long(&s) ? cstr_l_cap(&s) : cstr_s_cap; }
+STC_INLINE int cstr_cmp(const cstr* s1, const cstr* s2)
+ { return strcmp(cstr_str(s1), cstr_str(s2)); }
+
+STC_INLINE bool cstr_eq(const cstr* s1, const cstr* s2) {
+ csview x = cstr_sv(s1), y = cstr_sv(s2);
+ return x.size == y.size && !memcmp(x.str, y.str, x.size);
+}
+
STC_INLINE bool cstr_equals(cstr s1, const char* str)
{ return strcmp(cstr_str(&s1), str) == 0; }
STC_INLINE bool cstr_equals_s(cstr s1, cstr s2)
- { return strcmp(cstr_str(&s1), cstr_str(&s2)) == 0; }
-
-STC_INLINE bool cstr_eq(const cstr* s1, const cstr* s2)
- { return strcmp(cstr_str(s1), cstr_str(s2)) == 0; }
-
-STC_INLINE int cstr_cmp(const cstr* s1, const cstr* s2)
- { return strcmp(cstr_str(s1), cstr_str(s2)); }
+ { return cstr_cmp(&s1, &s2) == 0; }
STC_INLINE size_t cstr_find(cstr s, const char* needle) {
const char *str = cstr_str(&s), *res = strstr(str, needle);
@@ -205,8 +212,8 @@ STC_INLINE bool cstr_starts_with_s(cstr s, cstr sub) { return cstr_starts_with(s, cstr_str(&sub)); }
STC_INLINE bool cstr_ends_with(cstr s, const char* sub) {
- cstr_rep_t r = cstr_rep(&s); size_t n = strlen(sub);
- return n <= r.size && memcmp(r.data + r.size - n, sub, n) == 0;
+ csview sv = cstr_sv(&s); size_t n = strlen(sub);
+ return n <= sv.size && memcmp(sv.str + sv.size - n, sub, n) == 0;
}
STC_INLINE bool cstr_ends_with_s(cstr s, cstr sub)
@@ -216,16 +223,16 @@ STC_INLINE void cstr_assign(cstr* self, const char* str) { cstr_assign_n(self, str, strlen(str)); }
STC_INLINE void cstr_copy(cstr* self, cstr s) {
- cstr_rep_t r = cstr_rep(&s);
- cstr_assign_n(self, r.data, r.size);
+ csview sv = cstr_sv(&s);
+ cstr_assign_n(self, sv.str, sv.size);
}
STC_INLINE void cstr_append(cstr* self, const char* str)
{ cstr_append_n(self, str, strlen(str)); }
STC_INLINE void cstr_append_s(cstr* self, cstr s) {
- cstr_rep_t r = cstr_rep(&s);
- cstr_append_n(self, r.data, r.size);
+ csview sv = cstr_sv(&s);
+ cstr_append_n(self, sv.str, sv.size);
}
STC_INLINE void cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n) {
@@ -237,8 +244,8 @@ STC_INLINE void cstr_replace(cstr* self, size_t pos, size_t len, const char* str { cstr_replace_n(self, pos, len, str, strlen(str)); }
STC_INLINE void cstr_replace_s(cstr* self, size_t pos, size_t len, cstr s) {
- cstr_rep_t r = cstr_rep(&s);
- cstr_replace_n(self, pos, len, r.data, r.size);
+ csview sv = cstr_sv(&s);
+ cstr_replace_n(self, pos, len, sv.str, sv.size);
}
STC_INLINE void cstr_insert_n(cstr* self, size_t pos, const char* str, size_t n)
@@ -248,18 +255,17 @@ STC_INLINE void cstr_insert(cstr* self, size_t pos, const char* str) { cstr_replace_n(self, pos, 0, str, strlen(str)); }
STC_INLINE void cstr_insert_s(cstr* self, size_t pos, cstr s) {
- cstr_rep_t r = cstr_rep(&s);
- cstr_replace_n(self, pos, 0, r.data, r.size);
+ csview sv = cstr_sv(&s);
+ cstr_replace_n(self, pos, 0, sv.str, sv.size);
}
STC_INLINE bool cstr_getline(cstr *self, FILE *fp)
{ return cstr_getdelim(self, '\n', fp); }
-/* container adaptor functions: */
-#define cstr_cmp(xp, yp) strcmp(cstr_str(xp), cstr_str(yp))
-#define cstr_eq(xp, yp) (!cstr_cmp(xp, yp))
-STC_INLINE uint64_t cstr_hash(const cstr *self, size_t dummylen)
- { return c_default_hash(cstr_str(self), cstr_size(*self)); }
+STC_INLINE uint64_t cstr_hash(const cstr *self, size_t dummylen) {
+ csview sv = cstr_sv(self);
+ return c_default_hash(sv.str, sv.size);
+}
/* -------------------------- IMPLEMENTATION ------------------------- */
#if defined(_i_implement)
@@ -332,11 +338,11 @@ STC_DEF void cstr_resize(cstr* self, const size_t size, const char value) { }
STC_DEF size_t cstr_find_n(cstr s, const char* needle, const size_t pos, const size_t nmax) {
- cstr_rep_t r = cstr_rep(&s);
+ csview sv = cstr_sv(&s);
const size_t nlen = (size_t) strlen(needle);
- if (pos > r.size) return cstr_npos;
- char* res = c_strnstrn(r.data + pos, needle, r.size, nmax < nlen ? nmax : nlen);
- return res ? res - r.data : cstr_npos;
+ if (pos > sv.size) return cstr_npos;
+ char* res = c_strnstrn(sv.str + pos, needle, sv.size, nmax < nlen ? nmax : nlen);
+ return res ? res - sv.str : cstr_npos;
}
STC_DEF cstr* cstr_assign_n(cstr* self, const char* str, const size_t n) {
@@ -401,9 +407,15 @@ cstr_from_replace_all(const char* str, const size_t str_len, STC_DEF void
cstr_replace_all(cstr* self, const char* find, const char* repl) {
- cstr_rep_t r = cstr_rep(self);
- cstr_take(self, cstr_from_replace_all(r.data, r.size, find, strlen(find),
- repl, strlen(repl)));
+ csview sv = cstr_sv(self);
+ cstr_take(self, cstr_from_replace_all(sv.str, sv.size, find, strlen(find),
+ repl, strlen(repl)));
+}
+
+STC_DEF cstr
+cstr_from_replace_all_sv(csview sv, csview find, csview repl) {
+ return cstr_from_replace_all(sv.str, sv.size, find.str, find.size,
+ repl.str, repl.size);
}
STC_DEF void cstr_erase_n(cstr* self, const size_t pos, size_t n) {
diff --git a/include/stc/csview.h b/include/stc/csview.h index 59e853ca..be430a79 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -94,11 +94,6 @@ STC_INLINE csview csview_from_s(const cstr* self) STC_INLINE cstr cstr_from_sv(csview sv)
{ return cstr_from_n(sv.str, sv.size); }
-/*STC_INLINE cstr cstr_from_replace_all_sv(csview sv, csview find, csview repl)
- { return cstr_from_replace_all(sv.str, sv.size, find.str, find.size,
- repl.str, repl.size); }*/
-STC_INLINE csview cstr_to_sv(const cstr* self)
- { return c_make(csview){cstr_str(self), cstr_size(*self)}; }
STC_INLINE csview cstr_substr(const cstr* self, intptr_t pos, size_t n)
{ return csview_substr(csview_from_s(self), pos, n); }
STC_INLINE csview cstr_slice(const cstr* self, intptr_t p1, intptr_t p2)
@@ -127,12 +122,12 @@ STC_INLINE bool cstr_ends_with_sv(cstr s, csview sub) #endif
/* ---- Container helper functions ---- */
-STC_INLINE int csview_cmp(const csview* x, const csview* y) {
- const size_t m = x->size < y->size ? x->size : y->size;
- const int c = memcmp(x->str, y->str, m);
- return c ? c : x->size - y->size;
- }
-#define csview_eq(xp, yp) (!csview_cmp(xp, yp))
+STC_INLINE int csview_cmp(const csview* x, const csview* y)
+ { return strcmp(x->str, y->str); }
+
+STC_INLINE bool csview_eq(const csview* x, const csview* y)
+ { return x->size == y->size && !memcmp(x->str, y->str, x->size); }
+
STC_INLINE uint64_t csview_hash(const csview *self, size_t sz)
{ return c_default_hash(self->str, self->size); }
diff --git a/include/stc/template.h b/include/stc/template.h index 48881edf..659f6a72 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -106,7 +106,8 @@ #define i_key_bind cstr
#define i_keyraw csview
#define i_keyfrom cstr_from_sv
- #define i_keyto cstr_to_sv
+ #define i_keyto cstr_sv
+ #define i_eq csview_eq
#ifndef i_tag
#define i_tag ssv
#endif
@@ -197,7 +198,7 @@ #define i_val cstr
#define i_valraw csview
#define i_valfrom cstr_from_sv
- #define i_valto cstr_to_sv
+ #define i_valto cstr_sv
#define i_valdrop cstr_drop
#elif defined i_val_arcbox
#define i_val_bind i_val_arcbox
|
