Add string functions
This commit is contained in:
parent
ae83ee66e1
commit
45f07986c3
0
.gitignore
vendored
Normal file → Executable file
0
.gitignore
vendored
Normal file → Executable file
BIN
doc/._.DS_Store
Executable file
BIN
doc/._.DS_Store
Executable file
Binary file not shown.
BIN
doc/._guf_dict-diagram.png
Executable file
BIN
doc/._guf_dict-diagram.png
Executable file
Binary file not shown.
0
doc/guf_dict-diagram.png
Normal file → Executable file
0
doc/guf_dict-diagram.png
Normal file → Executable file
|
Before Width: | Height: | Size: 133 KiB After Width: | Height: | Size: 133 KiB |
0
src/guf_alloc.h
Normal file → Executable file
0
src/guf_alloc.h
Normal file → Executable file
0
src/guf_alloc_libc.h
Normal file → Executable file
0
src/guf_alloc_libc.h
Normal file → Executable file
0
src/guf_alloc_tracker.h
Normal file → Executable file
0
src/guf_alloc_tracker.h
Normal file → Executable file
1
src/guf_assert.h
Normal file → Executable file
1
src/guf_assert.h
Normal file → Executable file
@ -26,6 +26,7 @@ typedef enum guf_err {
|
|||||||
GUF_ERR_NOT_FOUND,
|
GUF_ERR_NOT_FOUND,
|
||||||
GUF_ERR_ALREADY_EXISTS,
|
GUF_ERR_ALREADY_EXISTS,
|
||||||
GUF_ERR_ASSERT_FAIL,
|
GUF_ERR_ASSERT_FAIL,
|
||||||
|
GUF_ERR_IO,
|
||||||
GUF_ERR_TYPES_NUM
|
GUF_ERR_TYPES_NUM
|
||||||
} guf_err;
|
} guf_err;
|
||||||
|
|
||||||
|
|||||||
0
src/guf_common.h
Normal file → Executable file
0
src/guf_common.h
Normal file → Executable file
0
src/guf_cstr.h
Normal file → Executable file
0
src/guf_cstr.h
Normal file → Executable file
0
src/guf_dbuf.h
Normal file → Executable file
0
src/guf_dbuf.h
Normal file → Executable file
0
src/guf_hash.h
Normal file → Executable file
0
src/guf_hash.h
Normal file → Executable file
0
src/guf_id_pool.h
Normal file → Executable file
0
src/guf_id_pool.h
Normal file → Executable file
0
src/guf_init.h
Normal file → Executable file
0
src/guf_init.h
Normal file → Executable file
0
src/guf_linalg.h
Normal file → Executable file
0
src/guf_linalg.h
Normal file → Executable file
0
src/guf_math.h
Normal file → Executable file
0
src/guf_math.h
Normal file → Executable file
0
src/guf_math_ckdint.h
Normal file → Executable file
0
src/guf_math_ckdint.h
Normal file → Executable file
0
src/guf_rand.h
Normal file → Executable file
0
src/guf_rand.h
Normal file → Executable file
0
src/guf_sort.h
Normal file → Executable file
0
src/guf_sort.h
Normal file → Executable file
96
src/guf_str.h
Normal file → Executable file
96
src/guf_str.h
Normal file → Executable file
@ -128,6 +128,10 @@ GUF_STR_KWRDS guf_str_tok_state guf_str_tok_state_new(guf_str_view str, guf_str_
|
|||||||
*/
|
*/
|
||||||
GUF_STR_KWRDS bool guf_str_tok_next(guf_str_tok_state *state, bool preserve_delims);
|
GUF_STR_KWRDS bool guf_str_tok_next(guf_str_tok_state *state, bool preserve_delims);
|
||||||
|
|
||||||
|
GUF_STR_KWRDS unsigned guf_str_view_read_uint(guf_str_view *sv); // TODO: Handle overflow, signs etc.
|
||||||
|
GUF_STR_KWRDS uint_least64_t guf_str_view_read_u64(guf_str_view *sv);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 2.) guf_str:
|
// 2.) guf_str:
|
||||||
|
|
||||||
@ -208,6 +212,14 @@ GUF_STR_KWRDS guf_str *guf_str_append_one_char(guf_str *str, char c);
|
|||||||
GUF_STR_KWRDS guf_str *guf_str_try_append(guf_str *str, guf_str_view sv, guf_err *err);
|
GUF_STR_KWRDS guf_str *guf_str_try_append(guf_str *str, guf_str_view sv, guf_err *err);
|
||||||
GUF_STR_KWRDS guf_str *guf_str_append(guf_str *str, guf_str_view sv);
|
GUF_STR_KWRDS guf_str *guf_str_append(guf_str *str, guf_str_view sv);
|
||||||
|
|
||||||
|
// Append from file:
|
||||||
|
#define GUF_READ_FILE_BUFSIZE 128
|
||||||
|
GUF_STR_KWRDS ptrdiff_t guf_str_try_append_file(guf_str *to_append, const char *fname, guf_err *err);
|
||||||
|
GUF_STR_KWRDS ptrdiff_t guf_str_append_file(guf_str *to_append, const char *fname);
|
||||||
|
|
||||||
|
// Append ints:
|
||||||
|
GUF_STR_KWRDS guf_str *guf_str_append_u64(guf_str *str, uint_least64_t n);
|
||||||
|
|
||||||
// Return a pointer to the null-terminated char array representing the string (works like std::string::c_str in C++)
|
// Return a pointer to the null-terminated char array representing the string (works like std::string::c_str in C++)
|
||||||
GUF_STR_KWRDS const char *guf_str_const_cstr(const guf_str *str);
|
GUF_STR_KWRDS const char *guf_str_const_cstr(const guf_str *str);
|
||||||
GUF_STR_KWRDS char *guf_str_try_get_cstr(guf_str *str, guf_err *err); // Error if str is readonly.
|
GUF_STR_KWRDS char *guf_str_try_get_cstr(guf_str *str, guf_err *err); // Error if str is readonly.
|
||||||
@ -1142,6 +1154,66 @@ GUF_STR_KWRDS guf_str *guf_str_append(guf_str *str, guf_str_view sv)
|
|||||||
return guf_str_try_append(str, sv, NULL);
|
return guf_str_try_append(str, sv, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GUF_STR_KWRDS ptrdiff_t guf_str_try_append_file(guf_str *to_append, const char *fname, guf_err *err)
|
||||||
|
{
|
||||||
|
if (!fname) {
|
||||||
|
guf_err_set_or_panic(err, GUF_ERR_NULL_PTR, GUF_ERR_MSG("guf_str_read_file: fname is NULL"));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *f = fopen(fname, "r");
|
||||||
|
if (!f) {
|
||||||
|
guf_err_set_or_panic(err, GUF_ERR_IO, GUF_ERR_MSG("guf_str_read_file:: Failed to open file (fopen returned NULL)"));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t chars_read = 0, total_chars_read = 0;
|
||||||
|
char buf[GUF_READ_FILE_BUFSIZE] = {'\0'};
|
||||||
|
while ((chars_read = fread(buf, sizeof buf[0], GUF_ARR_SIZE(buf), f)) && !ferror(f)) {
|
||||||
|
guf_str_append(to_append, (guf_str_view){.len = chars_read, .str = buf});
|
||||||
|
total_chars_read += chars_read;
|
||||||
|
if (feof(f)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ferror(f)) {
|
||||||
|
fclose(f);
|
||||||
|
guf_err_set_or_panic(err, GUF_ERR_IO, GUF_ERR_MSG("read_file: Unexpected error while reading file"));
|
||||||
|
} else {
|
||||||
|
GUF_ASSERT(feof(f));
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
return total_chars_read;
|
||||||
|
}
|
||||||
|
|
||||||
|
GUF_STR_KWRDS ptrdiff_t guf_str_append_file(guf_str *to_append, const char *fname) {
|
||||||
|
return guf_str_try_append_file(to_append, fname, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
GUF_STR_KWRDS guf_str *guf_str_append_u64(guf_str *str, uint_least64_t n) {
|
||||||
|
|
||||||
|
char buf[20] = {'\0'};
|
||||||
|
int start_idx = GUF_ARR_SIZE(buf);
|
||||||
|
int num_digits = 0;
|
||||||
|
do {
|
||||||
|
GUF_ASSERT(start_idx > 0);
|
||||||
|
char c = (n % 10) + '0';
|
||||||
|
buf[--start_idx] = c;
|
||||||
|
++num_digits;
|
||||||
|
} while ((n = n / 10));
|
||||||
|
|
||||||
|
GUF_ASSERT(num_digits == (int)GUF_ARR_SIZE(buf) - start_idx);
|
||||||
|
|
||||||
|
const guf_str_view num_sv = (guf_str_view) {.str = buf + start_idx, .len = num_digits};
|
||||||
|
|
||||||
|
return guf_str_append(str, num_sv);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
GUF_STR_KWRDS guf_str *guf_str_try_substr(guf_str *str, ptrdiff_t pos, ptrdiff_t count, guf_err *err)
|
GUF_STR_KWRDS guf_str *guf_str_try_substr(guf_str *str, ptrdiff_t pos, ptrdiff_t count, guf_err *err)
|
||||||
{
|
{
|
||||||
GUF_ASSERT(guf_str_is_valid(str));
|
GUF_ASSERT(guf_str_is_valid(str));
|
||||||
@ -1526,6 +1598,30 @@ GUF_STR_KWRDS bool guf_str_view_equal_val_arg(guf_str_view a_val, guf_str_view b
|
|||||||
return guf_str_view_equal(&a_val, &b_val);
|
return guf_str_view_equal(&a_val, &b_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GUF_STR_KWRDS unsigned guf_str_view_read_uint(guf_str_view *sv)
|
||||||
|
{ // TODO: Handle overflow etc.
|
||||||
|
unsigned res = 0;
|
||||||
|
while (sv->len && sv->str[0] >= '0' && sv->str[0] <= '9') {
|
||||||
|
res *= 10;
|
||||||
|
res += (sv->str[0] - '0');
|
||||||
|
sv->len -= 1;
|
||||||
|
sv->str = sv->len > 0 ? sv->str + 1 : NULL;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
GUF_STR_KWRDS uint_least64_t guf_str_view_read_u64(guf_str_view *sv)
|
||||||
|
{ // TODO: Handle overflow etc.
|
||||||
|
uint_least64_t res = 0;
|
||||||
|
while (sv->len && sv->str[0] >= '0' && sv->str[0] <= '9') {
|
||||||
|
res *= 10;
|
||||||
|
res += (sv->str[0] - '0');
|
||||||
|
sv->len -= 1;
|
||||||
|
sv->str = sv->len > 0 ? sv->str + 1 : NULL;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
#undef GUF_STR_IMPL
|
#undef GUF_STR_IMPL
|
||||||
#undef GUF_STR_IMPL_STATIC
|
#undef GUF_STR_IMPL_STATIC
|
||||||
#endif /* end impl */
|
#endif /* end impl */
|
||||||
|
|||||||
0
src/guf_str_view_type.h
Normal file → Executable file
0
src/guf_str_view_type.h
Normal file → Executable file
0
src/guf_utf8.h
Normal file → Executable file
0
src/guf_utf8.h
Normal file → Executable file
0
src/guf_utils.h
Normal file → Executable file
0
src/guf_utils.h
Normal file → Executable file
0
src/test/data/bartleby.txt
Normal file → Executable file
0
src/test/data/bartleby.txt
Normal file → Executable file
0
src/test/data/utf8-test.txt
Normal file → Executable file
0
src/test/data/utf8-test.txt
Normal file → Executable file
0
src/test/example.c
Normal file → Executable file
0
src/test/example.c
Normal file → Executable file
0
src/test/impls/alloc_libc_impl.c
Normal file → Executable file
0
src/test/impls/alloc_libc_impl.c
Normal file → Executable file
0
src/test/impls/alloc_tracker_impl.c
Normal file → Executable file
0
src/test/impls/alloc_tracker_impl.c
Normal file → Executable file
0
src/test/impls/ckdint_impl.c
Normal file → Executable file
0
src/test/impls/ckdint_impl.c
Normal file → Executable file
0
src/test/impls/dbuf_impl.c
Normal file → Executable file
0
src/test/impls/dbuf_impl.c
Normal file → Executable file
0
src/test/impls/dbuf_impl.h
Normal file → Executable file
0
src/test/impls/dbuf_impl.h
Normal file → Executable file
0
src/test/impls/dict_impl.c
Normal file → Executable file
0
src/test/impls/dict_impl.c
Normal file → Executable file
0
src/test/impls/dict_impl.h
Normal file → Executable file
0
src/test/impls/dict_impl.h
Normal file → Executable file
0
src/test/impls/init_impl.c
Normal file → Executable file
0
src/test/impls/init_impl.c
Normal file → Executable file
0
src/test/impls/linalg_impl.c
Normal file → Executable file
0
src/test/impls/linalg_impl.c
Normal file → Executable file
0
src/test/impls/rand_impl.c
Normal file → Executable file
0
src/test/impls/rand_impl.c
Normal file → Executable file
0
src/test/impls/sort_impl.c
Normal file → Executable file
0
src/test/impls/sort_impl.c
Normal file → Executable file
0
src/test/impls/sort_impl.h
Normal file → Executable file
0
src/test/impls/sort_impl.h
Normal file → Executable file
0
src/test/impls/str_impl.c
Normal file → Executable file
0
src/test/impls/str_impl.c
Normal file → Executable file
0
src/test/test.cpp
Normal file → Executable file
0
src/test/test.cpp
Normal file → Executable file
0
src/test/test.hpp
Normal file → Executable file
0
src/test/test.hpp
Normal file → Executable file
0
src/test/test_ckdint.cpp
Normal file → Executable file
0
src/test/test_ckdint.cpp
Normal file → Executable file
0
src/test/test_ckdint.hpp
Normal file → Executable file
0
src/test/test_ckdint.hpp
Normal file → Executable file
0
src/test/test_dbuf.cpp
Normal file → Executable file
0
src/test/test_dbuf.cpp
Normal file → Executable file
0
src/test/test_dbuf.hpp
Normal file → Executable file
0
src/test/test_dbuf.hpp
Normal file → Executable file
0
src/test/test_dict.cpp
Normal file → Executable file
0
src/test/test_dict.cpp
Normal file → Executable file
0
src/test/test_dict.hpp
Normal file → Executable file
0
src/test/test_dict.hpp
Normal file → Executable file
0
src/test/test_str.cpp
Normal file → Executable file
0
src/test/test_str.cpp
Normal file → Executable file
0
src/test/test_str.hpp
Normal file → Executable file
0
src/test/test_str.hpp
Normal file → Executable file
0
src/test/test_utf8.cpp
Normal file → Executable file
0
src/test/test_utf8.cpp
Normal file → Executable file
0
src/test/test_utf8.hpp
Normal file → Executable file
0
src/test/test_utf8.hpp
Normal file → Executable file
2
todo.txt
Normal file → Executable file
2
todo.txt
Normal file → Executable file
@ -1,5 +1,7 @@
|
|||||||
- fix readonly str/uninit ?
|
- fix readonly str/uninit ?
|
||||||
|
|
||||||
|
- dict: allow set
|
||||||
|
|
||||||
- guf_stack, guf_queue, guf_dqueue, guf_prio_queue (using a heap), guf_ringbuf
|
- guf_stack, guf_queue, guf_dqueue, guf_prio_queue (using a heap), guf_ringbuf
|
||||||
- sort: add cpp #ifdef to remove restrict from declaration
|
- sort: add cpp #ifdef to remove restrict from declaration
|
||||||
|
|
||||||
|
|||||||
0
tools/ckdint-gen.py
Normal file → Executable file
0
tools/ckdint-gen.py
Normal file → Executable file
0
tools/intwrap-gen.py
Normal file → Executable file
0
tools/intwrap-gen.py
Normal file → Executable file
0
tools/min_max_clamp-gen.py
Normal file → Executable file
0
tools/min_max_clamp-gen.py
Normal file → Executable file
Loading…
x
Reference in New Issue
Block a user