bare-buffer 2.5.4 → 2.5.6

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.
Files changed (49) hide show
  1. package/CMakeLists.txt +2 -2
  2. package/package.json +2 -1
  3. package/prebuilds/android-arm/bare-buffer.bare +0 -0
  4. package/prebuilds/android-arm64/bare-buffer.bare +0 -0
  5. package/prebuilds/android-ia32/bare-buffer.bare +0 -0
  6. package/prebuilds/android-x64/bare-buffer.bare +0 -0
  7. package/prebuilds/darwin-arm64/bare-buffer.bare +0 -0
  8. package/prebuilds/darwin-x64/bare-buffer.bare +0 -0
  9. package/prebuilds/ios-arm64/bare-buffer.bare +0 -0
  10. package/prebuilds/ios-arm64-simulator/bare-buffer.bare +0 -0
  11. package/prebuilds/ios-x64-simulator/bare-buffer.bare +0 -0
  12. package/prebuilds/linux-arm64/bare-buffer.bare +0 -0
  13. package/prebuilds/linux-x64/bare-buffer.bare +0 -0
  14. package/prebuilds/win32-arm64/bare-buffer.bare +0 -0
  15. package/prebuilds/win32-x64/bare-buffer.bare +0 -0
  16. package/vendor/libutf/CMakeLists.txt +23 -6
  17. package/vendor/libutf/include/utf/string.h +46 -19
  18. package/vendor/libutf/include/utf.h +87 -0
  19. package/vendor/libutf/src/ascii/validate.c +47 -0
  20. package/vendor/libutf/src/endianness.c +1 -1
  21. package/vendor/libutf/src/latin1/convert-to-utf16.c +37 -0
  22. package/vendor/libutf/src/latin1/convert-to-utf32.c +34 -0
  23. package/vendor/libutf/src/latin1/convert-to-utf8.c +58 -0
  24. package/vendor/libutf/src/latin1/length-from-utf16.c +26 -0
  25. package/vendor/libutf/src/latin1/length-from-utf32.c +26 -0
  26. package/vendor/libutf/src/latin1/length-from-utf8.c +34 -0
  27. package/vendor/libutf/src/utf16/convert-to-latin1.c +41 -0
  28. package/vendor/libutf/src/utf16/convert-to-utf32.c +56 -0
  29. package/vendor/libutf/src/utf16/{utf8-convert.c → convert-to-utf8.c} +1 -3
  30. package/vendor/libutf/src/utf16/length-from-latin1.c +26 -0
  31. package/vendor/libutf/src/utf16/length-from-utf32.c +33 -0
  32. package/vendor/libutf/src/{utf8/utf16-length.c → utf16/length-from-utf8.c} +0 -1
  33. package/vendor/libutf/src/utf16/validate.c +1 -1
  34. package/vendor/libutf/src/utf32/convert-to-latin1.c +40 -0
  35. package/vendor/libutf/src/utf32/convert-to-utf16.c +56 -0
  36. package/vendor/libutf/src/utf32/convert-to-utf8.c +71 -0
  37. package/vendor/libutf/src/utf32/length-from-latin1.c +26 -0
  38. package/vendor/libutf/src/utf32/length-from-utf16.c +35 -0
  39. package/vendor/libutf/src/utf32/length-from-utf8.c +35 -0
  40. package/vendor/libutf/src/utf32/validate.c +39 -0
  41. package/vendor/libutf/src/utf8/convert-to-latin1.c +70 -0
  42. package/vendor/libutf/src/utf8/{utf16-convert.c → convert-to-utf16.c} +10 -5
  43. package/vendor/libutf/src/utf8/convert-to-utf32.c +110 -0
  44. package/vendor/libutf/src/utf8/length-from-latin1.c +32 -0
  45. package/vendor/libutf/src/{utf16/utf8-length.c → utf8/length-from-utf16.c} +1 -2
  46. package/vendor/libutf/src/utf8/length-from-utf32.c +35 -0
  47. package/vendor/libutf/src/utf8/string.c +2 -1
  48. package/vendor/libutf/src/utf8/validate.c +9 -9
  49. /package/vendor/libutf/{include/utf → src}/endianness.h +0 -0
package/CMakeLists.txt CHANGED
@@ -1,8 +1,8 @@
1
1
  cmake_minimum_required(VERSION 3.25)
2
2
 
3
- project(bare_buffer C)
3
+ find_package(cmake-bare REQUIRED PATHS node_modules/cmake-bare)
4
4
 
5
- include(bare)
5
+ project(bare_buffer C)
6
6
 
7
7
  if(NOT TARGET utf)
8
8
  add_subdirectory(vendor/libutf EXCLUDE_FROM_ALL)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-buffer",
3
- "version": "2.5.4",
3
+ "version": "2.5.6",
4
4
  "description": "Native buffers for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -37,6 +37,7 @@
37
37
  "homepage": "https://github.com/holepunchto/bare-buffer#readme",
38
38
  "devDependencies": {
39
39
  "brittle": "^3.1.1",
40
+ "cmake-bare": "^1.1.6",
40
41
  "standard": "^17.0.0"
41
42
  }
42
43
  }
@@ -15,17 +15,33 @@ target_sources(
15
15
  utf
16
16
  INTERFACE
17
17
  include/utf.h
18
- include/utf/endianness.h
19
18
  include/utf/string.h
20
19
  PRIVATE
21
20
  src/endianness.c
22
- src/utf8/utf16-convert.c
23
- src/utf8/utf16-length.c
24
- src/utf8/validate.c
21
+ src/endianness.h
22
+ src/ascii/validate.c
23
+ src/utf8/convert-to-latin1.c
24
+ src/utf8/convert-to-utf16.c
25
+ src/utf8/convert-to-utf32.c
26
+ src/utf8/length-from-latin1.c
27
+ src/utf8/length-from-utf16.c
28
+ src/utf8/length-from-utf32.c
25
29
  src/utf8/string.c
26
- src/utf16/utf8-convert.c
27
- src/utf16/utf8-length.c
30
+ src/utf8/validate.c
31
+ src/utf16/convert-to-latin1.c
32
+ src/utf16/convert-to-utf8.c
33
+ src/utf16/convert-to-utf32.c
34
+ src/utf16/length-from-latin1.c
35
+ src/utf16/length-from-utf8.c
36
+ src/utf16/length-from-utf32.c
28
37
  src/utf16/validate.c
38
+ src/utf32/convert-to-latin1.c
39
+ src/utf32/convert-to-utf8.c
40
+ src/utf32/convert-to-utf16.c
41
+ src/utf32/length-from-latin1.c
42
+ src/utf32/length-from-utf8.c
43
+ src/utf32/length-from-utf16.c
44
+ src/utf32/validate.c
29
45
  )
30
46
 
31
47
  target_include_directories(
@@ -72,5 +88,6 @@ install(DIRECTORY include/utf DESTINATION include)
72
88
 
73
89
  if(PROJECT_IS_TOP_LEVEL)
74
90
  enable_testing()
91
+
75
92
  add_subdirectory(test)
76
93
  endif()
@@ -24,7 +24,10 @@ typedef struct utf8_string_view_s utf8_string_view_t;
24
24
  struct utf8_string_s {
25
25
  utf8_t *data;
26
26
  size_t len;
27
- size_t cap;
27
+ union {
28
+ size_t cap;
29
+ utf8_t buf[8];
30
+ };
28
31
  };
29
32
 
30
33
  struct utf8_string_view_s {
@@ -34,53 +37,77 @@ struct utf8_string_view_s {
34
37
 
35
38
  inline void
36
39
  utf8_string_init (utf8_string_t *string) {
40
+ string->data = string->buf;
37
41
  string->len = 0;
38
- string->cap = 0;
39
- string->data = NULL;
40
42
  }
41
43
 
42
44
  inline utf8_string_view_t
43
45
  utf8_string_view_init (const utf8_t *data, size_t len) {
44
- return (utf8_string_view_t){data, len};
46
+ utf8_string_view_t view = {data, len};
47
+ return view;
45
48
  }
46
49
 
47
50
  inline void
48
51
  utf8_string_destroy (utf8_string_t *string) {
49
- free(string->data);
52
+ if (string->data != string->buf) free(string->data);
50
53
  }
51
54
 
52
55
  inline int
53
56
  utf8_string_reserve (utf8_string_t *string, size_t len) {
54
- size_t cap = string->cap;
57
+ size_t cap = string->data == string->buf ? sizeof(string->buf) : string->cap;
55
58
 
56
- if (len < cap) return 0;
59
+ if (len <= cap) return 0;
57
60
 
58
- if (cap == 0) cap = sizeof(utf8_string_t) - 1;
61
+ // https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
62
+ len--;
63
+ len |= len >> 1;
64
+ len |= len >> 2;
65
+ len |= len >> 4;
66
+ len |= len >> 8;
67
+ len |= len >> 16;
68
+ if (sizeof(len) == 8) len |= len >> 32;
69
+ len++;
59
70
 
60
- while (cap < len) {
61
- cap = cap * 2 + 1;
62
- }
71
+ cap = len;
72
+
73
+ utf8_t *data;
63
74
 
64
- void *data = realloc(string->data, cap);
75
+ if (string->data == string->buf) {
76
+ data = (utf8_t *) malloc(cap);
77
+
78
+ memcpy(data, string->data, string->len);
79
+ } else {
80
+ data = (utf8_t *) realloc(string->data, cap);
81
+ }
65
82
 
66
83
  if (data == NULL) return -1;
67
84
 
68
- string->cap = cap;
69
85
  string->data = data;
86
+ string->cap = cap;
70
87
 
71
88
  return 0;
72
89
  }
73
90
 
74
91
  inline int
75
92
  utf8_string_shrink_to_fit (utf8_string_t *string) {
76
- size_t cap = string->len + 1;
93
+ if (string->data == string->buf) return 0;
77
94
 
78
- void *data = realloc(string->data, cap);
95
+ size_t cap = string->len;
79
96
 
80
- if (data == NULL) return -1;
97
+ if (cap <= sizeof(string->buf)) {
98
+ memcpy(string->buf, string->data, cap);
81
99
 
82
- string->cap = cap;
83
- string->data = data;
100
+ free(string->data);
101
+
102
+ string->data = string->buf;
103
+ } else {
104
+ utf8_t *data = (utf8_t *) realloc(string->data, cap);
105
+
106
+ if (data == NULL) return -1;
107
+
108
+ string->data = data;
109
+ string->cap = cap;
110
+ }
84
111
 
85
112
  return 0;
86
113
  }
@@ -96,7 +123,7 @@ utf8_string_clear (utf8_string_t *string) {
96
123
  }
97
124
 
98
125
  inline bool
99
- utf8_string_empty (utf8_string_t *string) {
126
+ utf8_string_empty (const utf8_string_t *string) {
100
127
  return string->len == 0;
101
128
  }
102
129
 
@@ -11,6 +11,9 @@ extern "C" {
11
11
 
12
12
  typedef uint_least8_t utf8_t;
13
13
  typedef uint_least16_t utf16_t;
14
+ typedef uint_least32_t utf32_t;
15
+ typedef uint_least8_t latin1_t;
16
+ typedef uint_least8_t ascii_t;
14
17
 
15
18
  /**
16
19
  * UTF-8
@@ -22,9 +25,21 @@ utf8_validate (const utf8_t *data, size_t len);
22
25
  size_t
23
26
  utf8_length_from_utf16le (const utf16_t *data, size_t len);
24
27
 
28
+ size_t
29
+ utf8_length_from_utf32 (const utf32_t *data, size_t len);
30
+
31
+ size_t
32
+ utf8_length_from_latin1 (const latin1_t *data, size_t len);
33
+
25
34
  size_t
26
35
  utf8_convert_to_utf16le (const utf8_t *data, size_t len, utf16_t *result);
27
36
 
37
+ size_t
38
+ utf8_convert_to_utf32 (const utf8_t *data, size_t len, utf32_t *result);
39
+
40
+ size_t
41
+ utf8_convert_to_latin1 (const utf8_t *data, size_t len, latin1_t *result);
42
+
28
43
  /**
29
44
  * UTF-16
30
45
  */
@@ -35,9 +50,81 @@ utf16le_validate (const utf16_t *data, size_t len);
35
50
  size_t
36
51
  utf16_length_from_utf8 (const utf8_t *data, size_t len);
37
52
 
53
+ size_t
54
+ utf16_length_from_utf32 (const utf32_t *data, size_t len);
55
+
56
+ size_t
57
+ utf16_length_from_latin1 (const latin1_t *data, size_t len);
58
+
38
59
  size_t
39
60
  utf16le_convert_to_utf8 (const utf16_t *data, size_t len, utf8_t *result);
40
61
 
62
+ size_t
63
+ utf16le_convert_to_utf32 (const utf16_t *data, size_t len, utf32_t *result);
64
+
65
+ size_t
66
+ utf16le_convert_to_latin1 (const utf16_t *data, size_t len, latin1_t *result);
67
+
68
+ /**
69
+ * UTF-32
70
+ */
71
+
72
+ bool
73
+ utf32_validate (const utf32_t *data, size_t len);
74
+
75
+ size_t
76
+ utf32_length_from_utf8 (const utf8_t *data, size_t len);
77
+
78
+ size_t
79
+ utf32_length_from_utf16le (const utf16_t *data, size_t len);
80
+
81
+ size_t
82
+ utf32_length_from_latin1 (const latin1_t *data, size_t len);
83
+
84
+ size_t
85
+ utf32_convert_to_utf8 (const utf32_t *data, size_t len, utf8_t *result);
86
+
87
+ size_t
88
+ utf32_convert_to_utf16le (const utf32_t *data, size_t len, utf16_t *result);
89
+
90
+ size_t
91
+ utf32_convert_to_latin1 (const utf32_t *data, size_t len, latin1_t *result);
92
+
93
+ /**
94
+ * Latin-1
95
+ */
96
+
97
+ size_t
98
+ latin1_length_from_utf8 (const utf8_t *data, size_t len);
99
+
100
+ size_t
101
+ latin1_length_from_utf16le (const utf16_t *data, size_t len);
102
+
103
+ size_t
104
+ latin1_length_from_utf32 (const utf32_t *data, size_t len);
105
+
106
+ size_t
107
+ latin1_convert_to_utf8 (const latin1_t *data, size_t len, utf8_t *result);
108
+
109
+ size_t
110
+ latin1_convert_to_utf16le (const latin1_t *data, size_t len, utf16_t *result);
111
+
112
+ size_t
113
+ latin1_convert_to_utf32 (const latin1_t *data, size_t len, utf32_t *result);
114
+
115
+ /**
116
+ * ASCII
117
+ */
118
+
119
+ bool
120
+ ascii_validate (const ascii_t *data, size_t len);
121
+
122
+ /**
123
+ * Strings
124
+ */
125
+
126
+ #include <utf/string.h>
127
+
41
128
  #ifdef __cplusplus
42
129
  }
43
130
  #endif
@@ -0,0 +1,47 @@
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
+ ascii_validate (const ascii_t *data, size_t len) {
28
+ uint64_t pos = 0;
29
+
30
+ for (; pos + 16 <= len; pos += 16) {
31
+ uint64_t v1;
32
+ memcpy(&v1, data + pos, sizeof(uint64_t));
33
+ uint64_t v2;
34
+ memcpy(&v2, data + pos + sizeof(uint64_t), sizeof(uint64_t));
35
+ uint64_t v = v1 | v2;
36
+ if ((v & 0x8080808080808080) != 0) {
37
+ return false;
38
+ }
39
+ }
40
+ for (; pos < len; pos++) {
41
+ if (data[pos] >= 0b10000000) {
42
+ return false;
43
+ }
44
+ }
45
+
46
+ return true;
47
+ }
@@ -1,7 +1,7 @@
1
1
  #include <stdbool.h>
2
2
  #include <stdint.h>
3
3
 
4
- #include "../include/utf/endianness.h"
4
+ #include "endianness.h"
5
5
 
6
6
  extern utf_endianness_t
7
7
  utf_endianness (void);
@@ -0,0 +1,37 @@
1
+ #include <stddef.h>
2
+ #include <stdint.h>
3
+ #include <string.h>
4
+
5
+ #include "../../include/utf.h"
6
+ #include "../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
+ latin1_convert_to_utf16le (const latin1_t *data, size_t len, utf16_t *result) {
28
+ size_t pos = 0;
29
+ utf16_t *start = result;
30
+
31
+ while (pos < len) {
32
+ *result++ = utf_is_be() ? utf_swap_uint16(data[pos]) : data[pos];
33
+ pos++;
34
+ }
35
+
36
+ return result - start;
37
+ }
@@ -0,0 +1,34 @@
1
+ #include <stddef.h>
2
+ #include <stdint.h>
3
+ #include <string.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
+ latin1_convert_to_utf32 (const latin1_t *data, size_t len, utf32_t *result) {
27
+ utf32_t *start = result;
28
+
29
+ for (size_t i = 0; i < len; i++) {
30
+ *result++ = data[i];
31
+ }
32
+
33
+ return result - start;
34
+ }
@@ -0,0 +1,58 @@
1
+ #include <stddef.h>
2
+ #include <stdint.h>
3
+ #include <string.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
+ latin1_convert_to_utf8 (const latin1_t *data, size_t len, utf8_t *result) {
27
+ size_t pos = 0;
28
+ utf8_t *start = result;
29
+
30
+ while (pos < len) {
31
+ if (pos + 16 <= len) {
32
+ uint64_t v1;
33
+ memcpy(&v1, data + pos, sizeof(uint64_t));
34
+ uint64_t v2;
35
+ memcpy(&v2, data + pos + sizeof(uint64_t), sizeof(uint64_t));
36
+ uint64_t v = v1 | v2;
37
+ if ((v & 0x8080808080808080) == 0) {
38
+ size_t final_pos = pos + 16;
39
+ while (pos < final_pos) {
40
+ *result++ = data[pos];
41
+ pos++;
42
+ }
43
+ continue;
44
+ }
45
+ }
46
+ unsigned char byte = data[pos];
47
+ if ((byte & 0x80) == 0) {
48
+ *result++ = byte;
49
+ pos++;
50
+ } else {
51
+ *result++ = (byte >> 6) | 0b11000000;
52
+ *result++ = (byte & 0b111111) | 0b10000000;
53
+ pos++;
54
+ }
55
+ }
56
+
57
+ return result - start;
58
+ }
@@ -0,0 +1,26 @@
1
+ #include <stddef.h>
2
+
3
+ #include "../../include/utf.h"
4
+
5
+ /**
6
+ * Modified from https://github.com/simdutf/simdutf
7
+ *
8
+ * Copyright 2020 The simdutf authors
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+
23
+ size_t
24
+ latin1_length_from_utf16le (const utf16_t *data, size_t len) {
25
+ return len;
26
+ }
@@ -0,0 +1,26 @@
1
+ #include <stddef.h>
2
+
3
+ #include "../../include/utf.h"
4
+
5
+ /**
6
+ * Modified from https://github.com/simdutf/simdutf
7
+ *
8
+ * Copyright 2020 The simdutf authors
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+
23
+ size_t
24
+ latin1_length_from_utf32 (const utf32_t *data, size_t len) {
25
+ return len;
26
+ }
@@ -0,0 +1,34 @@
1
+ #include <stddef.h>
2
+
3
+ #include "../../include/utf.h"
4
+
5
+ /**
6
+ * Modified from https://github.com/simdutf/simdutf
7
+ *
8
+ * Copyright 2020 The simdutf authors
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+
23
+ size_t
24
+ latin1_length_from_utf8 (const utf8_t *data, size_t len) {
25
+ size_t counter = 0;
26
+
27
+ for (size_t i = 0; i < len; i++) {
28
+ if ((int8_t) data[i] > -65) {
29
+ counter++;
30
+ }
31
+ }
32
+
33
+ return counter;
34
+ }
@@ -0,0 +1,41 @@
1
+ #include <stddef.h>
2
+ #include <stdint.h>
3
+
4
+ #include "../../include/utf.h"
5
+ #include "../endianness.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
+ utf16le_convert_to_latin1 (const utf16_t *data, size_t len, latin1_t *result) {
27
+ size_t pos = 0;
28
+ uint16_t word, overflow = 0;
29
+ latin1_t *start = result;
30
+
31
+ while (pos < len) {
32
+ word = utf_is_be() ? utf_swap_uint16(data[pos]) : data[pos];
33
+ overflow |= word;
34
+ *result++ = word & 0xff;
35
+ pos++;
36
+ }
37
+
38
+ if (overflow & 0xff00) return 0;
39
+
40
+ return result - start;
41
+ }