Let wuffs_base__utf_8__etc take ptr-to-const-u8

This commit is contained in:
Nigel Tao committed 2020-07-22 15:42:54 +10:00
1 parent 03a87ea3ef
commit 702c7b299f
7 files changed
+96 -94

No files matched your search

+39 -38
View File
@@ -4504,7 +4504,7 @@ WUFFS_BASE__MAYBE_STATIC size_t //
wuffs_base__utf_8__encode(wuffs_base__slice_u8 dst, uint32_t code_point);
// wuffs_base__utf_8__next returns the next UTF-8 code point (and that code
// point's byte length) at the start of s.
// point's byte length) at the start of the read-only slice (s_ptr, s_len).
//
// There are exactly two cases in which this function returns something where
// wuffs_base__utf_8__next__output__is_valid is false:
@@ -4517,7 +4517,7 @@ wuffs_base__utf_8__encode(wuffs_base__slice_u8 dst, uint32_t code_point);
//
// 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).
// - (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
@@ -4528,44 +4528,46 @@ wuffs_base__utf_8__encode(wuffs_base__slice_u8 dst, uint32_t code_point);
// function requires the WUFFS_CONFIG__MODULE__BASE__UTF8 sub-module, not just
// WUFFS_CONFIG__MODULE__BASE__CORE.
WUFFS_BASE__MAYBE_STATIC wuffs_base__utf_8__next__output //
wuffs_base__utf_8__next(wuffs_base__slice_u8 s);
wuffs_base__utf_8__next(const uint8_t* s_ptr, size_t s_len);
// wuffs_base__utf_8__next_from_end is like wuffs_base__utf_8__next except that
// it looks at the end of s instead of the start.
// it looks at the end of (s_ptr, s_len) instead of the start.
//
// For modular builds that divide the base module into sub-modules, using this
// function requires the WUFFS_CONFIG__MODULE__BASE__UTF8 sub-module, not just
// WUFFS_CONFIG__MODULE__BASE__CORE.
WUFFS_BASE__MAYBE_STATIC wuffs_base__utf_8__next__output //
wuffs_base__utf_8__next_from_end(wuffs_base__slice_u8 s);
wuffs_base__utf_8__next_from_end(const uint8_t* s_ptr, size_t s_len);
// wuffs_base__utf_8__longest_valid_prefix returns the largest n such that the
// sub-slice s[..n] is valid UTF-8.
// sub-slice s[..n] is valid UTF-8, where s is the read-only slice (s_ptr,
// s_len).
//
// In particular, it returns s.len if and only if all of s 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
// 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.
//
// For modular builds that divide the base module into sub-modules, using this
// function requires the WUFFS_CONFIG__MODULE__BASE__UTF8 sub-module, not just
// WUFFS_CONFIG__MODULE__BASE__CORE.
WUFFS_BASE__MAYBE_STATIC size_t //
wuffs_base__utf_8__longest_valid_prefix(wuffs_base__slice_u8 s);
wuffs_base__utf_8__longest_valid_prefix(const uint8_t* s_ptr, size_t s_len);
// wuffs_base__ascii__longest_valid_prefix returns the largest n such that the
// sub-slice s[..n] is valid ASCII.
// sub-slice s[..n] is valid ASCII, where s is the read-only slice (s_ptr,
// s_len).
//
// In particular, it returns s.len if and only if all of s 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.
//
// For modular builds that divide the base module into sub-modules, using this
// function requires the WUFFS_CONFIG__MODULE__BASE__UTF8 sub-module, not just
// WUFFS_CONFIG__MODULE__BASE__CORE.
WUFFS_BASE__MAYBE_STATIC size_t //
wuffs_base__ascii__longest_valid_prefix(wuffs_base__slice_u8 s);
wuffs_base__ascii__longest_valid_prefix(const uint8_t* s_ptr, size_t s_len);
// ---------------- Interface Declarations.
@@ -14638,20 +14640,20 @@ static const uint8_t wuffs_base__utf_8__byte_length_minus_1[256] = {
};
WUFFS_BASE__MAYBE_STATIC wuffs_base__utf_8__next__output //
wuffs_base__utf_8__next(wuffs_base__slice_u8 s) {
if (s.len == 0) {
wuffs_base__utf_8__next(const uint8_t* s_ptr, size_t s_len) {
if (s_len == 0) {
return wuffs_base__make_utf_8__next__output(0, 0);
}
uint32_t c = s.ptr[0];
uint32_t c = s_ptr[0];
switch (wuffs_base__utf_8__byte_length_minus_1[c & 0xFF]) {
case 0:
return wuffs_base__make_utf_8__next__output(c, 1);
case 1:
if (s.len < 2) {
if (s_len < 2) {
break;
}
c = wuffs_base__load_u16le__no_bounds_check(s.ptr);
c = wuffs_base__load_u16le__no_bounds_check(s_ptr);
if ((c & 0xC000) != 0x8000) {
break;
}
@@ -14659,10 +14661,10 @@ wuffs_base__utf_8__next(wuffs_base__slice_u8 s) {
return wuffs_base__make_utf_8__next__output(c, 2);
case 2:
if (s.len < 3) {
if (s_len < 3) {
break;
}
c = wuffs_base__load_u24le__no_bounds_check(s.ptr);
c = wuffs_base__load_u24le__no_bounds_check(s_ptr);
if ((c & 0xC0C000) != 0x808000) {
break;
}
@@ -14674,10 +14676,10 @@ wuffs_base__utf_8__next(wuffs_base__slice_u8 s) {
return wuffs_base__make_utf_8__next__output(c, 3);
case 3:
if (s.len < 4) {
if (s_len < 4) {
break;
}
c = wuffs_base__load_u32le__no_bounds_check(s.ptr);
c = wuffs_base__load_u32le__no_bounds_check(s_ptr);
if ((c & 0xC0C0C000) != 0x80808000) {
break;
}
@@ -14694,16 +14696,16 @@ wuffs_base__utf_8__next(wuffs_base__slice_u8 s) {
}
WUFFS_BASE__MAYBE_STATIC wuffs_base__utf_8__next__output //
wuffs_base__utf_8__next_from_end(wuffs_base__slice_u8 s) {
if (s.len == 0) {
wuffs_base__utf_8__next_from_end(const uint8_t* s_ptr, size_t s_len) {
if (s_len == 0) {
return wuffs_base__make_utf_8__next__output(0, 0);
}
uint8_t* ptr = &s.ptr[s.len - 1];
const uint8_t* ptr = &s_ptr[s_len - 1];
if (*ptr < 0x80) {
return wuffs_base__make_utf_8__next__output(*ptr, 1);
} else if (*ptr < 0xC0) {
uint8_t* too_far = &s.ptr[(s.len > 4) ? (s.len - 4) : 0];
const uint8_t* too_far = &s_ptr[(s_len > 4) ? (s_len - 4) : 0];
uint32_t n = 1;
while (ptr != too_far) {
ptr--;
@@ -14713,8 +14715,7 @@ wuffs_base__utf_8__next_from_end(wuffs_base__slice_u8 s) {
} else if (*ptr < 0xC0) {
continue;
}
wuffs_base__utf_8__next__output o =
wuffs_base__utf_8__next(wuffs_base__make_slice_u8(ptr, n));
wuffs_base__utf_8__next__output o = wuffs_base__utf_8__next(ptr, n);
if (o.byte_length != n) {
break;
}
@@ -14727,29 +14728,29 @@ wuffs_base__utf_8__next_from_end(wuffs_base__slice_u8 s) {
}
WUFFS_BASE__MAYBE_STATIC size_t //
wuffs_base__utf_8__longest_valid_prefix(wuffs_base__slice_u8 s) {
wuffs_base__utf_8__longest_valid_prefix(const uint8_t* s_ptr, size_t s_len) {
// TODO: possibly optimize the all-ASCII case (4 or 8 bytes at a time).
//
// TODO: possibly optimize this by manually inlining the
// wuffs_base__utf_8__next calls.
size_t original_len = s.len;
while (s.len > 0) {
wuffs_base__utf_8__next__output o = wuffs_base__utf_8__next(s);
size_t original_len = s_len;
while (s_len > 0) {
wuffs_base__utf_8__next__output o = wuffs_base__utf_8__next(s_ptr, s_len);
if ((o.code_point > 0x7F) && (o.byte_length == 1)) {
break;
}
s.ptr += o.byte_length;
s.len -= o.byte_length;
s_ptr += o.byte_length;
s_len -= o.byte_length;
}
return original_len - s.len;
return original_len - s_len;
}
WUFFS_BASE__MAYBE_STATIC size_t //
wuffs_base__ascii__longest_valid_prefix(wuffs_base__slice_u8 s) {
wuffs_base__ascii__longest_valid_prefix(const uint8_t* s_ptr, size_t s_len) {
// TODO: possibly optimize this by checking 4 or 8 bytes at a time.
uint8_t* original_ptr = s.ptr;
uint8_t* p = s.ptr;
uint8_t* q = s.ptr + s.len;
const uint8_t* original_ptr = s_ptr;
const uint8_t* p = s_ptr;
const uint8_t* q = s_ptr + s_len;
for (; (p != q) && ((*p & 0x80) == 0); p++) {
}
return (size_t)(p - original_ptr);