@ohos-ports/bare-buffer 3.7.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +26 -0
- package/binding.c +1077 -0
- package/binding.gyp +32 -0
- package/binding.js +1 -0
- package/build/Release/bare_buffer.node +0 -0
- package/deps/libbase64/.clang-format +10 -0
- package/deps/libbase64/.gitattributes +1 -0
- package/deps/libbase64/.github/workflows/integrate.yml +45 -0
- package/deps/libbase64/.github/workflows/nightly.yml +28 -0
- package/deps/libbase64/.prettierrc +1 -0
- package/deps/libbase64/CMakeLists.txt +71 -0
- package/deps/libbase64/LICENSE +201 -0
- package/deps/libbase64/NOTICE +13 -0
- package/deps/libbase64/README.md +11 -0
- package/deps/libbase64/include/base64.h +323 -0
- package/deps/libbase64/package.json +25 -0
- package/deps/libbase64/src/base64.c +22 -0
- package/deps/libbase64/test/CMakeLists.txt +31 -0
- package/deps/libbase64/test/decode-invalid.c +35 -0
- package/deps/libbase64/test/decode-length.c +23 -0
- package/deps/libbase64/test/decode.c +36 -0
- package/deps/libbase64/test/encode-length.c +22 -0
- package/deps/libbase64/test/encode.c +42 -0
- package/deps/libbase64/test/fuzz/CMakeLists.txt +18 -0
- package/deps/libbase64/test/fuzz/decode/utf16le.c +53 -0
- package/deps/libbase64/test/fuzz/decode/utf8.c +47 -0
- package/deps/libbase64/test/fuzz/encode/utf16le.c +56 -0
- package/deps/libbase64/test/fuzz/encode/utf8.c +56 -0
- package/deps/libhex/.clang-format +10 -0
- package/deps/libhex/.gitattributes +1 -0
- package/deps/libhex/.github/workflows/integrate.yml +45 -0
- package/deps/libhex/.github/workflows/nightly.yml +28 -0
- package/deps/libhex/.prettierrc +1 -0
- package/deps/libhex/CMakeLists.txt +72 -0
- package/deps/libhex/LICENSE +201 -0
- package/deps/libhex/NOTICE +13 -0
- package/deps/libhex/README.md +11 -0
- package/deps/libhex/include/hex.h +163 -0
- package/deps/libhex/package.json +25 -0
- package/deps/libhex/src/hex.c +16 -0
- package/deps/libhex/test/CMakeLists.txt +31 -0
- package/deps/libhex/test/decode-invalid.c +37 -0
- package/deps/libhex/test/decode-length.c +23 -0
- package/deps/libhex/test/decode.c +25 -0
- package/deps/libhex/test/encode-length.c +22 -0
- package/deps/libhex/test/encode.c +25 -0
- package/deps/libhex/test/fuzz/CMakeLists.txt +18 -0
- package/deps/libhex/test/fuzz/decode/utf16le.c +53 -0
- package/deps/libhex/test/fuzz/decode/utf8.c +47 -0
- package/deps/libhex/test/fuzz/encode/utf16le.c +36 -0
- package/deps/libhex/test/fuzz/encode/utf8.c +36 -0
- package/deps/libutf/.clang-format +10 -0
- package/deps/libutf/.gitattributes +1 -0
- package/deps/libutf/.github/workflows/integrate.yml +45 -0
- package/deps/libutf/.github/workflows/nightly.yml +28 -0
- package/deps/libutf/.prettierrc +1 -0
- package/deps/libutf/CMakeLists.txt +105 -0
- package/deps/libutf/LICENSE +201 -0
- package/deps/libutf/NOTICE +13 -0
- package/deps/libutf/README.md +11 -0
- package/deps/libutf/include/utf/string.h +1655 -0
- package/deps/libutf/include/utf.h +132 -0
- package/deps/libutf/package.json +24 -0
- package/deps/libutf/src/ascii/validate.c +47 -0
- package/deps/libutf/src/endianness.c +19 -0
- package/deps/libutf/src/endianness.h +54 -0
- package/deps/libutf/src/latin1/convert-to-utf16.c +37 -0
- package/deps/libutf/src/latin1/convert-to-utf32.c +34 -0
- package/deps/libutf/src/latin1/convert-to-utf8.c +58 -0
- package/deps/libutf/src/latin1/length-from-utf16.c +26 -0
- package/deps/libutf/src/latin1/length-from-utf32.c +26 -0
- package/deps/libutf/src/latin1/length-from-utf8.c +34 -0
- package/deps/libutf/src/utf16/convert-to-latin1.c +41 -0
- package/deps/libutf/src/utf16/convert-to-utf32.c +56 -0
- package/deps/libutf/src/utf16/convert-to-utf8.c +80 -0
- package/deps/libutf/src/utf16/length-from-latin1.c +26 -0
- package/deps/libutf/src/utf16/length-from-utf32.c +33 -0
- package/deps/libutf/src/utf16/length-from-utf8.c +38 -0
- package/deps/libutf/src/utf16/validate.c +53 -0
- package/deps/libutf/src/utf32/convert-to-latin1.c +40 -0
- package/deps/libutf/src/utf32/convert-to-utf16.c +56 -0
- package/deps/libutf/src/utf32/convert-to-utf8.c +71 -0
- package/deps/libutf/src/utf32/length-from-latin1.c +26 -0
- package/deps/libutf/src/utf32/length-from-utf16.c +35 -0
- package/deps/libutf/src/utf32/length-from-utf8.c +35 -0
- package/deps/libutf/src/utf32/string.c +149 -0
- package/deps/libutf/src/utf32/validate.c +39 -0
- package/deps/libutf/src/utf8/convert-to-latin1.c +70 -0
- package/deps/libutf/src/utf8/convert-to-utf16.c +95 -0
- package/deps/libutf/src/utf8/convert-to-utf32.c +110 -0
- package/deps/libutf/src/utf8/length-from-latin1.c +32 -0
- package/deps/libutf/src/utf8/length-from-utf16.c +48 -0
- package/deps/libutf/src/utf8/length-from-utf32.c +35 -0
- package/deps/libutf/src/utf8/string.c +149 -0
- package/deps/libutf/src/utf8/validate.c +107 -0
- package/deps/libutf/test/CMakeLists.txt +56 -0
- package/deps/libutf/test/fuzz/CMakeLists.txt +26 -0
- package/deps/libutf/test/fuzz/corpus/.gitignore +2 -0
- package/deps/libutf/test/fuzz/crashes/.gitignore +2 -0
- package/deps/libutf/test/fuzz/latin1/convert-to-utf16.c +32 -0
- package/deps/libutf/test/fuzz/latin1/convert-to-utf32.c +32 -0
- package/deps/libutf/test/fuzz/latin1/convert-to-utf8.c +33 -0
- package/deps/libutf/test/fuzz/utf16/convert-to-latin1.c +36 -0
- package/deps/libutf/test/fuzz/utf16/convert-to-utf32.c +38 -0
- package/deps/libutf/test/fuzz/utf16/convert-to-utf8.c +38 -0
- package/deps/libutf/test/fuzz/utf32/convert-to-latin1.c +36 -0
- package/deps/libutf/test/fuzz/utf32/convert-to-utf16.c +38 -0
- package/deps/libutf/test/fuzz/utf32/convert-to-utf8.c +38 -0
- package/deps/libutf/test/fuzz/utf8/convert-to-latin1.c +33 -0
- package/deps/libutf/test/fuzz/utf8/convert-to-utf16.c +35 -0
- package/deps/libutf/test/fuzz/utf8/convert-to-utf32.c +35 -0
- package/deps/libutf/test/utf16/utf8-convert.c +45 -0
- package/deps/libutf/test/utf16/utf8-length.c +41 -0
- package/deps/libutf/test/utf16/validate-invalid.c +50 -0
- package/deps/libutf/test/utf16/validate.c +32 -0
- package/deps/libutf/test/utf32/string-append.c +74 -0
- package/deps/libutf/test/utf32/string-compare.c +108 -0
- package/deps/libutf/test/utf32/string-erase.c +79 -0
- package/deps/libutf/test/utf32/string-index.c +102 -0
- package/deps/libutf/test/utf32/string-insert.c +92 -0
- package/deps/libutf/test/utf32/string-last-index.c +71 -0
- package/deps/libutf/test/utf32/string-prepend.c +51 -0
- package/deps/libutf/test/utf32/string-replace.c +88 -0
- package/deps/libutf/test/utf32/string-reserve.c +99 -0
- package/deps/libutf/test/utf32/string-substring.c +93 -0
- package/deps/libutf/test/utf8/latin1-convert.c +23 -0
- package/deps/libutf/test/utf8/string-append.c +23 -0
- package/deps/libutf/test/utf8/string-compare.c +67 -0
- package/deps/libutf/test/utf8/string-erase.c +75 -0
- package/deps/libutf/test/utf8/string-index.c +99 -0
- package/deps/libutf/test/utf8/string-last-index.c +68 -0
- package/deps/libutf/test/utf8/string-prepend.c +23 -0
- package/deps/libutf/test/utf8/string-replace.c +74 -0
- package/deps/libutf/test/utf8/string-reserve.c +51 -0
- package/deps/libutf/test/utf8/string-substring.c +68 -0
- package/deps/libutf/test/utf8/utf16-convert.c +32 -0
- package/deps/libutf/test/utf8/utf16-length.c +29 -0
- package/deps/libutf/test/utf8/validate-invalid.c +26 -0
- package/deps/libutf/test/utf8/validate.c +21 -0
- package/global.d.ts +21 -0
- package/global.js +6 -0
- package/index.d.ts +769 -0
- package/index.js +1339 -0
- package/lib/ascii.js +36 -0
- package/lib/base64.js +25 -0
- package/lib/base64url.js +25 -0
- package/lib/checked.js +23 -0
- package/lib/constants.d.ts +7 -0
- package/lib/constants.js +3 -0
- package/lib/hex.js +20 -0
- package/lib/latin1.js +14 -0
- package/lib/utf16le.js +20 -0
- package/lib/utf8.js +18 -0
- package/package.json +64 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#include <assert.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
|
|
4
|
+
#include "../../include/utf.h"
|
|
5
|
+
#include "../../include/utf/string.h"
|
|
6
|
+
|
|
7
|
+
int
|
|
8
|
+
main() {
|
|
9
|
+
int e;
|
|
10
|
+
|
|
11
|
+
// Two empty strings are equal, and neither is compared over any code points at
|
|
12
|
+
// all.
|
|
13
|
+
{
|
|
14
|
+
utf32_string_t a, b;
|
|
15
|
+
utf32_string_init(&a);
|
|
16
|
+
utf32_string_init(&b);
|
|
17
|
+
|
|
18
|
+
assert(utf32_string_compare(&a, &b) == 0);
|
|
19
|
+
|
|
20
|
+
static const utf32_t nothing[] = {0};
|
|
21
|
+
|
|
22
|
+
utf32_string_view_t empty = utf32_string_view_init(nothing, 0);
|
|
23
|
+
|
|
24
|
+
assert(utf32_string_view_compare(empty, empty) == 0);
|
|
25
|
+
assert(utf32_string_compare_literal(&a, nothing, 0) == 0);
|
|
26
|
+
assert(utf32_string_view_compare_literal(empty, nothing, 0) == 0);
|
|
27
|
+
|
|
28
|
+
utf32_string_destroy(&a);
|
|
29
|
+
utf32_string_destroy(&b);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Ordering follows the code points, with the shorter of two strings that share
|
|
33
|
+
// a prefix coming first.
|
|
34
|
+
{
|
|
35
|
+
static const utf32_t banana[] = {'b', 'a', 'n', 'a', 'n', 'a', 0};
|
|
36
|
+
static const utf32_t banan[] = {'b', 'a', 'n', 'a', 'n', 0};
|
|
37
|
+
static const utf32_t bananas[] = {'b', 'a', 'n', 'a', 'n', 'a', 's', 0};
|
|
38
|
+
static const utf32_t apple[] = {'a', 'p', 'p', 'l', 'e', 0};
|
|
39
|
+
static const utf32_t cherry[] = {'c', 'h', 'e', 'r', 'r', 'y', 0};
|
|
40
|
+
|
|
41
|
+
utf32_string_t a;
|
|
42
|
+
utf32_string_init(&a);
|
|
43
|
+
|
|
44
|
+
e = utf32_string_append_literal(&a, banana, (size_t) -1);
|
|
45
|
+
assert(e == 0);
|
|
46
|
+
assert(a.len == 6);
|
|
47
|
+
|
|
48
|
+
assert(utf32_string_compare_literal(&a, banana, (size_t) -1) == 0);
|
|
49
|
+
assert(utf32_string_compare_literal(&a, banan, (size_t) -1) > 0);
|
|
50
|
+
assert(utf32_string_compare_literal(&a, bananas, (size_t) -1) < 0);
|
|
51
|
+
assert(utf32_string_compare_literal(&a, apple, (size_t) -1) > 0);
|
|
52
|
+
assert(utf32_string_compare_literal(&a, cherry, (size_t) -1) < 0);
|
|
53
|
+
|
|
54
|
+
utf32_string_destroy(&a);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Ordering is by the value of a code point rather than by its bytes. A
|
|
58
|
+
// `memcmp` would put U+0100 before U+00FF on a little endian platform, its
|
|
59
|
+
// first byte being the lesser of the two.
|
|
60
|
+
{
|
|
61
|
+
static const utf32_t high[] = {0x100};
|
|
62
|
+
static const utf32_t low[] = {0xff};
|
|
63
|
+
|
|
64
|
+
utf32_string_view_t a = utf32_string_view_init(high, 1);
|
|
65
|
+
utf32_string_view_t b = utf32_string_view_init(low, 1);
|
|
66
|
+
|
|
67
|
+
assert(utf32_string_view_compare(a, b) > 0);
|
|
68
|
+
assert(utf32_string_view_compare(b, a) < 0);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// The same holds above the basic multilingual plane, where a `memcmp` would
|
|
72
|
+
// put U+10000 before U+FFFF.
|
|
73
|
+
{
|
|
74
|
+
static const utf32_t supplementary[] = {0x10000};
|
|
75
|
+
static const utf32_t basic[] = {0xffff};
|
|
76
|
+
|
|
77
|
+
utf32_string_view_t a = utf32_string_view_init(supplementary, 1);
|
|
78
|
+
utf32_string_view_t b = utf32_string_view_init(basic, 1);
|
|
79
|
+
|
|
80
|
+
assert(utf32_string_view_compare(a, b) > 0);
|
|
81
|
+
assert(utf32_string_view_compare(b, a) < 0);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// A null code point is compared like any other, rather than ending the
|
|
85
|
+
// comparison as it would were these C strings.
|
|
86
|
+
{
|
|
87
|
+
static const utf32_t abc[] = {'a', 0, 'b'};
|
|
88
|
+
static const utf32_t acc[] = {'a', 0, 'c'};
|
|
89
|
+
static const utf32_t ab[] = {'a', 0};
|
|
90
|
+
|
|
91
|
+
utf32_string_view_t a = utf32_string_view_init(abc, 3);
|
|
92
|
+
utf32_string_view_t b = utf32_string_view_init(acc, 3);
|
|
93
|
+
utf32_string_view_t c = utf32_string_view_init(abc, 3);
|
|
94
|
+
|
|
95
|
+
assert(utf32_string_view_compare(a, b) < 0);
|
|
96
|
+
assert(utf32_string_view_compare(b, a) > 0);
|
|
97
|
+
assert(utf32_string_view_compare(a, c) == 0);
|
|
98
|
+
|
|
99
|
+
// The code points past a null one also tell apart strings of unequal length.
|
|
100
|
+
utf32_string_view_t d = utf32_string_view_init(ab, 2);
|
|
101
|
+
|
|
102
|
+
assert(utf32_string_view_compare(d, a) < 0);
|
|
103
|
+
assert(utf32_string_view_compare(a, d) > 0);
|
|
104
|
+
|
|
105
|
+
assert(utf32_string_view_compare_literal(a, abc, 3) == 0);
|
|
106
|
+
assert(utf32_string_view_compare_literal(a, acc, 3) < 0);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
#include <assert.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
|
|
5
|
+
#include "../../include/utf.h"
|
|
6
|
+
#include "../../include/utf/string.h"
|
|
7
|
+
|
|
8
|
+
int
|
|
9
|
+
main() {
|
|
10
|
+
int e;
|
|
11
|
+
|
|
12
|
+
static const utf32_t hello_world[] = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0};
|
|
13
|
+
static const utf32_t hello[] = {'h', 'e', 'l', 'l', 'o', 0};
|
|
14
|
+
static const utf32_t he[] = {'h', 'e', 0};
|
|
15
|
+
static const utf32_t h[] = {'h', 0};
|
|
16
|
+
|
|
17
|
+
// Plain erase in the middle.
|
|
18
|
+
{
|
|
19
|
+
utf32_string_t s;
|
|
20
|
+
utf32_string_init(&s);
|
|
21
|
+
|
|
22
|
+
e = utf32_string_append_literal(&s, hello_world, (size_t) -1);
|
|
23
|
+
assert(e == 0);
|
|
24
|
+
|
|
25
|
+
e = utf32_string_erase(&s, 5, 6);
|
|
26
|
+
assert(e == 0);
|
|
27
|
+
assert(s.len == 5);
|
|
28
|
+
assert(utf32_string_compare_literal(&s, hello, (size_t) -1) == 0);
|
|
29
|
+
|
|
30
|
+
utf32_string_destroy(&s);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Erase with len > string->len - pos must clamp.
|
|
34
|
+
{
|
|
35
|
+
utf32_string_t s;
|
|
36
|
+
utf32_string_init(&s);
|
|
37
|
+
|
|
38
|
+
e = utf32_string_append_literal(&s, hello, (size_t) -1);
|
|
39
|
+
assert(e == 0);
|
|
40
|
+
|
|
41
|
+
e = utf32_string_erase(&s, 2, 100);
|
|
42
|
+
assert(e == 0);
|
|
43
|
+
assert(s.len == 2);
|
|
44
|
+
assert(utf32_string_compare_literal(&s, he, (size_t) -1) == 0);
|
|
45
|
+
|
|
46
|
+
utf32_string_destroy(&s);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Erase with pos + len overflowing size_t must also clamp.
|
|
50
|
+
{
|
|
51
|
+
utf32_string_t s;
|
|
52
|
+
utf32_string_init(&s);
|
|
53
|
+
|
|
54
|
+
e = utf32_string_append_literal(&s, hello, (size_t) -1);
|
|
55
|
+
assert(e == 0);
|
|
56
|
+
|
|
57
|
+
e = utf32_string_erase(&s, 1, SIZE_MAX);
|
|
58
|
+
assert(e == 0);
|
|
59
|
+
assert(s.len == 1);
|
|
60
|
+
assert(utf32_string_compare_literal(&s, h, (size_t) -1) == 0);
|
|
61
|
+
|
|
62
|
+
utf32_string_destroy(&s);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Erase with pos > string->len is an error.
|
|
66
|
+
{
|
|
67
|
+
utf32_string_t s;
|
|
68
|
+
utf32_string_init(&s);
|
|
69
|
+
|
|
70
|
+
e = utf32_string_append_literal(&s, hello, (size_t) -1);
|
|
71
|
+
assert(e == 0);
|
|
72
|
+
|
|
73
|
+
e = utf32_string_erase(&s, 10, 1);
|
|
74
|
+
assert(e == -1);
|
|
75
|
+
assert(s.len == 5);
|
|
76
|
+
|
|
77
|
+
utf32_string_destroy(&s);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#include <assert.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
|
|
4
|
+
#include "../../include/utf.h"
|
|
5
|
+
#include "../../include/utf/string.h"
|
|
6
|
+
|
|
7
|
+
int
|
|
8
|
+
main() {
|
|
9
|
+
int e;
|
|
10
|
+
|
|
11
|
+
static const utf32_t banana[] = {'b', 'a', 'n', 'a', 'n', 'a', 0};
|
|
12
|
+
static const utf32_t abc[] = {'a', 'b', 'c', 0};
|
|
13
|
+
|
|
14
|
+
// Empty string with pos = 0 or pos = (size_t) -1 must return not-found rather
|
|
15
|
+
// than search over a length of SIZE_MAX.
|
|
16
|
+
{
|
|
17
|
+
utf32_string_t s;
|
|
18
|
+
utf32_string_init(&s);
|
|
19
|
+
|
|
20
|
+
assert(utf32_string_index_of_character(&s, 0, 'a') == (size_t) -1);
|
|
21
|
+
assert(utf32_string_index_of_character(&s, (size_t) -1, 'a') == (size_t) -1);
|
|
22
|
+
|
|
23
|
+
utf32_string_view_t view = utf32_string_view_init(banana, 0);
|
|
24
|
+
assert(utf32_string_view_index_of_character(view, 0, 'a') == (size_t) -1);
|
|
25
|
+
assert(utf32_string_view_index_of_character(view, (size_t) -1, 'a') == (size_t) -1);
|
|
26
|
+
|
|
27
|
+
utf32_string_destroy(&s);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Search from the start and find the first match.
|
|
31
|
+
{
|
|
32
|
+
utf32_string_t s;
|
|
33
|
+
utf32_string_init(&s);
|
|
34
|
+
|
|
35
|
+
e = utf32_string_append_literal(&s, banana, (size_t) -1);
|
|
36
|
+
assert(e == 0);
|
|
37
|
+
|
|
38
|
+
assert(utf32_string_index_of_character(&s, 0, 'b') == 0);
|
|
39
|
+
assert(utf32_string_index_of_character(&s, 0, 'a') == 1);
|
|
40
|
+
assert(utf32_string_index_of_character(&s, 0, 'n') == 2);
|
|
41
|
+
|
|
42
|
+
// Searching from a later position skips the earlier matches.
|
|
43
|
+
assert(utf32_string_index_of_character(&s, 1, 'a') == 1);
|
|
44
|
+
assert(utf32_string_index_of_character(&s, 2, 'a') == 3);
|
|
45
|
+
assert(utf32_string_index_of_character(&s, 4, 'a') == 5);
|
|
46
|
+
assert(utf32_string_index_of_character(&s, 5, 'n') == (size_t) -1);
|
|
47
|
+
|
|
48
|
+
utf32_string_view_t view = utf32_string_view(&s);
|
|
49
|
+
|
|
50
|
+
assert(utf32_string_view_index_of_character(view, 0, 'b') == 0);
|
|
51
|
+
assert(utf32_string_view_index_of_character(view, 2, 'a') == 3);
|
|
52
|
+
assert(utf32_string_view_index_of_character(view, 5, 'n') == (size_t) -1);
|
|
53
|
+
|
|
54
|
+
utf32_string_destroy(&s);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// A search with no match returns not-found.
|
|
58
|
+
{
|
|
59
|
+
utf32_string_t s;
|
|
60
|
+
utf32_string_init(&s);
|
|
61
|
+
|
|
62
|
+
e = utf32_string_append_literal(&s, abc, (size_t) -1);
|
|
63
|
+
assert(e == 0);
|
|
64
|
+
|
|
65
|
+
assert(utf32_string_index_of_character(&s, 0, 'z') == (size_t) -1);
|
|
66
|
+
|
|
67
|
+
// pos at or past the end returns not-found.
|
|
68
|
+
assert(utf32_string_index_of_character(&s, 3, 'a') == (size_t) -1);
|
|
69
|
+
assert(utf32_string_index_of_character(&s, 100, 'a') == (size_t) -1);
|
|
70
|
+
|
|
71
|
+
utf32_string_destroy(&s);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// A null code point is one like any other, both to search for and to search
|
|
75
|
+
// past.
|
|
76
|
+
{
|
|
77
|
+
static const utf32_t with_null[] = {'a', 0, 'b'};
|
|
78
|
+
|
|
79
|
+
utf32_string_view_t view = utf32_string_view_init(with_null, 3);
|
|
80
|
+
|
|
81
|
+
assert(utf32_string_view_index_of_character(view, 0, 0) == 1);
|
|
82
|
+
assert(utf32_string_view_index_of_character(view, 0, 'b') == 2);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// A code point outside the basic multilingual plane is found by its value, not
|
|
86
|
+
// by any single byte of it.
|
|
87
|
+
{
|
|
88
|
+
utf32_string_t s;
|
|
89
|
+
utf32_string_init(&s);
|
|
90
|
+
|
|
91
|
+
e = utf32_string_append_character(&s, 0x10348);
|
|
92
|
+
assert(e == 0);
|
|
93
|
+
|
|
94
|
+
e = utf32_string_append_character(&s, 0x48);
|
|
95
|
+
assert(e == 0);
|
|
96
|
+
|
|
97
|
+
assert(utf32_string_index_of_character(&s, 0, 0x10348) == 0);
|
|
98
|
+
assert(utf32_string_index_of_character(&s, 0, 0x48) == 1);
|
|
99
|
+
|
|
100
|
+
utf32_string_destroy(&s);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
#include <assert.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
|
|
4
|
+
#include "../../include/utf.h"
|
|
5
|
+
#include "../../include/utf/string.h"
|
|
6
|
+
|
|
7
|
+
int
|
|
8
|
+
main() {
|
|
9
|
+
int e;
|
|
10
|
+
|
|
11
|
+
static const utf32_t hello[] = {'h', 'e', 'l', 'l', 'o', 0};
|
|
12
|
+
|
|
13
|
+
// Inserting in the middle shifts the tail along.
|
|
14
|
+
{
|
|
15
|
+
static const utf32_t there[] = {' ', 't', 'h', 'e', 'r', 'e', 0};
|
|
16
|
+
static const utf32_t expected[] = {'h', 'e', 'l', ' ', 't', 'h', 'e', 'r', 'e', 'l', 'o', 0};
|
|
17
|
+
|
|
18
|
+
utf32_string_t string;
|
|
19
|
+
utf32_string_init(&string);
|
|
20
|
+
|
|
21
|
+
e = utf32_string_append_literal(&string, hello, (size_t) -1);
|
|
22
|
+
assert(e == 0);
|
|
23
|
+
|
|
24
|
+
e = utf32_string_insert_literal(&string, 3, there, (size_t) -1);
|
|
25
|
+
assert(e == 0);
|
|
26
|
+
|
|
27
|
+
assert(utf32_string_compare_literal(&string, expected, (size_t) -1) == 0);
|
|
28
|
+
|
|
29
|
+
utf32_string_destroy(&string);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Inserting at either end is a prepend and an append respectively.
|
|
33
|
+
{
|
|
34
|
+
utf32_string_t string;
|
|
35
|
+
utf32_string_init(&string);
|
|
36
|
+
|
|
37
|
+
e = utf32_string_append_literal(&string, hello, (size_t) -1);
|
|
38
|
+
assert(e == 0);
|
|
39
|
+
|
|
40
|
+
e = utf32_string_insert_character(&string, 0, '<');
|
|
41
|
+
assert(e == 0);
|
|
42
|
+
|
|
43
|
+
e = utf32_string_insert_character(&string, string.len, '>');
|
|
44
|
+
assert(e == 0);
|
|
45
|
+
|
|
46
|
+
assert(string.len == 7);
|
|
47
|
+
assert(string.data[0] == '<');
|
|
48
|
+
assert(string.data[6] == '>');
|
|
49
|
+
|
|
50
|
+
utf32_string_destroy(&string);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Inserting past the end is an error, leaving the string untouched.
|
|
54
|
+
{
|
|
55
|
+
utf32_string_t string;
|
|
56
|
+
utf32_string_init(&string);
|
|
57
|
+
|
|
58
|
+
e = utf32_string_append_literal(&string, hello, (size_t) -1);
|
|
59
|
+
assert(e == 0);
|
|
60
|
+
|
|
61
|
+
e = utf32_string_insert_character(&string, 6, 'x');
|
|
62
|
+
assert(e == -1);
|
|
63
|
+
assert(string.len == 5);
|
|
64
|
+
|
|
65
|
+
e = utf32_string_insert_character(&string, (size_t) -1, 'x');
|
|
66
|
+
assert(e == -1);
|
|
67
|
+
assert(string.len == 5);
|
|
68
|
+
|
|
69
|
+
utf32_string_destroy(&string);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Repeatedly inserting at a position grows the string past the inline buffer
|
|
73
|
+
// without disturbing the order of what is already there.
|
|
74
|
+
{
|
|
75
|
+
utf32_string_t string;
|
|
76
|
+
utf32_string_init(&string);
|
|
77
|
+
|
|
78
|
+
for (size_t i = 0; i < UTF32_STRING_INLINE_CAPACITY * 2; i++) {
|
|
79
|
+
e = utf32_string_insert_character(&string, i, (utf32_t) (0x30000 + i));
|
|
80
|
+
assert(e == 0);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
assert(string.len == UTF32_STRING_INLINE_CAPACITY * 2);
|
|
84
|
+
assert(string.data != string.buf);
|
|
85
|
+
|
|
86
|
+
for (size_t i = 0; i < string.len; i++) {
|
|
87
|
+
assert(string.data[i] == 0x30000 + i);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
utf32_string_destroy(&string);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#include <assert.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
|
|
4
|
+
#include "../../include/utf.h"
|
|
5
|
+
#include "../../include/utf/string.h"
|
|
6
|
+
|
|
7
|
+
int
|
|
8
|
+
main() {
|
|
9
|
+
int e;
|
|
10
|
+
|
|
11
|
+
static const utf32_t banana[] = {'b', 'a', 'n', 'a', 'n', 'a', 0};
|
|
12
|
+
static const utf32_t abc[] = {'a', 'b', 'c', 0};
|
|
13
|
+
|
|
14
|
+
// Empty string with pos = (size_t) -1 must return not-found instead of
|
|
15
|
+
// reading data[SIZE_MAX].
|
|
16
|
+
{
|
|
17
|
+
utf32_string_t s;
|
|
18
|
+
utf32_string_init(&s);
|
|
19
|
+
|
|
20
|
+
assert(utf32_string_last_index_of_character(&s, (size_t) -1, 'a') == (size_t) -1);
|
|
21
|
+
|
|
22
|
+
utf32_string_view_t view = utf32_string_view_init(banana, 0);
|
|
23
|
+
assert(utf32_string_view_last_index_of_character(view, (size_t) -1, 'a') == (size_t) -1);
|
|
24
|
+
|
|
25
|
+
utf32_string_destroy(&s);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Search from the end and find a match.
|
|
29
|
+
{
|
|
30
|
+
utf32_string_t s;
|
|
31
|
+
utf32_string_init(&s);
|
|
32
|
+
|
|
33
|
+
e = utf32_string_append_literal(&s, banana, (size_t) -1);
|
|
34
|
+
assert(e == 0);
|
|
35
|
+
|
|
36
|
+
assert(utf32_string_last_index_of_character(&s, (size_t) -1, 'a') == 5);
|
|
37
|
+
assert(utf32_string_last_index_of_character(&s, (size_t) -1, 'n') == 4);
|
|
38
|
+
assert(utf32_string_last_index_of_character(&s, (size_t) -1, 'b') == 0);
|
|
39
|
+
|
|
40
|
+
utf32_string_destroy(&s);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Search with no match must terminate (loop must not run forever or read
|
|
44
|
+
// before the buffer).
|
|
45
|
+
{
|
|
46
|
+
utf32_string_t s;
|
|
47
|
+
utf32_string_init(&s);
|
|
48
|
+
|
|
49
|
+
e = utf32_string_append_literal(&s, abc, (size_t) -1);
|
|
50
|
+
assert(e == 0);
|
|
51
|
+
|
|
52
|
+
assert(utf32_string_last_index_of_character(&s, (size_t) -1, 'z') == (size_t) -1);
|
|
53
|
+
assert(utf32_string_last_index_of_character(&s, 0, 'z') == (size_t) -1);
|
|
54
|
+
|
|
55
|
+
utf32_string_destroy(&s);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// pos >= string->len returns not-found.
|
|
59
|
+
{
|
|
60
|
+
utf32_string_t s;
|
|
61
|
+
utf32_string_init(&s);
|
|
62
|
+
|
|
63
|
+
e = utf32_string_append_literal(&s, abc, (size_t) -1);
|
|
64
|
+
assert(e == 0);
|
|
65
|
+
|
|
66
|
+
assert(utf32_string_last_index_of_character(&s, 3, 'a') == (size_t) -1);
|
|
67
|
+
assert(utf32_string_last_index_of_character(&s, 100, 'a') == (size_t) -1);
|
|
68
|
+
|
|
69
|
+
utf32_string_destroy(&s);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#include <assert.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
|
|
4
|
+
#include "../../include/utf.h"
|
|
5
|
+
#include "../../include/utf/string.h"
|
|
6
|
+
|
|
7
|
+
int
|
|
8
|
+
main() {
|
|
9
|
+
int e;
|
|
10
|
+
|
|
11
|
+
static const utf32_t hello[] = {'h', 'e', 'l', 'l', 'o', 0};
|
|
12
|
+
static const utf32_t world[] = {' ', 'w', 'o', 'r', 'l', 'd', 0};
|
|
13
|
+
static const utf32_t expected[] = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0};
|
|
14
|
+
|
|
15
|
+
{
|
|
16
|
+
utf32_string_t string;
|
|
17
|
+
utf32_string_init(&string);
|
|
18
|
+
|
|
19
|
+
e = utf32_string_prepend_literal(&string, world, (size_t) -1);
|
|
20
|
+
assert(e == 0);
|
|
21
|
+
|
|
22
|
+
e = utf32_string_prepend_literal(&string, hello, (size_t) -1);
|
|
23
|
+
assert(e == 0);
|
|
24
|
+
|
|
25
|
+
assert(string.len == 11);
|
|
26
|
+
assert(utf32_string_compare_literal(&string, expected, (size_t) -1) == 0);
|
|
27
|
+
|
|
28
|
+
utf32_string_destroy(&string);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Prepending a single code point shifts the rest along, including once the
|
|
32
|
+
// string has outgrown the inline buffer.
|
|
33
|
+
{
|
|
34
|
+
utf32_string_t string;
|
|
35
|
+
utf32_string_init(&string);
|
|
36
|
+
|
|
37
|
+
for (size_t i = 0; i < UTF32_STRING_INLINE_CAPACITY * 2; i++) {
|
|
38
|
+
e = utf32_string_prepend_character(&string, (utf32_t) (0x20000 + i));
|
|
39
|
+
assert(e == 0);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
assert(string.len == UTF32_STRING_INLINE_CAPACITY * 2);
|
|
43
|
+
|
|
44
|
+
// The first code point prepended ended up last.
|
|
45
|
+
for (size_t i = 0; i < string.len; i++) {
|
|
46
|
+
assert(string.data[i] == 0x20000 + (string.len - 1 - i));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
utf32_string_destroy(&string);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#include <assert.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
|
|
5
|
+
#include "../../include/utf.h"
|
|
6
|
+
#include "../../include/utf/string.h"
|
|
7
|
+
|
|
8
|
+
int
|
|
9
|
+
main() {
|
|
10
|
+
int e;
|
|
11
|
+
|
|
12
|
+
static const utf32_t hello_world[] = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0};
|
|
13
|
+
static const utf32_t hello[] = {'h', 'e', 'l', 'l', 'o', 0};
|
|
14
|
+
|
|
15
|
+
{
|
|
16
|
+
static const utf32_t there[] = {' ', 't', 'h', 'e', 'r', 'e', ' ', 0};
|
|
17
|
+
static const utf32_t expected[] = {'h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e', ' ', 'w', 'o', 'r', 'l', 'd', 0};
|
|
18
|
+
|
|
19
|
+
utf32_string_t string;
|
|
20
|
+
utf32_string_init(&string);
|
|
21
|
+
|
|
22
|
+
e = utf32_string_append_literal(&string, hello_world, (size_t) -1);
|
|
23
|
+
assert(e == 0);
|
|
24
|
+
|
|
25
|
+
e = utf32_string_replace_literal(&string, 5, 1, there, (size_t) -1);
|
|
26
|
+
assert(e == 0);
|
|
27
|
+
|
|
28
|
+
assert(utf32_string_compare_literal(&string, expected, (size_t) -1) == 0);
|
|
29
|
+
|
|
30
|
+
utf32_string_destroy(&string);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// replace_character must clamp len to (string->len - pos) so memmove doesn't
|
|
34
|
+
// underflow size_t.
|
|
35
|
+
{
|
|
36
|
+
static const utf32_t expected[] = {'h', 'e', 'X', 0};
|
|
37
|
+
|
|
38
|
+
utf32_string_t s;
|
|
39
|
+
utf32_string_init(&s);
|
|
40
|
+
|
|
41
|
+
e = utf32_string_append_literal(&s, hello, (size_t) -1);
|
|
42
|
+
assert(e == 0);
|
|
43
|
+
|
|
44
|
+
e = utf32_string_replace_character(&s, 2, 100, 'X');
|
|
45
|
+
assert(e == 0);
|
|
46
|
+
assert(s.len == 3);
|
|
47
|
+
assert(utf32_string_compare_literal(&s, expected, (size_t) -1) == 0);
|
|
48
|
+
|
|
49
|
+
utf32_string_destroy(&s);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// replace_character with pos + len overflowing size_t must also clamp.
|
|
53
|
+
{
|
|
54
|
+
static const utf32_t expected[] = {'h', 'Y', 0};
|
|
55
|
+
|
|
56
|
+
utf32_string_t s;
|
|
57
|
+
utf32_string_init(&s);
|
|
58
|
+
|
|
59
|
+
e = utf32_string_append_literal(&s, hello, (size_t) -1);
|
|
60
|
+
assert(e == 0);
|
|
61
|
+
|
|
62
|
+
e = utf32_string_replace_character(&s, 1, SIZE_MAX, 'Y');
|
|
63
|
+
assert(e == 0);
|
|
64
|
+
assert(s.len == 2);
|
|
65
|
+
assert(utf32_string_compare_literal(&s, expected, (size_t) -1) == 0);
|
|
66
|
+
|
|
67
|
+
utf32_string_destroy(&s);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// replace_literal with pos + len overflowing size_t must also clamp.
|
|
71
|
+
{
|
|
72
|
+
static const utf32_t replacement[] = {'i', '!', 0};
|
|
73
|
+
static const utf32_t expected[] = {'h', 'i', '!', 0};
|
|
74
|
+
|
|
75
|
+
utf32_string_t s;
|
|
76
|
+
utf32_string_init(&s);
|
|
77
|
+
|
|
78
|
+
e = utf32_string_append_literal(&s, hello, (size_t) -1);
|
|
79
|
+
assert(e == 0);
|
|
80
|
+
|
|
81
|
+
e = utf32_string_replace_literal(&s, 1, SIZE_MAX, replacement, 2);
|
|
82
|
+
assert(e == 0);
|
|
83
|
+
assert(s.len == 3);
|
|
84
|
+
assert(utf32_string_compare_literal(&s, expected, (size_t) -1) == 0);
|
|
85
|
+
|
|
86
|
+
utf32_string_destroy(&s);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
#include <assert.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
|
|
5
|
+
#include "../../include/utf.h"
|
|
6
|
+
#include "../../include/utf/string.h"
|
|
7
|
+
|
|
8
|
+
int
|
|
9
|
+
main() {
|
|
10
|
+
int e;
|
|
11
|
+
|
|
12
|
+
// Normal reserve grows past the inline buffer.
|
|
13
|
+
{
|
|
14
|
+
utf32_string_t s;
|
|
15
|
+
utf32_string_init(&s);
|
|
16
|
+
|
|
17
|
+
e = utf32_string_reserve(&s, 512);
|
|
18
|
+
assert(e == 0);
|
|
19
|
+
assert(s.data != s.buf);
|
|
20
|
+
assert(s.cap >= 512);
|
|
21
|
+
|
|
22
|
+
utf32_string_destroy(&s);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Reserve within the inline buffer is a no-op. The capacity counts code
|
|
26
|
+
// points, so the whole of the buffer is available rather than a quarter of it.
|
|
27
|
+
{
|
|
28
|
+
utf32_string_t s;
|
|
29
|
+
utf32_string_init(&s);
|
|
30
|
+
|
|
31
|
+
e = utf32_string_reserve(&s, UTF32_STRING_INLINE_CAPACITY);
|
|
32
|
+
assert(e == 0);
|
|
33
|
+
assert(s.data == s.buf);
|
|
34
|
+
|
|
35
|
+
utf32_string_destroy(&s);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// One code point past the inline buffer does allocate.
|
|
39
|
+
{
|
|
40
|
+
utf32_string_t s;
|
|
41
|
+
utf32_string_init(&s);
|
|
42
|
+
|
|
43
|
+
e = utf32_string_reserve(&s, UTF32_STRING_INLINE_CAPACITY + 1);
|
|
44
|
+
assert(e == 0);
|
|
45
|
+
assert(s.data != s.buf);
|
|
46
|
+
|
|
47
|
+
utf32_string_destroy(&s);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Reserve must reject sizes that would overflow the power-of-two rounding,
|
|
51
|
+
// rather than wrapping to cap = 0 and returning success.
|
|
52
|
+
{
|
|
53
|
+
utf32_string_t s;
|
|
54
|
+
utf32_string_init(&s);
|
|
55
|
+
|
|
56
|
+
e = utf32_string_reserve(&s, SIZE_MAX);
|
|
57
|
+
assert(e == -1);
|
|
58
|
+
|
|
59
|
+
e = utf32_string_reserve(&s, (SIZE_MAX >> 1) + 2);
|
|
60
|
+
assert(e == -1);
|
|
61
|
+
|
|
62
|
+
utf32_string_destroy(&s);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Reserve must also reject a count of code points that survives the rounding
|
|
66
|
+
// but overflows once scaled to the number of bytes that they occupy. This has
|
|
67
|
+
// no counterpart for UTF-8, where a code unit is a single byte.
|
|
68
|
+
{
|
|
69
|
+
utf32_string_t s;
|
|
70
|
+
utf32_string_init(&s);
|
|
71
|
+
|
|
72
|
+
e = utf32_string_reserve(&s, SIZE_MAX / sizeof(utf32_t));
|
|
73
|
+
assert(e == -1);
|
|
74
|
+
|
|
75
|
+
utf32_string_destroy(&s);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Shrinking back to within the inline buffer returns to it, keeping the
|
|
79
|
+
// contents.
|
|
80
|
+
{
|
|
81
|
+
utf32_string_t s;
|
|
82
|
+
utf32_string_init(&s);
|
|
83
|
+
|
|
84
|
+
e = utf32_string_reserve(&s, 512);
|
|
85
|
+
assert(e == 0);
|
|
86
|
+
assert(s.data != s.buf);
|
|
87
|
+
|
|
88
|
+
e = utf32_string_append_character(&s, 0x10348);
|
|
89
|
+
assert(e == 0);
|
|
90
|
+
|
|
91
|
+
e = utf32_string_shrink_to_fit(&s);
|
|
92
|
+
assert(e == 0);
|
|
93
|
+
assert(s.data == s.buf);
|
|
94
|
+
assert(s.len == 1);
|
|
95
|
+
assert(s.data[0] == 0x10348);
|
|
96
|
+
|
|
97
|
+
utf32_string_destroy(&s);
|
|
98
|
+
}
|
|
99
|
+
}
|