Skip to content

Commit

Permalink
WIP add u8string, extend string.h as parent
Browse files Browse the repository at this point in the history
enable string.h to be a parent, for now only u8string.h, but later u8ident.h
Also maybe wstring, u16string, pmr::string, ...
Eventually split it into base_string and base_string_view.
  • Loading branch information
rurban committed Feb 21, 2024
1 parent 3b7df3a commit 7f30291
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 64 deletions.
2 changes: 2 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ help:

ctl/string.i:
$(call expand,$(subst .i,,$@))
ctl/u8string.i:
$(call expand,$(subst .i,,$@))
ctl/map.i:
$(call expand,$(subst .i,,$@),-DT=strint -DPOD)
ctl/unordered_map.i:
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ all containers in ISO C99/C11:
| [ctl/set.h](docs/set.md) | std::set | set |
| [ctl/stack.h](docs/stack.md) | std::stack | stack |
| [ctl/string.h](docs/string.md) | std::string | str |
| [ctl/u8string.h](docs/u8string.md) | std::string | str |
| [ctl/vector.h](docs/vector.md) | std::vector | vec |
| [ctl/array.h](docs/array.md) | std::array | arrNNNN |
| [ctl/map.h](docs/map.md) | std::map | map |
Expand Down Expand Up @@ -284,6 +285,7 @@ make ctl/queue.i
make ctl/set.i
make ctl/stack.i
make ctl/string.i
make ctl/u8string.i
make ctl/vector.i
make ctl/array.i
make ctl/map.i
Expand All @@ -301,7 +303,7 @@ STL variants of multi-sets and multi-maps will not be implemented because
similar behaviour can be implemented as an amalgamation of a `set` and `list`.
See `tests/func/test_container_composing.cc`

UTF-8 strings and identifiers will be added eventually, Wide, UTF-16 or UTF-32
UTF-8 strings and identifiers are in work. Wide, UTF-16 or UTF-32
not. Parallel variants of all containers and algos in `pctl` with openmp are in
planning.

Expand Down Expand Up @@ -359,10 +361,10 @@ And in its grandiosity (esp. not header-only):

## Base Implementation Details


array.h: stack/heap allocated
vector.h: realloc
string.h: vector.h
u8string.h: vector.h ++
deque.h: realloc (paged)
queue.h: deque.h
stack.h: deque.h
Expand Down Expand Up @@ -739,6 +741,8 @@ Support not only GNU make, but also BSD make and MSVC nmake.
Tested also on macOS (default apple clang++ with libc++), FreeBSD (default
clang with libc++), and Windows MSVC (default CL 19).

Added u8string with proper utf-8/unicode and identifier-security support. (u8ident)

### Differences to the STL

Our iterators are safe and fat with an `end` range. We supprt the equivalent of
Expand Down
1 change: 1 addition & 0 deletions ctl/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,5 @@ static inline I JOIN(A, insert_or_assign_found)(A *self, T key, int *foundp)
#undef B
#undef I
#undef GI
#undef CTL_SET
#undef CTL_MAP
2 changes: 1 addition & 1 deletion ctl/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,7 @@ static inline bool JOIN(A, find_first_of_range)(I *range1, GI *range2)
#endif

#ifndef HOLD
#undef CTL_SET
#undef POD
#undef NOT_INTEGRAL
#undef T
Expand All @@ -1231,7 +1232,6 @@ static inline bool JOIN(A, find_first_of_range)(I *range1, GI *range2)
#else
#undef HOLD
#endif
#undef CTL_SET

#ifdef USE_INTERNAL_VERIFY
#undef USE_INTERNAL_VERIFY
Expand Down
86 changes: 43 additions & 43 deletions ctl/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#ifndef __CTL_STRING__H__
#define __CTL_STRING__H__

/* Should *string derive from string.h?
Rather from some base_string.h
*/
#ifdef T
#error "Template type T defined for <ctl/string.h>"
#endif
Expand Down Expand Up @@ -38,7 +41,6 @@
#undef str_find
#undef str_begin
#undef str_end
#undef vec_char

#include <stdint.h>
#include <string.h>
Expand All @@ -64,9 +66,13 @@ static inline int str_char_equal(char *a, char *b)
return *a == *b;
}

static inline str str_init(const char *c_str)
/* STL clash */
#define str_equal(a, b) (str_compare(a, b) == 0)
#define str_key_equal(a, b) (str_key_compare(a, b) == 0)

static inline A JOIN(A, init)(const T* c_str)
{
str self = str___INIT();
A self = str___INIT();
size_t len = strlen(c_str);
#ifndef _LIBCPP_STD_VER
size_t min = 15;
Expand All @@ -81,7 +87,7 @@ static inline str str_init(const char *c_str)
return self;
}

static inline void str_append(str *self, const char *s)
static inline void JOIN(A, append)(A *self, const T *s)
{
size_t start = self->size;
size_t len = strlen(s);
Expand All @@ -90,7 +96,7 @@ static inline void str_append(str *self, const char *s)
self->vector[start + i] = s[i];
}

static inline void str_insert_str(str *self, size_t index, const char *s)
static inline void JOIN(A, insert_str)(A *self, size_t index, const T *s)
{
size_t start = self->size;
size_t len = strlen(s);
Expand All @@ -103,7 +109,7 @@ static inline void str_insert_str(str *self, size_t index, const char *s)
}
}

static inline void str_replace(str *self, size_t index, size_t size, const char *s)
static inline void JOIN(A, replace)(A *self, size_t index, size_t size, const T *s)
{
size_t end = index + size;
if (end >= self->size)
Expand All @@ -113,21 +119,21 @@ static inline void str_replace(str *self, size_t index, size_t size, const char
str_insert_str(self, index, s);
}

static inline char *str_c_str(str *self)
static inline T *JOIN(A, c_str)(A *self)
{
return str_data(self);
}

static inline size_t str_find(str *self, const char *s)
static inline size_t JOIN(A, find)(A *self, const T *s)
{
char *c_str = self->vector;
char *found = strstr(c_str, s);
T *c_str = self->vector;
T *found = strstr(c_str, s);
if (found)
return found - c_str;
return SIZE_MAX;
}

static inline int str_count(str *self, char c)
static inline int JOIN(A, count)(A *self, T c)
{
size_t count = 0;
for (size_t i = 0; i < self->size; i++)
Expand All @@ -136,12 +142,12 @@ static inline int str_count(str *self, char c)
return count;
}

static inline size_t str_rfind(str *self, const char *s)
static inline size_t JOIN(A, rfind)(A *self, const T *s)
{
char *c_str = self->vector;
T *c_str = self->vector;
for (size_t i = self->size; i != SIZE_MAX; i--)
{
char *found = strstr(&c_str[i], s);
T *found = strstr(&c_str[i], s);
if (found)
return found - c_str;
}
Expand Down Expand Up @@ -191,35 +197,36 @@ static inline bool str_find_first_of_range(str_it *range1, str_it *range2)
}

// see algorithm.h for the range variant
static inline size_t str_find_first_of(str *self, const char *s)
static inline size_t JOIN(A, find_first_of)(A *self, const T *s)
{
#if 1
size_t i = strcspn(self->vector, s);
return i >= self->size ? SIZE_MAX : i;
#else
for (size_t i = 0; i < self->size; i++)
for (const char *p = s; *p; p++)
for (const T *p = s; *p; p++)
if (self->vector[i] == *p)
return i;
return SIZE_MAX;
#endif
}

static inline size_t str_find_last_of(str *self, const char *s)
// TODO: proper string search
static inline size_t JOIN(A, find_last_of)(A *self, const T *s)
{
for (size_t i = self->size; i != SIZE_MAX; i--)
for (const char *p = s; *p; p++)
for (const T *p = s; *p; p++)
if (self->vector[i] == *p)
return i;
return SIZE_MAX;
}

static inline size_t str_find_first_not_of(str *self, const char *s)
static inline size_t JOIN(A, find_first_not_of)(A *self, const T *s)
{
for (size_t i = 0; i < self->size; i++)
{
size_t count = 0;
for (const char *p = s; *p; p++)
for (const T *p = s; *p; p++)
if (self->vector[i] == *p)
count++;
if (count == 0)
Expand All @@ -228,12 +235,12 @@ static inline size_t str_find_first_not_of(str *self, const char *s)
return SIZE_MAX;
}

static inline size_t str_find_last_not_of(str *self, const char *s)
static inline size_t JOIN(A, find_last_not_of)(A *self, const T *s)
{
for (size_t i = self->size - 1; i != SIZE_MAX; i--)
{
size_t count = 0;
for (const char *p = s; *p; p++)
for (const T *p = s; *p; p++)
if (self->vector[i] == *p)
count++;
if (count == 0)
Expand All @@ -242,49 +249,42 @@ static inline size_t str_find_last_not_of(str *self, const char *s)
return SIZE_MAX;
}

static inline str str_substr(str *self, size_t index, size_t size)
static inline str JOIN(A, substr)(A *self, size_t index, size_t size)
{
str substr = str_init("");
#ifndef _LIBCPP_STD_VER // gcc shrinks, llvm not
#endif
str_resize(&substr, size, '\0');
str substr = JOIN(A, init)("");
JOIN(A, resize)(&substr, size, '\0');
for (size_t i = 0; i < size; i++)
substr.vector[i] = self->vector[index + i];
return substr;
}

/* STL clash */
static inline int str_compare(str *self, const char *s)
static inline int JOIN(A, key_compare)(A *self, const T *s)
{
return strcmp(self->vector, s);
}

/* STL clash
static inline int
str_equal(str* self, const char* s)
{
return str_compare(self, other) == 0;
}
*/

static inline int str_key_compare(str *self, str *other)
static inline int JOIN(A, compare)(A *self, A *other)
{
return strcmp(self->vector, other->vector);
}

static inline int str_equal(str *self, str *other)
{
return strcmp(self->vector, other->vector) == 0;
}
//static inline int
//JOIN(A, equal)(str* self, str* other)
//{
// return strcmp (self->vector, other->vector) == 0;
//}

#undef POD
#ifndef HOLD
#undef vec_char
#undef MUST_ALIGN_16
#undef A
#undef I
#undef T
#else
#undef HOLD
#endif
#undef CTL_STR
#undef POD

#else

Expand Down
Loading

0 comments on commit 7f30291

Please sign in to comment.