Skip to content

Commit

Permalink
Added src/corestc.c source file. Reorganised some code.
Browse files Browse the repository at this point in the history
  • Loading branch information
tylov committed Nov 15, 2024
1 parent 65b3ecb commit 54b6193
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 60 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,20 @@ After erasing the elements found:
## Installation
STC uses meson build system. Make sure to have meson and ninja installed, e.g. as a python pip package from a bash shell:
```bash
pip install meson ninja
export LIBRARY_PATH=$LIBRARY_PATH:~/.local/lib
export CPATH=$CPATH:~/.local/include
export CC=gcc
```
To create a build folder and to set the install folder to e.g. ~/.local:
```bash
meson setup --buildtype debug build --prefix ~/.local
cd build
ninja
ninja install
```
STC is mixed *"headers-only"* / traditional library, i.e the templated container headers (and the *sort*/*lower_bound*
algorithms) can simply be included - they have no library dependencies. By default, all templated functions are
static (many inlined). This is often optimal for both performance and compiled binary size. However, for frequently
Expand Down
43 changes: 26 additions & 17 deletions include/stc/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,28 +230,20 @@ typedef const char* cstr_raw;
// General functions

// hashing
size_t c_basehash_n(const void* key, isize len);

STC_INLINE size_t c_hash_n(const void* key, isize len) {
union { size_t block; uint64_t b8; uint32_t b4; } u = {0};
uint64_t b8; uint32_t b4;
switch (len) {
case 8: memcpy(&u.b8, key, 8); return (size_t)(u.b8 * 0xc6a4a7935bd1e99d);
case 4: memcpy(&u.b4, key, 4); return u.b4 * (size_t)0xa2ffeb2f01000193;
case 8: memcpy(&b8, key, 8); return (size_t)(b8 * 0xc6a4a7935bd1e99d);
case 4: memcpy(&b4, key, 4); return b4 * (size_t)0xa2ffeb2f01000193;
case 0: return 0x811c9dc5;
}
size_t hash = 0x811c9dc5;
const uint8_t* msg = (const uint8_t*)key;
while (len >= c_sizeof(size_t)) {
memcpy(&u.block, msg, sizeof(size_t));
hash = (hash ^ u.block) * (size_t)0x89bb179901000193;
msg += c_sizeof(size_t);
len -= c_sizeof(size_t);
}
c_memcpy(&u.block, msg, len);
hash = (hash ^ u.block) * (size_t)0xb0340f4501000193;
return hash ^ (hash >> 3);
return c_basehash_n(key, len);
}

STC_INLINE size_t c_hash_str(const char *str)
{ return c_hash_n(str, c_strlen(str)); }
{ return c_basehash_n(str, c_strlen(str)); }

STC_INLINE size_t _chash_mix(size_t h[], int n) {
for (int i = 1; i < n; ++i) h[0] += h[0] ^ h[i];
Expand Down Expand Up @@ -283,8 +275,11 @@ STC_INLINE isize c_next_pow2(isize n) {
}

// substring in substring?
STC_INLINE char* c_strnstrn(const char *str, isize slen,
const char *needle, isize nlen) {
char* c_strnstrn(const char *str, isize slen, const char *needle, isize nlen);

#if defined STC_IMPLEMENT_COMMON

char* c_strnstrn(const char *str, isize slen, const char *needle, isize nlen) {
if (nlen == 0) return (char *)str;
if (nlen > slen) return NULL;
slen -= nlen;
Expand All @@ -296,4 +291,18 @@ STC_INLINE char* c_strnstrn(const char *str, isize slen,
return NULL;
}

size_t c_basehash_n(const void* key, isize len) {
size_t block, hash = 0x811c9dc5;
const uint8_t* msg = (const uint8_t*)key;
while (len >= c_sizeof(size_t)) {
memcpy(&block, msg, sizeof(size_t));
hash = (hash ^ block) * (size_t)0x89bb179901000193;
msg += c_sizeof(size_t);
len -= c_sizeof(size_t);
}
c_memcpy(&block, msg, len);
hash = (hash ^ block) * (size_t)0xb0340f4501000193;
return hash ^ (hash >> 3);
}
#endif
#endif // STC_COMMON_H_INCLUDED
2 changes: 1 addition & 1 deletion include/stc/coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ int cco_taskrunner(struct cco_taskrunner* co) {
}
cco_cleanup:
if (rt->error_code != 0) {
fprintf(stderr, __FILE__ "(%d): error: unhandled error '%d' in a coroutine task at line %d\n",
fprintf(stderr, __FILE__ ":%d: error: unhandled error '%d' in a coroutine task at line %d.\n",
__LINE__, rt->error_code, rt->error_line);
}
}
Expand Down
52 changes: 23 additions & 29 deletions include/stc/csview.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define i_header // external linkage by default. override with i_static.
#include "priv/linkage.h"

// csview is a non-zero-terminated string view.

#ifndef STC_CSVIEW_H_INCLUDED
Expand All @@ -36,15 +33,13 @@
#define csview_drop(p) c_default_drop(p)
#define csview_clone(sv) c_default_clone(sv)

STC_API csview_iter csview_advance(csview_iter it, isize u8pos);
STC_API isize csview_find_sv(csview sv, csview search);
STC_API size_t csview_hash(const csview *self);
STC_API csview csview_slice_ex(csview sv, isize p1, isize p2);
STC_API csview csview_subview_ex(csview sv, isize pos, isize n);
STC_API csview csview_token(csview sv, const char* sep, isize* pos);
STC_API csview csview_u8_subview(csview sv, isize u8pos, isize u8len);
STC_API csview csview_u8_tail(csview sv, isize u8len);
STC_API csview csview_u8_chr(csview sv, isize u8pos);
csview_iter csview_advance(csview_iter it, isize u8pos);
csview csview_slice_ex(csview sv, isize p1, isize p2);
csview csview_subview_ex(csview sv, isize pos, isize n);
csview csview_token(csview sv, const char* sep, isize* pos);
csview csview_u8_subview(csview sv, isize u8pos, isize u8len);
csview csview_u8_tail(csview sv, isize u8len);
csview csview_u8_chr(csview sv, isize u8pos);

STC_INLINE csview csview_from(const char* str)
{ return c_literal(csview){str, c_strlen(str)}; }
Expand All @@ -61,6 +56,14 @@ STC_INLINE bool csview_equals_sv(csview sv1, csview sv2)
STC_INLINE bool csview_equals(csview sv, const char* str)
{ return csview_equals_sv(sv, c_sv_2(str, c_strlen(str))); }

STC_INLINE size_t csview_hash(const csview *self)
{ return c_basehash_n(self->buf, self->size); }

STC_INLINE isize csview_find_sv(csview sv, csview search) {
char* res = c_strnstrn(sv.buf, sv.size, search.buf, search.size);
return res ? (res - sv.buf) : c_NPOS;
}

STC_INLINE isize csview_find(csview sv, const char* str)
{ return csview_find_sv(sv, c_sv_2(str, c_strlen(str))); }

Expand Down Expand Up @@ -176,14 +179,11 @@ STC_INLINE bool csview_iends_with(csview sv, const char* str) {
#endif // STC_CSVIEW_H_INCLUDED

/* -------------------------- IMPLEMENTATION ------------------------- */
#if defined i_implement || defined i_static
#if defined STC_IMPLEMENT || defined i_implement
#ifndef STC_CSVIEW_C_INCLUDED
#define STC_CSVIEW_C_INCLUDED

STC_DEF size_t csview_hash(const csview *self)
{ return c_hash_n(self->buf, self->size); }

STC_DEF csview_iter csview_advance(csview_iter it, isize u8pos) {
csview_iter csview_advance(csview_iter it, isize u8pos) {
int inc = 1;
if (u8pos < 0) u8pos = -u8pos, inc = -1;
while (u8pos && it.ref != it.u8.end)
Expand All @@ -193,12 +193,7 @@ STC_DEF csview_iter csview_advance(csview_iter it, isize u8pos) {
return it;
}

STC_DEF isize csview_find_sv(csview sv, csview search) {
char* res = c_strnstrn(sv.buf, sv.size, search.buf, search.size);
return res ? (res - sv.buf) : c_NPOS;
}

STC_DEF csview csview_subview_ex(csview sv, isize pos, isize n) {
csview csview_subview_ex(csview sv, isize pos, isize n) {
if (pos < 0) {
pos += sv.size;
if (pos < 0) pos = 0;
Expand All @@ -209,7 +204,7 @@ STC_DEF csview csview_subview_ex(csview sv, isize pos, isize n) {
return sv;
}

STC_DEF csview csview_slice_ex(csview sv, isize p1, isize p2) {
csview csview_slice_ex(csview sv, isize p1, isize p2) {
if (p1 < 0) {
p1 += sv.size;
if (p1 < 0) p1 = 0;
Expand All @@ -220,7 +215,7 @@ STC_DEF csview csview_slice_ex(csview sv, isize p1, isize p2) {
return sv;
}

STC_DEF csview csview_token(csview sv, const char* sep, isize* pos) {
csview csview_token(csview sv, const char* sep, isize* pos) {
isize sep_size = c_strlen(sep);
csview slice = {sv.buf + *pos, sv.size - *pos};
const char* res = c_strnstrn(slice.buf, slice.size, sep, sep_size);
Expand All @@ -229,7 +224,7 @@ STC_DEF csview csview_token(csview sv, const char* sep, isize* pos) {
return tok;
}

STC_DEF csview csview_u8_subview(csview sv, isize u8pos, isize u8len) {
csview csview_u8_subview(csview sv, isize u8pos, isize u8len) {
const char* s, *end = &sv.buf[sv.size];
while ((u8pos > 0) & (sv.buf != end))
u8pos -= (*++sv.buf & 0xC0) != 0x80;
Expand All @@ -239,15 +234,15 @@ STC_DEF csview csview_u8_subview(csview sv, isize u8pos, isize u8len) {
sv.size = s - sv.buf; return sv;
}

STC_DEF csview csview_u8_tail(csview sv, isize u8len) {
csview csview_u8_tail(csview sv, isize u8len) {
const char* p = &sv.buf[sv.size];
while (u8len && p != sv.buf)
u8len -= (*--p & 0xC0) != 0x80;
sv.size -= p - sv.buf, sv.buf = p;
return sv;
}

STC_DEF csview csview_u8_chr(csview sv, isize u8pos) {
csview csview_u8_chr(csview sv, isize u8pos) {
const char *end = &sv.buf[sv.size];
while ((u8pos > 0) & (sv.buf != end))
u8pos -= (*++sv.buf & 0xC0) != 0x80;
Expand All @@ -261,4 +256,3 @@ STC_DEF csview csview_u8_chr(csview sv, isize u8pos) {
#if defined i_import
#include "priv/utf8_prv.c"
#endif
#include "priv/linkage2.h"
3 changes: 0 additions & 3 deletions include/stc/zsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define i_header // external linkage by default. override with i_static.
#include "priv/linkage.h"

// zsview is a zero-terminated string view.

Expand Down Expand Up @@ -176,4 +174,3 @@ STC_INLINE bool zsview_iends_with(zsview zs, const char* str) {
#if defined i_import
#include "priv/utf8_prv.c"
#endif
#include "priv/linkage2.h"
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ libsrc = files(
'src/csview.c',
'src/fmt.c',
'src/random.c',
'src/corestc.c',
)

inc = include_directories('include')
Expand Down
10 changes: 0 additions & 10 deletions meson_build.sh

This file was deleted.

3 changes: 3 additions & 0 deletions src/corestc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define STC_IMPLEMENT_COMMON
#define STC_IMPLEMENT
#include "../include/stc/coroutine.h"

0 comments on commit 54b6193

Please sign in to comment.