diff options
| -rw-r--r-- | README.md | 6 | ||||
| -rwxr-xr-x | examples/make.sh | 56 | ||||
| -rw-r--r-- | include/stc/cstr.h | 17 |
3 files changed, 43 insertions, 36 deletions
@@ -425,8 +425,8 @@ but still not expose or include the full implementation / API of the container. ```c // Header file #include <stc/forward.h> // only include data structures -declare_cstack(cstack_pnt, struct Point); // declare cstack_pnt and cstack_pnt_value, cstack_pnt_iter; - // the element may be forward declared type as well +declare_cstack(cstack_pnt, struct Point); // declare cstack_pnt (and cstack_pnt_value, cstack_pnt_iter); + // struct Point may be an incomplete type. typedef struct Dataset { cstack_pnt vertices; cstack_pnt colors; @@ -434,7 +434,7 @@ typedef struct Dataset { ... // Implementation -#define c_opt c_declared // flag that the container was forward declared. +#define c_opt c_declared // flag that the container was forward declared. #define i_val struct Point #define i_tag pnt #include <stc/cstack.h> diff --git a/examples/make.sh b/examples/make.sh index c60ac3d4..28d95abe 100755 --- a/examples/make.sh +++ b/examples/make.sh @@ -1,43 +1,49 @@ -#!/bin/bash -cc='gcc -s -O2 -Wall -std=c99 -pedantic -Wfatal-errors -fsanitize=address' -#cc='tcc -s -O2 -Wall -std=c99 -pedantic -Wfatal-errors' -#cc='clang -s -O2 -Wall -std=c99 -pedantic -Wfatal-errors' -#cc='clang -s -O2 -Wall -std=c99 -pedantic -Wfatal-errors -DSTC_CSTR_V1 -DSTC_CSMAP_V1' -#cc='gcc -x c++ -s -O2 -Wall -std=c++20' -#cc='g++ -x c++ -s -O2 -Wall' -#cc='clang' -#cc='clang -c -DSTC_HEADER' -#cc='cl -O2 -nologo -W2 -MD' -#cc='cl -nologo -TP' -#cc='cl -nologo -std:c11' -libs='' -#oflag='/Fe:' -oflag='-o ' -run=0 -if [ -z "$OS" ]; then - libs='-lm -pthread' +#!/bin/sh + +if [ "$(uname)" = 'Linux' ]; then + sanitize='-fsanitize=address' + clibs='-lm' # -pthread + oflag='-o ' +fi + +cc=gcc; cflags="-s -O2 -Wall -std=c99 -pedantic -Wfatal-errors $sanitize" +#cc=tcc; cflags="-Wall -std=c99" +#cc=clang; cflags="-s -O2 -Wall -std=c99 -pedantic -Wfatal-errors" +#cc=clang; cflags="-s -O2 -Wall -std=c99 -pedantic -Wfatal-errors -DSTC_CSTR_V1 -DSTC_CSMAP_V1" +#cc=gcc; cflags="-x c++ -s -O2 -Wall -std=c++20" +#cc=g++; cflags="-x c++ -s -O2 -Wall" +#cc=cl; cflags="-O2 -nologo -W2 -MD" +#cc=cl; cflags="-nologo -TP" +#cc=cl; cflags="-nologo -std:c11" + +if [ "$cc" = "cl" ]; then + oflag='/Fe:' +else + oflag='-o ' fi -if [ "$1" == '-h' -o "$1" == '--help' ]; then + +run=0 +if [ "$1" = '-h' -o "$1" = '--help' ]; then echo usage: runall.sh [-run] [compiler + options] exit fi -if [ "$1" == '-run' ]; then +if [ "$1" = '-run' ]; then run=1 shift fi if [ ! -z "$1" ] ; then - cc=$@ + comp=$@ fi if [ $run = 0 ] ; then for i in *.c ; do - echo $cc -I../include $i $libs $oflag$(basename $i .c).exe - $cc -I../include $i $libs $oflag$(basename $i .c).exe + echo $cc $cflags -I../include $i $clibs $oflag$(basename $i .c).exe + $cc $cflags -I../include $i $clibs $oflag$(basename $i .c).exe done else for i in *.c ; do - echo $cc -I../include $i $libs - $cc -I../include $i $libs + echo $cc $cflags -I../include $i $clibs + $cc $cflags -I../include $i $clibs if [ -f $(basename -s .c $i).exe ]; then ./$(basename -s .c $i).exe; fi if [ -f ./a.exe ]; then ./a.exe; fi if [ -f ./a.out ]; then ./a.out; fi diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 5f6e19a8..c762d0e0 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -72,7 +72,7 @@ STC_API char* _cstr_internal_move(cstr* self, size_t pos1, size_t pos2); #define cstr_new(literal) cstr_from_n(literal, c_strlen_lit(literal)) #define cstr_npos (SIZE_MAX >> 1) -#define cstr_null (c_init(cstr){{{0}}}) +#define cstr_null (c_init(cstr){{{0}, 0}}) #define cstr_toraw(self) cstr_str(self) STC_API char* cstr_reserve(cstr* self, size_t cap); @@ -228,7 +228,7 @@ STC_INLINE cstr_iter cstr_begin(const cstr* self) { return c_init(cstr_iter){.u8 = {{sv.str, utf8_chr_size(sv.str)}}}; } STC_INLINE cstr_iter cstr_end(const cstr* self) { - return c_init(cstr_iter){NULL}; + (void)self; return c_init(cstr_iter){NULL}; } STC_INLINE void cstr_next(cstr_iter* it) { it->ref += it->u8.chr.size; @@ -283,7 +283,7 @@ STC_INLINE bool cstr_iequals(const cstr* self, const char* str) STC_INLINE size_t cstr_find(const cstr* self, const char* search) { const char *str = cstr_str(self), *res = strstr((char*)str, search); - return res ? res - str : cstr_npos; + return res ? (size_t)(res - str) : cstr_npos; } STC_API size_t cstr_find_sv(const cstr* self, csview search); @@ -417,15 +417,16 @@ STC_DEF uint64_t cstr_hash(const cstr *self) { } STC_DEF size_t cstr_find_sv(const cstr* self, csview search) { - char* res = cstrnstrn(cstr_str(self), search.str, cstr_size(self), search.size); - return res ? res - cstr_str(self) : cstr_npos; + csview sv = cstr_sv(self); + char* res = cstrnstrn(sv.str, search.str, sv.size, search.size); + return res ? (size_t)(res - sv.str) : cstr_npos; } STC_DEF char* _cstr_internal_move(cstr* self, const size_t pos1, const size_t pos2) { cstr_buf r = cstr_buffer(self); if (pos1 != pos2) { - const size_t newlen = r.size + pos2 - pos1; - if (newlen > r.cap) + const intptr_t newlen = r.size + pos2 - pos1; + if (newlen > (intptr_t)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, newlen); @@ -492,7 +493,7 @@ STC_DEF size_t cstr_find_at(const cstr* self, const size_t pos, const char* sear csview sv = cstr_sv(self); if (pos > sv.size) return cstr_npos; const char* res = strstr((char*)sv.str + pos, search); - return res ? res - sv.str : cstr_npos; + return res ? (size_t)(res - sv.str) : cstr_npos; } STC_DEF char* cstr_assign_n(cstr* self, const char* str, const size_t len) { |
