Add wuffs_base__utf_8__next

This commit is contained in:
Nigel Tao committed 2020-02-27 14:41:45 +11:00
1 parent 1ad5cfac4d
commit 809220e6cc
7 files changed
+676 -3

No files matched your search

+123
View File
@@ -52,3 +52,126 @@ wuffs_base__parse_number_i64(wuffs_base__slice_u8 s);
// "__0D_1_002" would successfully parse as "one thousand and two".
wuffs_base__result_u64 //
wuffs_base__parse_number_u64(wuffs_base__slice_u8 s);
// ---------------- Unicode and UTF-8
#define WUFFS_BASE__UNICODE_CODE_POINT__MIN_INCL 0x00000000
#define WUFFS_BASE__UNICODE_CODE_POINT__MAX_INCL 0x0010FFFF
#define WUFFS_BASE__UNICODE_REPLACEMENT_CHARACTER 0x0000FFFD
#define WUFFS_BASE__UNICODE_SURROGATE__MIN_INCL 0x0000D800
#define WUFFS_BASE__UNICODE_SURROGATE__MAX_INCL 0x0000DFFF
#define WUFFS_BASE__ASCII__MIN_INCL 0x00
#define WUFFS_BASE__ASCII__MAX_INCL 0x7F
#define WUFFS_BASE__UTF_8__BYTE_LENGTH__MIN_INCL 1
#define WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL 4
#define WUFFS_BASE__UTF_8__BYTE_LENGTH_1__CODE_POINT__MIN_INCL 0x00000000
#define WUFFS_BASE__UTF_8__BYTE_LENGTH_1__CODE_POINT__MAX_INCL 0x0000007F
#define WUFFS_BASE__UTF_8__BYTE_LENGTH_2__CODE_POINT__MIN_INCL 0x00000080
#define WUFFS_BASE__UTF_8__BYTE_LENGTH_2__CODE_POINT__MAX_INCL 0x000007FF
#define WUFFS_BASE__UTF_8__BYTE_LENGTH_3__CODE_POINT__MIN_INCL 0x00000800
#define WUFFS_BASE__UTF_8__BYTE_LENGTH_3__CODE_POINT__MAX_INCL 0x0000FFFF
#define WUFFS_BASE__UTF_8__BYTE_LENGTH_4__CODE_POINT__MIN_INCL 0x00010000
#define WUFFS_BASE__UTF_8__BYTE_LENGTH_4__CODE_POINT__MAX_INCL 0x0010FFFF
// --------
// wuffs_base__utf_8__next__output is the type returned by
// wuffs_base__utf_8__next.
typedef struct {
uint32_t code_point;
uint32_t byte_length;
#ifdef __cplusplus
inline bool is_valid() const;
#endif // __cplusplus
} wuffs_base__utf_8__next__output;
static inline wuffs_base__utf_8__next__output //
wuffs_base__make_utf_8__next__output(uint32_t code_point,
uint32_t byte_length) {
wuffs_base__utf_8__next__output ret;
ret.code_point = code_point;
ret.byte_length = byte_length;
return ret;
}
static inline bool //
wuffs_base__utf_8__next__output__is_valid(
const wuffs_base__utf_8__next__output* o) {
if (o) {
uint32_t cp = o->code_point;
switch (o->byte_length) {
case 1:
return (cp <= 0x7F);
case 2:
return (0x080 <= cp) && (cp <= 0x7FF);
case 3:
// Avoid the 0xD800 ..= 0xDFFF surrogate range.
return ((0x0800 <= cp) && (cp <= 0xD7FF)) ||
((0xE000 <= cp) && (cp <= 0xFFFF));
case 4:
return (0x00010000 <= cp) && (cp <= 0x0010FFFF);
}
}
return false;
}
#ifdef __cplusplus
inline bool //
wuffs_base__utf_8__next__output::is_valid() const {
return wuffs_base__utf_8__next__output__is_valid(this);
}
#endif // __cplusplus
// --------
// wuffs_base__utf_8__next returns the next UTF-8 code point (and that code
// point's byte length) at the start of s.
//
// There are exactly two cases in which this function returns something where
// wuffs_base__utf_8__next__output__is_valid is false:
// - If s is empty then it returns {.code_point=0, .byte_length=0}.
// - If s is non-empty and starts with invalid UTF-8 then it returns
// {.code_point=WUFFS_BASE__UNICODE_REPLACEMENT_CHARACTER, .byte_length=1}.
//
// Otherwise, it returns something where
// wuffs_base__utf_8__next__output__is_valid is true.
//
// In any case, it always returns an output that satisfies both of:
// - (output.code_point <= WUFFS_BASE__UNICODE_CODE_POINT__MAX_INCL).
// - (output.byte_length <= s.len).
//
// If s is a sub-slice of a larger slice of valid UTF-8, but that sub-slice
// boundary occurs in the middle of a multi-byte UTF-8 encoding of a single
// code point, then this function may return something invalid. It is the
// caller's responsibility to split on or otherwise manage UTF-8 boundaries.
wuffs_base__utf_8__next__output //
wuffs_base__utf_8__next(wuffs_base__slice_u8 s);
// wuffs_base__utf_8__longest_valid_prefix returns the largest n such that the
// sub-slice s[..n] is valid UTF-8.
//
// In particular, it returns s.len if and only if all of s is valid UTF-8.
//
// If s is a sub-slice of a larger slice of valid UTF-8, but that sub-slice
// boundary occurs in the middle of a multi-byte UTF-8 encoding of a single
// code point, then this function will return less than s.len. It is the
// caller's responsibility to split on or otherwise manage UTF-8 boundaries.
size_t //
wuffs_base__utf_8__longest_valid_prefix(wuffs_base__slice_u8 s);
// wuffs_base__ascii__longest_valid_prefix returns the largest n such that the
// sub-slice s[..n] is valid ASCII.
//
// In particular, it returns s.len if and only if all of s is valid ASCII.
// Equivalently, when none of the bytes in s have the 0x80 high bit set.
size_t //
wuffs_base__ascii__longest_valid_prefix(wuffs_base__slice_u8 s);