diff options
| author | Tyge Løvset <[email protected]> | 2022-08-07 08:03:46 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-08-07 08:20:34 +0200 |
| commit | c87898773d1af364a9847610401a9959f6019fe7 (patch) | |
| tree | 5e64c9d96fb9e12192ce298f1d2909d43b72571a | |
| parent | 618b5704e6f85cfe1b6e5c9c9373abe76a8bb628 (diff) | |
| download | STC-modified-c87898773d1af364a9847610401a9959f6019fe7.tar.gz STC-modified-c87898773d1af364a9847610401a9959f6019fe7.zip | |
Internal: moved some functions in csview/cstr to implementation sections.
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | docs/cmap_api.md | 12 | ||||
| -rw-r--r-- | docs/cstr_api.md | 2 | ||||
| -rw-r--r-- | docs/csview_api.md | 12 | ||||
| -rw-r--r-- | include/stc/cstr.h | 20 | ||||
| -rw-r--r-- | include/stc/csview.h | 87 | ||||
| -rw-r--r-- | include/stc/utf8.h | 61 | ||||
| -rw-r--r-- | src/libstc.c | 1 |
8 files changed, 95 insertions, 104 deletions
@@ -204,8 +204,8 @@ int main(void) { c_auto (csmap_int, map) { int nums[4] = {10, 20, 30, 40}; - struct Point pts[4] = { {10, 1}, {20, 2}, {30, 3}, {40, 4} }; - int pairs[4][2] = { {20, 2}, {10, 1}, {30, 3}, {40, 4} }; + struct Point pts[4] = {{10, 1}, {20, 2}, {30, 3}, {40, 4}}; + int pairs[4][2] = {{20, 2}, {10, 1}, {30, 3}, {40, 4}}; /* add some elements to each container */ for (int i = 0; i < 4; ++i) { diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 8fbfb30b..717e7677 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -398,13 +398,11 @@ static inline RViking Viking_toraw(const Viking* vp) { int main() { c_auto (Vikings, vikings) { - Vikings_raw arr[] = { - { {"Einar", "Norway"}, 20 }, - { {"Olaf", "Denmark"}, 24 }, - { {"Harald", "Iceland"}, 12 }, - }; - c_forrange (i, c_arraylen(arr)) - Vikings_emplace(&vikings, arr[i].first, arr[i].second); + c_forarray (Vikings_raw, v, { + {{"Einar", "Norway"}, 20}, + {{"Olaf", "Denmark"}, 24}, + {{"Harald", "Iceland"}, 12} + }) Vikings_emplace(&vikings, v->first, v->second); Vikings_emplace_or_assign(&vikings, (RViking){"Bjorn", "Sweden"}, 10); diff --git a/docs/cstr_api.md b/docs/cstr_api.md index a05dc1ae..e4edb4c5 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -49,7 +49,7 @@ void cstr_clear(cstr* self); char* cstr_assign(cstr* self, const char* str); char* cstr_assign_n(cstr* self, const char* str, size_t n); // assign n first bytes of str -char* cstr_assign_sv(cstr* self, csview sv) +char* cstr_assign_sv(cstr* self, csview sv); char* cstr_copy(cstr* self, cstr s); // copy-assign a cstr int cstr_printf(cstr* self, const char* fmt, ...); // source and target must not overlap. diff --git a/docs/csview_api.md b/docs/csview_api.md index d0ca44e1..09f377d1 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -51,21 +51,21 @@ csview csview_token(csview sv, csview sep, size_t* start); // *start size_t csview_u8_size(csview sv); csview csview_u8_substr(csview sv, size_t u8pos, size_t u8len); csview csview_u8_slice(csview sv, size_t u8p1, size_t u8p2); +bool csview_valid_utf8(csview sv); // requires linking with src/utf8code.c csview_iter csview_begin(const csview* self); csview_iter csview_end(const csview* self); void csview_next(csview_iter* it); // utf8 codepoint step, not byte! -// requires linking with src/utf8code.c: -bool csview_valid_utf8(csview sv); -// from utf8.h, linking src/utf8code.c: -bool utf8_valid(const char* s); -bool utf8_valid_n(const char* s, size_t nbytes); +// from utf8.h size_t utf8_size(const char *s); size_t utf8_size_n(const char *s, size_t nbytes); // number of UTF8 codepoints within n bytes const char* utf8_at(const char *s, size_t index); // from UTF8 index to char* position size_t utf8_pos(const char* s, size_t index); // from UTF8 index to byte index position -unsigned utf8_chr_size(const char* s); // 0-4 (0 if s[0] means illegal utf8) +unsigned utf8_chr_size(const char* s); // UTF8 character size: 1-4 +// implemented in src/utf8code.c: +bool utf8_valid(const char* s); +bool utf8_valid_n(const char* s, size_t nbytes); uint32_t utf8_decode(utf8_decode_t *d, uint8_t byte); // decode next byte to utf8, return state. unsigned utf8_encode(char *out, uint32_t codepoint); // encode unicode cp into out buffer uint32_t utf8_peek(const char* s, int pos); // codepoint value at utf8 pos (may be negative) diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 92f49d92..68f35674 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -261,10 +261,7 @@ STC_INLINE size_t cstr_find(cstr s, const char* search) { return res ? res - str : cstr_npos; } -STC_INLINE size_t cstr_find_sv(cstr s, csview search) { - char* res = c_strnstrn(cstr_str(&s), search.str, cstr_size(s), search.size); - return res ? res - cstr_str(&s) : cstr_npos; -} +STC_API size_t cstr_find_sv(cstr s, csview search); STC_INLINE size_t cstr_find_s(cstr s, cstr search) { return cstr_find(s, cstr_str(&search)); } @@ -274,7 +271,7 @@ STC_INLINE bool cstr_contains(cstr s, const char* search) { return strstr(cstr_data(&s), search) != NULL; } STC_INLINE bool cstr_contains_sv(cstr s, csview search) - { return c_strnstrn(cstr_str(&s), search.str, cstr_size(s), search.size) != NULL; } + { return cstr_find_sv(s, search) != cstr_npos; } STC_INLINE bool cstr_contains_s(cstr s, cstr search) { return strstr(cstr_data(&s), cstr_str(&search)) != NULL; } @@ -377,13 +374,20 @@ STC_INLINE void cstr_insert_s(cstr* self, size_t pos, cstr s) { STC_INLINE bool cstr_getline(cstr *self, FILE *fp) { return cstr_getdelim(self, '\n', fp); } -STC_INLINE uint64_t cstr_hash(const cstr *self) { +STC_API uint64_t cstr_hash(const cstr *self); + +/* -------------------------- IMPLEMENTATION ------------------------- */ +#if defined(i_implement) || defined(i_extern) + +STC_DEF uint64_t cstr_hash(const cstr *self) { csview sv = cstr_sv(self); return c_fasthash(sv.str, sv.size); } -/* -------------------------- IMPLEMENTATION ------------------------- */ -#if defined(i_implement) || defined(i_extern) +STC_DEF size_t cstr_find_sv(cstr s, csview search) { + char* res = c_strnstrn(cstr_str(&s), search.str, cstr_size(s), search.size); + return res ? res - cstr_str(&s) : cstr_npos; +} STC_DEF char* _cstr_internal_move(cstr* self, const size_t pos1, const size_t pos2) { cstr_buf r = cstr_buffer(self); diff --git a/include/stc/csview.h b/include/stc/csview.h index b4b701f2..2ebcaabe 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -42,13 +42,10 @@ STC_INLINE bool csview_empty(csview sv) { return sv.size == 0; } STC_INLINE bool csview_equals(csview sv, csview sv2) { return sv.size == sv2.size && !memcmp(sv.str, sv2.str, sv.size); } -STC_INLINE size_t csview_find(csview sv, csview search) { - char* res = c_strnstrn(sv.str, search.str, sv.size, search.size); - return res ? res - sv.str : csview_npos; -} +STC_API size_t csview_find(csview sv, csview search); STC_INLINE bool csview_contains(csview sv, csview search) - { return c_strnstrn(sv.str, search.str, sv.size, search.size) != NULL; } + { return csview_find(sv, search) != csview_npos; } STC_INLINE bool csview_starts_with(csview sv, csview sub) { if (sub.size > sv.size) return false; @@ -98,40 +95,9 @@ STC_INLINE csview csview_u8_slice(csview sv, size_t u8p1, size_t u8p2) STC_INLINE bool csview_valid_utf8(csview sv) // depends on src/utf8code.c { return utf8_valid_n(sv.str, sv.size); } -/* "Rarely" used extended substr_ex(), slice_ex(), and token() function */ - -STC_INLINE csview -csview_substr_ex(csview sv, intptr_t pos, size_t n) { - if (pos < 0) { - pos += sv.size; - if (pos < 0) pos = 0; - } - if (pos > (intptr_t)sv.size) pos = sv.size; - if (pos + n > sv.size) n = sv.size - pos; - sv.str += pos, sv.size = n; - return sv; -} - -STC_INLINE csview -csview_slice_ex(csview sv, intptr_t p1, intptr_t p2) { - if (p1 < 0) { - p1 += sv.size; - if (p1 < 0) p1 = 0; - } - if (p2 < 0) p2 += sv.size; - if (p2 > (intptr_t)sv.size) p2 = sv.size; - sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0; - return sv; -} - -STC_INLINE csview -csview_token(csview sv, csview sep, size_t* start) { - csview slice = {sv.str + *start, sv.size - *start}; - const char* res = c_strnstrn(slice.str, sep.str, slice.size, sep.size); - csview tok = {slice.str, res ? res - slice.str : slice.size}; - *start += tok.size + sep.size; - return tok; -} +STC_API csview csview_substr_ex(csview sv, intptr_t pos, size_t n); +STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2); +STC_API csview csview_token(csview sv, csview sep, size_t* start); /* csview interaction with cstr: */ #ifdef CSTR_H_INCLUDED @@ -166,9 +132,50 @@ STC_INLINE int csview_icmp(const csview* x, const csview* y) STC_INLINE bool csview_eq(const csview* x, const csview* y) { return x->size == y->size && !memcmp(x->str, y->str, x->size); } -STC_INLINE uint64_t csview_hash(const csview *self) +STC_API uint64_t csview_hash(const csview *self); + +/* -------------------------- IMPLEMENTATION ------------------------- */ +#if defined(i_implement) || defined(i_extern) + +STC_DEF size_t csview_find(csview sv, csview search) { + char* res = c_strnstrn(sv.str, search.str, sv.size, search.size); + return res ? res - sv.str : csview_npos; +} + +STC_DEF uint64_t csview_hash(const csview *self) { return c_fasthash(self->str, self->size); } +STC_DEF csview csview_substr_ex(csview sv, intptr_t pos, size_t n) { + if (pos < 0) { + pos += sv.size; + if (pos < 0) pos = 0; + } + if (pos > (intptr_t)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 += sv.size; + if (p1 < 0) p1 = 0; + } + if (p2 < 0) p2 += sv.size; + if (p2 > (intptr_t)sv.size) p2 = sv.size; + sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0; + return sv; +} + +STC_DEF csview csview_token(csview sv, csview sep, size_t* start) { + csview slice = {sv.str + *start, sv.size - *start}; + const char* res = c_strnstrn(slice.str, sep.str, slice.size, sep.size); + csview tok = {slice.str, res ? res - slice.str : slice.size}; + *start += tok.size + sep.size; + return tok; +} + +#endif #endif #undef i_opt #undef i_header diff --git a/include/stc/utf8.h b/include/stc/utf8.h index c6fb6944..34368737 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -1,46 +1,27 @@ #ifndef UTF8_H_INCLUDED #define UTF8_H_INCLUDED -/* -// Example: -#include <stc/cstr.h> -#include <stc/csview.h> -int main() -{ - c_auto (cstr, s1) { - s1 = cstr_new("hell😀 w😀rld"); - printf("%s\n", cstr_str(&s1)); - cstr_replace_sv(&s1, utf8_substr(cstr_str(&s1), 7, 1), c_sv("🐨")); - printf("%s\n", cstr_str(&s1)); - - c_foreach (i, cstr, s1) - printf("%.*s,", c_ARGsv(i.chr)); - } -} -// Output: -// hell😀 w😀rld -// hell😀 w🐨rld -// h,e,l,l,😀, ,w,🐨,r,l,d, -*/ #include "ccommon.h" #include <ctype.h> // utf8 methods defined in src/utf8code.c: -bool utf8_islower(uint32_t c); -bool utf8_isupper(uint32_t c); -bool utf8_isspace(uint32_t c); -bool utf8_isdigit(uint32_t c); -bool utf8_isxdigit(uint32_t c); -bool utf8_isalpha(uint32_t c); -bool utf8_isalnum(uint32_t c); -uint32_t utf8_casefold(uint32_t c); -uint32_t utf8_tolower(uint32_t c); -uint32_t utf8_toupper(uint32_t c); -bool utf8_valid_n(const char* s, size_t nbytes); -int utf8_icmp_n(size_t u8max, const char* s1, size_t n1, - const char* s2, size_t n2); -unsigned utf8_encode(char *out, uint32_t c); -uint32_t utf8_peek(const char *s, int u8pos); +extern bool utf8_islower(uint32_t c); +extern bool utf8_isupper(uint32_t c); +extern bool utf8_isspace(uint32_t c); +extern bool utf8_isdigit(uint32_t c); +extern bool utf8_isxdigit(uint32_t c); +extern bool utf8_isalpha(uint32_t c); +extern bool utf8_isalnum(uint32_t c); +extern uint32_t utf8_casefold(uint32_t c); +extern uint32_t utf8_tolower(uint32_t c); +extern uint32_t utf8_toupper(uint32_t c); +extern bool utf8_valid_n(const char* s, size_t nbytes); +extern int utf8_icmp_n(size_t u8max, const char* s1, size_t n1, + const char* s2, size_t n2); +extern unsigned utf8_encode(char *out, uint32_t c); +extern uint32_t utf8_peek(const char *s, int u8pos); + +/* following functions uses src/utf8code.c */ /* decode next utf8 codepoint. https://bjoern.hoehrmann.de/utf-8/decoder/dfa */ typedef struct { uint32_t state, codep; } utf8_decode_t; @@ -62,15 +43,17 @@ STC_INLINE bool utf8_valid(const char* s) { return utf8_valid_n(s, ~(size_t)0); } +/* 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) { unsigned b = (uint8_t)*s; if (b < 0x80) return 1; - if (b < 0xC2) return 0; + /*if (b < 0xC2) return 0;*/ if (b < 0xE0) return 2; if (b < 0xF0) return 3; - if (b < 0xF5) return 4; - return 0; + /*if (b < 0xF5)*/ return 4; + /*return 0;*/ } /* number of codepoints in the utf8 string s */ diff --git a/src/libstc.c b/src/libstc.c index 0c78272d..30c610c6 100644 --- a/src/libstc.c +++ b/src/libstc.c @@ -8,5 +8,4 @@ #include "../include/stc/cstr.h" #include "../include/stc/csview.h" -#include "../include/stc/cbits.h" #include "../include/stc/crandom.h" |
