From 0468971b404cee5582d360d7d1c66bb4148e1614 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 16 Mar 2022 21:41:40 +0100 Subject: Bugfix: carc and cbox cmp functions had bug. Renamed: i_key_sptr / i_val_sptr to i_key_arcbox / i_val_arcbox. Other smaller updates. --- README.md | 14 ++++++-------- docs/carc_api.md | 4 ++-- docs/cbox_api.md | 4 ++-- examples/arc_containers.c | 4 ++-- examples/arc_demo.c | 4 ++-- examples/arcvec_erase.c | 4 ++-- examples/box.c | 2 +- examples/box2.c | 4 ++-- examples/music_arc.c | 2 +- examples/new_sptr.c | 2 +- examples/person_arc.c | 2 +- examples/utf8replace_c.c | 21 +++++++++++++++++++++ examples/utf8replace_rs.rs | 19 +++++++++++++++++++ include/stc/carc.h | 4 ++-- include/stc/cbox.h | 4 ++-- include/stc/cdeq.h | 2 +- include/stc/csview.h | 6 +++--- include/stc/cvec.h | 4 ++-- include/stc/forward.h | 36 ++++++++++++++++++------------------ include/stc/template.h | 26 ++++++++++++++------------ include/stc/utf8.h | 10 ++++++---- 21 files changed, 110 insertions(+), 68 deletions(-) create mode 100644 examples/utf8replace_c.c create mode 100644 examples/utf8replace_rs.rs diff --git a/README.md b/README.md index 5d253987..f7250b5f 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ which by the compiler is seen as different code because of macro name substituti - [***ccommon*** - RAII and iterator macros](docs/ccommon_api.md) - [***carc*** - **std::shared_ptr** alike support](docs/carc_api.md) -- [***carr2/3 - **2d** and **3d** dynamic **array** type](docs/carray_api.md) +- [***carr2/3*** - **2d** and **3d** dynamic **array** type](docs/carray_api.md) - [***cbits*** - **std::bitset** alike type](docs/cbits_api.md) - [***cbox*** - **std::unique_ptr** alike type](docs/cbox_api.md) - [***cdeq*** - **std::deque** alike type](docs/cdeq_api.md) @@ -291,9 +291,9 @@ Val: Special: - `i_key_str` - Define key type `cstr` and container i_tag = `str`. It binds type convertion from/to const char*, and the cmp, eq, hash, and keydrop functions. -- `i_key_sptr TYPE` - Define key type where TYPE is a smart pointer `carc` or `cbox`. I.e. not to be used when defining carc/cbox types themselves. +- `i_key_arcbox TYPE` - Define key type where TYPE is a smart pointer `carc` or `cbox`. I.e. not to be used when defining carc/cbox types themselves. - `i_key_bind TYPE` - General version of the two above - will auto-bind to standard named functions based on TYPE. Use for elements where the following functions are defined: *TYPE_cmp*, *TYPE_clone*, *TYPE_drop*, *TYPE_hash*, and *TYPE_eq*. Only the functions required by the particular container needs to be defined. e.g. **cmap** and **cset** are the only types that require *TYPE_hash* and *TYPE_eq* to be defined. *TYPE_cmp* and *TYPE_clone* are not required if `i_opt c_no_cmp|c_no_clone` is defined. *TYPE_drop* is always required when using `_bind`. -- `i_val_str`, `i_val_sptr`, `i_val_bind` - Same as for key. +- `i_val_str`, `i_val_bind`, `i_val_arcbox` - Same as for key. Notes: - For non-associative containers, `i_drop` and `i_from` may be defined instead of `i_valdrop` and `i_valfrom`. @@ -436,7 +436,7 @@ Memory efficiency - Renamed: ***i_equ*** to `i_eq`, and ***_equalto*** to `_eq`. - Renamed: ***i_cnt*** to `i_type` for defining the complete container type name. - Renamed: type **csptr** to [**carc**](docs/carc_api.md) (atomic reference counted) smart pointer. -- Renamed: ***i_key_csptr*** / ***i_val_csptr*** to `i_key_sptr` / `i_val_sptr` for specifying **carc** and **cbox** values in containers. +- Renamed: ***i_key_csptr*** / ***i_val_csptr*** to `i_key_arcbox` / `i_val_arcbox` for specifying **carc** and **cbox** values in containers. - Renamed: *csptr_X_make()* to `carc_X_from()`. - Renamed: *cstr_lit()* to `cstr_new(literal)`, and *cstr_assign_fmt()* to `cstr_printf()`. - Renamed: *c_default_fromraw()* to `c_default_from()`. @@ -460,10 +460,8 @@ Replace (whole word + match case): - `i_valdel` ⟶ `i_valdrop` - `i_cnt` ⟶ `i_type` - `cstr_lit` ⟶ `cstr_new` -- `i_key_csptr` / `i_key_ref` ⟶ `i_key_sptr` -- `i_val_csptr` / `i_val_ref` ⟶ `i_val_sptr` -- `c_apply` ⟶ `c_apply_OLD` // replaced by new `c_apply` -- `c_apply_pair` ⟶ `c_apply_pair_OLD` // replaced by new `c_apply` +- `i_key_sptr` ⟶ `i_key_arcbox` +- `i_val_sptr` ⟶ `i_val_arcbox` Non-regex, global match case replace: - `csptr` ⟶ `carc` diff --git a/docs/carc_api.md b/docs/carc_api.md index a83bc194..c137094d 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -88,11 +88,11 @@ bool carc_X_value_eq(const i_val* x, const i_val* y); // cbox_X_value_c #include #define i_type Stack -#define i_val_sptr Arc // define i_val_sptr for carc/cbox value, not i_val or i_val_bind +#define i_val_arcbox Arc // note: define i_val_arcbox for carc/cbox value #include #define i_type List -#define i_val_sptr Arc // as above +#define i_val_arcbox Arc // as above #include int main() diff --git a/docs/cbox_api.md b/docs/cbox_api.md index 32271f58..fe23fa8a 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -77,11 +77,11 @@ void int_drop(int* x) { #include #define i_type ISet -#define i_key_sptr IBox // NB: use i_key_sptr instead of i_key +#define i_key_arcbox IBox // NB: use i_key_arcbox instead of i_key #include // ISet : std::set> #define i_type IVec -#define i_val_sptr IBox // NB: use i_val_sptr instead of i_val +#define i_val_arcbox IBox // NB: use i_val_arcbox instead of i_val #include // IVec : std::vector> int main() diff --git a/examples/arc_containers.c b/examples/arc_containers.c index 0873d297..ea9395e8 100644 --- a/examples/arc_containers.c +++ b/examples/arc_containers.c @@ -15,11 +15,11 @@ #include #define i_type Stack -#define i_val_sptr Arc // define i_val_bind for carc/cbox value (not i_val) +#define i_val_arcbox Arc // define i_val_bind for carc/cbox value (not i_val) #include #define i_type List -#define i_val_sptr Arc // as above +#define i_val_arcbox Arc // as above #include int main() diff --git a/examples/arc_demo.c b/examples/arc_demo.c index 8626e2d4..036b7a00 100644 --- a/examples/arc_demo.c +++ b/examples/arc_demo.c @@ -13,10 +13,10 @@ void int_drop(int* x) { #define i_drop int_drop // optional, just to display the elements destroyed #include // iref -#define i_key_sptr iref // note: use i_key_bind instead of i_key for carc/cbox elements +#define i_key_arcbox iref // note: use i_key_bind instead of i_key for carc/cbox elements #include // csset_iref (like: std::set>) -#define i_val_sptr iref // note: as above. +#define i_val_arcbox iref // note: as above. #include // cvec_iref (like: std::vector>) int main() diff --git a/examples/arcvec_erase.c b/examples/arcvec_erase.c index d40d8fe7..12547f6c 100644 --- a/examples/arcvec_erase.c +++ b/examples/arcvec_erase.c @@ -12,8 +12,8 @@ void show_drop(int* x) { printf("drop: %d\n", *x); } #include // Shared pointer to int #define i_type Vec -#define i_val_sptr Arc -#include // Vec: cvec +#define i_val_arcbox Arc +#include // Vec: cvec int main() diff --git a/examples/box.c b/examples/box.c index d97545b6..00a092e2 100644 --- a/examples/box.c +++ b/examples/box.c @@ -28,7 +28,7 @@ void Person_drop(Person* p) { #include #define i_type Persons -#define i_val_sptr PBox // informs that PBox is a smart pointer. +#define i_val_arcbox PBox // informs that PBox is a smart pointer. #include int main() diff --git a/examples/box2.c b/examples/box2.c index e5b4dacd..68a9ccba 100644 --- a/examples/box2.c +++ b/examples/box2.c @@ -26,8 +26,8 @@ struct { #include // cbox_Rectangle // Box in box: -#define i_val_sptr cbox_Point // NB: adviced to use i_val_sptr when value is a cbox or carc! - // it will auto-set i_drop, i_from, i_cmp for you. +#define i_val_arcbox cbox_Point // NB: use i_val_arcbox when value is a cbox or carc! + // it will auto-set i_drop, i_from, i_cmp for you. #define i_opt c_no_cmp #define i_tag BoxPoint #include // cbox_BoxPoint diff --git a/examples/music_arc.c b/examples/music_arc.c index 42bb3455..b2661ffe 100644 --- a/examples/music_arc.c +++ b/examples/music_arc.c @@ -24,7 +24,7 @@ void Song_drop(Song* s) { #include #define i_type SongVec -#define i_val_sptr SongPtr +#define i_val_arcbox SongPtr #include void example3() diff --git a/examples/new_sptr.c b/examples/new_sptr.c index 3a038629..61eaf5ae 100644 --- a/examples/new_sptr.c +++ b/examples/new_sptr.c @@ -25,7 +25,7 @@ void Person_drop(Person* p) { #define i_drop(x) printf("drop: %d\n", *(x)) #include -#define i_val_sptr SPtr +#define i_val_arcbox SPtr #define i_tag iptr #include diff --git a/examples/person_arc.c b/examples/person_arc.c index 0c22bd68..e668cda6 100644 --- a/examples/person_arc.c +++ b/examples/person_arc.c @@ -28,7 +28,7 @@ void Person_drop(Person* p) { #include #define i_type Persons -#define i_val_sptr PSPtr // binds PSPtr_cmp, ... +#define i_val_arcbox PSPtr // binds PSPtr_cmp, ... #include diff --git a/examples/utf8replace_c.c b/examples/utf8replace_c.c new file mode 100644 index 00000000..e04f2cb8 --- /dev/null +++ b/examples/utf8replace_c.c @@ -0,0 +1,21 @@ +#include +#include +#include + +int main() { + c_auto (cstr, hello) { + hello = cstr_new("hell😀 world"); + printf("%s\n", hello.str); + + cstr_replace_sv( + &hello, + utf8_substr(hello.str, 4, 1), + c_sv("🐨") + ); + printf("%s\n", hello.str); + + csview sv = csview_from_s(hello); + c_foreach (c, csview, sv) + printf(c_PRIsv ",", c_ARGsv(c.codep)); + } +} diff --git a/examples/utf8replace_rs.rs b/examples/utf8replace_rs.rs new file mode 100644 index 00000000..717978aa --- /dev/null +++ b/examples/utf8replace_rs.rs @@ -0,0 +1,19 @@ + +pub fn main() { + let mut hello = String::from("hell😀 world"); + println!("{}", hello); + + hello.replace_range( + hello + .char_indices() + .nth(4) + .map(|(pos, ch)| (pos..pos + ch.len_utf8())) + .unwrap(), + "🐨", + ); + println!("{}", hello); + + for c in hello.chars() { + print!("{},", c); + } +} diff --git a/include/stc/carc.h b/include/stc/carc.h index e3deb376..7ea1eb8e 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -187,7 +187,7 @@ _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #if c_option(c_no_cmp) return c_default_cmp(&x, &y); #else - _cx_raw rx = i_valto(x), ry = i_valto(x); + _cx_raw rx = i_valto(x), ry = i_valto(y); return i_cmp(&rx, &ry); #endif } @@ -197,7 +197,7 @@ _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { #if c_option(c_no_cmp) return x == y; #else - _cx_raw rx = i_valto(x), ry = i_valto(x); + _cx_raw rx = i_valto(x), ry = i_valto(y); return i_eq(&rx, &ry); #endif } diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 41d8f654..7b1cf377 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -163,7 +163,7 @@ _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #if c_option(c_no_cmp) return c_default_cmp(&x, &y); #else - _cx_raw rx = i_valto(x), ry = i_valto(x); + _cx_raw rx = i_valto(x), ry = i_valto(y); return i_cmp(&rx, &ry); #endif } @@ -173,7 +173,7 @@ _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { #if c_option(c_no_cmp) return x == y; #else - _cx_raw rx = i_valto(x), ry = i_valto(x); + _cx_raw rx = i_valto(x), ry = i_valto(y); return i_eq(&rx, &ry); #endif } diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index cb291fbe..6b8e7efa 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -400,7 +400,7 @@ STC_DEF _cx_iter _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) { for (; i1.ref != i2.ref; ++i1.ref) { i_valraw r = i_valto(i1.ref); - if (i_cmp(&raw, &r) == 0) return i1; + if (i_eq(&raw, &r)) return i1; } return i2; } diff --git a/include/stc/csview.h b/include/stc/csview.h index 85ade552..cd482b2c 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -66,11 +66,11 @@ STC_INLINE bool csview_ends_with(csview sv, csview sub) { if (sub.size > sv.size) return false; return !memcmp(sv.str + sv.size - sub.size, sub.str, sub.size); } STC_INLINE csview_iter csview_begin(const csview* self) - { return c_make(csview_iter){.cp = {self->str, utf8_codep_size(self->str)}}; } + { return c_make(csview_iter){.codep = {self->str, utf8_codep_size(self->str)}}; } STC_INLINE csview_iter csview_end(const csview* self) { return c_make(csview_iter){self->str + self->size}; } STC_INLINE void csview_next(csview_iter* it) - { it->ref += it->cp.size; it->cp.size = utf8_codep_size(it->ref); } + { it->ref += it->codep.size; it->codep.size = utf8_codep_size(it->ref); } /* utf8 */ STC_INLINE bool utf8_valid_sv(csview sv) @@ -82,7 +82,7 @@ STC_INLINE size_t utf8_size_sv(csview sv) STC_INLINE csview utf8_substr(const char* str, size_t pos, size_t n) { csview sv; sv.str = utf8_at(str, pos); - sv.size = utf8_at(sv.str, n) - sv.str; + sv.size = utf8_pos(sv.str, n); return sv; } diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 9278eada..f3ae9d47 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -209,7 +209,7 @@ _cx_memb(_find)(const _cx_self* self, i_valraw raw) { STC_INLINE const _cx_value* _cx_memb(_get)(const _cx_self* self, i_valraw raw) { _cx_iter end = _cx_memb(_end)(self); - _cx_value* val = _cx_memb(_find_in)(_cx_memb(_begin)(self), end, raw).ref; + _cx_value* val = _cx_memb(_find)(self, raw).ref; return val == end.ref ? NULL : val; } @@ -368,7 +368,7 @@ STC_DEF _cx_iter _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) { for (; i1.ref != i2.ref; ++i1.ref) { i_valraw r = i_valto(i1.ref); - if (i_cmp(&raw, &r) == 0) return i1; + if (i_eq(&raw, &r)) return i1; } return i2; } diff --git a/include/stc/forward.h b/include/stc/forward.h index 6f450219..bffdf154 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -25,8 +25,10 @@ #include +#define forward_carc(CX, VAL) _c_carc_types(CX, VAL) #define forward_carr2(CX, VAL) _c_carr2_types(CX, VAL) #define forward_carr3(CX, VAL) _c_carr3_types(CX, VAL) +#define forward_cbox(CX, VAL) _c_cbox_types(CX, VAL) #define forward_cdeq(CX, VAL) _c_cdeq_types(CX, VAL) #define forward_clist(CX, VAL) _c_clist_types(CX, VAL) #define forward_cmap(CX, KEY, VAL) _c_chash_types(CX, KEY, VAL, uint32_t, c_true, c_false) @@ -35,10 +37,8 @@ #define forward_cset_huge(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, size_t, c_false, c_true) #define forward_csmap(CX, KEY, VAL) _c_aatree_types(CX, KEY, VAL, uint32_t, c_true, c_false) #define forward_csset(CX, KEY) _c_aatree_types(CX, KEY, KEY, uint32_t, c_false, c_true) -#define forward_cbox(CX, VAL) _c_cbox_types(CX, VAL) -#define forward_carc(CX, VAL) _c_carc_types(CX, VAL) -#define forward_cpque(CX, VAL) _c_cpque_types(CX, VAL) #define forward_cstack(CX, VAL) _c_cstack_types(CX, VAL) +#define forward_cpque(CX, VAL) _c_cpque_types(CX, VAL) #define forward_cqueue(CX, VAL) _c_cdeq_types(CX, VAL) #define forward_cvec(CX, VAL) _c_cvec_types(CX, VAL) @@ -46,12 +46,20 @@ typedef struct cstr { char* str; } cstr; typedef char cstr_value; typedef struct csview { const char* str; size_t size; } csview; -typedef union csview_iter { const char *ref; csview cp; } csview_iter; +typedef union csview_iter { const char *ref; csview codep; } csview_iter; typedef char csview_value; #define c_true(...) __VA_ARGS__ #define c_false(...) +#define _c_carc_types(SELF, VAL) \ + typedef VAL SELF##_value; \ +\ + typedef struct { \ + SELF##_value* get; \ + long* use_count; \ + } SELF + #define _c_carr2_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct { SELF##_value *ref; } SELF##_iter; \ @@ -62,6 +70,12 @@ typedef char csview_value; typedef struct { SELF##_value *ref; } 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 { \ + SELF##_value* get; \ + } SELF + #define _c_cdeq_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct {SELF##_value *ref; } SELF##_iter; \ @@ -132,20 +146,6 @@ typedef char csview_value; SELF##_node *nodes; \ } SELF -#define _c_cbox_types(SELF, VAL) \ - typedef VAL SELF##_value; \ - typedef struct { \ - SELF##_value* get; \ - } SELF - -#define _c_carc_types(SELF, VAL) \ - typedef VAL SELF##_value; \ -\ - typedef struct { \ - SELF##_value* get; \ - long* use_count; \ - } SELF - #define _c_cstack_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct { SELF##_value *ref; } SELF##_iter; \ diff --git a/include/stc/template.h b/include/stc/template.h index c61aded4..12c015f6 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -40,9 +40,11 @@ #define _cx_size _cx_memb(_size_t) #endif -#if defined i_cnt || defined i_equ // [deprecated] - #define i_type i_cnt - #error "i_cnt and i_equ no longer supported: use new name i_type / i_eq" +#ifdef i_key_sptr // [deprecated] + #define i_key_arcbox i_key_sptr +#endif +#ifdef i_val_sptr // [deprecated] + #define i_val_arcbox i_val_sptr #endif #ifdef i_type #define i_tag i_type @@ -64,9 +66,9 @@ #ifndef i_tag #define i_tag str #endif -#elif defined i_key_sptr - #define i_key_bind i_key_sptr - #define i_keyraw c_PASTE(i_key_sptr, _value) +#elif defined i_key_arcbox + #define i_key_bind i_key_arcbox + #define i_keyraw c_PASTE(i_key_arcbox, _value) #endif #ifdef i_key_bind @@ -123,9 +125,9 @@ #if !defined i_tag && !defined i_key #define i_tag str #endif -#elif defined i_val_sptr - #define i_val_bind i_val_sptr - #define i_valraw c_PASTE(i_val_sptr, _value) +#elif defined i_val_arcbox + #define i_val_bind i_val_arcbox + #define i_valraw c_PASTE(i_val_arcbox, _value) #endif #ifdef i_val_bind @@ -221,10 +223,11 @@ #undef i_hash #undef i_from #undef i_drop +#undef i_size #undef i_val #undef i_val_str -#undef i_val_sptr +#undef i_val_arcbox #undef i_val_bind #undef i_valraw #undef i_valfrom @@ -233,13 +236,12 @@ #undef i_key #undef i_key_str -#undef i_key_sptr +#undef i_key_arcbox #undef i_key_bind #undef i_keyraw #undef i_keyfrom #undef i_keyto #undef i_keydrop -#undef i_size #undef _i_prefix #undef _i_no_raw diff --git a/include/stc/utf8.h b/include/stc/utf8.h index 60599519..0a1ba161 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -9,17 +9,19 @@ int main() { c_auto (cstr, s1) { s1 = cstr_new("hell😀 w😀rld"); - cstr_replace_sv(&s1, utf8_substr(s1.str, 7, 1), c_sv("x")); + printf("%s\n", s1.str); + cstr_replace_sv(&s1, utf8_substr(s1.str, 7, 1), c_sv("🐨")); printf("%s\n", s1.str); csview sv = csview_from_s(s1); c_foreach (i, csview, sv) - printf(c_PRIsv ",", c_ARGsv(i.cp)); + printf(c_PRIsv ",", c_ARGsv(i.codep)); } } // Output: -// hell😀 wxrld -// h,e,l,l,😀, ,w,x,r,l,d, +// hell😀 w😀rld +// hell😀 w🐨rld +// h,e,l,l,😀, ,w,🐨,r,l,d, */ #include "ccommon.h" #include -- cgit v1.2.3