diff options
| author | Tyge Løvset <[email protected]> | 2023-01-31 18:55:08 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-01-31 18:55:08 +0100 |
| commit | b677a0c3950b8294ba6458e682a885351273ac08 (patch) | |
| tree | f309f7f7571fb588f0f65254d17fa09d678a8e3c /include | |
| parent | a24ecd6bbfffc2e0b75b8ed48fcb5306d367ad3e (diff) | |
| download | STC-modified-b677a0c3950b8294ba6458e682a885351273ac08.tar.gz STC-modified-b677a0c3950b8294ba6458e682a885351273ac08.zip | |
Converted all containers but the maps and examples to signed sizes and indices.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/cbits.h | 92 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 11 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 110 | ||||
| -rw-r--r-- | include/stc/clist.h | 16 | ||||
| -rw-r--r-- | include/stc/cpque.h | 32 | ||||
| -rw-r--r-- | include/stc/cregex.h | 2 | ||||
| -rw-r--r-- | include/stc/cstack.h | 32 | ||||
| -rw-r--r-- | include/stc/cstr.h | 212 | ||||
| -rw-r--r-- | include/stc/csview.h | 74 | ||||
| -rw-r--r-- | include/stc/cvec.h | 66 | ||||
| -rw-r--r-- | include/stc/forward.h | 28 | ||||
| -rw-r--r-- | include/stc/utf8.h | 24 |
12 files changed, 346 insertions, 353 deletions
diff --git a/include/stc/cbits.h b/include/stc/cbits.h index 41ee1713..9f0afc9c 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -56,40 +56,40 @@ int main() { #include <string.h> #define _cbits_bit(i) ((uint64_t)1 << ((i) & 63)) -#define _cbits_words(n) (((n) + 63)>>6) -#define _cbits_bytes(n) (_cbits_words(n) * sizeof(uint64_t)) +#define _cbits_words(n) (intptr_t)(((n) + 63)>>6) +#define _cbits_bytes(n) (_cbits_words(n) * c_sizeof(uint64_t)) #if defined(__GNUC__) || defined(__clang__) - STC_INLINE uint64_t cpopcount64(uint64_t x) {return (uint64_t)__builtin_popcountll(x);} + STC_INLINE int cpopcount64(uint64_t x) {return (int)__builtin_popcountll(x);} #elif defined(_MSC_VER) && defined(_WIN64) #include <intrin.h> - STC_INLINE uint64_t cpopcount64(uint64_t x) {return __popcnt64(x);} + STC_INLINE int cpopcount64(uint64_t x) {return __popcnt64(x);} #else - STC_INLINE uint64_t cpopcount64(uint64_t x) { /* http://en.wikipedia.org/wiki/Hamming_weight */ + STC_INLINE int cpopcount64(uint64_t x) { /* http://en.wikipedia.org/wiki/Hamming_weight */ x -= (x >> 1) & 0x5555555555555555; x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; - return (x * 0x0101010101010101) >> 56; + return (int)((x * 0x0101010101010101) >> 56); } #endif -STC_INLINE size_t _cbits_count(const uint64_t* set, const size_t sz) { - const size_t n = sz>>6; - size_t count = 0; - for (size_t i = 0; i < n; ++i) +STC_INLINE intptr_t _cbits_count(const uint64_t* set, const intptr_t sz) { + const intptr_t n = sz>>6; + intptr_t count = 0; + for (intptr_t i = 0; i < n; ++i) count += cpopcount64(set[i]); if (sz & 63) count += cpopcount64(set[n] & (_cbits_bit(sz) - 1)); return count; } -STC_INLINE char* _cbits_to_str(const uint64_t* set, const size_t sz, - char* out, size_t start, size_t stop) { +STC_INLINE char* _cbits_to_str(const uint64_t* set, const intptr_t sz, + char* out, intptr_t start, intptr_t stop) { if (stop > sz) stop = sz; assert(start <= stop); - memset(out, '0', stop - start); - for (size_t i = start; i < stop; ++i) + c_memset(out, '0', stop - start); + for (intptr_t i = start; i < stop; ++i) if ((set[i>>6] & _cbits_bit(i)) != 0) out[i - start] = '1'; out[stop - start] = '\0'; @@ -97,8 +97,8 @@ STC_INLINE char* _cbits_to_str(const uint64_t* set, const size_t sz, } #define _cbits_OPR(OPR, VAL) \ - const size_t n = sz>>6; \ - for (size_t i = 0; i < n; ++i) \ + const uint64_t n = (uint64_t)sz>>6; \ + for (uint64_t i = 0; i < n; ++i) \ if ((set[i] OPR other[i]) != VAL) \ return false; \ if (!(sz & 63)) \ @@ -106,10 +106,10 @@ STC_INLINE char* _cbits_to_str(const uint64_t* set, const size_t sz, const uint64_t i = n, m = _cbits_bit(sz) - 1; \ return ((set[i] OPR other[i]) & m) == (VAL & m) -STC_INLINE bool _cbits_subset_of(const uint64_t* set, const uint64_t* other, const size_t sz) +STC_INLINE bool _cbits_subset_of(const uint64_t* set, const uint64_t* other, const intptr_t sz) { _cbits_OPR(|, set[i]); } -STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, const size_t sz) +STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, const intptr_t sz) { _cbits_OPR(&, 0); } #endif // CBITS_H_INCLUDED @@ -121,12 +121,12 @@ STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, cons #define _i_assert(x) assert(x) #define i_type cbits -struct { uint64_t *data64; size_t _size; } typedef i_type; +struct { uint64_t *data64; intptr_t _size; } typedef i_type; STC_INLINE cbits cbits_init(void) { return c_LITERAL(cbits){NULL}; } STC_INLINE void cbits_create(cbits* self) { self->data64 = NULL; self->_size = 0; } STC_INLINE void cbits_drop(cbits* self) { c_free(self->data64); } -STC_INLINE size_t cbits_size(const cbits* self) { return self->_size; } +STC_INLINE intptr_t cbits_size(const cbits* self) { return self->_size; } STC_INLINE cbits* cbits_take(cbits* self, cbits other) { if (self->data64 != other.data64) { @@ -137,8 +137,8 @@ STC_INLINE cbits* cbits_take(cbits* self, cbits other) { } STC_INLINE cbits cbits_clone(cbits other) { - const size_t bytes = _cbits_bytes(other._size); - cbits set = {(uint64_t *)memcpy(c_malloc(bytes), other.data64, bytes), other._size}; + const intptr_t bytes = _cbits_bytes(other._size); + cbits set = {(uint64_t *)c_memcpy(c_malloc(bytes), other.data64, bytes), other._size}; return set; } @@ -147,16 +147,16 @@ STC_INLINE cbits* cbits_copy(cbits* self, const cbits* other) { return self; if (self->_size != other->_size) return cbits_take(self, cbits_clone(*other)); - memcpy(self->data64, other->data64, _cbits_bytes(other->_size)); + c_memcpy(self->data64, other->data64, _cbits_bytes(other->_size)); return self; } -STC_INLINE void cbits_resize(cbits* self, const size_t size, const bool value) { - const size_t new_n = _cbits_words(size), osize = self->_size, old_n = _cbits_words(osize); +STC_INLINE void cbits_resize(cbits* self, const intptr_t size, const bool value) { + const intptr_t new_n = _cbits_words(size), osize = self->_size, old_n = _cbits_words(osize); self->data64 = (uint64_t *)c_realloc(self->data64, new_n*8); self->_size = size; if (new_n >= old_n) { - memset(self->data64 + old_n, -(int)value, (new_n - old_n)*8); + c_memset(self->data64 + old_n, -(int)value, (new_n - old_n)*8); if (old_n > 0) { uint64_t m = _cbits_bit(osize) - 1; /* mask */ value ? (self->data64[old_n - 1] |= ~m) @@ -174,13 +174,13 @@ STC_INLINE cbits cbits_move(cbits* self) { return tmp; } -STC_INLINE cbits cbits_with_size(const size_t size, const bool value) { +STC_INLINE cbits cbits_with_size(const intptr_t size, const bool value) { cbits set = {(uint64_t *)c_malloc(_cbits_bytes(size)), size}; cbits_set_all(&set, value); return set; } -STC_INLINE cbits cbits_with_pattern(const size_t size, const uint64_t pattern) { +STC_INLINE cbits cbits_with_pattern(const intptr_t size, const uint64_t pattern) { cbits set = {(uint64_t *)c_malloc(_cbits_bytes(size)), size}; cbits_set_pattern(&set, pattern); return set; @@ -198,7 +198,7 @@ struct { uint64_t data64[(i_capacity - 1)/64 + 1]; } typedef i_type; STC_INLINE i_type _i_memb(_init)(void) { return c_LITERAL(i_type){0}; } STC_INLINE void _i_memb(_create)(i_type* self) {} STC_INLINE void _i_memb(_drop)(i_type* self) {} -STC_INLINE size_t _i_memb(_size)(const i_type* self) { return i_capacity; } +STC_INLINE intptr_t _i_memb(_size)(const i_type* self) { return i_capacity; } STC_INLINE i_type _i_memb(_move)(i_type* self) { return *self; } STC_INLINE i_type* _i_memb(_take)(i_type* self, i_type other) @@ -213,13 +213,13 @@ STC_INLINE i_type* _i_memb(_copy)(i_type* self, const i_type* other) STC_INLINE void _i_memb(_set_all)(i_type *self, const bool value); STC_INLINE void _i_memb(_set_pattern)(i_type *self, const uint64_t pattern); -STC_INLINE i_type _i_memb(_with_size)(const size_t size, const bool value) { +STC_INLINE i_type _i_memb(_with_size)(const intptr_t size, const bool value) { assert(size <= i_capacity); i_type set; _i_memb(_set_all)(&set, value); return set; } -STC_INLINE i_type _i_memb(_with_pattern)(const size_t size, const uint64_t pattern) { +STC_INLINE i_type _i_memb(_with_pattern)(const intptr_t size, const uint64_t pattern) { assert(size <= i_capacity); i_type set; _i_memb(_set_pattern)(&set, pattern); return set; @@ -229,39 +229,39 @@ STC_INLINE i_type _i_memb(_with_pattern)(const size_t size, const uint64_t patte // COMMON: STC_INLINE void _i_memb(_set_all)(i_type *self, const bool value) - { memset(self->data64, value? ~0 : 0, _cbits_bytes(_i_memb(_size)(self))); } + { c_memset(self->data64, value? ~0 : 0, _cbits_bytes(_i_memb(_size)(self))); } STC_INLINE void _i_memb(_set_pattern)(i_type *self, const uint64_t pattern) { - size_t n = _cbits_words(_i_memb(_size)(self)); + intptr_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] = pattern; } -STC_INLINE bool _i_memb(_test)(const i_type* self, const size_t i) +STC_INLINE bool _i_memb(_test)(const i_type* self, const intptr_t i) { return (self->data64[i>>6] & _cbits_bit(i)) != 0; } -STC_INLINE bool _i_memb(_at)(const i_type* self, const size_t i) +STC_INLINE bool _i_memb(_at)(const i_type* self, const intptr_t i) { return (self->data64[i>>6] & _cbits_bit(i)) != 0; } -STC_INLINE void _i_memb(_set)(i_type *self, const size_t i) +STC_INLINE void _i_memb(_set)(i_type *self, const intptr_t i) { self->data64[i>>6] |= _cbits_bit(i); } -STC_INLINE void _i_memb(_reset)(i_type *self, const size_t i) +STC_INLINE void _i_memb(_reset)(i_type *self, const intptr_t i) { self->data64[i>>6] &= ~_cbits_bit(i); } -STC_INLINE void _i_memb(_set_value)(i_type *self, const size_t i, const bool b) { +STC_INLINE void _i_memb(_set_value)(i_type *self, const intptr_t i, const bool b) { self->data64[i>>6] ^= ((uint64_t)-(int)b ^ self->data64[i>>6]) & _cbits_bit(i); } -STC_INLINE void _i_memb(_flip)(i_type *self, const size_t i) +STC_INLINE void _i_memb(_flip)(i_type *self, const intptr_t i) { self->data64[i>>6] ^= _cbits_bit(i); } STC_INLINE void _i_memb(_flip_all)(i_type *self) { - size_t n = _cbits_words(_i_memb(_size)(self)); + intptr_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] ^= ~(uint64_t)0; } STC_INLINE i_type _i_memb(_from)(const char* str) { - size_t n = strlen(str); + intptr_t n = c_strlen(str); i_type set = _i_memb(_with_size)(n, false); while (n--) if (str[n] == '1') _i_memb(_set)(&set, n); return set; @@ -270,26 +270,26 @@ STC_INLINE i_type _i_memb(_from)(const char* str) { /* Intersection */ STC_INLINE void _i_memb(_intersect)(i_type *self, const i_type* other) { _i_assert(self->_size == other->_size); - size_t n = _cbits_words(_i_memb(_size)(self)); + intptr_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] &= other->data64[n]; } /* Union */ STC_INLINE void _i_memb(_union)(i_type *self, const i_type* other) { _i_assert(self->_size == other->_size); - size_t n = _cbits_words(_i_memb(_size)(self)); + intptr_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] |= other->data64[n]; } /* Exclusive disjunction */ STC_INLINE void _i_memb(_xor)(i_type *self, const i_type* other) { _i_assert(self->_size == other->_size); - size_t n = _cbits_words(_i_memb(_size)(self)); + intptr_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] ^= other->data64[n]; } -STC_INLINE size_t _i_memb(_count)(const i_type* self) +STC_INLINE intptr_t _i_memb(_count)(const i_type* self) { return _cbits_count(self->data64, _i_memb(_size)(self)); } -STC_INLINE char* _i_memb(_to_str)(const i_type* self, char* out, size_t start, size_t stop) +STC_INLINE char* _i_memb(_to_str)(const i_type* self, char* out, intptr_t start, intptr_t stop) { return _cbits_to_str(self->data64, _i_memb(_size)(self), out, start, stop); } STC_INLINE bool _i_memb(_subset_of)(const i_type* self, const i_type* other) { diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 39ece500..f0d157e3 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -31,12 +31,11 @@ #include <assert.h> #include "priv/altnames.h" +#define c_NPOS INTPTR_MAX #if SIZE_MAX == UINT32_MAX #define c_ZU PRIu32 - #define c_NPOS INT32_MAX #elif SIZE_MAX == UINT64_MAX #define c_ZU PRIu64 - #define c_NPOS INT64_MAX #endif #if defined STC_NDEBUG || defined NDEBUG #define c_ASSERT(expr) (void)(expr) @@ -91,11 +90,13 @@ #define c_swap(T, xp, yp) do { T *_xp = xp, *_yp = yp, \ _tv = *_xp; *_xp = *_yp; *_yp = _tv; } while (0) #define c_sizeof (intptr_t)sizeof -#define c_strlen(str) (intptr_t)strlen(str) -#define c_strncmp(d, s, ilen) strncmp(d, s, c_i2u(ilen)) +#define c_strlen(s) (intptr_t)strlen(s) +#define c_strncmp(a, b, ilen) strncmp(a, b, c_i2u(ilen)) #define c_memcpy(d, s, ilen) memcpy(d, s, c_i2u(ilen)) #define c_memmove(d, s, ilen) memmove(d, s, c_i2u(ilen)) -#define c_memcmp(d, s, ilen) memcmp(d, s, c_i2u(ilen)) +#define c_memset(d, val, ilen) memset(d, val, c_i2u(ilen)) +#define c_memcmp(a, b, ilen) memcmp(a, b, c_i2u(ilen)) + #define c_u2i(u) ((intptr_t)((u) + 0*sizeof((u) == 1U))) #define c_i2u(i) ((size_t)(i) + 0*sizeof((i) == 1)) #define c_LTu(a, b) ((size_t)(a) < (size_t)(b)) diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index f648547b..44c5e8d5 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -42,15 +42,15 @@ _cx_deftypes(_c_cdeq_types, _cx_self, i_key); typedef i_keyraw _cx_raw; STC_API _cx_self _cx_memb(_init)(void); -STC_API _cx_self _cx_memb(_with_capacity)(const size_t n); -STC_API bool _cx_memb(_reserve)(_cx_self* self, const size_t n); +STC_API _cx_self _cx_memb(_with_capacity)(const intptr_t n); +STC_API bool _cx_memb(_reserve)(_cx_self* self, const intptr_t n); STC_API void _cx_memb(_clear)(_cx_self* self); STC_API void _cx_memb(_drop)(_cx_self* self); STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_key value); STC_API void _cx_memb(_shrink_to_fit)(_cx_self *self); -STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } #if !defined _i_queue #if !defined i_no_emplace @@ -89,8 +89,8 @@ STC_API _cx_self _cx_memb(_clone)(_cx_self cx); STC_INLINE i_key _cx_memb(_value_clone)(i_key val) { return i_keyclone(val); } #endif // !i_no_clone -STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) { return self->_len; } -STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { return self->_cap; } +STC_INLINE intptr_t _cx_memb(_size)(const _cx_self* self) { return self->_len; } +STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* self) { return self->_cap; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return !self->_len; } STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _cx_value* pval) { return i_keyto(pval); } STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->data; } @@ -100,7 +100,7 @@ STC_INLINE void _cx_memb(_pop_front)(_cx_self* self) // == _pop() when _ { i_keydrop(self->data); ++self->data; --self->_len; } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { - size_t n = self->_len; + intptr_t n = self->_len; return c_LITERAL(_cx_iter){n ? self->data : NULL, self->data + n}; } @@ -115,15 +115,15 @@ STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t n) #if !defined _i_queue -STC_INLINE size_t _cx_memb(_index)(const _cx_self* self, _cx_iter it) - { return (size_t)(it.ref - self->data); } +STC_INLINE intptr_t _cx_memb(_index)(const _cx_self* self, _cx_iter it) + { return (it.ref - self->data); } STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } -STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, const size_t idx) { +STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, const intptr_t idx) { assert(idx < self->_len); return self->data + idx; } -STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, const size_t idx) { +STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, const intptr_t idx) { assert(idx < self->_len); return self->data + idx; } @@ -131,11 +131,11 @@ STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value) { return _cx_memb(_push)(self, value); } STC_INLINE _cx_iter -_cx_memb(_insert)(_cx_self* self, const size_t idx, i_key value) { +_cx_memb(_insert)(_cx_self* self, const intptr_t idx, i_key value) { return _cx_memb(_insert_range)(self, self->data + idx, &value, &value + 1); } STC_INLINE _cx_iter -_cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], const size_t n) { +_cx_memb(_insert_n)(_cx_self* self, const intptr_t idx, const _cx_value arr[], const intptr_t n) { return _cx_memb(_insert_range)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter @@ -144,7 +144,7 @@ _cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) { } STC_INLINE _cx_iter -_cx_memb(_erase_n)(_cx_self* self, const size_t idx, const size_t n) { +_cx_memb(_erase_n)(_cx_self* self, const intptr_t idx, const intptr_t n) { return _cx_memb(_erase_range_p)(self, self->data + idx, self->data + idx + n); } STC_INLINE _cx_iter @@ -167,7 +167,7 @@ STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw) { } STC_INLINE _cx_iter -_cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], const size_t n) { +_cx_memb(_emplace_n)(_cx_self* self, const intptr_t idx, const _cx_raw arr[], const intptr_t n) { return _cx_memb(_emplace_range)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter @@ -207,7 +207,7 @@ _cx_memb(_sort)(_cx_self* self) { /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) -#define _cdeq_nfront(self) (size_t)((self)->data - (self)->_base) +#define _cdeq_nfront(self) ((self)->data - (self)->_base) STC_DEF _cx_self _cx_memb(_init)(void) { @@ -228,8 +228,8 @@ _cx_memb(_clear)(_cx_self* self) { STC_DEF void _cx_memb(_shrink_to_fit)(_cx_self *self) { if (self->_len != self->_cap) { - memmove(self->_base, self->data, self->_len*sizeof(i_key)); - _cx_value* d = (_cx_value*)c_realloc(self->_base, self->_len*sizeof(i_key)); + c_memmove(self->_base, self->data, self->_len*c_sizeof(i_key)); + _cx_value* d = (_cx_value*)c_realloc(self->_base, self->_len*c_sizeof(i_key)); if (d) { self->_base = d; self->_cap = self->_len; @@ -246,11 +246,11 @@ _cx_memb(_drop)(_cx_self* self) { } } -static size_t -_cx_memb(_realloc_)(_cx_self* self, const size_t n) { - const size_t cap = (size_t)((float)self->_len*1.7f) + n + 7U; - const size_t nfront = _cdeq_nfront(self); - _cx_value* d = (_cx_value*)c_realloc(self->_base, cap*sizeof(i_key)); +static intptr_t +_cx_memb(_realloc_)(_cx_self* self, const intptr_t n) { + const intptr_t cap = (intptr_t)((float)self->_len*1.7f) + n + 7; + const intptr_t nfront = _cdeq_nfront(self); + _cx_value* d = (_cx_value*)c_realloc(self->_base, cap*c_sizeof(i_key)); if (!d) return 0; self->_cap = cap; @@ -260,37 +260,37 @@ _cx_memb(_realloc_)(_cx_self* self, const size_t n) { } static bool -_cx_memb(_expand_right_half_)(_cx_self* self, const size_t idx, const size_t n) { - const size_t sz = self->_len, cap = self->_cap; - const size_t nfront = _cdeq_nfront(self), nback = cap - sz - nfront; - if (nback >= n || (size_t)((float)sz*1.3f) + n > cap) { +_cx_memb(_expand_right_half_)(_cx_self* self, const intptr_t idx, const intptr_t n) { + const intptr_t sz = self->_len, cap = self->_cap; + const intptr_t nfront = _cdeq_nfront(self), nback = cap - sz - nfront; + if (nback >= n || (intptr_t)((float)sz*1.3f) + n > cap) { if (!_cx_memb(_realloc_)(self, n)) return false; - memmove(self->data + idx + n, self->data + idx, (sz - idx)*sizeof(i_key)); + c_memmove(self->data + idx + n, self->data + idx, (sz - idx)*c_sizeof(i_key)); } else { #if !defined _i_queue - const size_t unused = cap - (sz + n); - const size_t pos = (nfront*2 < unused) ? nfront : unused/2; + const intptr_t unused = cap - (sz + n); + const intptr_t pos = (nfront*2 < unused) ? nfront : unused/2; #else - const size_t pos = 0; + const intptr_t pos = 0; #endif - memmove(self->_base + pos, self->data, idx*sizeof(i_key)); - memmove(self->data + pos + idx + n, self->data + idx, (sz - idx)*sizeof(i_key)); + c_memmove(self->_base + pos, self->data, idx*c_sizeof(i_key)); + c_memmove(self->data + pos + idx + n, self->data + idx, (sz - idx)*c_sizeof(i_key)); self->data = self->_base + pos; } return true; } STC_DEF _cx_self -_cx_memb(_with_capacity)(const size_t n) { +_cx_memb(_with_capacity)(const intptr_t n) { _cx_self cx = _cx_memb(_init)(); _cx_memb(_expand_right_half_)(&cx, 0, n); return cx; } STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const size_t n) { - const size_t sz = self->_len; +_cx_memb(_reserve)(_cx_self* self, const intptr_t n) { + const intptr_t sz = self->_len; return n <= sz || _cx_memb(_expand_right_half_)(self, sz, n - sz); } @@ -308,7 +308,7 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self cx) { _cx_self out = _cx_memb(_with_capacity)(cx._len); if (out._base) - for (size_t i = 0; i < cx._len; ++i) + for (intptr_t i = 0; i < cx._len; ++i) out.data[i] = i_keyclone(cx.data[i]); return out; } @@ -317,27 +317,27 @@ _cx_memb(_clone)(_cx_self cx) { #if !defined _i_queue static void -_cx_memb(_expand_left_half_)(_cx_self* self, const size_t idx, const size_t n) { - size_t cap = self->_cap; - const size_t sz = self->_len; - const size_t nfront = _cdeq_nfront(self), nback = cap - sz - nfront; +_cx_memb(_expand_left_half_)(_cx_self* self, const intptr_t idx, const intptr_t n) { + intptr_t cap = self->_cap; + const intptr_t sz = self->_len; + const intptr_t nfront = _cdeq_nfront(self), nback = cap - sz - nfront; if (nfront >= n) { - self->data = (_cx_value *)memmove(self->data - n, self->data, idx*sizeof(i_key)); + self->data = (_cx_value *)c_memmove(self->data - n, self->data, idx*c_sizeof(i_key)); } else { - if ((size_t)((float)sz*1.3f) + n > cap) + if ((intptr_t)((float)sz*1.3f) + n > cap) cap = _cx_memb(_realloc_)(self, n); - const size_t unused = cap - (sz + n); - const size_t pos = (nback*2 < unused) ? unused - nback : unused/2; - memmove(self->_base + pos + idx + n, self->data + idx, (sz - idx)*sizeof(i_key)); - self->data = (_cx_value *)memmove(self->_base + pos, self->data, idx*sizeof(i_key)); + const intptr_t unused = cap - (sz + n); + const intptr_t pos = (nback*2 < unused) ? unused - nback : unused/2; + c_memmove(self->_base + pos + idx + n, self->data + idx, (sz - idx)*c_sizeof(i_key)); + self->data = (_cx_value *)c_memmove(self->_base + pos, self->data, idx*c_sizeof(i_key)); } } static _cx_iter -_cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n) { +_cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const intptr_t n) { if (n) { if (!pos) pos = self->data + self->_len; - const size_t idx = (size_t)(pos - self->data); + const intptr_t idx = (pos - self->data); if (idx*2 < self->_len) _cx_memb(_expand_left_half_)(self, idx, n); else @@ -362,9 +362,9 @@ _cx_memb(_push_front)(_cx_self* self, i_key value) { STC_DEF _cx_iter _cx_memb(_insert_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) - memcpy(it.ref, p1, (size_t)(p2 - p1)*sizeof *p1); + c_memcpy(it.ref, p1, (p2 - p1)*c_sizeof *p1); return it; } @@ -375,8 +375,8 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { _cx_value* p = p1, *end = self->data + self->_len; for (; p != p2; ++p) { i_keydrop(p); } - memmove(p1, p2, (size_t)(end - p2)*sizeof *p1); - self->_len -= (size_t)len; + c_memmove(p1, p2, (end - p2)*c_sizeof *p1); + self->_len -= len; return c_LITERAL(_cx_iter){p2 == end ? NULL : p1, end - len}; } @@ -384,7 +384,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { STC_DEF _cx_iter _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) for (_cx_value* p = it.ref; p1 != p2; ++p1) *p++ = i_keyclone((*p1)); @@ -396,7 +396,7 @@ _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos, STC_DEF _cx_iter _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) for (_cx_value* p = it.ref; p1 != p2; ++p1) *p++ = i_keyfrom((*p1)); diff --git a/include/stc/clist.h b/include/stc/clist.h index 4eac2954..283c8774 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -98,7 +98,7 @@ STC_API _cx_iter _cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key STC_API _cx_iter _cx_memb(_erase_at)(_cx_self* self, _cx_iter it); STC_API _cx_iter _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2); #if !defined i_no_cmp -STC_API size_t _cx_memb(_remove)(_cx_self* self, _cx_raw val); +STC_API intptr_t _cx_memb(_remove)(_cx_self* self, _cx_raw val); STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw val); STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y); STC_API int _cx_memb(_sort_cmp_)(const clist_VOID_node* x, const clist_VOID_node* y); @@ -134,11 +134,11 @@ STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) #endif // !i_no_emplace STC_INLINE _cx_self _cx_memb(_init)(void) { return c_LITERAL(_cx_self){NULL}; } -STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) { while (n--) _cx_memb(_push_back)(self, i_keyfrom(*raw++)); } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } -STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { return true; } +STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, intptr_t n) { return true; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return self->last == NULL; } STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); } STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, i_key value) @@ -150,9 +150,9 @@ STC_INLINE _cx_node* _cx_memb(_unlink_node_front)(_cx_self* self) STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return &self->last->next->value; } STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self) { return &self->last->value; } -STC_INLINE size_t +STC_INLINE intptr_t _cx_memb(_count)(const _cx_self* self) { - size_t n = 1; const _cx_node *node = self->last; + intptr_t n = 1; const _cx_node *node = self->last; if (!node) return 0; while ((node = node->next) != self->last) ++n; return n; @@ -414,9 +414,9 @@ _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw val) { it2.ref = NULL; return it2; } -STC_DEF size_t +STC_DEF intptr_t _cx_memb(_remove)(_cx_self* self, _cx_raw val) { - size_t n = 0; + intptr_t n = 0; _cx_node *prev = self->last, *node; if (prev) do { node = prev->next; diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 59419f16..21130864 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -41,19 +41,19 @@ typedef i_keyraw _cx_raw; STC_API void _cx_memb(_make_heap)(_cx_self* self); -STC_API void _cx_memb(_erase_at)(_cx_self* self, size_t idx); +STC_API void _cx_memb(_erase_at)(_cx_self* self, intptr_t idx); STC_API void _cx_memb(_push)(_cx_self* self, _cx_value value); STC_INLINE _cx_self _cx_memb(_init)(void) { return c_LITERAL(_cx_self){NULL}; } -STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } -STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, const size_t cap) { +STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, const intptr_t cap) { if (cap != self->_len && cap <= self->_cap) return true; _cx_value *d = (_cx_value *)c_realloc(self->data, cap*sizeof *d); return d ? (self->data = d, self->_cap = cap, true) : false; @@ -62,32 +62,32 @@ STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, const size_t cap) { STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) { _cx_memb(_reserve)(self, self->_len); } -STC_INLINE _cx_self _cx_memb(_with_capacity)(const size_t cap) { +STC_INLINE _cx_self _cx_memb(_with_capacity)(const intptr_t cap) { _cx_self out = {NULL}; _cx_memb(_reserve)(&out, cap); return out; } -STC_INLINE _cx_self _cx_memb(_with_size)(const size_t size, i_key null) { +STC_INLINE _cx_self _cx_memb(_with_size)(const intptr_t size, i_key null) { _cx_self out = {NULL}; _cx_memb(_reserve)(&out, size); while (out._len < size) out.data[out._len++] = null; return out; } STC_INLINE void _cx_memb(_clear)(_cx_self* self) { - size_t i = self->_len; self->_len = 0; + intptr_t i = self->_len; self->_len = 0; while (i--) { i_keydrop((self->data + i)); } } STC_INLINE void _cx_memb(_drop)(_cx_self* self) { _cx_memb(_clear)(self); c_free(self->data); } -STC_INLINE size_t _cx_memb(_size)(const _cx_self* q) +STC_INLINE intptr_t _cx_memb(_size)(const _cx_self* q) { return q->_len; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* q) { return !q->_len; } -STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* q) +STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* q) { return q->_cap; } STC_INLINE const _cx_value* _cx_memb(_top)(const _cx_self* self) @@ -117,9 +117,9 @@ STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) #if defined(i_implement) STC_DEF void -_cx_memb(_sift_down_)(_cx_self* self, const size_t idx, const size_t n) { +_cx_memb(_sift_down_)(_cx_self* self, const intptr_t idx, const intptr_t n) { _cx_value t, *arr = self->data - 1; - for (size_t r = idx, c = idx*2; c <= n; c *= 2) { + for (intptr_t r = idx, c = idx*2; c <= n; c *= 2) { c += i_less_functor(self, (&arr[c]), (&arr[c + (c < n)])); if (!(i_less_functor(self, (&arr[r]), (&arr[c])))) return; t = arr[r], arr[r] = arr[c], arr[r = c] = t; @@ -128,8 +128,8 @@ _cx_memb(_sift_down_)(_cx_self* self, const size_t idx, const size_t n) { STC_DEF void _cx_memb(_make_heap)(_cx_self* self) { - size_t n = self->_len; - for (size_t k = n/2; k != 0; --k) + intptr_t n = self->_len; + for (intptr_t k = n/2; k != 0; --k) _cx_memb(_sift_down_)(self, k, n); } @@ -143,9 +143,9 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self q) { #endif STC_DEF void -_cx_memb(_erase_at)(_cx_self* self, const size_t idx) { +_cx_memb(_erase_at)(_cx_self* self, const intptr_t idx) { i_keydrop((self->data + idx)); - const size_t n = --self->_len; + const intptr_t n = --self->_len; self->data[idx] = self->data[n]; _cx_memb(_sift_down_)(self, idx + 1, n); } @@ -155,7 +155,7 @@ _cx_memb(_push)(_cx_self* self, _cx_value value) { if (self->_len == self->_cap) _cx_memb(_reserve)(self, self->_len*3/2 + 4); _cx_value *arr = self->data - 1; /* base 1 */ - size_t c = ++self->_len; + intptr_t c = ++self->_len; for (; c > 1 && (i_less_functor(self, (&arr[c/2]), (&value))); c /= 2) arr[c] = arr[c/2]; arr[c] = value; diff --git a/include/stc/cregex.h b/include/stc/cregex.h index 206c319d..c41fbb67 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -143,7 +143,7 @@ cstr cregex_replace_sv_6(const cregex* re, csview input, const char* replace, un #define cregex_replace_3(re, input, replace) cregex_replace_4(re, input, replace, ~0U) STC_INLINE cstr cregex_replace_4(const cregex* re, const char* input, const char* replace, unsigned count) { - csview sv = {input, strlen(input)}; + csview sv = {input, c_strlen(input)}; return cregex_replace_sv_4(re, sv, replace, count); } diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 69ea6f0c..97d5256c 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -58,13 +58,13 @@ STC_INLINE void _cx_memb(_create)(_cx_self* self) STC_INLINE void _cx_memb(_create)(_cx_self* self) { self->_len = 0; self->_cap = 0; self->data = NULL; } -STC_INLINE _cx_self _cx_memb(_with_capacity)(size_t cap) { - _cx_self out = {(_cx_value *) c_malloc(cap*sizeof(i_key)), 0, cap}; +STC_INLINE _cx_self _cx_memb(_with_capacity)(intptr_t cap) { + _cx_self out = {(_cx_value *) c_malloc(cap*c_sizeof(i_key)), 0, cap}; return out; } -STC_INLINE _cx_self _cx_memb(_with_size)(size_t size, i_key null) { - _cx_self out = {(_cx_value *) c_malloc(size*sizeof null), size, size}; +STC_INLINE _cx_self _cx_memb(_with_size)(intptr_t size, i_key null) { + _cx_self out = {(_cx_value *) c_malloc(size*c_sizeof null), size, size}; while (size) out.data[--size] = null; return out; } @@ -83,13 +83,13 @@ STC_INLINE void _cx_memb(_drop)(_cx_self* self) { #endif } -STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) +STC_INLINE intptr_t _cx_memb(_size)(const _cx_self* self) { return self->_len; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return !self->_len; } -STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { +STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* self) { #ifndef i_capacity return self->_cap; #else @@ -97,17 +97,17 @@ STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { #endif } -STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { +STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, intptr_t n) { if (n < self->_len) return true; #ifndef i_capacity - _cx_value *t = (_cx_value *)c_realloc(self->data, n*sizeof *t); + _cx_value *t = (_cx_value *)c_realloc(self->data, n*c_sizeof *t); if (t) { self->_cap = n, self->data = t; return true; } #endif return false; } -STC_INLINE _cx_value* _cx_memb(_append_uninit)(_cx_self *self, size_t n) { - size_t len = self->_len; +STC_INLINE _cx_value* _cx_memb(_append_uninit)(_cx_self *self, intptr_t n) { + intptr_t len = self->_len; if (!_cx_memb(_reserve)(self, len + n)) return NULL; self->_len += n; return self->data + len; @@ -136,15 +136,15 @@ STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value val) { STC_INLINE void _cx_memb(_pop)(_cx_self* self) { assert(!_cx_memb(_empty)(self)); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } -STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } -STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx) +STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, intptr_t idx) { assert(idx < self->_len); return self->data + idx; } -STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, size_t idx) +STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, intptr_t idx) { assert(idx < self->_len); return self->data + idx; } #if !defined i_no_emplace @@ -154,9 +154,9 @@ STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) #if !defined i_no_clone STC_INLINE _cx_self _cx_memb(_clone)(_cx_self v) { - _cx_self out = {(_cx_value *)c_malloc(v._len*sizeof(_cx_value)), v._len, v._len}; + _cx_self out = {(_cx_value *)c_malloc(v._len*c_sizeof(_cx_value)), v._len, v._len}; if (!out.data) out._cap = 0; - else for (size_t i = 0; i < v._len; ++v.data) + else for (intptr_t i = 0; i < v._len; ++v.data) out.data[i++] = i_keyclone((*v.data)); return out; } diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 9f6415ac..f965487c 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -47,26 +47,26 @@ #endif enum { cstr_s_cap = sizeof(cstr_buf) - 2 }; -#define cstr_s_size(s) ((size_t)(s)->sml.size) +#define cstr_s_size(s) ((intptr_t)(s)->sml.size) #define cstr_s_set_size(s, len) ((s)->sml.size = (uint8_t)(len), (s)->sml.data[len] = 0) #define cstr_s_data(s) (s)->sml.data #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ #define byte_rotl_(x, b) ((x) << (b)*8 | (x) >> (sizeof(x) - (b))*8) - #define cstr_l_cap(s) (~byte_rotl_((s)->lon.ncap, sizeof((s)->lon.ncap) - 1)) - #define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~byte_rotl_(cap, 1)) + #define cstr_l_cap(s) (intptr_t)(~byte_rotl_((s)->lon.ncap, sizeof((s)->lon.ncap) - 1)) + #define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~byte_rotl_((size_t)(cap), 1)) #else - #define cstr_l_cap(s) (~(s)->lon.ncap) - #define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~(cap)) + #define cstr_l_cap(s) (intptr_t)(~(s)->lon.ncap) + #define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~(size_t)(cap)) #endif -#define cstr_l_size(s) ((s)->lon.size) -#define cstr_l_set_size(s, len) ((s)->lon.data[(s)->lon.size = (len)] = 0) +#define cstr_l_size(s) (intptr_t)((s)->lon.size) +#define cstr_l_set_size(s, len) ((s)->lon.data[(s)->lon.size = (size_t)(len)] = 0) #define cstr_l_data(s) (s)->lon.data #define cstr_l_drop(s) c_free((s)->lon.data) #define cstr_is_long(s) ((s)->sml.size > 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_API char* _cstr_init(cstr* self, intptr_t len, intptr_t cap); +STC_API char* _cstr_internal_move(cstr* self, intptr_t pos1, intptr_t pos2); /**************************** PUBLIC API **********************************/ @@ -74,19 +74,19 @@ STC_API char* _cstr_internal_move(cstr* self, size_t pos1, size_t pos2); #define cstr_NULL (c_LITERAL(cstr){{{0}, 0}}) #define cstr_toraw(self) cstr_str(self) -STC_API char* cstr_reserve(cstr* self, size_t cap); -STC_API void cstr_shrink_to_fit(cstr* self); -STC_API void cstr_resize(cstr* self, size_t size, char value); -STC_API size_t cstr_find_at(const cstr* self, size_t pos, const char* search); -STC_API char* cstr_assign_n(cstr* self, const char* str, size_t len); -STC_API char* cstr_append_n(cstr* self, const char* str, size_t len); -STC_API bool cstr_getdelim(cstr *self, int delim, FILE *fp); -STC_API void cstr_erase(cstr* self, size_t pos, size_t len); -STC_API void cstr_u8_erase(cstr* self, size_t bytepos, size_t u8len); -STC_API cstr cstr_from_fmt(const char* fmt, ...); -STC_API size_t cstr_append_fmt(cstr* self, const char* fmt, ...); -STC_API size_t cstr_printf(cstr* self, const char* fmt, ...); -STC_API cstr cstr_replace_sv(csview sv, csview search, csview repl, unsigned count); +STC_API char* cstr_reserve(cstr* self, intptr_t cap); +STC_API void cstr_shrink_to_fit(cstr* self); +STC_API void cstr_resize(cstr* self, intptr_t size, char value); +STC_API intptr_t cstr_find_at(const cstr* self, intptr_t pos, const char* search); +STC_API char* cstr_assign_n(cstr* self, const char* str, intptr_t len); +STC_API char* cstr_append_n(cstr* self, const char* str, intptr_t len); +STC_API bool cstr_getdelim(cstr *self, int delim, FILE *fp); +STC_API void cstr_erase(cstr* self, intptr_t pos, intptr_t len); +STC_API void cstr_u8_erase(cstr* self, intptr_t bytepos, intptr_t u8len); +STC_API cstr cstr_from_fmt(const char* fmt, ...); +STC_API intptr_t cstr_append_fmt(cstr* self, const char* fmt, ...); +STC_API intptr_t cstr_printf(cstr* self, const char* fmt, ...); +STC_API cstr cstr_replace_sv(csview sv, csview search, csview repl, int32_t count); STC_INLINE cstr_buf cstr_buffer(cstr* s) { return cstr_is_long(s) @@ -101,25 +101,25 @@ STC_INLINE csview cstr_sv(const cstr* s) { STC_INLINE cstr cstr_init(void) { return cstr_NULL; } -STC_INLINE cstr cstr_from_n(const char* str, const size_t len) { +STC_INLINE cstr cstr_from_n(const char* str, const intptr_t len) { cstr s; - memcpy(_cstr_init(&s, len, len), str, len); + c_memcpy(_cstr_init(&s, len, len), str, len); return s; } STC_INLINE cstr cstr_from(const char* str) - { return cstr_from_n(str, strlen(str)); } + { return cstr_from_n(str, c_strlen(str)); } STC_INLINE cstr cstr_from_sv(csview sv) { return cstr_from_n(sv.str, sv.size); } -STC_INLINE cstr cstr_with_size(const size_t size, const char value) { +STC_INLINE cstr cstr_with_size(const intptr_t size, const char value) { cstr s; - memset(_cstr_init(&s, size, size), value, size); + c_memset(_cstr_init(&s, size, size), value, size); return s; } -STC_INLINE cstr cstr_with_capacity(const size_t cap) { +STC_INLINE cstr cstr_with_capacity(const intptr_t cap) { cstr s; _cstr_init(&s, 0, cap); return s; @@ -150,7 +150,7 @@ STC_INLINE void cstr_drop(cstr* self) { #define SSO_CALL(s, call) (cstr_is_long(s) ? cstr_l_##call : cstr_s_##call) -STC_INLINE void _cstr_set_size(cstr* self, size_t len) +STC_INLINE void _cstr_set_size(cstr* self, intptr_t len) { SSO_CALL(self, set_size(self, len)); } STC_INLINE char* cstr_data(cstr* self) @@ -162,10 +162,10 @@ STC_INLINE const char* cstr_str(const cstr* self) STC_INLINE bool cstr_empty(const cstr* self) { return self->sml.size == 0; } -STC_INLINE size_t cstr_size(const cstr* self) +STC_INLINE intptr_t cstr_size(const cstr* self) { return SSO_CALL(self, size(self)); } -STC_INLINE size_t cstr_capacity(const cstr* self) +STC_INLINE intptr_t cstr_capacity(const cstr* self) { return cstr_is_long(self) ? cstr_l_cap(self) : cstr_s_cap; } // utf8 methods defined in/depending on src/utf8code.c: @@ -182,10 +182,10 @@ STC_INLINE cstr cstr_toupper_sv(csview sv) { return cstr_tocase(sv, 2); } STC_INLINE cstr cstr_tolower(const char* str) - { return cstr_tolower_sv(c_SV(str, strlen(str))); } + { return cstr_tolower_sv(c_SV(str, c_strlen(str))); } STC_INLINE cstr cstr_toupper(const char* str) - { return cstr_toupper_sv(c_SV(str, strlen(str))); } + { return cstr_toupper_sv(c_SV(str, c_strlen(str))); } STC_INLINE void cstr_lowercase(cstr* self) { cstr_take(self, cstr_tolower_sv(cstr_sv(self))); } @@ -198,19 +198,19 @@ STC_INLINE bool cstr_valid_utf8(const cstr* self) // other utf8 -STC_INLINE size_t cstr_u8_size(const cstr* self) +STC_INLINE intptr_t cstr_u8_size(const cstr* self) { return utf8_size(cstr_str(self)); } -STC_INLINE size_t cstr_u8_size_n(const cstr* self, size_t nbytes) +STC_INLINE intptr_t cstr_u8_size_n(const cstr* self, intptr_t nbytes) { return utf8_size_n(cstr_str(self), nbytes); } -STC_INLINE size_t cstr_u8_to_pos(const cstr* self, size_t u8idx) +STC_INLINE intptr_t cstr_u8_to_pos(const cstr* self, intptr_t u8idx) { return utf8_pos(cstr_str(self), u8idx); } -STC_INLINE const char* cstr_u8_at(const cstr* self, size_t u8idx) +STC_INLINE const char* cstr_u8_at(const cstr* self, intptr_t u8idx) { return utf8_at(cstr_str(self), u8idx); } -STC_INLINE csview cstr_u8_chr(const cstr* self, size_t u8idx) { +STC_INLINE csview cstr_u8_chr(const cstr* self, intptr_t u8idx) { const char* str = cstr_str(self); csview sv; sv.str = utf8_at(str, u8idx); @@ -246,8 +246,8 @@ STC_INLINE cstr_iter cstr_advance(cstr_iter it, intptr_t pos) { STC_INLINE void cstr_clear(cstr* self) { _cstr_set_size(self, 0); } -STC_INLINE char* cstr_append_uninit(cstr *self, size_t len) { - size_t sz = cstr_size(self); +STC_INLINE char* cstr_append_uninit(cstr *self, intptr_t len) { + intptr_t sz = cstr_size(self); char* d = cstr_reserve(self, sz + len); if (!d) return NULL; _cstr_set_size(self, sz + len); @@ -262,7 +262,7 @@ STC_INLINE int cstr_icmp(const cstr* s1, const cstr* 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); + return x.size == y.size && !c_memcmp(x.str, y.str, x.size); } @@ -270,7 +270,7 @@ STC_INLINE bool cstr_equals(const cstr* self, const char* str) { return !strcmp(cstr_str(self), str); } STC_INLINE bool cstr_equals_sv(const cstr* self, csview sv) - { return sv.size == cstr_size(self) && !memcmp(cstr_str(self), sv.str, sv.size); } + { return sv.size == cstr_size(self) && !c_memcmp(cstr_str(self), sv.str, sv.size); } STC_INLINE bool cstr_equals_s(const cstr* self, cstr s) { return !cstr_cmp(self, &s); } @@ -279,14 +279,14 @@ STC_INLINE bool cstr_iequals(const cstr* self, const char* str) { return !utf8_icmp(cstr_str(self), str); } -STC_INLINE size_t cstr_find(const cstr* self, const char* search) { +STC_INLINE intptr_t cstr_find(const cstr* self, const char* search) { const char *str = cstr_str(self), *res = strstr((char*)str, search); - return res ? (size_t)(res - str) : c_NPOS; + return res ? (res - str) : c_NPOS; } -STC_API size_t cstr_find_sv(const cstr* self, csview search); +STC_API intptr_t cstr_find_sv(const cstr* self, csview search); -STC_INLINE size_t cstr_find_s(const cstr* self, cstr search) +STC_INLINE intptr_t cstr_find_s(const cstr* self, cstr search) { return cstr_find(self, cstr_str(&search)); } @@ -302,7 +302,7 @@ STC_INLINE bool cstr_contains_s(const cstr* self, cstr search) STC_INLINE bool cstr_starts_with_sv(const cstr* self, csview sub) { if (sub.size > cstr_size(self)) return false; - return !memcmp(cstr_str(self), sub.str, sub.size); + return !c_memcmp(cstr_str(self), sub.str, sub.size); } STC_INLINE bool cstr_starts_with(const cstr* self, const char* sub) { @@ -316,7 +316,7 @@ STC_INLINE bool cstr_starts_with_s(const cstr* self, cstr sub) STC_INLINE bool cstr_istarts_with(const cstr* self, const char* sub) { csview sv = cstr_sv(self); - size_t len = strlen(sub); + intptr_t len = c_strlen(sub); return len <= sv.size && !utf8_icmp_sv(sv, c_SV(sub, len)); } @@ -324,24 +324,24 @@ STC_INLINE bool cstr_istarts_with(const cstr* self, const char* sub) { STC_INLINE bool cstr_ends_with_sv(const cstr* self, csview sub) { csview sv = cstr_sv(self); if (sub.size > sv.size) return false; - return !memcmp(sv.str + sv.size - sub.size, sub.str, sub.size); + return !c_memcmp(sv.str + sv.size - sub.size, sub.str, sub.size); } STC_INLINE bool cstr_ends_with_s(const cstr* self, cstr sub) { return cstr_ends_with_sv(self, cstr_sv(&sub)); } STC_INLINE bool cstr_ends_with(const cstr* self, const char* sub) - { return cstr_ends_with_sv(self, c_SV(sub, strlen(sub))); } + { return cstr_ends_with_sv(self, c_SV(sub, c_strlen(sub))); } STC_INLINE bool cstr_iends_with(const cstr* self, const char* sub) { csview sv = cstr_sv(self); - size_t n = strlen(sub); + intptr_t n = c_strlen(sub); return n <= sv.size && !utf8_icmp(sv.str + sv.size - n, sub); } STC_INLINE char* cstr_assign(cstr* self, const char* str) - { return cstr_assign_n(self, str, strlen(str)); } + { return cstr_assign_n(self, str, c_strlen(str)); } STC_INLINE char* cstr_assign_sv(cstr* self, csview sv) { return cstr_assign_n(self, sv.str, sv.size); } @@ -359,11 +359,11 @@ STC_INLINE void cstr_pop(cstr* self) { csview sv = cstr_sv(self); const char* s = sv.str + sv.size; while ((*--s & 0xC0) == 0x80) ; - _cstr_set_size(self, (size_t)(s - sv.str)); + _cstr_set_size(self, (s - sv.str)); } STC_INLINE char* cstr_append(cstr* self, const char* str) - { return cstr_append_n(self, str, strlen(str)); } + { return cstr_append_n(self, str, c_strlen(str)); } STC_INLINE void cstr_append_sv(cstr* self, csview sv) { cstr_append_n(self, sv.str, sv.size); } @@ -375,33 +375,33 @@ STC_INLINE char* cstr_append_s(cstr* self, cstr s) { #define cstr_replace(...) c_MACRO_OVERLOAD(cstr_replace, __VA_ARGS__) #define cstr_replace_3(self, search, repl) cstr_replace_4(self, search, repl, ~0U) -STC_INLINE void cstr_replace_4(cstr* self, const char* search, const char* repl, unsigned count) { - cstr_take(self, cstr_replace_sv(cstr_sv(self), c_SV(search, strlen(search)), - c_SV(repl, strlen(repl)), count)); +STC_INLINE void cstr_replace_4(cstr* self, const char* search, const char* repl, int32_t count) { + cstr_take(self, cstr_replace_sv(cstr_sv(self), c_SV(search, c_strlen(search)), + c_SV(repl, c_strlen(repl)), count)); } -STC_INLINE void cstr_replace_at_sv(cstr* self, size_t pos, size_t len, const csview repl) { +STC_INLINE void cstr_replace_at_sv(cstr* self, intptr_t pos, intptr_t len, const csview repl) { char* d = _cstr_internal_move(self, pos + len, pos + repl.size); - memcpy(d + pos, repl.str, repl.size); + c_memcpy(d + pos, repl.str, repl.size); } -STC_INLINE void cstr_replace_at(cstr* self, size_t pos, size_t len, const char* repl) - { cstr_replace_at_sv(self, pos, len, c_SV(repl, strlen(repl))); } +STC_INLINE void cstr_replace_at(cstr* self, intptr_t pos, intptr_t len, const char* repl) + { cstr_replace_at_sv(self, pos, len, c_SV(repl, c_strlen(repl))); } -STC_INLINE void cstr_replace_at_s(cstr* self, size_t pos, size_t len, cstr repl) +STC_INLINE void cstr_replace_at_s(cstr* self, intptr_t pos, intptr_t len, cstr repl) { cstr_replace_at_sv(self, pos, len, cstr_sv(&repl)); } -STC_INLINE void cstr_u8_replace_at(cstr* self, size_t bytepos, size_t u8len, csview repl) +STC_INLINE void cstr_u8_replace_at(cstr* self, intptr_t bytepos, intptr_t u8len, csview repl) { cstr_replace_at_sv(self, bytepos, utf8_pos(cstr_str(self) + bytepos, u8len), repl); } -STC_INLINE void cstr_insert(cstr* self, size_t pos, const char* str) - { cstr_replace_at_sv(self, pos, 0, c_SV(str, strlen(str))); } +STC_INLINE void cstr_insert(cstr* self, intptr_t pos, const char* str) + { cstr_replace_at_sv(self, pos, 0, c_SV(str, c_strlen(str))); } -STC_INLINE void cstr_insert_sv(cstr* self, size_t pos, csview sv) +STC_INLINE void cstr_insert_sv(cstr* self, intptr_t pos, csview sv) { cstr_replace_at_sv(self, pos, 0, sv); } -STC_INLINE void cstr_insert_s(cstr* self, size_t pos, cstr s) { +STC_INLINE void cstr_insert_s(cstr* self, intptr_t pos, cstr s) { csview sv = cstr_sv(&s); cstr_replace_at_sv(self, pos, 0, sv); } @@ -425,7 +425,7 @@ cstr cstr_tocase(csview sv, int k) { cstr out = cstr_init(); char *buf = cstr_reserve(&out, sv.size*3/2); const char *end = sv.str + sv.size; - uint32_t cp; size_t sz = 0; + uint32_t cp; intptr_t sz = 0; utf8_decode_t d = {.state=0}; while (sv.str < end) { @@ -451,25 +451,25 @@ STC_DEF uint64_t cstr_hash(const cstr *self) { return cfasthash(sv.str, sv.size); } -STC_DEF size_t cstr_find_sv(const cstr* self, csview search) { +STC_DEF intptr_t cstr_find_sv(const cstr* self, csview search) { csview sv = cstr_sv(self); char* res = cstrnstrn(sv.str, search.str, sv.size, search.size); - return res ? (size_t)(res - sv.str) : c_NPOS; + return res ? (res - sv.str) : c_NPOS; } -STC_DEF char* _cstr_internal_move(cstr* self, const size_t pos1, const size_t pos2) { +STC_DEF char* _cstr_internal_move(cstr* self, const intptr_t pos1, const intptr_t pos2) { cstr_buf r = cstr_buffer(self); if (pos1 != pos2) { - const intptr_t newlen = (intptr_t)(r.size + pos2 - pos1); - if (newlen > (intptr_t)r.cap) + const intptr_t newlen = (r.size + pos2 - pos1); + if (newlen > r.cap) r.data = cstr_reserve(self, r.size*3/2 + pos2 - pos1); - memmove(&r.data[pos2], &r.data[pos1], r.size - pos1); - _cstr_set_size(self, (size_t)newlen); + c_memmove(&r.data[pos2], &r.data[pos1], r.size - pos1); + _cstr_set_size(self, newlen); } return r.data; } -STC_DEF char* _cstr_init(cstr* self, const size_t len, const size_t cap) { +STC_DEF char* _cstr_init(cstr* self, const intptr_t len, const intptr_t cap) { if (cap > cstr_s_cap) { self->lon.data = (char *)c_malloc(cap + 1); cstr_l_set_size(self, len); @@ -488,13 +488,13 @@ STC_DEF void cstr_shrink_to_fit(cstr* self) { self->lon.data = (char *)c_realloc(self->lon.data, r.size + 1); cstr_l_set_cap(self, r.size); } else if (r.cap > cstr_s_cap) { - memcpy(self->sml.data, r.data, r.size + 1); + c_memcpy(self->sml.data, r.data, r.size + 1); cstr_s_set_size(self, r.size); c_free(r.data); } } -STC_DEF char* cstr_reserve(cstr* self, const size_t cap) { +STC_DEF char* cstr_reserve(cstr* self, const intptr_t cap) { if (cstr_is_long(self)) { if (cap > cstr_l_cap(self)) { self->lon.data = (char *)c_realloc(self->lon.data, cap + 1); @@ -505,47 +505,47 @@ STC_DEF char* cstr_reserve(cstr* self, const size_t cap) { /* from short to long: */ if (cap > cstr_s_cap) { char* data = (char *)c_malloc(cap + 1); - const size_t len = cstr_s_size(self); - memcpy(data, self->sml.data, cstr_s_cap + 1); + const intptr_t len = cstr_s_size(self); + c_memcpy(data, self->sml.data, cstr_s_cap + 1); self->lon.data = data; - self->lon.size = len; + self->lon.size = (size_t)len; cstr_l_set_cap(self, cap); return data; } return self->sml.data; } -STC_DEF void cstr_resize(cstr* self, const size_t size, const char value) { +STC_DEF void cstr_resize(cstr* self, const intptr_t size, const char value) { cstr_buf r = cstr_buffer(self); if (size > r.size) { if (size > r.cap) r.data = cstr_reserve(self, size); - memset(r.data + r.size, value, size - r.size); + c_memset(r.data + r.size, value, size - r.size); } _cstr_set_size(self, size); } -STC_DEF size_t cstr_find_at(const cstr* self, const size_t pos, const char* search) { +STC_DEF intptr_t cstr_find_at(const cstr* self, const intptr_t pos, const char* search) { csview sv = cstr_sv(self); if (pos > sv.size) return c_NPOS; const char* res = strstr((char*)sv.str + pos, search); - return res ? (size_t)(res - sv.str) : c_NPOS; + return res ? (res - sv.str) : c_NPOS; } -STC_DEF char* cstr_assign_n(cstr* self, const char* str, const size_t len) { +STC_DEF char* cstr_assign_n(cstr* self, const char* str, const intptr_t len) { char* d = cstr_reserve(self, len); - memmove(d, str, len); + c_memmove(d, str, len); _cstr_set_size(self, len); return d; } -STC_DEF char* cstr_append_n(cstr* self, const char* str, const size_t len) { +STC_DEF char* cstr_append_n(cstr* self, const char* str, const intptr_t len) { cstr_buf r = cstr_buffer(self); if (r.size + len > r.cap) { const size_t off = (size_t)(str - r.data); r.data = cstr_reserve(self, r.size*3/2 + len); - if (off <= r.size) str = r.data + off; /* handle self append */ + if (off <= (size_t)r.size) str = r.data + off; /* handle self append */ } - memcpy(r.data + r.size, str, len); + c_memcpy(r.data + r.size, str, len); _cstr_set_size(self, r.size + len); return r.data; } @@ -554,7 +554,7 @@ STC_DEF bool cstr_getdelim(cstr *self, const int delim, FILE *fp) { int c = fgetc(fp); if (c == EOF) return false; - size_t pos = 0; + intptr_t pos = 0; cstr_buf r = cstr_buffer(self); for (;;) { if (c == delim || c == EOF) { @@ -571,13 +571,13 @@ STC_DEF bool cstr_getdelim(cstr *self, const int delim, FILE *fp) { } STC_DEF cstr -cstr_replace_sv(csview in, csview search, csview repl, unsigned count) { +cstr_replace_sv(csview in, csview search, csview repl, int32_t count) { cstr out = cstr_NULL; - size_t from = 0; char* res; - if (!count) count = ~0U; + intptr_t from = 0; char* res; + if (!count) count = INT32_MAX; if (search.size) while (count-- && (res = cstrnstrn(in.str + from, search.str, in.size - from, search.size))) { - const size_t pos = (size_t)(res - in.str); + const intptr_t pos = (res - in.str); cstr_append_n(&out, in.str + from, pos - from); cstr_append_n(&out, repl.str, repl.size); from = pos + search.size; @@ -586,17 +586,17 @@ cstr_replace_sv(csview in, csview search, csview repl, unsigned count) { return out; } -STC_DEF void cstr_erase(cstr* self, const size_t pos, size_t len) { +STC_DEF void cstr_erase(cstr* self, const intptr_t pos, intptr_t len) { cstr_buf r = cstr_buffer(self); if (len > r.size - pos) len = r.size - pos; - memmove(&r.data[pos], &r.data[pos + len], r.size - (pos + len)); + c_memmove(&r.data[pos], &r.data[pos + len], r.size - (pos + len)); _cstr_set_size(self, r.size - len); } -STC_DEF void cstr_u8_erase(cstr* self, const size_t bytepos, const size_t u8len) { +STC_DEF void cstr_u8_erase(cstr* self, const intptr_t bytepos, const intptr_t u8len) { cstr_buf r = cstr_buffer(self); - size_t len = utf8_pos(r.data + bytepos, u8len); - memmove(&r.data[bytepos], &r.data[bytepos + len], r.size - (bytepos + len)); + intptr_t len = utf8_pos(r.data + bytepos, u8len); + c_memmove(&r.data[bytepos], &r.data[bytepos + len], r.size - (bytepos + len)); _cstr_set_size(self, r.size - len); } @@ -608,10 +608,10 @@ STC_DEF void cstr_u8_erase(cstr* self, const size_t bytepos, const size_t u8len) # pragma warning(disable: 4996) #endif -STC_DEF size_t cstr_vfmt(cstr* self, size_t start, const char* fmt, va_list args) { +STC_DEF intptr_t cstr_vfmt(cstr* self, intptr_t start, const char* fmt, va_list args) { va_list args2; va_copy(args2, args); - const size_t n = (size_t)vsnprintf(NULL, (size_t)0, fmt, args); + const int n = vsnprintf(NULL, 0ULL, fmt, args); vsprintf(cstr_reserve(self, start + n) + start, fmt, args2); va_end(args2); _cstr_set_size(self, start + n); @@ -632,19 +632,19 @@ STC_DEF cstr cstr_from_fmt(const char* fmt, ...) { return s; } -STC_DEF size_t cstr_append_fmt(cstr* self, const char* fmt, ...) { +STC_DEF intptr_t cstr_append_fmt(cstr* self, const char* fmt, ...) { va_list args; va_start(args, fmt); - const size_t n = cstr_vfmt(self, cstr_size(self), fmt, args); + const intptr_t n = cstr_vfmt(self, cstr_size(self), fmt, args); va_end(args); return n; } /* NB! self-data in args is UB */ -STC_DEF size_t cstr_printf(cstr* self, const char* fmt, ...) { +STC_DEF intptr_t cstr_printf(cstr* self, const char* fmt, ...) { va_list args; va_start(args, fmt); - const size_t n = cstr_vfmt(self, 0, fmt, args); + const intptr_t n = cstr_vfmt(self, 0, fmt, args); va_end(args); return n; } diff --git a/include/stc/csview.h b/include/stc/csview.h index 4df735b3..a30672cd 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -34,41 +34,41 @@ #define csview_lit(literal) c_SV_1(literal) #define csview_from_n(str, n) c_SV_2(str, n) -STC_API size_t csview_find_sv(csview sv, csview search); +STC_API intptr_t csview_find_sv(csview sv, csview search); STC_INLINE csview csview_from(const char* str) - { return c_LITERAL(csview){str, strlen(str)}; } + { return c_LITERAL(csview){str, c_strlen(str)}; } STC_INLINE void csview_clear(csview* self) { *self = csview_NULL; } -STC_INLINE size_t csview_size(csview sv) { return sv.size; } +STC_INLINE intptr_t csview_size(csview sv) { return sv.size; } STC_INLINE bool csview_empty(csview sv) { return sv.size == 0; } STC_INLINE bool csview_equals(csview sv, const char* str) - { size_t n = strlen(str); return sv.size == n && !memcmp(sv.str, str, n); } + { intptr_t n = c_strlen(str); return sv.size == n && !c_memcmp(sv.str, str, n); } -STC_INLINE size_t csview_find(csview sv, const char* str) - { return csview_find_sv(sv, c_SV(str, strlen(str))); } +STC_INLINE intptr_t csview_find(csview sv, const char* str) + { return csview_find_sv(sv, c_SV(str, c_strlen(str))); } STC_INLINE bool csview_contains(csview sv, const char* str) { return csview_find(sv, str) != c_NPOS; } STC_INLINE bool csview_starts_with(csview sv, const char* str) { - size_t n = strlen(str); - return n > sv.size ? false : !memcmp(sv.str, str, n); + intptr_t n = c_strlen(str); + return n > sv.size ? false : !c_memcmp(sv.str, str, n); } STC_INLINE bool csview_ends_with(csview sv, const char* str) { - size_t n = strlen(str); - return n > sv.size ? false : !memcmp(sv.str + sv.size - n, str, n); + intptr_t n = c_strlen(str); + return n > sv.size ? false : !c_memcmp(sv.str + sv.size - n, str, n); } -STC_INLINE csview csview_substr(csview sv, size_t pos, size_t n) { +STC_INLINE csview csview_substr(csview sv, intptr_t pos, intptr_t n) { if (pos + n > sv.size) n = sv.size - pos; sv.str += pos, sv.size = n; return sv; } -STC_INLINE csview csview_slice(csview sv, size_t p1, size_t p2) { +STC_INLINE csview csview_slice(csview sv, intptr_t p1, intptr_t p2) { if (p2 > sv.size) p2 = sv.size; sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0; return sv; @@ -99,10 +99,10 @@ STC_INLINE csview_iter csview_advance(csview_iter it, intptr_t pos) { /* utf8 */ -STC_INLINE size_t csview_u8_size(csview sv) +STC_INLINE intptr_t csview_u8_size(csview sv) { return utf8_size_n(sv.str, sv.size); } -STC_INLINE csview csview_u8_substr(csview sv, size_t bytepos, size_t u8len) { +STC_INLINE csview csview_u8_substr(csview sv, intptr_t bytepos, intptr_t u8len) { sv.str += bytepos; sv.size = utf8_pos(sv.str, u8len); return sv; @@ -111,12 +111,12 @@ STC_INLINE csview csview_u8_substr(csview sv, size_t bytepos, size_t u8len) { STC_INLINE bool csview_valid_utf8(csview sv) // depends on src/utf8code.c { return utf8_valid_n(sv.str, sv.size); } -STC_API csview csview_substr_ex(csview sv, intptr_t pos, size_t n); +STC_API csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n); STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2); -STC_API csview csview_token(csview sv, const char* sep, size_t* start); +STC_API csview csview_token(csview sv, const char* sep, intptr_t* start); #define c_FORTOKEN_SV(it, inputsv, sep) \ - for (struct { csview _inp, token, *ref; const char *_sep; size_t pos; } \ + for (struct { csview _inp, token, *ref; const char *_sep; intptr_t pos; } \ it = {._inp=inputsv, .token=it._inp, .ref=&it.token, ._sep=sep} \ ; it.pos <= it._inp.size && (it.token = csview_token(it._inp, it._sep, &it.pos)).str ; ) @@ -126,27 +126,27 @@ STC_API csview csview_token(csview sv, const char* sep, size_t* start); /* csview interaction with cstr: */ #ifdef CSTR_H_INCLUDED -STC_INLINE csview cstr_substr(const cstr* self, size_t pos, size_t n) +STC_INLINE csview cstr_substr(const cstr* self, intptr_t pos, intptr_t n) { return csview_substr(cstr_sv(self), pos, n); } -STC_INLINE csview cstr_slice(const cstr* self, size_t p1, size_t p2) +STC_INLINE csview cstr_slice(const cstr* self, intptr_t p1, intptr_t p2) { return csview_slice(cstr_sv(self), p1, p2); } -STC_INLINE csview cstr_substr_ex(const cstr* self, intptr_t pos, size_t n) +STC_INLINE csview cstr_substr_ex(const cstr* self, intptr_t pos, intptr_t n) { return csview_substr_ex(cstr_sv(self), pos, n); } STC_INLINE csview cstr_slice_ex(const cstr* self, intptr_t p1, intptr_t p2) { return csview_slice_ex(cstr_sv(self), p1, p2); } -STC_INLINE csview cstr_u8_substr(const cstr* self , size_t bytepos, size_t u8len) +STC_INLINE csview cstr_u8_substr(const cstr* self , intptr_t bytepos, intptr_t u8len) { return csview_u8_substr(cstr_sv(self), bytepos, u8len); } #endif /* ---- Container helper functions ---- */ STC_INLINE int csview_cmp(const csview* x, const csview* y) { - size_t n = x->size < y->size ? x->size : y->size; - int c = memcmp(x->str, y->str, n); + intptr_t n = x->size < y->size ? x->size : y->size; + int c = c_memcmp(x->str, y->str, n); return c ? c : (int)(x->size - y->size); } @@ -154,48 +154,48 @@ STC_INLINE int csview_icmp(const csview* x, const csview* y) { return utf8_icmp_sv(*x, *y); } STC_INLINE bool csview_eq(const csview* x, const csview* y) - { return x->size == y->size && !memcmp(x->str, y->str, x->size); } + { return x->size == y->size && !c_memcmp(x->str, y->str, x->size); } STC_API uint64_t csview_hash(const csview *self); /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) -STC_DEF size_t csview_find_sv(csview sv, csview search) { +STC_DEF intptr_t csview_find_sv(csview sv, csview search) { char* res = cstrnstrn(sv.str, search.str, sv.size, search.size); - return res ? (size_t)(res - sv.str) : c_NPOS; + return res ? (res - sv.str) : c_NPOS; } STC_DEF uint64_t csview_hash(const csview *self) { return cfasthash(self->str, self->size); } -STC_DEF csview csview_substr_ex(csview sv, intptr_t pos, size_t n) { +STC_DEF csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n) { if (pos < 0) { - pos += (intptr_t)sv.size; + pos += sv.size; if (pos < 0) pos = 0; } - if ((size_t)pos > sv.size) pos = (intptr_t)sv.size; - if ((size_t)pos + n > sv.size) n = sv.size - (size_t)pos; + if (pos > sv.size) pos = sv.size; + if (pos + n > sv.size) n = sv.size - pos; sv.str += pos, sv.size = n; return sv; } STC_DEF csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2) { if (p1 < 0) { - p1 += (intptr_t)sv.size; + p1 += sv.size; if (p1 < 0) p1 = 0; } - if (p2 < 0) p2 += (intptr_t)sv.size; - if (p2 > (intptr_t)sv.size) p2 = (intptr_t)sv.size; - sv.str += p1, sv.size = (size_t)(p2 > p1 ? p2 - p1 : 0); + if (p2 < 0) p2 += sv.size; + if (p2 > sv.size) p2 = sv.size; + sv.str += p1, sv.size = (p2 > p1 ? p2 - p1 : 0); return sv; } -STC_DEF csview csview_token(csview sv, const char* sep, size_t* start) { - size_t sep_size = strlen(sep); +STC_DEF csview csview_token(csview sv, const char* sep, intptr_t* start) { + intptr_t sep_size = c_strlen(sep); csview slice = {sv.str + *start, sv.size - *start}; const char* res = cstrnstrn(slice.str, sep, slice.size, sep_size); - csview tok = {slice.str, res ? (size_t)(res - slice.str) : slice.size}; + csview tok = {slice.str, res ? (res - slice.str) : slice.size}; *start += tok.size + sep_size; return tok; } diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 83d94652..11d145e6 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -80,13 +80,13 @@ typedef i_keyraw _cx_raw; STC_API _cx_self _cx_memb(_init)(void); STC_API void _cx_memb(_drop)(_cx_self* self); STC_API void _cx_memb(_clear)(_cx_self* self); -STC_API bool _cx_memb(_reserve)(_cx_self* self, size_t cap); -STC_API bool _cx_memb(_resize)(_cx_self* self, size_t size, i_key null); +STC_API bool _cx_memb(_reserve)(_cx_self* self, intptr_t cap); +STC_API bool _cx_memb(_resize)(_cx_self* self, intptr_t size, i_key null); STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_key value); STC_API _cx_iter _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2); STC_API _cx_iter _cx_memb(_insert_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2); -STC_API _cx_iter _cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n); +STC_API _cx_iter _cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const intptr_t n); #if !defined i_no_cmp STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y); STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw raw); @@ -101,7 +101,7 @@ STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw) { return _cx_memb(_push)(self, i_keyfrom(raw)); } STC_INLINE _cx_iter -_cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], const size_t n) { +_cx_memb(_emplace_n)(_cx_self* self, const intptr_t idx, const _cx_raw arr[], const intptr_t n) { return _cx_memb(_emplace_range)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter @@ -114,9 +114,9 @@ _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) { STC_API _cx_self _cx_memb(_clone)(_cx_self cx); STC_API _cx_iter _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2); -STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val) { return i_keyclone(val); } @@ -128,8 +128,8 @@ STC_INLINE void _cx_memb(_copy)(_cx_self* self, const _cx_self* other) { } #endif // !i_no_clone -STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) { return self->_len; } -STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { return self->_cap; } +STC_INLINE intptr_t _cx_memb(_size)(const _cx_self* self) { return self->_len; } +STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* self) { return self->_cap; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return !self->_len; } STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _cx_value* val) { return i_keyto(val); } STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->data; } @@ -142,14 +142,14 @@ STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value) STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); } STC_INLINE _cx_self -_cx_memb(_with_size)(const size_t size, i_key null) { +_cx_memb(_with_size)(const intptr_t size, i_key null) { _cx_self cx = _cx_memb(_init)(); _cx_memb(_resize)(&cx, size, null); return cx; } STC_INLINE _cx_self -_cx_memb(_with_capacity)(const size_t cap) { +_cx_memb(_with_capacity)(const intptr_t cap) { _cx_self cx = _cx_memb(_init)(); _cx_memb(_reserve)(&cx, cap); return cx; @@ -162,11 +162,11 @@ _cx_memb(_shrink_to_fit)(_cx_self* self) { STC_INLINE _cx_iter -_cx_memb(_insert)(_cx_self* self, const size_t idx, i_key value) { +_cx_memb(_insert)(_cx_self* self, const intptr_t idx, i_key value) { return _cx_memb(_insert_range)(self, self->data + idx, &value, &value + 1); } STC_INLINE _cx_iter -_cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], const size_t n) { +_cx_memb(_insert_n)(_cx_self* self, const intptr_t idx, const _cx_value arr[], const intptr_t n) { return _cx_memb(_insert_range)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter @@ -175,7 +175,7 @@ _cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) { } STC_INLINE _cx_iter -_cx_memb(_erase_n)(_cx_self* self, const size_t idx, const size_t n) { +_cx_memb(_erase_n)(_cx_self* self, const intptr_t idx, const intptr_t n) { return _cx_memb(_erase_range_p)(self, self->data + idx, self->data + idx + n); } STC_INLINE _cx_iter @@ -188,17 +188,17 @@ _cx_memb(_erase_range)(_cx_self* self, _cx_iter i1, _cx_iter i2) { } STC_INLINE const _cx_value* -_cx_memb(_at)(const _cx_self* self, const size_t idx) { +_cx_memb(_at)(const _cx_self* self, const intptr_t idx) { assert(idx < self->_len); return self->data + idx; } STC_INLINE _cx_value* -_cx_memb(_at_mut)(_cx_self* self, const size_t idx) { +_cx_memb(_at_mut)(_cx_self* self, const intptr_t idx) { assert(idx < self->_len); return self->data + idx; } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { - size_t n = self->_len; + intptr_t n = self->_len; return c_LITERAL(_cx_iter){n ? self->data : NULL, self->data + n}; } @@ -211,8 +211,8 @@ STC_INLINE void _cx_memb(_next)(_cx_iter* it) STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t n) { if ((it.ref += n) >= it.end) it.ref = NULL; return it; } -STC_INLINE size_t _cx_memb(_index)(const _cx_self* self, _cx_iter it) - { return (size_t)(it.ref - self->data); } +STC_INLINE intptr_t _cx_memb(_index)(const _cx_self* self, _cx_iter it) + { return (it.ref - self->data); } #if !defined i_no_cmp @@ -281,9 +281,9 @@ _cx_memb(_drop)(_cx_self* self) { } STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const size_t cap) { +_cx_memb(_reserve)(_cx_self* self, const intptr_t cap) { if (cap > self->_cap || (cap && cap == self->_len)) { - _cx_value* d = (_cx_value*)c_realloc(self->data, cap*sizeof(i_key)); + _cx_value* d = (_cx_value*)c_realloc(self->data, cap*c_sizeof(i_key)); if (!d) return false; self->data = d; @@ -293,13 +293,13 @@ _cx_memb(_reserve)(_cx_self* self, const size_t cap) { } STC_DEF bool -_cx_memb(_resize)(_cx_self* self, const size_t len, i_key null) { +_cx_memb(_resize)(_cx_self* self, const intptr_t len, i_key null) { if (!_cx_memb(_reserve)(self, len)) return false; - const size_t n = self->_len; - for (size_t i = len; i < n; ++i) + const intptr_t n = self->_len; + for (intptr_t i = len; i < n; ++i) { i_keydrop((self->data + i)); } - for (size_t i = n; i < len; ++i) + for (intptr_t i = n; i < len; ++i) self->data[i] = null; self->_len = len; return true; @@ -316,16 +316,16 @@ _cx_memb(_push)(_cx_self* self, i_key value) { } STC_DEF _cx_iter -_cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n) { +_cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const intptr_t n) { if (n) { if (!pos) pos = self->data + self->_len; - const size_t idx = (size_t)(pos - self->data); + const intptr_t idx = (pos - self->data); if (self->_len + n > self->_cap) { if (!_cx_memb(_reserve)(self, self->_len*3/2 + n)) return _cx_memb(_end)(self); pos = self->data + idx; } - memmove(pos + n, pos, (self->_len - idx)*sizeof *pos); + c_memmove(pos + n, pos, (self->_len - idx)*c_sizeof *pos); self->_len += n; } return c_LITERAL(_cx_iter){pos, self->data + self->_len}; @@ -334,19 +334,19 @@ _cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n) { STC_DEF _cx_iter _cx_memb(_insert_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) - memcpy(it.ref, p1, (size_t)(p2 - p1)*sizeof *p1); + c_memcpy(it.ref, p1, (p2 - p1)*c_sizeof *p1); return it; } STC_DEF _cx_iter _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { - size_t len = (size_t)(p2 - p1); + intptr_t len = (p2 - p1); _cx_value* p = p1, *end = self->data + self->_len; for (; p != p2; ++p) { i_keydrop(p); } - memmove(p1, p2, (size_t)(end - p2)*sizeof *p1); + c_memmove(p1, p2, (end - p2)*c_sizeof *p1); self->_len -= len; return c_LITERAL(_cx_iter){p2 == end ? NULL : p1, end - len}; } @@ -362,7 +362,7 @@ _cx_memb(_clone)(_cx_self cx) { STC_DEF _cx_iter _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) for (_cx_value* p = it.ref; p1 != p2; ++p1) *p++ = i_keyclone((*p1)); @@ -374,7 +374,7 @@ _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos, STC_DEF _cx_iter _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) for (_cx_value* p = it.ref; p1 != p2; ++p1) *p++ = i_keyfrom((*p1)); diff --git a/include/stc/forward.h b/include/stc/forward.h index 60c18c37..dfb43a9f 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -23,7 +23,7 @@ #ifndef STC_FORWARD_H_INCLUDED #define STC_FORWARD_H_INCLUDED -#include <stddef.h> +#include <stdint.h> #define forward_carc(CX, VAL) _c_carc_types(CX, VAL) #define forward_carr2(CX, VAL) _c_carr2_types(CX, VAL) @@ -42,15 +42,17 @@ #define forward_cqueue(CX, VAL) _c_cdeq_types(CX, VAL) #define forward_cvec(CX, VAL) _c_cvec_types(CX, VAL) +// csview typedef const char csview_value; -typedef struct { csview_value* str; size_t size; } csview; +typedef struct { csview_value* str; intptr_t size; } csview; typedef union { csview_value* ref; struct { csview chr; csview_value* end; } u8; } csview_iter; +// cstr typedef char cstr_value; -typedef struct { cstr_value* data; size_t size, cap; } cstr_buf; +typedef struct { cstr_value* data; intptr_t size, cap; } cstr_buf; typedef union { struct { cstr_value data[sizeof(cstr_buf) - 1]; unsigned char size; } sml; struct { cstr_value* data; size_t size, ncap; } lon; @@ -71,16 +73,6 @@ typedef union { catomic_long* use_count; \ } SELF -#define _c_carr2_types(SELF, VAL) \ - typedef VAL SELF##_value; \ - typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value **data; size_t xdim, ydim; } SELF - -#define _c_carr3_types(SELF, VAL) \ - typedef VAL SELF##_value; \ - typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value ***data; size_t xdim, ydim, zdim; } SELF - #define _c_cbox_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct { \ @@ -90,7 +82,7 @@ typedef union { #define _c_cdeq_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value *_base, *data; size_t _len, _cap; } SELF + typedef struct { SELF##_value *_base, *data; intptr_t _len, _cap; } SELF #define _c_clist_types(SELF, VAL) \ typedef VAL SELF##_value; \ @@ -188,20 +180,20 @@ typedef union { #define _c_cstack_fixed(SELF, VAL, CAP) \ typedef VAL SELF##_value; \ typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value data[CAP]; size_t _len; } SELF + typedef struct { SELF##_value data[CAP]; intptr_t _len; } SELF #define _c_cstack_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value* data; size_t _len, _cap; } SELF + typedef struct { SELF##_value* data; intptr_t _len, _cap; } SELF #define _c_cvec_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value *data; size_t _len, _cap; } SELF + typedef struct { SELF##_value *data; intptr_t _len, _cap; } SELF #define _c_cpque_types(SELF, VAL) \ typedef VAL SELF##_value; \ - typedef struct { SELF##_value* data; size_t _len, _cap; } SELF + typedef struct { SELF##_value* data; intptr_t _len, _cap; } SELF #endif // STC_FORWARD_H_INCLUDED diff --git a/include/stc/utf8.h b/include/stc/utf8.h index ea661bb6..ce50af87 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -23,7 +23,7 @@ extern uint32_t utf8_tolower(uint32_t c); extern uint32_t utf8_toupper(uint32_t c); extern bool utf8_iscased(uint32_t c); extern bool utf8_isword(uint32_t c); -extern bool utf8_valid_n(const char* s, size_t nbytes); +extern bool utf8_valid_n(const char* s, intptr_t nbytes); extern int utf8_icmp_sv(csview s1, csview s2); extern unsigned utf8_encode(char *out, uint32_t c); extern uint32_t utf8_peek_off(const char *s, int offset); @@ -35,7 +35,7 @@ STC_INLINE bool utf8_islower(uint32_t c) { return utf8_toupper(c) != c; } STC_INLINE bool utf8_isalnum(uint32_t c) { - if (c < 128) return isalnum(c) != 0; + if (c < 128) return isalnum((int)c) != 0; return utf8_isalpha(c) || utf8_isgroup(U8G_Nd, c); } @@ -68,17 +68,17 @@ STC_INLINE uint32_t utf8_peek(const char* s) { /* case-insensitive utf8 string comparison */ STC_INLINE int utf8_icmp(const char* s1, const char* s2) { - return utf8_icmp_sv(c_SV(s1, ~(size_t)0), c_SV(s2, ~(size_t)0)); + return utf8_icmp_sv(c_SV(s1, INTPTR_MAX), c_SV(s2, INTPTR_MAX)); } STC_INLINE bool utf8_valid(const char* s) { - return utf8_valid_n(s, ~(size_t)0); + return utf8_valid_n(s, INTPTR_MAX); } /* following functions are independent but assume valid utf8 strings: */ /* number of bytes in the utf8 codepoint from s */ -STC_INLINE unsigned utf8_chr_size(const char *s) { +STC_INLINE int utf8_chr_size(const char *s) { unsigned b = (uint8_t)*s; if (b < 0x80) return 1; /*if (b < 0xC2) return 0;*/ @@ -89,29 +89,29 @@ STC_INLINE unsigned utf8_chr_size(const char *s) { } /* number of codepoints in the utf8 string s */ -STC_INLINE size_t utf8_size(const char *s) { - size_t size = 0; +STC_INLINE intptr_t utf8_size(const char *s) { + intptr_t size = 0; while (*s) size += (*++s & 0xC0) != 0x80; return size; } -STC_INLINE size_t utf8_size_n(const char *s, size_t nbytes) { - size_t size = 0; +STC_INLINE intptr_t utf8_size_n(const char *s, intptr_t nbytes) { + intptr_t size = 0; while ((nbytes-- != 0) & (*s != 0)) { size += (*++s & 0xC0) != 0x80; } return size; } -STC_INLINE const char* utf8_at(const char *s, size_t index) { +STC_INLINE const char* utf8_at(const char *s, intptr_t index) { while ((index > 0) & (*s != 0)) index -= (*++s & 0xC0) != 0x80; return s; } -STC_INLINE size_t utf8_pos(const char* s, size_t index) - { return (size_t)(utf8_at(s, index) - s); } +STC_INLINE intptr_t utf8_pos(const char* s, intptr_t index) + { return (intptr_t)(utf8_at(s, index) - s); } #endif // UTF8_H_INCLUDED #if defined i_extern || defined STC_EXTERN |
