diff options
| author | Tyge Løvset <[email protected]> | 2022-01-11 00:22:01 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-01-11 00:22:01 +0100 |
| commit | 14c0c1b9ab2ce6a4f98baf389d712515b7c68933 (patch) | |
| tree | 07a3f42e6aa7e806dcbfa899b6f71fd1d6ca5600 /include/stc | |
| parent | b1c62797f6344eaea6cb55959c9f438481359c75 (diff) | |
| download | STC-modified-14c0c1b9ab2ce6a4f98baf389d712515b7c68933.tar.gz STC-modified-14c0c1b9ab2ce6a4f98baf389d712515b7c68933.zip | |
Some updates on Regex. Will remove it for now, as it is functionally unstable.
Diffstat (limited to 'include/stc')
| -rw-r--r-- | include/stc/alt/cstr.h | 10 | ||||
| -rw-r--r-- | include/stc/utf8.h | 50 |
2 files changed, 30 insertions, 30 deletions
diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h index 2271be7c..adad3e3d 100644 --- a/include/stc/alt/cstr.h +++ b/include/stc/alt/cstr.h @@ -24,13 +24,15 @@ /* A string type with short string optimization in C99 with optimal short string
* utilization (23 characters with 24 bytes string representation).
*/
-#ifndef CSTR_INCLUDED
-#define CSTR_INCLUDED
+#ifndef CSTR_H_INCLUDED
+#define CSTR_H_INCLUDED
#include <stc/ccommon.h>
-#include <stdlib.h>
+#include <stdlib.h> /* malloc */
#include <string.h>
-#include <stdio.h>
+#include <stdarg.h>
+#include <stdio.h> /* vsnprintf */
+#include <ctype.h>
typedef struct { char* data; size_t size, cap; } _cstr_rep_t;
diff --git a/include/stc/utf8.h b/include/stc/utf8.h index 540ade39..d11bcc43 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -9,18 +9,16 @@ enum utf8_state { utf8_REJECT = 12
};
+typedef struct { bool valid; size_t size; } utf8_result;
+/* number of codepoints in the utf8 string s, or SIZE_MAX if invalid utf8: */
+STC_API size_t utf8_codepoint_count(const char *s);
+STC_API size_t utf8_codepoint_count_n(const char *s, size_t n);
+/* decode next utf8 codepoint. */
STC_API uint32_t utf8_decode(uint32_t *state, uint32_t *codep, const uint32_t byte);
-STC_API bool utf8_valid_codepoints(const uint8_t *s, size_t *count);
-
-STC_INLINE bool utf8_is_valid(const char *s)
-{
- size_t count;
- return utf8_valid_codepoints((const uint8_t *)s, &count);
-}
STC_INLINE uint32_t utf8_peek(const char *s)
{
- uint32_t state = utf8_ACCEPT, codepoint;
+ uint32_t state = 0, codepoint;
utf8_decode(&state, &codepoint, (uint8_t)s[0]);
return codepoint;
}
@@ -44,16 +42,6 @@ STC_INLINE const char *utf8_next(const char *s) return (const char *)p;
}
-// assumes input is valid utf8! Use utf8_valid_codepoints() if unsure.
-STC_INLINE size_t utf8_size(const char *s)
-{
- size_t count = 0;
- while (*s)
- s += utf8_codepoint_width((uint8_t)*s), ++count;
- return count;
-}
-
-
// --------------------------- IMPLEMENTATION ---------------------------------
#ifdef _i_implement
@@ -74,10 +62,10 @@ static const uint8_t utf8_table[] = { };
STC_DEF uint32_t utf8_decode(uint32_t *state, uint32_t *codep,
- const uint32_t byte)
+ const uint32_t byte)
{
const uint32_t type = utf8_table[byte];
- const uint32_t x = (uint32_t) -(*state != utf8_ACCEPT);
+ const uint32_t x = (uint32_t) -(*state != 0);
*codep = (x & ((byte & 0x3fu) | (*codep << 6)))
| (~x & ((0xff >> type) & byte));
@@ -85,13 +73,23 @@ STC_DEF uint32_t utf8_decode(uint32_t *state, uint32_t *codep, return *state = utf8_table[256 + *state + type];
}
-STC_DEF bool utf8_valid_codepoints(const uint8_t *s, size_t *count)
+
+STC_DEF size_t utf8_codepoint_count(const char *s)
{
- uint32_t state = utf8_ACCEPT, codepoint;
-
- for (*count = 0; *s; ++s)
- *count += utf8_decode(&state, &codepoint, *s) == utf8_ACCEPT;
- return state == utf8_ACCEPT;
+ uint32_t state = 0, codepoint;
+ size_t size = 0;
+ while (*s)
+ size += !utf8_decode(&state, &codepoint, (uint8_t)*s++);
+ return size | (size_t) -(state != 0);
+}
+
+STC_DEF size_t utf8_codepoint_count_n(const char *s, size_t n)
+{
+ uint32_t state = 0, codepoint;
+ size_t size = 0;
+ while (n--)
+ size += !utf8_decode(&state, &codepoint, (uint8_t)*s++);
+ return size | (size_t) -(state != 0);
}
#endif
|
