From 14c0c1b9ab2ce6a4f98baf389d712515b7c68933 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 11 Jan 2022 00:22:01 +0100 Subject: Some updates on Regex. Will remove it for now, as it is functionally unstable. --- benchmarks/misc/string_bench.c | 6 ++--- examples/regex2.c | 6 ++--- examples/regex_match.c | 5 ++++- include/stc/alt/cstr.h | 10 +++++---- include/stc/utf8.h | 50 ++++++++++++++++++++---------------------- 5 files changed, 40 insertions(+), 37 deletions(-) diff --git a/benchmarks/misc/string_bench.c b/benchmarks/misc/string_bench.c index 28777e8d..e93328d4 100644 --- a/benchmarks/misc/string_bench.c +++ b/benchmarks/misc/string_bench.c @@ -21,7 +21,7 @@ cvec_str read_file(const char* name) c_auto (cstr, line) c_autovar (FILE* f = fopen(name, "r"), fclose(f)) while (cstr_getline(&line, f)) - cvec_str_emplace_back(&data, line.str); + cvec_str_emplace_back(&data, cstr_str(&line)); return data; } @@ -124,7 +124,7 @@ void benchmark(cvec_str vec_string, struct Maps maps) csmap_str_iter it, end = csmap_str_end(maps.snormal); for (size_t j = 0; j < cvec_str_size(vec_string); ++j) { - csmap_str_find_it(maps.snormal, vec_string.data[j].str, &it); + csmap_str_find_it(maps.snormal, cstr_str(&vec_string.data[j]), &it); if (it.ref != end.ref) total += it.ref->second; } @@ -141,7 +141,7 @@ void benchmark(cvec_str vec_string, struct Maps maps) cmap_str_iter it, end = cmap_str_end(maps.unormal); for (size_t j = 0; j < cvec_str_size(vec_string); ++j) { - it = cmap_str_find(maps.unormal, vec_string.data[j].str); + it = cmap_str_find(maps.unormal, cstr_str(&vec_string.data[j])); if (it.ref != end.ref) total += it.ref->second; } diff --git a/examples/regex2.c b/examples/regex2.c index 73abee45..0562af00 100644 --- a/examples/regex2.c +++ b/examples/regex2.c @@ -4,10 +4,10 @@ int main() { - const char* fnames[] = {"foofile.txt", "barfile.txt", "bazboy.dat", "zoidberg"}; + const char* fnames[] = {"home/foofile.txt", "cool/barfile.txt", "test/bazboy.dat", "hello/zoidberg"}; c_auto (cregex, re) { - re = cregex_new("([a-z]+)\\.([a-z]+)"); + re = cregex_new("([a-z]+)\\/([a-z]+)\\.([a-z]+)"); c_forrange (i, c_arraylen(fnames)) { @@ -17,7 +17,7 @@ int main() c_forrange (j, cregex_capture_size(re)) { csview cap; cregex_capture_v(&re, j, &cap); - printf(" submatch %d: " c_PRIsv "\n", j, c_ARGsv(cap)); + printf(" submatch %zu: " c_PRIsv "\n", j, c_ARGsv(cap)); } puts(""); } diff --git a/examples/regex_match.c b/examples/regex_match.c index dca8a127..72adeb8e 100644 --- a/examples/regex_match.c +++ b/examples/regex_match.c @@ -2,6 +2,9 @@ #include #include #include +#define i_val double +#define i_type Vecu64 +#include #include @@ -23,7 +26,7 @@ int main() printf("Could not find any digits\n"); } - csview sv = {s, 0}; + csview sv = {0}; while (cregex_find_next_v(&re, s, &sv)) { printf(c_PRIsv " ; ", c_ARGsv(sv)); } diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h index 2271be7c..adad3e3d 100644 --- a/include/stc/alt/cstr.h +++ b/include/stc/alt/cstr.h @@ -24,13 +24,15 @@ /* A string type with short string optimization in C99 with optimal short string * utilization (23 characters with 24 bytes string representation). */ -#ifndef CSTR_INCLUDED -#define CSTR_INCLUDED +#ifndef CSTR_H_INCLUDED +#define CSTR_H_INCLUDED #include -#include +#include /* malloc */ #include -#include +#include +#include /* vsnprintf */ +#include typedef struct { char* data; size_t size, cap; } _cstr_rep_t; diff --git a/include/stc/utf8.h b/include/stc/utf8.h index 540ade39..d11bcc43 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -9,18 +9,16 @@ enum utf8_state { utf8_REJECT = 12 }; +typedef struct { bool valid; size_t size; } utf8_result; +/* number of codepoints in the utf8 string s, or SIZE_MAX if invalid utf8: */ +STC_API size_t utf8_codepoint_count(const char *s); +STC_API size_t utf8_codepoint_count_n(const char *s, size_t n); +/* decode next utf8 codepoint. */ STC_API uint32_t utf8_decode(uint32_t *state, uint32_t *codep, const uint32_t byte); -STC_API bool utf8_valid_codepoints(const uint8_t *s, size_t *count); - -STC_INLINE bool utf8_is_valid(const char *s) -{ - size_t count; - return utf8_valid_codepoints((const uint8_t *)s, &count); -} STC_INLINE uint32_t utf8_peek(const char *s) { - uint32_t state = utf8_ACCEPT, codepoint; + uint32_t state = 0, codepoint; utf8_decode(&state, &codepoint, (uint8_t)s[0]); return codepoint; } @@ -44,16 +42,6 @@ STC_INLINE const char *utf8_next(const char *s) return (const char *)p; } -// assumes input is valid utf8! Use utf8_valid_codepoints() if unsure. -STC_INLINE size_t utf8_size(const char *s) -{ - size_t count = 0; - while (*s) - s += utf8_codepoint_width((uint8_t)*s), ++count; - return count; -} - - // --------------------------- IMPLEMENTATION --------------------------------- #ifdef _i_implement @@ -74,10 +62,10 @@ static const uint8_t utf8_table[] = { }; STC_DEF uint32_t utf8_decode(uint32_t *state, uint32_t *codep, - const uint32_t byte) + const uint32_t byte) { const uint32_t type = utf8_table[byte]; - const uint32_t x = (uint32_t) -(*state != utf8_ACCEPT); + const uint32_t x = (uint32_t) -(*state != 0); *codep = (x & ((byte & 0x3fu) | (*codep << 6))) | (~x & ((0xff >> type) & byte)); @@ -85,13 +73,23 @@ STC_DEF uint32_t utf8_decode(uint32_t *state, uint32_t *codep, return *state = utf8_table[256 + *state + type]; } -STC_DEF bool utf8_valid_codepoints(const uint8_t *s, size_t *count) + +STC_DEF size_t utf8_codepoint_count(const char *s) { - uint32_t state = utf8_ACCEPT, codepoint; - - for (*count = 0; *s; ++s) - *count += utf8_decode(&state, &codepoint, *s) == utf8_ACCEPT; - return state == utf8_ACCEPT; + uint32_t state = 0, codepoint; + size_t size = 0; + while (*s) + size += !utf8_decode(&state, &codepoint, (uint8_t)*s++); + return size | (size_t) -(state != 0); +} + +STC_DEF size_t utf8_codepoint_count_n(const char *s, size_t n) +{ + uint32_t state = 0, codepoint; + size_t size = 0; + while (n--) + size += !utf8_decode(&state, &codepoint, (uint8_t)*s++); + return size | (size_t) -(state != 0); } #endif -- cgit v1.2.3