bare-url 0.3.7 → 1.0.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/CMakeLists.txt +27 -0
- package/binding.c +111 -0
- package/binding.js +1 -0
- package/index.js +74 -90
- package/lib/errors.js +0 -105
- package/package.json +14 -3
- package/prebuilds/darwin-arm64/bare-url.bare +0 -0
- package/prebuilds/darwin-x64/bare-url.bare +0 -0
- package/prebuilds/linux-arm64/bare-url.bare +0 -0
- package/prebuilds/linux-x64/bare-url.bare +0 -0
- package/prebuilds/win32-x64/bare-url.bare +0 -0
- package/vendor/liburl/CMakeLists.txt +79 -0
- package/vendor/liburl/LICENSE +201 -0
- package/vendor/liburl/README.md +27 -0
- package/vendor/liburl/include/url.h +107 -0
- package/vendor/liburl/src/character-set.h +41 -0
- package/vendor/liburl/src/idna.h +14 -0
- package/vendor/liburl/src/infra.h +141 -0
- package/vendor/liburl/src/parse.h +1390 -0
- package/vendor/liburl/src/percent-encode.h +567 -0
- package/vendor/liburl/src/punycode.h +21 -0
- package/vendor/liburl/src/serialize.h +91 -0
- package/vendor/liburl/src/type.h +61 -0
- package/vendor/liburl/src/url.c +81 -0
- package/vendor/libutf/CMakeLists.txt +76 -0
- package/vendor/libutf/LICENSE +201 -0
- package/vendor/libutf/README.md +11 -0
- package/vendor/libutf/include/utf/endianness.h +54 -0
- package/vendor/libutf/include/utf/string.h +759 -0
- package/vendor/libutf/include/utf.h +45 -0
- package/vendor/libutf/src/endianness.c +19 -0
- package/vendor/libutf/src/utf16/utf8-convert.c +77 -0
- package/vendor/libutf/src/utf16/utf8-length.c +45 -0
- package/vendor/libutf/src/utf16/validate.c +53 -0
- package/vendor/libutf/src/utf8/string.c +148 -0
- package/vendor/libutf/src/utf8/utf16-convert.c +90 -0
- package/vendor/libutf/src/utf8/utf16-length.c +39 -0
- package/vendor/libutf/src/utf8/validate.c +107 -0
- package/lib/constants.js +0 -25
- package/lib/infra.js +0 -39
- package/lib/parse.js +0 -941
- package/lib/serialize.js +0 -98
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#ifndef UTF_H
|
|
2
|
+
#define UTF_H
|
|
3
|
+
|
|
4
|
+
#ifdef __cplusplus
|
|
5
|
+
extern "C" {
|
|
6
|
+
#endif
|
|
7
|
+
|
|
8
|
+
#include <stdbool.h>
|
|
9
|
+
#include <stddef.h>
|
|
10
|
+
#include <stdint.h>
|
|
11
|
+
|
|
12
|
+
typedef uint_least8_t utf8_t;
|
|
13
|
+
typedef uint_least16_t utf16_t;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* UTF-8
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
bool
|
|
20
|
+
utf8_validate (const utf8_t *data, size_t len);
|
|
21
|
+
|
|
22
|
+
size_t
|
|
23
|
+
utf8_length_from_utf16le (const utf16_t *data, size_t len);
|
|
24
|
+
|
|
25
|
+
size_t
|
|
26
|
+
utf8_convert_to_utf16le (const utf8_t *data, size_t len, utf16_t *result);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* UTF-16
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
bool
|
|
33
|
+
utf16le_validate (const utf16_t *data, size_t len);
|
|
34
|
+
|
|
35
|
+
size_t
|
|
36
|
+
utf16_length_from_utf8 (const utf8_t *data, size_t len);
|
|
37
|
+
|
|
38
|
+
size_t
|
|
39
|
+
utf16le_convert_to_utf8 (const utf16_t *data, size_t len, utf8_t *result);
|
|
40
|
+
|
|
41
|
+
#ifdef __cplusplus
|
|
42
|
+
}
|
|
43
|
+
#endif
|
|
44
|
+
|
|
45
|
+
#endif // UTF_H
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#include <stdbool.h>
|
|
2
|
+
#include <stdint.h>
|
|
3
|
+
|
|
4
|
+
#include "../include/utf/endianness.h"
|
|
5
|
+
|
|
6
|
+
extern utf_endianness_t
|
|
7
|
+
utf_endianness (void);
|
|
8
|
+
|
|
9
|
+
extern bool
|
|
10
|
+
utf_is_le (void);
|
|
11
|
+
|
|
12
|
+
extern bool
|
|
13
|
+
utf_is_be (void);
|
|
14
|
+
|
|
15
|
+
extern uint16_t
|
|
16
|
+
utf_swap_uint16 (uint16_t n);
|
|
17
|
+
|
|
18
|
+
extern uint32_t
|
|
19
|
+
utf_swap_uint32 (uint32_t n);
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#include <stdbool.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
|
|
6
|
+
#include "../../include/utf.h"
|
|
7
|
+
#include "../../include/utf/endianness.h"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Modified from https://github.com/simdutf/simdutf
|
|
11
|
+
*
|
|
12
|
+
* Copyright 2020 The simdutf authors
|
|
13
|
+
*
|
|
14
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
15
|
+
* you may not use this file except in compliance with the License.
|
|
16
|
+
* You may obtain a copy of the License at
|
|
17
|
+
*
|
|
18
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
19
|
+
*
|
|
20
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
21
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
22
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
23
|
+
* See the License for the specific language governing permissions and
|
|
24
|
+
* limitations under the License.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
size_t
|
|
28
|
+
utf16le_convert_to_utf8 (const utf16_t *data, size_t len, utf8_t *result) {
|
|
29
|
+
size_t pos = 0;
|
|
30
|
+
uint16_t word, diff;
|
|
31
|
+
utf8_t *start = result;
|
|
32
|
+
|
|
33
|
+
while (pos < len) {
|
|
34
|
+
if (pos + 4 <= len) {
|
|
35
|
+
uint64_t v;
|
|
36
|
+
memcpy(&v, data + pos, sizeof(uint64_t));
|
|
37
|
+
if (utf_is_be()) v = (v >> 8) | (v << (64 - 8));
|
|
38
|
+
if ((v & 0xff80ff80ff80ff80) == 0) {
|
|
39
|
+
size_t final_pos = pos + 4;
|
|
40
|
+
while (pos < final_pos) {
|
|
41
|
+
*result++ = utf_is_be() ? utf_swap_uint16(data[pos]) : data[pos];
|
|
42
|
+
pos++;
|
|
43
|
+
}
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
word = utf_is_be() ? utf_swap_uint16(data[pos]) : data[pos];
|
|
49
|
+
if ((word & 0xff80) == 0) {
|
|
50
|
+
*result++ = word;
|
|
51
|
+
pos++;
|
|
52
|
+
} else if ((word & 0xf800) == 0) {
|
|
53
|
+
*result++ = (word >> 6) | 0b11000000;
|
|
54
|
+
*result++ = (word & 0b111111) | 0b10000000;
|
|
55
|
+
pos++;
|
|
56
|
+
} else if ((word & 0xf800) != 0xd800) {
|
|
57
|
+
*result++ = (word >> 12) | 0b11100000;
|
|
58
|
+
*result++ = ((word >> 6) & 0b111111) | 0b10000000;
|
|
59
|
+
*result++ = (word & 0b111111) | 0b10000000;
|
|
60
|
+
pos++;
|
|
61
|
+
} else {
|
|
62
|
+
diff = word - 0xd800;
|
|
63
|
+
if (pos + 1 >= len) {
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
word = utf_is_be() ? utf_swap_uint16(data[pos + 1]) : data[pos + 1];
|
|
67
|
+
uint32_t value = (diff << 10) + (word - 0xdc00) + 0x10000;
|
|
68
|
+
*result++ = (value >> 18) | 0b11110000;
|
|
69
|
+
*result++ = ((value >> 12) & 0b111111) | 0b10000000;
|
|
70
|
+
*result++ = ((value >> 6) & 0b111111) | 0b10000000;
|
|
71
|
+
*result++ = (value & 0b111111) | 0b10000000;
|
|
72
|
+
pos += 2;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return result - start;
|
|
77
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#include <stdbool.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
|
|
5
|
+
#include "../../include/utf.h"
|
|
6
|
+
#include "../../include/utf/endianness.h"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Modified from https://github.com/simdutf/simdutf
|
|
10
|
+
*
|
|
11
|
+
* Copyright 2020 The simdutf authors
|
|
12
|
+
*
|
|
13
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
* you may not use this file except in compliance with the License.
|
|
15
|
+
* You may obtain a copy of the License at
|
|
16
|
+
*
|
|
17
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
+
*
|
|
19
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
20
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
21
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
22
|
+
* See the License for the specific language governing permissions and
|
|
23
|
+
* limitations under the License.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
size_t
|
|
27
|
+
utf8_length_from_utf16le (const utf16_t *data, size_t len) {
|
|
28
|
+
size_t counter = 0;
|
|
29
|
+
uint16_t word;
|
|
30
|
+
|
|
31
|
+
for (size_t i = 0; i < len; i++) {
|
|
32
|
+
word = utf_is_be() ? utf_swap_uint16(data[i]) : data[i];
|
|
33
|
+
if (word <= 0x7f) {
|
|
34
|
+
counter++;
|
|
35
|
+
} else if (word <= 0x7ff) {
|
|
36
|
+
counter += 2;
|
|
37
|
+
} else if ((word <= 0xd7ff) || (word >= 0xe000)) {
|
|
38
|
+
counter += 3;
|
|
39
|
+
} else {
|
|
40
|
+
counter += 2;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return counter;
|
|
45
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#include <stdbool.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
|
|
5
|
+
#include "../../include/utf.h"
|
|
6
|
+
#include "../../include/utf/endianness.h"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Modified from https://github.com/simdutf/simdutf
|
|
10
|
+
*
|
|
11
|
+
* Copyright 2020 The simdutf authors
|
|
12
|
+
*
|
|
13
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
* you may not use this file except in compliance with the License.
|
|
15
|
+
* You may obtain a copy of the License at
|
|
16
|
+
*
|
|
17
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
+
*
|
|
19
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
20
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
21
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
22
|
+
* See the License for the specific language governing permissions and
|
|
23
|
+
* limitations under the License.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
bool
|
|
27
|
+
utf16le_validate (const utf16_t *data, size_t len) {
|
|
28
|
+
uint64_t pos = 0;
|
|
29
|
+
uint16_t word, diff;
|
|
30
|
+
|
|
31
|
+
while (pos < len) {
|
|
32
|
+
word = utf_is_be() ? utf_swap_uint16(data[pos]) : data[pos];
|
|
33
|
+
if ((word & 0xf800) == 0xd800) {
|
|
34
|
+
if (pos + 1 >= len) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
diff = word - 0xd800;
|
|
38
|
+
if (diff > 0x3ff) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
word = utf_is_be() ? utf_swap_uint16(data[pos + 1]) : data[pos + 1];
|
|
42
|
+
diff = word - 0xdc00;
|
|
43
|
+
if (diff > 0x3ff) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
pos += 2;
|
|
47
|
+
} else {
|
|
48
|
+
pos++;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
#include <stddef.h>
|
|
2
|
+
|
|
3
|
+
#include "../../include/utf.h"
|
|
4
|
+
#include "../../include/utf/string.h"
|
|
5
|
+
|
|
6
|
+
extern void
|
|
7
|
+
utf8_string_init (utf8_string_t *string);
|
|
8
|
+
|
|
9
|
+
extern utf8_string_view_t
|
|
10
|
+
utf8_string_view_init (const utf8_t *data, size_t len);
|
|
11
|
+
|
|
12
|
+
extern void
|
|
13
|
+
utf8_string_destroy (utf8_string_t *string);
|
|
14
|
+
|
|
15
|
+
extern int
|
|
16
|
+
utf8_string_reserve (utf8_string_t *string, size_t len);
|
|
17
|
+
|
|
18
|
+
extern int
|
|
19
|
+
utf8_string_shrink_to_fit (utf8_string_t *string);
|
|
20
|
+
|
|
21
|
+
extern utf8_string_view_t
|
|
22
|
+
utf8_string_view (const utf8_string_t *string);
|
|
23
|
+
|
|
24
|
+
extern void
|
|
25
|
+
utf8_string_clear (utf8_string_t *string);
|
|
26
|
+
|
|
27
|
+
extern bool
|
|
28
|
+
utf8_string_empty (utf8_string_t *string);
|
|
29
|
+
|
|
30
|
+
extern bool
|
|
31
|
+
utf8_string_view_empty (const utf8_string_view_t view);
|
|
32
|
+
|
|
33
|
+
extern int
|
|
34
|
+
utf8_string_copy (const utf8_string_t *string, utf8_string_t *result);
|
|
35
|
+
|
|
36
|
+
extern int
|
|
37
|
+
utf8_string_view_copy (const utf8_string_view_t view, utf8_string_t *result);
|
|
38
|
+
|
|
39
|
+
extern int
|
|
40
|
+
utf8_string_append (utf8_string_t *string, const utf8_string_t *other);
|
|
41
|
+
|
|
42
|
+
extern int
|
|
43
|
+
utf8_string_append_view (utf8_string_t *string, const utf8_string_view_t view);
|
|
44
|
+
|
|
45
|
+
extern int
|
|
46
|
+
utf8_string_append_character (utf8_string_t *string, utf8_t c);
|
|
47
|
+
|
|
48
|
+
extern int
|
|
49
|
+
utf8_string_append_literal (utf8_string_t *string, const utf8_t *literal, size_t n);
|
|
50
|
+
|
|
51
|
+
extern int
|
|
52
|
+
utf8_string_prepend (utf8_string_t *string, const utf8_string_t *other);
|
|
53
|
+
|
|
54
|
+
extern int
|
|
55
|
+
utf8_string_prepend_view (utf8_string_t *string, const utf8_string_view_t view);
|
|
56
|
+
|
|
57
|
+
extern int
|
|
58
|
+
utf8_string_prepend_character (utf8_string_t *string, utf8_t c);
|
|
59
|
+
|
|
60
|
+
extern int
|
|
61
|
+
utf8_string_prepend_literal (utf8_string_t *string, const utf8_t *literal, size_t n);
|
|
62
|
+
|
|
63
|
+
extern int
|
|
64
|
+
utf8_string_insert (utf8_string_t *string, size_t pos, const utf8_string_t *other);
|
|
65
|
+
|
|
66
|
+
extern int
|
|
67
|
+
utf8_string_insert_view (utf8_string_t *string, size_t pos, const utf8_string_view_t other);
|
|
68
|
+
|
|
69
|
+
extern int
|
|
70
|
+
utf8_string_insert_character (utf8_string_t *string, size_t pos, utf8_t c);
|
|
71
|
+
|
|
72
|
+
extern int
|
|
73
|
+
utf8_string_insert_literal (utf8_string_t *string, size_t pos, const utf8_t *literal, size_t n);
|
|
74
|
+
|
|
75
|
+
extern int
|
|
76
|
+
utf8_string_replace (utf8_string_t *string, size_t pos, size_t len, const utf8_string_t *replacement);
|
|
77
|
+
|
|
78
|
+
extern int
|
|
79
|
+
utf8_string_replace_view (utf8_string_t *string, size_t pos, size_t len, const utf8_string_view_t replacement);
|
|
80
|
+
|
|
81
|
+
extern int
|
|
82
|
+
utf8_string_replace_character (utf8_string_t *string, size_t pos, size_t len, utf8_t c);
|
|
83
|
+
|
|
84
|
+
extern int
|
|
85
|
+
utf8_string_replace_literal (utf8_string_t *string, size_t pos, size_t len, const utf8_t *literal, size_t n);
|
|
86
|
+
|
|
87
|
+
extern int
|
|
88
|
+
utf8_string_erase (utf8_string_t *string, size_t pos, size_t len);
|
|
89
|
+
|
|
90
|
+
extern int
|
|
91
|
+
utf8_string_concat (const utf8_string_t *string, const utf8_string_t *other, utf8_string_t *result);
|
|
92
|
+
|
|
93
|
+
extern int
|
|
94
|
+
utf8_string_view_concat (const utf8_string_view_t view, const utf8_string_t *other, utf8_string_t *result);
|
|
95
|
+
|
|
96
|
+
extern int
|
|
97
|
+
utf8_string_concat_view (const utf8_string_t *string, const utf8_string_view_t other, utf8_string_t *result);
|
|
98
|
+
|
|
99
|
+
extern int
|
|
100
|
+
utf8_string_view_concat_view (const utf8_string_view_t view, const utf8_string_view_t other, utf8_string_t *result);
|
|
101
|
+
|
|
102
|
+
extern int
|
|
103
|
+
utf8_string_concat_character (const utf8_string_t *string, utf8_t c, utf8_string_t *result);
|
|
104
|
+
|
|
105
|
+
extern int
|
|
106
|
+
utf8_string_view_concat_character (const utf8_string_view_t view, utf8_t c, utf8_string_t *result);
|
|
107
|
+
|
|
108
|
+
extern int
|
|
109
|
+
utf8_string_concat_literal (const utf8_string_t *string, const utf8_t *literal, size_t n, utf8_string_t *result);
|
|
110
|
+
|
|
111
|
+
extern int
|
|
112
|
+
utf8_string_view_concat_literal (const utf8_string_view_t view, const utf8_t *literal, size_t n, utf8_string_t *result);
|
|
113
|
+
|
|
114
|
+
extern int
|
|
115
|
+
utf8_string_compare (const utf8_string_t *string, const utf8_string_t *other);
|
|
116
|
+
|
|
117
|
+
extern int
|
|
118
|
+
utf8_string_view_compare (const utf8_string_view_t view, const utf8_string_view_t other);
|
|
119
|
+
|
|
120
|
+
extern int
|
|
121
|
+
utf8_string_compare_literal (const utf8_string_t *string, const utf8_t *literal, size_t n);
|
|
122
|
+
|
|
123
|
+
extern int
|
|
124
|
+
utf8_string_view_compare_literal (const utf8_string_view_t view, const utf8_t *literal, size_t n);
|
|
125
|
+
|
|
126
|
+
extern utf8_string_view_t
|
|
127
|
+
utf8_string_substring (const utf8_string_t *string, size_t start, size_t end);
|
|
128
|
+
|
|
129
|
+
extern utf8_string_view_t
|
|
130
|
+
utf8_string_view_substring (const utf8_string_view_t view, size_t start, size_t end);
|
|
131
|
+
|
|
132
|
+
extern int
|
|
133
|
+
utf8_string_substring_copy (const utf8_string_t *string, size_t start, size_t end, utf8_string_t *result);
|
|
134
|
+
|
|
135
|
+
extern int
|
|
136
|
+
utf8_string_view_substring_copy (const utf8_string_view_t view, size_t start, size_t end, utf8_string_t *result);
|
|
137
|
+
|
|
138
|
+
extern size_t
|
|
139
|
+
utf8_string_index_of_character (const utf8_string_t *string, size_t pos, utf8_t c);
|
|
140
|
+
|
|
141
|
+
extern size_t
|
|
142
|
+
utf8_string_view_index_of_character (const utf8_string_view_t view, size_t pos, utf8_t c);
|
|
143
|
+
|
|
144
|
+
extern size_t
|
|
145
|
+
utf8_string_last_index_of_character (const utf8_string_t *string, size_t pos, utf8_t c);
|
|
146
|
+
|
|
147
|
+
extern size_t
|
|
148
|
+
utf8_string_view_last_index_of_character (const utf8_string_view_t view, size_t pos, utf8_t c);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#include <stdbool.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
|
|
6
|
+
#include "../../include/utf.h"
|
|
7
|
+
#include "../../include/utf/endianness.h"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Modified from https://github.com/simdutf/simdutf
|
|
11
|
+
*
|
|
12
|
+
* Copyright 2020 The simdutf authors
|
|
13
|
+
*
|
|
14
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
15
|
+
* you may not use this file except in compliance with the License.
|
|
16
|
+
* You may obtain a copy of the License at
|
|
17
|
+
*
|
|
18
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
19
|
+
*
|
|
20
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
21
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
22
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
23
|
+
* See the License for the specific language governing permissions and
|
|
24
|
+
* limitations under the License.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
size_t
|
|
28
|
+
utf8_convert_to_utf16le (const utf8_t *data, size_t len, utf16_t *result) {
|
|
29
|
+
size_t pos = 0;
|
|
30
|
+
utf16_t *start = result;
|
|
31
|
+
|
|
32
|
+
while (pos < len) {
|
|
33
|
+
if (pos + 8 <= len) {
|
|
34
|
+
uint64_t v;
|
|
35
|
+
memcpy(&v, data + pos, sizeof(uint64_t));
|
|
36
|
+
if ((v & 0x8080808080808080) == 0) {
|
|
37
|
+
size_t final_pos = pos + 8;
|
|
38
|
+
while (pos < final_pos) {
|
|
39
|
+
*result++ = utf_is_be() ? utf_swap_uint16(data[pos]) : data[pos];
|
|
40
|
+
pos++;
|
|
41
|
+
}
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
uint8_t leading_byte = data[pos];
|
|
46
|
+
if (leading_byte < 0b10000000) {
|
|
47
|
+
*result++ = utf_is_be() ? utf_swap_uint16(leading_byte) : leading_byte;
|
|
48
|
+
pos++;
|
|
49
|
+
} else if ((leading_byte & 0b11100000) == 0b11000000) {
|
|
50
|
+
if (pos + 1 >= len) {
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
uint16_t code_point = ((leading_byte & 0b00011111) << 6) | (data[pos + 1] & 0b00111111);
|
|
54
|
+
if (utf_is_be()) {
|
|
55
|
+
code_point = utf_swap_uint16(code_point);
|
|
56
|
+
}
|
|
57
|
+
*result++ = code_point;
|
|
58
|
+
pos += 2;
|
|
59
|
+
} else if ((leading_byte & 0b11110000) == 0b11100000) {
|
|
60
|
+
if (pos + 2 >= len) {
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
uint16_t code_point = ((leading_byte & 0b00001111) << 12) | ((data[pos + 1] & 0b00111111) << 6) | (data[pos + 2] & 0b00111111);
|
|
64
|
+
if (utf_is_be()) {
|
|
65
|
+
code_point = utf_swap_uint16(code_point);
|
|
66
|
+
}
|
|
67
|
+
*result++ = code_point;
|
|
68
|
+
pos += 3;
|
|
69
|
+
} else if ((leading_byte & 0b11111000) == 0b11110000) {
|
|
70
|
+
if (pos + 3 >= len) {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
uint32_t code_point = ((leading_byte & 0b00000111) << 18) | ((data[pos + 1] & 0b00111111) << 12) | ((data[pos + 2] & 0b00111111) << 6) | (data[pos + 3] & 0b00111111);
|
|
74
|
+
code_point -= 0x10000;
|
|
75
|
+
uint16_t high_surrogate = 0xd800 + (code_point >> 10);
|
|
76
|
+
uint16_t low_surrogate = 0xdc00 + (code_point & 0x3ff);
|
|
77
|
+
if (utf_is_be()) {
|
|
78
|
+
high_surrogate = utf_swap_uint16(high_surrogate);
|
|
79
|
+
low_surrogate = utf_swap_uint16(low_surrogate);
|
|
80
|
+
}
|
|
81
|
+
*result++ = high_surrogate;
|
|
82
|
+
*result++ = low_surrogate;
|
|
83
|
+
pos += 4;
|
|
84
|
+
} else {
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return result - start;
|
|
90
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#include <stdbool.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
|
|
5
|
+
#include "../../include/utf.h"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Modified from https://github.com/simdutf/simdutf
|
|
9
|
+
*
|
|
10
|
+
* Copyright 2020 The simdutf authors
|
|
11
|
+
*
|
|
12
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
13
|
+
* you may not use this file except in compliance with the License.
|
|
14
|
+
* You may obtain a copy of the License at
|
|
15
|
+
*
|
|
16
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
17
|
+
*
|
|
18
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
19
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
20
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
* See the License for the specific language governing permissions and
|
|
22
|
+
* limitations under the License.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
size_t
|
|
26
|
+
utf16_length_from_utf8 (const utf8_t *data, size_t len) {
|
|
27
|
+
size_t counter = 0;
|
|
28
|
+
|
|
29
|
+
for (size_t i = 0; i < len; i++) {
|
|
30
|
+
if ((int8_t) data[i] > -65) {
|
|
31
|
+
counter++;
|
|
32
|
+
}
|
|
33
|
+
if (data[i] >= 240) {
|
|
34
|
+
counter++;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return counter;
|
|
39
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
#include <stdbool.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
|
|
6
|
+
#include "../../include/utf.h"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Modified from https://github.com/simdutf/simdutf
|
|
10
|
+
*
|
|
11
|
+
* Copyright 2020 The simdutf authors
|
|
12
|
+
*
|
|
13
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
* you may not use this file except in compliance with the License.
|
|
15
|
+
* You may obtain a copy of the License at
|
|
16
|
+
*
|
|
17
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
+
*
|
|
19
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
20
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
21
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
22
|
+
* See the License for the specific language governing permissions and
|
|
23
|
+
* limitations under the License.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
bool
|
|
27
|
+
utf8_validate (const utf8_t *data, size_t len) {
|
|
28
|
+
uint64_t pos = 0;
|
|
29
|
+
uint32_t code_point;
|
|
30
|
+
uint8_t word;
|
|
31
|
+
|
|
32
|
+
while (pos < len) {
|
|
33
|
+
uint64_t next_pos = pos + 16;
|
|
34
|
+
if (next_pos <= len) {
|
|
35
|
+
uint64_t v1;
|
|
36
|
+
memcpy(&v1, data + pos, sizeof(uint64_t));
|
|
37
|
+
uint64_t v2;
|
|
38
|
+
memcpy(&v2, data + pos + sizeof(uint64_t), sizeof(uint64_t));
|
|
39
|
+
uint64_t v = v1 | v2;
|
|
40
|
+
if ((v & 0x8080808080808080) == 0) {
|
|
41
|
+
pos = next_pos;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
word = data[pos];
|
|
47
|
+
while (word < 0b10000000) {
|
|
48
|
+
if (++pos == len) return true;
|
|
49
|
+
word = data[pos];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if ((word & 0b11100000) == 0b11000000) {
|
|
53
|
+
next_pos = pos + 2;
|
|
54
|
+
if (next_pos > len) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
if ((data[pos + 1] & 0b11000000) != 0b10000000) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
code_point = (word & 0b00011111) << 6 | (data[pos + 1] & 0b00111111);
|
|
61
|
+
if ((code_point < 0x80) || (0x7ff < code_point)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
} else if ((word & 0b11110000) == 0b11100000) {
|
|
65
|
+
next_pos = pos + 3;
|
|
66
|
+
if (next_pos > len) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
if ((data[pos + 1] & 0b11000000) != 0b10000000) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
if ((data[pos + 2] & 0b11000000) != 0b10000000) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
code_point = (word & 0b00001111) << 12 |
|
|
76
|
+
(data[pos + 1] & 0b00111111) << 6 |
|
|
77
|
+
(data[pos + 2] & 0b00111111);
|
|
78
|
+
if ((code_point < 0x800) || (0xffff < code_point) || (0xd7ff < code_point && code_point < 0xe000)) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
} else if ((word & 0b11111000) == 0b11110000) {
|
|
82
|
+
next_pos = pos + 4;
|
|
83
|
+
if (next_pos > len) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if ((data[pos + 1] & 0b11000000) != 0b10000000) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
if ((data[pos + 2] & 0b11000000) != 0b10000000) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if ((data[pos + 3] & 0b11000000) != 0b10000000) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
code_point = (word & 0b00000111) << 18 | (data[pos + 1] & 0b00111111) << 12 |
|
|
96
|
+
(data[pos + 2] & 0b00111111) << 6 | (data[pos + 3] & 0b00111111);
|
|
97
|
+
if (code_point <= 0xffff || 0x10ffff < code_point) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
pos = next_pos;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return true;
|
|
107
|
+
}
|
package/lib/constants.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
// Parser states
|
|
3
|
-
// https://url.spec.whatwg.org/#url-parsing
|
|
4
|
-
STATE_SCHEME_START: 1,
|
|
5
|
-
STATE_SCHEME: 2,
|
|
6
|
-
STATE_NO_SCHEME: 3,
|
|
7
|
-
STATE_SPECIAL_RELATIVE_OR_AUTHORITY: 4,
|
|
8
|
-
STATE_PATH_OR_AUTHORITY: 5,
|
|
9
|
-
STATE_RELATIVE: 6,
|
|
10
|
-
STATE_RELATIVE_SLASH: 7,
|
|
11
|
-
STATE_SPECIAL_AUTHORITY_SLASHES: 8,
|
|
12
|
-
STATE_SPECIAL_AUTHORITY_IGNORE_SLASHES: 9,
|
|
13
|
-
STATE_AUTHORITY: 10,
|
|
14
|
-
STATE_HOST: 11,
|
|
15
|
-
STATE_HOSTNAME: 12,
|
|
16
|
-
STATE_PORT: 13,
|
|
17
|
-
STATE_FILE: 14,
|
|
18
|
-
STATE_FILE_SLASH: 15,
|
|
19
|
-
STATE_FILE_HOST: 16,
|
|
20
|
-
STATE_PATH_START: 17,
|
|
21
|
-
STATE_PATH: 18,
|
|
22
|
-
STATE_OPAQUE_PATH: 19,
|
|
23
|
-
STATE_QUERY: 20,
|
|
24
|
-
STATE_FRAGMENT: 21
|
|
25
|
-
}
|
package/lib/infra.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
// https://infra.spec.whatwg.org/#ascii-digit
|
|
2
|
-
exports.isASCIIDigit = function isASCIIDigit (c) {
|
|
3
|
-
return c >= 0x30 && c <= 0x39
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
// https://infra.spec.whatwg.org/#ascii-upper-hex-digit
|
|
7
|
-
exports.isASCIIUpperHexDigit = function isASCIIUpperHexDigit (c) {
|
|
8
|
-
return exports.isASCIIDigit(c) || (c >= 0x41 && c <= 0x46)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// https://infra.spec.whatwg.org/#ascii-lower-hex-digit
|
|
12
|
-
exports.isASCIILowerHexDigit = function isASCIILowerHexDigit (c) {
|
|
13
|
-
return exports.isASCIIDigit(c) || (c >= 0x61 && c <= 0x66)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// https://infra.spec.whatwg.org/#ascii-hex-digit
|
|
17
|
-
exports.isASCIIHexDigit = function isASCIIHexDigit (c) {
|
|
18
|
-
return exports.isASCIIDigit(c) || (c >= 0x41 && c <= 0x46) || (c >= 0x61 && c <= 0x66)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// https://infra.spec.whatwg.org/#ascii-upper-alpha
|
|
22
|
-
exports.isASCIIUpperAlpha = function isASCIIUpperAlpha (c) {
|
|
23
|
-
return c >= 0x41 && c <= 0x5a
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// https://infra.spec.whatwg.org/#ascii-lower-alpha
|
|
27
|
-
exports.isASCIILowerAlpha = function isASCIILowerAlpha (c) {
|
|
28
|
-
return c >= 0x61 && c <= 0x7a
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// https://infra.spec.whatwg.org/#ascii-alpha
|
|
32
|
-
exports.isASCIIAlpha = function isASCIIAlpha (c) {
|
|
33
|
-
return exports.isASCIIUpperAlpha(c) || exports.isASCIILowerAlpha(c)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// https://infra.spec.whatwg.org/#ascii-alphanumeric
|
|
37
|
-
exports.isASCIIAlphanumeric = function isASCIIAlphanumeric (c) {
|
|
38
|
-
return exports.isASCIIDigit(c) || exports.isASCIIAlpha(c)
|
|
39
|
-
}
|