summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-04-06 18:39:17 +0200
committerTyge Løvset <[email protected]>2022-04-06 18:39:17 +0200
commit3fea4e0b232f7371628944587e561626503fb5fb (patch)
treed132bd6ff15ca5bbd469af7bd20dd4e38d2f4526 /include
parentd1660bb47cbf3fc0a7066ef1472f2d3bdfeb92b3 (diff)
downloadSTC-modified-3fea4e0b232f7371628944587e561626503fb5fb.tar.gz
STC-modified-3fea4e0b232f7371628944587e561626503fb5fb.zip
Refactoring cbits.h and sso alt/cstr.h
Diffstat (limited to 'include')
-rw-r--r--include/stc/alt/cstr.h162
-rw-r--r--include/stc/cbits.h30
-rw-r--r--include/stc/forward.h2
3 files changed, 83 insertions, 111 deletions
diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h
index 5d84f538..37d6fcb5 100644
--- a/include/stc/alt/cstr.h
+++ b/include/stc/alt/cstr.h
@@ -37,15 +37,11 @@
/**************************** PRIVATE API **********************************/
-enum { SSO_CAP = sizeof(cstr_rep_t) - 1 };
-#define SSO_CALL(s, call) (cstr_is_long(s) ? cstr_l_##call : cstr_s_##call)
-
-#define cstr_is_long(s) ((s)->sso.last > 127)
-#define cstr_s_cap(s) SSO_CAP
-#define cstr_s_size(s) ((size_t)(SSO_CAP - (s)->sso.last))
-#define cstr_s_set_size(s, len) ((s)->sso.last = SSO_CAP - (len), (s)->sso.data[len] = 0)
-#define cstr_s_data(s) (s)->sso.data
-#define cstr_s_end(s) ((s)->sso.data + cstr_s_size(s))
+enum { cstr_s_cap = sizeof(cstr_rep_t) - 1 };
+#define cstr_s_size(s) ((size_t)(cstr_s_cap - (s)->sml.last))
+#define cstr_s_set_size(s, len) ((s)->sml.last = cstr_s_cap - (len), (s)->sml.data[len] = 0)
+#define cstr_s_data(s) (s)->sml.data
+#define cstr_s_end(s) ((s)->sml.data + cstr_s_size(s))
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define byte_rotl_(x, b) ((x) << (b)*8 | (x) >> (sizeof(x) - (b))*8)
@@ -61,18 +57,15 @@ enum { SSO_CAP = sizeof(cstr_rep_t) - 1 };
#define cstr_l_end(s) ((s)->lon.data + cstr_l_size(s))
#define cstr_l_drop(s) c_free((s)->lon.data)
+#define cstr_is_long(s) ((s)->sml.last > 127)
STC_API char* _cstr_init(cstr* self, size_t len, size_t cap);
STC_API char* _cstr_internal_move(cstr* self, size_t pos1, size_t pos2);
-STC_INLINE void _cstr_set_size(cstr* s, size_t len) {
- SSO_CALL(s, set_size(s, len));
-}
-
/**************************** PUBLIC API **********************************/
#define cstr_new(literal) cstr_from_n(literal, c_strlen_lit(literal))
#define cstr_npos (SIZE_MAX >> 1)
-#define cstr_null (c_make(cstr){.sso = {.last = SSO_CAP}})
+#define cstr_null (c_make(cstr){.sml = {.last = cstr_s_cap}})
#define cstr_toraw(self) cstr_str(self)
STC_API char* cstr_reserve(cstr* self, size_t cap);
@@ -89,12 +82,11 @@ STC_API int cstr_printf(cstr* self, const char* fmt, ...);
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->sso.data, cstr_s_size(s), cstr_s_cap(s)};
+ : c_make(cstr_rep_t){s->sml.data, cstr_s_size(s), cstr_s_cap};
}
-STC_INLINE cstr cstr_init(void) {
- return cstr_null;
-}
+STC_INLINE cstr cstr_init(void)
+ { return cstr_null; }
STC_INLINE cstr cstr_from_n(const char* str, const size_t n) {
cstr s;
@@ -102,9 +94,8 @@ STC_INLINE cstr cstr_from_n(const char* str, const size_t n) {
return s;
}
-STC_INLINE cstr cstr_from(const char* str) {
- return cstr_from_n(str, strlen(str));
-}
+STC_INLINE cstr cstr_from(const char* str)
+ { return cstr_from_n(str, strlen(str)); }
STC_INLINE cstr cstr_with_size(const size_t size, const char value) {
cstr s;
@@ -137,7 +128,8 @@ STC_INLINE cstr cstr_clone(cstr s) {
}
STC_INLINE void cstr_drop(cstr* self) {
- if (cstr_is_long(self)) cstr_l_drop(self);
+ if (cstr_is_long(self))
+ cstr_l_drop(self);
}
STC_INLINE void cstr_clear(cstr* self) {
@@ -145,62 +137,54 @@ STC_INLINE void cstr_clear(cstr* self) {
cstr_s_set_size(self, 0);
}
-STC_INLINE char* cstr_data(cstr* self) {
- return SSO_CALL(self, data(self));
-}
+#define SSO_CALL(s, call) (cstr_is_long(s) ? cstr_l_##call : cstr_s_##call)
-STC_INLINE const char* cstr_str(const cstr* self) {
- return SSO_CALL(self, data(self));
-}
+STC_INLINE void _cstr_set_size(cstr* self, size_t len)
+ { SSO_CALL(self, set_size(self, len)); }
-STC_INLINE bool cstr_empty(cstr s) {
- return SSO_CALL(&s, size(&s)) == 0;
-}
+STC_INLINE char* cstr_data(cstr* self)
+ { return SSO_CALL(self, data(self)); }
-STC_INLINE size_t cstr_size(cstr s) {
- return SSO_CALL(&s, size(&s));
-}
+STC_INLINE const char* cstr_str(const cstr* self)
+ { return SSO_CALL(self, data(self)); }
-STC_INLINE size_t cstr_length(cstr s) {
- return SSO_CALL(&s, size(&s));
-}
+STC_INLINE bool cstr_empty(cstr s)
+ { return s.sml.last == cstr_s_cap; }
-STC_INLINE size_t cstr_capacity(cstr s) {
- return SSO_CALL(&s, cap(&s));
-}
+STC_INLINE size_t cstr_size(cstr s)
+ { return SSO_CALL(&s, size(&s)); }
-STC_INLINE bool cstr_equals(cstr s1, const char* str) {
- return strcmp(cstr_str(&s1), str) == 0;
-}
+STC_INLINE size_t cstr_length(cstr s)
+ { return SSO_CALL(&s, size(&s)); }
-STC_INLINE bool cstr_equals_s(cstr s1, cstr s2) {
- return strcmp(cstr_str(&s1), cstr_str(&s2)) == 0;
-}
+STC_INLINE size_t cstr_capacity(cstr s)
+ { return cstr_is_long(&s) ? cstr_l_cap(&s) : cstr_s_cap; }
-STC_INLINE bool cstr_eq(const cstr* s1, const cstr* s2) {
- return strcmp(cstr_str(s1), cstr_str(s2)) == 0;
-}
+STC_INLINE bool cstr_equals(cstr s1, const char* str)
+ { return strcmp(cstr_str(&s1), str) == 0; }
-STC_INLINE int cstr_cmp(const cstr* s1, const cstr* s2) {
- return strcmp(cstr_str(s1), cstr_str(s2));
-}
+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)); }
STC_INLINE size_t cstr_find(cstr s, const char* needle) {
const char *str = cstr_str(&s), *res = strstr(str, needle);
return res ? res - str : cstr_npos;
}
-STC_INLINE bool cstr_find_s(cstr s, cstr needle) {
- return cstr_find(s, cstr_str(&needle));
-}
+STC_INLINE bool cstr_find_s(cstr s, cstr needle)
+ { return cstr_find(s, cstr_str(&needle)); }
-STC_INLINE bool cstr_contains(cstr s, const char* needle) {
- return strstr(cstr_str(&s), needle) != NULL;
-}
+STC_INLINE bool cstr_contains(cstr s, const char* needle)
+ { return strstr(cstr_str(&s), needle) != NULL; }
-STC_INLINE bool cstr_contains_s(cstr s, cstr needle) {
- return strstr(cstr_str(&s), cstr_str(&needle)) != NULL;
-}
+STC_INLINE bool cstr_contains_s(cstr s, cstr needle)
+ { return strstr(cstr_str(&s), cstr_str(&needle)) != NULL; }
STC_INLINE bool cstr_starts_with(cstr s, const char* sub) {
const char* str = cstr_str(&s);
@@ -208,31 +192,27 @@ STC_INLINE bool cstr_starts_with(cstr s, const char* sub) {
return *sub == 0;
}
-STC_INLINE bool cstr_starts_with_s(cstr s, cstr sub) {
- return cstr_starts_with(s, cstr_str(&sub));
-}
+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;
}
-STC_INLINE bool cstr_ends_with_s(cstr s, cstr sub) {
- return cstr_ends_with(s, cstr_str(&sub));
-}
+STC_INLINE bool cstr_ends_with_s(cstr s, cstr sub)
+ { return cstr_ends_with(s, cstr_str(&sub)); }
-STC_INLINE void cstr_assign(cstr* self, const char* str) {
- cstr_assign_n(self, str, strlen(str));
-}
+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);
}
-STC_INLINE void cstr_append(cstr* self, const char* str) {
- cstr_append_n(self, str, strlen(str));
-}
+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);
@@ -244,31 +224,27 @@ STC_INLINE void cstr_replace_n(cstr* self, size_t pos, size_t len, const char* s
memcpy(d + pos, str, n);
}
-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(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);
}
-STC_INLINE void cstr_insert_n(cstr* self, size_t pos, const char* str, size_t n) {
- cstr_replace_n(self, pos, 0, str, n);
-}
+STC_INLINE void cstr_insert_n(cstr* self, size_t pos, const char* str, size_t n)
+ { cstr_replace_n(self, pos, 0, str, n); }
-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(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);
}
-STC_INLINE bool cstr_getline(cstr *self, FILE *fp) {
- return cstr_getdelim(self, '\n', fp);
-}
+STC_INLINE bool cstr_getline(cstr *self, FILE *fp)
+ { return cstr_getdelim(self, '\n', fp); }
/* -------------------------- IMPLEMENTATION ------------------------- */
#if defined(_i_implement)
@@ -286,25 +262,25 @@ STC_DEF char* _cstr_internal_move(cstr* self, const size_t pos1, const size_t po
}
STC_DEF char* _cstr_init(cstr* self, const size_t len, const size_t cap) {
- if (cap > SSO_CAP) {
+ if (cap > cstr_s_cap) {
self->lon.data = (char *)c_malloc(cap + 1);
cstr_l_set_size(self, len);
cstr_l_set_cap(self, cap);
return self->lon.data;
}
cstr_s_set_size(self, len);
- return self->sso.data;
+ return self->sml.data;
}
STC_DEF void cstr_shrink_to_fit(cstr* self) {
cstr_rep_t r = cstr_rep(self);
if (r.size == r.cap)
return;
- if (r.size > SSO_CAP) {
+ if (r.size > cstr_s_cap) {
self->lon.data = (char *)c_realloc(self->lon.data, r.size + 1);
cstr_l_set_cap(self, r.size);
- } else if (r.cap > SSO_CAP) {
- memcpy(self->sso.data, r.data, r.size + 1);
+ } else if (r.cap > cstr_s_cap) {
+ memcpy(self->sml.data, r.data, r.size + 1);
cstr_s_set_size(self, r.size);
c_free(r.data);
}
@@ -319,16 +295,16 @@ STC_DEF char* cstr_reserve(cstr* self, const size_t cap) {
return self->lon.data;
}
/* from short to long: */
- if (cap > cstr_s_cap(self)) {
+ if (cap > cstr_s_cap) {
char* data = (char *)c_malloc(cap + 1);
const size_t len = cstr_s_size(self);
- memcpy(data, self->sso.data, len);
+ memcpy(data, self->sml.data, len);
self->lon.data = data;
cstr_l_set_size(self, len);
cstr_l_set_cap(self, cap);
return data;
}
- return self->sso.data;
+ return self->sml.data;
}
STC_DEF void cstr_resize(cstr* self, const size_t size, const char value) {
diff --git a/include/stc/cbits.h b/include/stc/cbits.h
index af9336c1..347261d8 100644
--- a/include/stc/cbits.h
+++ b/include/stc/cbits.h
@@ -77,6 +77,8 @@ STC_INLINE void cbits_clear(cbits* self) { self->size = 0; }
STC_INLINE void cbits_drop(cbits* self) { c_free(self->data64); }
STC_INLINE size_t cbits_size(cbits set) { return set.size; }
+#define _cbits_bit(i) ((uint64_t)1 << ((i) & 63))
+
#define cbits_new(literal) \
cbits_from_n(literal, c_strlen_lit(literal))
@@ -91,33 +93,27 @@ STC_INLINE cbits cbits_move(cbits* self) {
}
STC_INLINE bool cbits_test(cbits set, size_t i) {
- return (set.data64[i >> 6] & (1ull << (i & 63))) != 0;
+ return (set.data64[i >> 6] & _cbits_bit(i)) != 0;
}
STC_INLINE bool cbits_at(cbits set, size_t i) {
- return (set.data64[i >> 6] & (1ull << (i & 63))) != 0;
+ return (set.data64[i >> 6] & _cbits_bit(i)) != 0;
}
STC_INLINE void cbits_set(cbits *self, size_t i) {
- self->data64[i >> 6] |= 1ull << (i & 63);
+ self->data64[i >> 6] |= _cbits_bit(i);
}
STC_INLINE void cbits_reset(cbits *self, size_t i) {
- self->data64[i >> 6] &= ~(1ull << (i & 63));
+ self->data64[i >> 6] &= ~_cbits_bit(i);
}
-#ifdef _MSC_VER
-#pragma warning(disable: 4146) // unary minus operator applied to unsigned type
-#endif
STC_INLINE void cbits_set_value(cbits *self, size_t i, bool value) {
- self->data64[i >> 6] ^= (-(uint64_t)value ^ self->data64[i >> 6]) & 1ull << (i & 63);
+ self->data64[i >> 6] ^= ((uint64_t)-(int)value ^ self->data64[i >> 6]) & _cbits_bit(i);
}
-#ifdef _MSC_VER
-#pragma warning(default: 4146)
-#endif
STC_INLINE void cbits_flip(cbits *self, size_t i) {
- self->data64[i >> 6] ^= 1ull << (i & 63);
+ self->data64[i >> 6] ^= _cbits_bit(i);
}
STC_INLINE void cbits_set_all(cbits *self, bool value) {
@@ -131,7 +127,7 @@ STC_INLINE void cbits_set_values(cbits *self, uint64_t pattern) {
STC_INLINE void cbits_flip_all(cbits *self) {
size_t n = (self->size + 63) >> 6;
- for (size_t i=0; i<n; ++i) self->data64[i] ^= ~0ull;
+ for (size_t i=0; i<n; ++i) self->data64[i] ^= ~(uint64_t)0;
}
/* Intersection */
@@ -172,7 +168,7 @@ STC_INLINE void cbits_xor(cbits *self, cbits other) {
STC_DEF cbits* cbits_copy(cbits* self, cbits other) {
if (self->data64 == other.data64) return self;
if (self->size != other.size) return cbits_take(self, cbits_clone(other));
- memcpy(self->data64, other.data64, ((other.size + 63) >> 6)*8);
+ memcpy(self->data64, other.data64, ((other.size + 63) >> 6) * 8);
return self;
}
@@ -183,7 +179,7 @@ STC_DEF void cbits_resize(cbits* self, size_t size, bool value) {
if (new_n >= old_n) {
memset(self->data64 + old_n, -(int)value, (new_n - old_n) * 8);
if (old_n > 0) {
- uint64_t m = (1ull << (osize & 63)) - 1; /* mask */
+ uint64_t m = _cbits_bit(osize) - 1; /* mask */
value ? (self->data64[old_n - 1] |= ~m) : (self->data64[old_n - 1] &= m);
}
}
@@ -221,7 +217,7 @@ STC_DEF cbits cbits_clone(cbits other) {
STC_DEF size_t cbits_count(cbits s) {
size_t count = 0, n = s.size >> 6;
for (size_t i = 0; i < n; ++i) count += cpopcount64(s.data64[i]);
- if (s.size & 63) count += cpopcount64(s.data64[n] & ((1ull << (s.size & 63)) - 1));
+ if (s.size & 63) count += cpopcount64(s.data64[n] & (_cbits_bit(s.size) - 1));
return count;
}
@@ -232,7 +228,7 @@ STC_DEF size_t cbits_count(cbits s) {
if ((s.data64[i] OPR other.data64[i]) != x) \
return false; \
if (!(s.size & 63)) return true; \
- uint64_t i = n, m = (1ull << (s.size & 63)) - 1; \
+ uint64_t i = n, m = _cbits_bit(s.size) - 1; \
return ((s.data64[i] OPR other.data64[i]) & m) == (x & m)
STC_DEF bool cbits_subset_of(cbits s, cbits other) { _cbits_SETOP(|, s.data64[i]); }
diff --git a/include/stc/forward.h b/include/stc/forward.h
index 06ec3897..2351176a 100644
--- a/include/stc/forward.h
+++ b/include/stc/forward.h
@@ -46,7 +46,7 @@ typedef struct { char* data; size_t size, cap; } cstr_rep_t;
typedef char cstr_value;
#ifdef STC_USE_SSO
typedef union {
- struct { char data[sizeof(cstr_rep_t) - 1]; uint8_t last; } sso;
+ struct { char data[sizeof(cstr_rep_t) - 1]; uint8_t last; } sml;
struct { char* data; size_t size, ncap; } lon;
} cstr;
#else