Skip to content

Commit

Permalink
fix UTF-8 tests + rename some minor things + prep string escape/uesca…
Browse files Browse the repository at this point in the history
…pe code for re-use
  • Loading branch information
boazsegev committed Oct 4, 2024
1 parent 41ca1cc commit 751157f
Show file tree
Hide file tree
Showing 8 changed files with 794 additions and 568 deletions.
679 changes: 396 additions & 283 deletions fio-stl.h

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions fio-stl.md
Original file line number Diff line number Diff line change
Expand Up @@ -5621,8 +5621,8 @@ Default reallocation callback implementation
#### `FIO_STRING_ALLOC_COPY`

```c
#define FIO_STRING_ALLOC_COPY fio_string_default_copy_and_reallocate
void fio_string_default_copy_and_reallocate(fio_str_info_s *dest, size_t new_capa);
#define FIO_STRING_ALLOC_COPY fio_string_default_allocate_copy
void fio_string_default_allocate_copy(fio_str_info_s *dest, size_t new_capa);
```

Default reallocation callback for memory that mustn't be freed.
Expand Down
22 changes: 12 additions & 10 deletions fio-stl/000 core.h
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ typedef struct fio_buf_info_s {
.buf = fio___stack_mem___##name, .capa = (capacity) \
}

/** Tests to see if memory reallocation happened. */
#define FIO_STR_INFO_TMP_IS_REALLOCATED(name) \
(fio___stack_mem___##name != name.buf)

/* *****************************************************************************
UTF-8 Support (basic)
***************************************************************************** */
Expand Down Expand Up @@ -783,27 +787,25 @@ FIO_IFUNC size_t fio_utf8_char_len_unsafe(uint8_t c) {

/** Returns the number of valid UTF-8 bytes used by first char at `str`. */
FIO_IFUNC size_t fio_utf8_char_len(const void *str_) {
size_t r, tst;
size_t r, tst = 1;
const uint8_t *s = (uint8_t *)str_;
r = fio_utf8_char_len_unsafe(*s);
r &= 7;
if (r < 2)
return r;
tst = fio_utf8_char_len_unsafe(s[1]); /* avoid overflowing more than 1 char */
tst &= fio_utf8_char_len_unsafe(s[(tst >> 3) + (r > 2)]);
tst &= fio_utf8_char_len_unsafe(s[((tst >> 3) + (r > 3)) | (tst >> 3)]);
tst &= 8;
tst -= tst >> 3;
r &= tst;
tst += (fio_utf8_char_len_unsafe(s[tst]) >> 3) & (r > 3);
tst += (fio_utf8_char_len_unsafe(s[tst]) >> 3) & (r > 2);
tst += (fio_utf8_char_len_unsafe(s[tst]) >> 3);
if (r != tst)
r = 0;
return r;
}

/** Writes code point to `dest` using UFT-8. Returns number of bytes written. */
FIO_IFUNC size_t fio_utf8_write(void *dest_, uint32_t u) {
const uint8_t len = fio_utf8_code_len(u);
uint8_t *dest = (uint8_t *)dest_;
if (!len)
return len;
if (len == 1) {
if (len < 2) { /* writes, but doesn't report on len == 0 */
*dest = u;
return len;
}
Expand Down
2 changes: 1 addition & 1 deletion fio-stl/001 memalt.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ SFUNC FIO___ASAN_AVOID size_t fio_strlen(const char *str) {
goto found_nul_byte0;
str += 8;
}
str = FIO_PTR_MATH_RMASK(const char, str, 6);
str = FIO_PTR_MATH_RMASK(const char, str, 6); /* compiler hint */
/* loop endlessly */
for (;;) {
for (size_t i = 0; i < 8; ++i) {
Expand Down
Loading

0 comments on commit 751157f

Please sign in to comment.