diff options
| -rw-r--r-- | docs/coroutine_api.md | 12 | ||||
| -rw-r--r-- | docs/crawstr_api.md | 43 | ||||
| -rw-r--r-- | docs/cspan_api.md | 10 | ||||
| -rw-r--r-- | docs/cstr_api.md | 12 | ||||
| -rw-r--r-- | include/stc/crawstr.h | 9 | ||||
| -rw-r--r-- | include/stc/cspan.h | 2 |
6 files changed, 42 insertions, 46 deletions
diff --git a/docs/coroutine_api.md b/docs/coroutine_api.md index c44f4a4d..b917e0a1 100644 --- a/docs/coroutine_api.md +++ b/docs/coroutine_api.md @@ -231,25 +231,25 @@ void print_time() // PRODUCER cco_task_struct (produce_items, struct next_value next; - cstr str; + cstr text; ); int produce_items(struct produce_items* p, cco_runtime* rt) { cco_routine (p) { - p->str = cstr_init(); + p->text = cstr_init(); p->next.cco_func = next_value; while (true) { // await for CCO_YIELD (or CCO_DONE) cco_await_task(&p->next, rt, CCO_YIELD); - cstr_printf(&p->str, "item %d", p->next.val); + cstr_printf(&p->text, "item %d", p->next.val); print_time(); - printf("produced %s\n", cstr_str(&p->str)); + printf("produced %s\n", cstr_str(&p->text)); cco_yield(); } cco_final: - cstr_drop(&p->str); + cstr_drop(&p->text); puts("done produce"); } return 0; @@ -271,7 +271,7 @@ int consume_items(struct consume_items* c, cco_runtime* rt) printf("consume #%d\n", c->i); cco_await_task(&c->produce, rt, CCO_YIELD); print_time(); - printf("consumed %s\n", cstr_str(&c->produce.str)); + printf("consumed %s\n", cstr_str(&c->produce.text)); } cco_final: cco_stop(&c->produce); diff --git a/docs/crawstr_api.md b/docs/crawstr_api.md index d44c302d..59087d06 100644 --- a/docs/crawstr_api.md +++ b/docs/crawstr_api.md @@ -22,31 +22,37 @@ All crawstr definitions and prototypes are available by including a single heade ## Methods ```c -crawstr crawstr_from(const char* str); // construct from const char* -crawstr c_rs(const char literal_only[]); // construct from literal, no strlen() +crawstr crawstr_from(const char* str); // construct from const char* +crawstr c_rs(const char literal_only[]); // construct from literal, no strlen() intptr_t crawstr_size(crawstr rs); -bool crawstr_empty(crawstr rs); // check if size == 0 +bool crawstr_empty(crawstr rs); // check if size == 0 void crawstr_clear(crawstr* self); -csview crawstr_sv(crawstr rs); // convert to csview type +csview crawstr_sv(crawstr rs); // convert to csview type +const char* crawstr_str(crawstr rs); // get null-terminated const char* bool crawstr_equals(crawstr rs, const char* str); intptr_t crawstr_find(crawstr rs, const char* str); bool crawstr_contains(crawstr rs, const char* str); bool crawstr_starts_with(crawstr rs, const char* str); bool crawstr_ends_with(crawstr rs, const char* str); -``` - -#### UTF8 methods -```c -intptr_t crawstr_u8_size(crawstr rs); -bool crawstr_valid_utf8(crawstr rs); // depends on src/utf8code.c crawstr_iter crawstr_begin(const crawstr* self); crawstr_iter crawstr_end(const crawstr* self); -void crawstr_next(crawstr_iter* it); // utf8 codepoint step, not byte! +void crawstr_next(crawstr_iter* it); // utf8 codepoint step, not byte! crawstr_iter crawstr_advance(crawstr_iter it, intptr_t n); +``` +#### Helper methods for usage in containers +```c +int crawstr_cmp(const crawstr* x, const crawstr* y); +int crawstr_icmp(const crawstr* x, const crawstr* y); // depends on src/utf8code.c: +bool crawstr_eq(const crawstr* x, const crawstr* y); +uint64_t crawstr_hash(const crawstr* x); +``` + +#### UTF8 methods +```c // from utf8.h intptr_t utf8_size(const char *s); intptr_t utf8_size_n(const char *s, intptr_t nbytes); // number of UTF8 codepoints within n bytes @@ -62,14 +68,6 @@ uint32_t utf8_peek(const char* s); // codep uint32_t utf8_peek_off(const char* s, int offset); // codepoint value at utf8 pos (may be negative) ``` -#### Helper methods -```c -int crawstr_cmp(const crawstr* x, const crawstr* y); -int crawstr_icmp(const crawstr* x, const crawstr* y); // depends on src/utf8code.c: -bool crawstr_eq(const crawstr* x, const crawstr* y); -uint64_t crawstr_hash(const crawstr* x); -``` - ## Types | Type name | Type definition | Used to represent... | @@ -86,14 +84,14 @@ uint64_t crawstr_hash(const crawstr* x); int main(void) { - cstr str = cstr_from("Liberté, égalité, fraternité."); - crawstr rs = cstr_rs(&str); + crawstr rs = c_rs("Liberté, égalité, fraternité."); + printf("%s\n", rs.str); c_foreach (i, crawstr, rs) printf("%.*s ", c_SV(i.u8.chr)); puts(""); - cstr_uppercase(&str); + cstr str = cstr_toupper_sv(crawstr_sv(rs)); printf("%s\n", cstr_str(&str)); cstr_drop(&str); @@ -101,6 +99,7 @@ int main(void) ``` Output: ``` +Liberté, égalité, fraternité. L i b e r t é , é g a l i t é , f r a t e r n i t é . LIBERTÉ, ÉGALITÉ, FRATERNITÉ. ``` diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 606e63c9..39b97473 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -222,9 +222,9 @@ int main(void) // slice without reducing rank: Span3 ss3 = cspan_slice(Span3, &span3, {c_ALL}, {3,4}, {c_ALL}); - c_forrange (i, ss3.shape[0]) { - c_forrange (j, ss3.shape[1]) { - c_forrange (k, ss3.shape[2]) + for (int i=0; i < ss3.shape[0]; ++i) { + for (int j=0; j < ss3.shape[1]; ++j) { + for (int k=0; k < ss3.shape[2]; ++k) printf(" %2d", *cspan_at(&ss3, i, j, k)); puts(""); } @@ -234,8 +234,8 @@ int main(void) // slice and reduce rank: Span2 ss2 = cspan_slice(Span2, &span3, {c_ALL}, {3}, {c_ALL}); - c_forrange (i, ss2.shape[0]) { - c_forrange (j, ss2.shape[1]) + for (int i=0; i < ss2.shape[0]; ++i) { + for (int j=0; j < ss2.shape[1]; ++j) printf(" %2d", *cspan_at(&ss2, i, j)); puts(""); } diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 39bd4e94..bcb0d172 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -1,11 +1,17 @@ # STC [cstr](../include/stc/cstr.h): String  -A **cstr** object represent sequences of characters. It supports an interface similar to that of a standard container of bytes, but adding features specifically designed to operate with strings of single-byte characters, terminated by the null character. +A **cstr** object represent sequences of characters. It supports an interface similar +to that of a standard container of bytes, but adding features specifically designed to +operate with strings of single-byte characters, terminated by the null character. -**cstr** has basic support for *UTF8* encoded strings, and has a set of compact and efficient functions for handling case-foldings and comparisons of UTF strings. +**cstr** has basic support for *UTF8* encoded strings, and has a set of compact and +efficient functions for handling case-conversion, iteration and indexing into UTF8 +codepoints. -**cstr** uses short strings optimization (sso), which eliminates heap memory allocation for string capacity less than 24 bytes. `sizeof(cstr)` is also 24. In comparison, c++ `sizeof(std::string)` is typically 32, but sso capacity is only 15 bytes. +**cstr** uses short strings optimization (sso), which eliminates heap memory allocation +for string capacity up to 22 bytes. `sizeof(cstr)` is 24. In comparison, C++ +`sizeof(std::string)` is typically 32, but sso capacity is only 15 bytes. ## Header file diff --git a/include/stc/crawstr.h b/include/stc/crawstr.h index 7cf62e94..3b836222 100644 --- a/include/stc/crawstr.h +++ b/include/stc/crawstr.h @@ -83,19 +83,10 @@ STC_INLINE crawstr_iter crawstr_advance(crawstr_iter it, intptr_t pos) { return it; } -/* utf8 size */ -STC_INLINE intptr_t crawstr_u8_size(crawstr rs) - { return utf8_size_n(rs.str, rs.size); } - -/* utf8 validation: depends on src/utf8code.c */ -STC_INLINE bool crawstr_valid_utf8(crawstr rs) - { return utf8_valid_n(rs.str, rs.size); } - /* utf8 ignore case cmp: depends on src/utf8code.c */ STC_INLINE int crawstr_icmp(const crawstr* x, const crawstr* y) { return utf8_icmp_sv(c_sv_2(x->str, x->size), c_sv_2(y->str, y->size)); } - STC_INLINE int crawstr_cmp(const crawstr* x, const crawstr* y) { intptr_t n = x->size < y->size ? x->size : y->size; int c = c_memcmp(x->str, y->str, n); diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 1b57d4d4..f806ed8f 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -24,7 +24,7 @@ /* #include <stdio.h> #include <stc/cspan.h> -#include <stc/algo/filter.h> +#include <stc/algorithm.h> using_cspan(Span2f, float, 2); using_cspan(Intspan, int); |
