Debian Codegraph @

third_party/rust/encoding_c_mem/include/encoding_rs_mem.h

raw
1 // Copyright Mozilla Foundation. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #ifndef encoding_rs_mem_h_
11 #define encoding_rs_mem_h_
12
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16
17 /*
18 * _Note:_ "Latin1" in this header refers to the Unicode range from U+0000 to
19 * U+00FF, inclusive, and does not refer to the windows-1252 range. This
20 * in-memory encoding is sometimes used as a storage optimization of text
21 * when UTF-16 indexing and length semantics are exposed.
22 */
23
24 /**
25 * Classification of text as Latin1 (all code points are below U+0100),
26 * left-to-right with some non-Latin1 characters or as containing at least
27 * some right-to-left characters.
28 */
29 typedef enum {
30 /**
31 * Every character is below U+0100.
32 */
33 Latin1 = 0,
34 /**
35 * There is at least one character that's U+0100 or higher, but there
36 * are no right-to-left characters.
37 */
38 LeftToRight = 1,
39 /**
40 * There is at least one right-to-left character.
41 */
42 Bidi = 2,
43 } Latin1Bidi;
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif // __cplusplus
48
49 /**
50 * Checks whether a valid UTF-8 buffer contains code points
51 * that trigger right-to-left processing or is all-Latin1.
52 *
53 * Possibly more efficient than performing the checks separately.
54 *
55 * Returns `Latin1Bidi::Latin1` if `is_str_latin1()` would return `true`.
56 * Otherwise, returns `Latin1Bidi::Bidi` if `is_str_bidi()` would return
57 * `true`. Otherwise, returns `Latin1Bidi::LeftToRight`.
58 *
59 * # Undefined behavior
60 *
61 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block,
62 * if `buffer` is `NULL`, or if the memory designated by `buffer` and
63 * `buffer_len` does not contain valid UTF-8. (If `buffer_len` is `0`, `buffer`
64 * may be bogus but still has to be non-`NULL`.)
65 */
66 Latin1Bidi encoding_mem_check_str_for_latin1_and_bidi(const char* buffer,
67 size_t len);
68
69 /**
70 * Checks whether a potentially invalid UTF-16 buffer contains code points
71 * that trigger right-to-left processing or is all-Latin1.
72 *
73 * Possibly more efficient than performing the checks separately.
74 *
75 * Returns `Latin1Bidi::Latin1` if `is_utf16_latin1()` would return `true`.
76 * Otherwise, returns `Latin1Bidi::Bidi` if `is_utf16_bidi()` would return
77 * `true`. Otherwise, returns `Latin1Bidi::LeftToRight`.
78 *
79 * # Undefined behavior
80 *
81 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
82 * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
83 * still has to be non-`NULL` and aligned.)
84 */
85 Latin1Bidi encoding_mem_check_utf16_for_latin1_and_bidi(const char16_t* buffer,
86 size_t len);
87
88 /**
89 * Checks whether a potentially invalid UTF-8 buffer contains code points
90 * that trigger right-to-left processing or is all-Latin1.
91 *
92 * Possibly more efficient than performing the checks separately.
93 *
94 * Returns `Latin1Bidi::Latin1` if `is_utf8_latin1()` would return `true`.
95 *
96 * Otherwise, returns `Latin1Bidi::Bidi` if `is_utf8_bidi()` would return
97 * `true`. Otherwise, returns `Latin1Bidi::LeftToRight`.
98 *
99 * # Undefined behavior
100 *
101 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
102 * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
103 * still has to be non-`NULL`.)
104 */
105 Latin1Bidi encoding_mem_check_utf8_for_latin1_and_bidi(const char* buffer,
106 size_t len);
107
108 /**
109 * Converts bytes whose unsigned value is interpreted as Unicode code point
110 * (i.e. U+0000 to U+00FF, inclusive) to UTF-16.
111 *
112 * The length of the destination buffer must be at least the length of the
113 * source buffer.
114 *
115 * The number of `char16_t`s written equals the length of the source buffer.
116 *
117 * # Panics
118 *
119 * Panics if the destination buffer is shorter than stated above.
120 *
121 * # Undefined behavior
122 *
123 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
124 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
125 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
126 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
127 * aligned. Likewise for `dst` and `dst_len`.)
128 */
129 void encoding_mem_convert_latin1_to_utf16(const char* src, size_t src_len,
130 char16_t* dst, size_t dst_len);
131
132 /**
133 * Converts bytes whose unsigned value is interpreted as Unicode code point
134 * (i.e. U+0000 to U+00FF, inclusive) to UTF-8.
135 *
136 * The length of the destination buffer must be at least the length of the
137 * source buffer times two.
138 *
139 * Returns the number of bytes written.
140 *
141 * # Panics
142 *
143 * Panics if the destination buffer is shorter than stated above.
144 *
145 * # Safety
146 *
147 * Note that this function may write garbage beyond the number of bytes
148 * indicated by the return value.
149 *
150 * # Undefined behavior
151 *
152 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
153 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
154 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
155 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
156 * aligned. Likewise for `dst` and `dst_len`.)
157 */
158 size_t encoding_mem_convert_latin1_to_utf8(const char* src, size_t src_len,
159 char* dst, size_t dst_len);
160
161 /**
162 * Converts bytes whose unsigned value is interpreted as Unicode code point
163 * (i.e. U+0000 to U+00FF, inclusive) to UTF-8 with potentially insufficient
164 * output space.
165 *
166 * Writes the number of code units read into `*src_len` and the number of
167 * bytes written into `*dst_len`.
168 *
169 * If the output isn't large enough, not all input is consumed.
170 *
171 * # Undefined behavior
172 *
173 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
174 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
175 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
176 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
177 * aligned. Likewise for `dst` and `dst_len`.)
178 */
179 void encoding_mem_convert_latin1_to_utf8_partial(const char* src,
180 size_t* src_len, char* dst,
181 size_t* dst_len);
182
183 /**
184 * Converts valid UTF-8 to valid UTF-16.
185 *
186 * The length of the destination buffer must be at least the length of the
187 * source buffer.
188 *
189 * Returns the number of `char16_t`s written.
190 *
191 * # Panics
192 *
193 * Panics if the destination buffer is shorter than stated above.
194 *
195 * # Undefined behavior
196 *
197 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
198 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
199 * block, if `dst` is `NULL`, if the two memory blocks overlap, of if the
200 * buffer designated by `src` and `src_len` does not contain valid UTF-8. (If
201 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
202 * aligned. Likewise for `dst` and `dst_len`.)
203 */
204 size_t encoding_mem_convert_str_to_utf16(const char* src, size_t src_len,
205 char16_t* dst, size_t dst_len);
206
207 /**
208 * If the input is valid UTF-16 representing only Unicode code points from
209 * U+0000 to U+00FF, inclusive, converts the input into output that
210 * represents the value of each code point as the unsigned byte value of
211 * each output byte.
212 *
213 * If the input does not fulfill the condition stated above, does something
214 * that is memory-safe without any promises about any properties of the
215 * output and will probably assert in debug builds in future versions.
216 * In particular, callers shouldn't assume the output to be the same across
217 * crate versions or CPU architectures and should not assume that non-ASCII
218 * input can't map to ASCII output.
219 *
220 * The length of the destination buffer must be at least the length of the
221 * source buffer.
222 *
223 * The number of bytes written equals the length of the source buffer.
224 *
225 * # Panics
226 *
227 * Panics if the destination buffer is shorter than stated above.
228 * (Probably in future versions if debug assertions are enabled (and not
229 * fuzzing) and the input is not in the range U+0000 to U+00FF, inclusive.)
230 *
231 * # Undefined behavior
232 *
233 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
234 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
235 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
236 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
237 * aligned. Likewise for `dst` and `dst_len`.)
238 */
239 void encoding_mem_convert_utf16_to_latin1_lossy(const char16_t* src,
240 size_t src_len, char* dst,
241 size_t dst_len);
242
243 /**
244 * Converts potentially-invalid UTF-16 to valid UTF-8 with errors replaced
245 * with the REPLACEMENT CHARACTER.
246 *
247 * The length of the destination buffer must be at least the length of the
248 * source buffer times three.
249 *
250 * Returns the number of bytes written.
251 *
252 * # Panics
253 *
254 * Panics if the destination buffer is shorter than stated above.
255 *
256 * # Undefined behavior
257 *
258 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
259 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
260 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
261 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
262 * aligned. Likewise for `dst` and `dst_len`.)
263 */
264 size_t encoding_mem_convert_utf16_to_utf8(const char16_t* src, size_t src_len,
265 char* dst, size_t dst_len);
266
267 /**
268 * Converts potentially-invalid UTF-16 to valid UTF-8 with errors replaced
269 * with the REPLACEMENT CHARACTER with potentially insufficient output
270 * space.
271 *
272 * Writes the number of code units read into `*src_len` and the number of
273 * bytes written into `*dst_len`.
274 *
275 * Guarantees that the bytes in the destination beyond the number of
276 * bytes claimed as written by the second item of the return tuple
277 * are left unmodified.
278 *
279 * Not all code units are read if there isn't enough output space.
280 * Note that this method isn't designed for general streamability but for
281 * not allocating memory for the worst case up front. Specifically,
282 * if the input starts with or ends with an unpaired surrogate, those are
283 * replaced with the REPLACEMENT CHARACTER.
284 *
285 * Matches the semantics of `TextEncoder.encodeInto()` from the
286 * Encoding Standard.
287 *
288 * # Undefined behavior
289 *
290 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
291 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
292 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
293 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
294 * aligned. Likewise for `dst` and `dst_len`.)
295 */
296 void encoding_mem_convert_utf16_to_utf8_partial(const char16_t* src,
297 size_t* src_len, char* dst,
298 size_t* dst_len);
299
300 /**
301 * If the input is valid UTF-8 representing only Unicode code points from
302 * U+0000 to U+00FF, inclusive, converts the input into output that
303 * represents the value of each code point as the unsigned byte value of
304 * each output byte.
305 *
306 * If the input does not fulfill the condition stated above, this function
307 * panics if debug assertions are enabled (and fuzzing isn't) and otherwise
308 * does something that is memory-safe without any promises about any
309 * properties of the output. In particular, callers shouldn't assume the
310 * output to be the same across crate versions or CPU architectures and
311 * should not assume that non-ASCII input can't map to ASCII output.
312 * The length of the destination buffer must be at least the length of the
313 * source buffer.
314 *
315 * Returns the number of bytes written.
316 *
317 * # Panics
318 *
319 * Panics if the destination buffer is shorter than stated above.
320 * If debug assertions are enabled (and not fuzzing) and the input is
321 * not in the range U+0000 to U+00FF, inclusive.
322 *
323 * # Undefined behavior
324 *
325 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
326 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
327 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
328 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
329 * aligned. Likewise for `dst` and `dst_len`.)
330 */
331 size_t encoding_mem_convert_utf8_to_latin1_lossy(const char* src,
332 size_t src_len, char* dst,
333 size_t dst_len);
334
335 /**
336 * Converts potentially-invalid UTF-8 to valid UTF-16 with errors replaced
337 * with the REPLACEMENT CHARACTER.
338 *
339 * The length of the destination buffer must be at least the length of the
340 * source buffer _plus one_.
341 *
342 * Returns the number of `char16_t`s written.
343 *
344 * # Panics
345 *
346 * Panics if the destination buffer is shorter than stated above.
347 *
348 * # Undefined behavior
349 *
350 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
351 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
352 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
353 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
354 * aligned. Likewise for `dst` and `dst_len`.)
355 */
356 size_t encoding_mem_convert_utf8_to_utf16(const char* src, size_t src_len,
357 char16_t* dst, size_t dst_len);
358
359 /**
360 * Converts potentially-invalid UTF-8 to valid UTF-16 signaling on error.
361 *
362 * The length of the destination buffer must be at least the length of the
363 * source buffer.
364 *
365 * Returns the number of `char16_t`s written or `SIZE_MAX` if the input was
366 * invalid.
367 *
368 * When the input was invalid, some output may have been written.
369 *
370 * # Panics
371 *
372 * Panics if the destination buffer is shorter than stated above.
373 *
374 * # Undefined behavior
375 *
376 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
377 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
378 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
379 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
380 * aligned. Likewise for `dst` and `dst_len`.)
381 */
382 size_t encoding_mem_convert_utf8_to_utf16_without_replacement(const char* src,
383 size_t src_len,
384 char16_t* dst,
385 size_t dst_len);
386
387 /**
388 * Copies ASCII from source to destination up to the first non-ASCII byte
389 * (or the end of the input if it is ASCII in its entirety).
390 *
391 * The length of the destination buffer must be at least the length of the
392 * source buffer.
393 *
394 * Returns the number of bytes written.
395 *
396 * # Panics
397 *
398 * Panics if the destination buffer is shorter than stated above.
399 *
400 * # Undefined behavior
401 *
402 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
403 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
404 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
405 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
406 * aligned. Likewise for `dst` and `dst_len`.)
407 */
408 size_t encoding_mem_copy_ascii_to_ascii(const char* src, size_t src_len,
409 char* dst, size_t dst_len);
410
411 /**
412 * Copies ASCII from source to destination zero-extending it to UTF-16 up to
413 * the first non-ASCII byte (or the end of the input if it is ASCII in its
414 * entirety).
415 *
416 * The length of the destination buffer must be at least the length of the
417 * source buffer.
418 *
419 * Returns the number of `char16_t`s written.
420 *
421 * # Panics
422 *
423 * Panics if the destination buffer is shorter than stated above.
424 *
425 * # Undefined behavior
426 *
427 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
428 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
429 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
430 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
431 * aligned. Likewise for `dst` and `dst_len`.)
432 */
433 size_t encoding_mem_copy_ascii_to_basic_latin(const char* src, size_t src_len,
434 char16_t* dst, size_t dst_len);
435
436 /**
437 * Copies Basic Latin from source to destination narrowing it to ASCII up to
438 * the first non-Basic Latin code unit (or the end of the input if it is
439 * Basic Latin in its entirety).
440 *
441 * The length of the destination buffer must be at least the length of the
442 * source buffer.
443 *
444 * Returns the number of bytes written.
445 *
446 * # Panics
447 *
448 * Panics if the destination buffer is shorter than stated above.
449 *
450 * # Undefined behavior
451 *
452 * UB ensues if `src` and `src_len` don't designate a valid memory block, if
453 * `src` is `NULL`, if `dst` and `dst_len` don't designate a valid memory
454 * block, if `dst` is `NULL` or if the two memory blocks overlap. (If
455 * `src_len` is `0`, `src` may be bogus but still has to be non-`NULL` and
456 * aligned. Likewise for `dst` and `dst_len`.)
457 */
458 size_t encoding_mem_copy_basic_latin_to_ascii(const char16_t* src,
459 size_t src_len, char* dst,
460 size_t dst_len);
461
462 /**
463 * Replaces unpaired surrogates in the input with the REPLACEMENT CHARACTER.
464 *
465 * # Undefined behavior
466 *
467 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
468 * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
469 * still has to be non-`NULL` and aligned.)
470 */
471 void encoding_mem_ensure_utf16_validity(char16_t* buffer, size_t len);
472
473 /**
474 * Checks whether the buffer is all-ASCII.
475 *
476 * May read the entire buffer even if it isn't all-ASCII. (I.e. the function
477 * is not guaranteed to fail fast.)
478 *
479 * # Undefined behavior
480 *
481 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
482 * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
483 * still has to be non-`NULL`.)
484 */
485 bool encoding_mem_is_ascii(const char* buffer, size_t len);
486
487 /**
488 * Checks whether the buffer is all-Basic Latin (i.e. UTF-16 representing
489 * only ASCII characters).
490 *
491 * May read the entire buffer even if it isn't all-ASCII. (I.e. the function
492 * is not guaranteed to fail fast.)
493 *
494 * # Undefined behavior
495 *
496 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
497 * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
498 * still has to be non-`NULL` and aligned.)
499 */
500 bool encoding_mem_is_basic_latin(const char16_t* buffer, size_t len);
501
502 /**
503 * Checks whether a scalar value triggers right-to-left processing.
504 *
505 * The check is done on a Unicode block basis without regard to assigned
506 * vs. unassigned code points in the block. Hebrew presentation forms in
507 * the Alphabetic Presentation Forms block are treated as if they formed
508 * a block on their own (i.e. it treated as right-to-left). Additionally,
509 * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
510 * for. Control characters that are technically bidi controls but do not
511 * cause right-to-left behavior without the presence of right-to-left
512 * characters or right-to-left controls are not checked for. As a special
513 * case, U+FEFF is excluded from Arabic Presentation Forms-B.
514 *
515 * # Undefined behavior
516 *
517 * Undefined behavior ensues if `c` is not a valid Unicode Scalar Value.
518 */
519 bool encoding_mem_is_char_bidi(char32_t c);
520
521 /**
522 * Checks whether a valid UTF-8 buffer contains code points that trigger
523 * right-to-left processing.
524 *
525 * The check is done on a Unicode block basis without regard to assigned
526 * vs. unassigned code points in the block. Hebrew presentation forms in
527 * the Alphabetic Presentation Forms block are treated as if they formed
528 * a block on their own (i.e. it treated as right-to-left). Additionally,
529 * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
530 * for. Control characters that are technically bidi controls but do not
531 * cause right-to-left behavior without the presence of right-to-left
532 * characters or right-to-left controls are not checked for. As a special
533 * case, U+FEFF is excluded from Arabic Presentation Forms-B.
534 *
535 * # Undefined behavior
536 *
537 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block,
538 * if `buffer` is `NULL`, or if the memory designated by `buffer` and
539 * `buffer_len` does not contain valid UTF-8. (If `buffer_len` is `0`, `buffer`
540 * may be bogus but still has to be non-`NULL`.)
541 */
542 bool encoding_mem_is_str_bidi(const char* buffer, size_t len);
543
544 /**
545 * Checks whether the buffer represents only code points less than or equal
546 * to U+00FF.
547 *
548 * Fails fast. (I.e. returns before having read the whole buffer if code
549 * points above U+00FF are discovered.
550 *
551 * # Undefined behavior
552 *
553 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block,
554 * if `buffer` is `NULL`, or if the memory designated by `buffer` and
555 * `buffer_len` does not contain valid UTF-8. (If `buffer_len` is `0`, `buffer`
556 * may be bogus but still has to be non-`NULL`.)
557 */
558 bool encoding_mem_is_str_latin1(const char* buffer, size_t len);
559
560 /**
561 * Checks whether a UTF-16 buffer contains code points that trigger
562 * right-to-left processing.
563 *
564 * The check is done on a Unicode block basis without regard to assigned
565 * vs. unassigned code points in the block. Hebrew presentation forms in
566 * the Alphabetic Presentation Forms block are treated as if they formed
567 * a block on their own (i.e. it treated as right-to-left). Additionally,
568 * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
569 * for. Control characters that are technically bidi controls but do not
570 * cause right-to-left behavior without the presence of right-to-left
571 * characters or right-to-left controls are not checked for. As a special
572 * case, U+FEFF is excluded from Arabic Presentation Forms-B.
573 * Returns `true` if the input contains an RTL character or an unpaired
574 * high surrogate that could be the high half of an RTL character.
575 * Returns `false` if the input contains neither RTL characters nor
576 * unpaired high surrogates that could be higher halves of RTL characters.
577 *
578 * # Undefined behavior
579 *
580 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
581 * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
582 * still has to be non-`NULL` and aligned.)
583 */
584 bool encoding_mem_is_utf16_bidi(const char16_t* buffer, size_t len);
585
586 /**
587 * Checks whether a UTF-16 code unit triggers right-to-left processing.
588 *
589 * The check is done on a Unicode block basis without regard to assigned
590 * vs. unassigned code points in the block. Hebrew presentation forms in
591 * the Alphabetic Presentation Forms block are treated as if they formed
592 * a block on their own (i.e. it treated as right-to-left). Additionally,
593 * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
594 * for. Control characters that are technically bidi controls but do not
595 * cause right-to-left behavior without the presence of right-to-left
596 * characters or right-to-left controls are not checked for. As a special
597 * case, U+FEFF is excluded from Arabic Presentation Forms-B.
598 * Since supplementary-plane right-to-left blocks are identifiable from the
599 * high surrogate without examining the low surrogate, this function returns
600 * `true` for such high surrogates making the function suitable for handling
601 * supplementary-plane text without decoding surrogate pairs to scalar
602 * values. Obviously, such high surrogates are then reported as right-to-left
603 * even if actually unpaired.
604 */
605 bool encoding_mem_is_utf16_code_unit_bidi(char16_t u);
606
607 /**
608 * Checks whether the buffer represents only code point less than or equal
609 * to U+00FF.
610 *
611 * May read the entire buffer even if it isn't all-Latin1. (I.e. the function
612 * is not guaranteed to fail fast.)
613 *
614 * # Undefined behavior
615 *
616 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
617 * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
618 * still has to be non-`NULL` and aligned.)
619 */
620 bool encoding_mem_is_utf16_latin1(const char16_t* buffer, size_t len);
621
622 /**
623 * Checks whether a potentially-invalid UTF-8 buffer contains code points
624 * that trigger right-to-left processing.
625 *
626 * The check is done on a Unicode block basis without regard to assigned
627 * vs. unassigned code points in the block. Hebrew presentation forms in
628 * the Alphabetic Presentation Forms block are treated as if they formed
629 * a block on their own (i.e. it treated as right-to-left). Additionally,
630 * the four RIGHT-TO-LEFT FOO controls in General Punctuation are checked
631 * for. Control characters that are technically bidi controls but do not
632 * cause right-to-left behavior without the presence of right-to-left
633 * characters or right-to-left controls are not checked for. As a special
634 * case, U+FEFF is excluded from Arabic Presentation Forms-B.
635 * Returns `true` if the input is invalid UTF-8 or the input contains an
636 * RTL character. Returns `false` if the input is valid UTF-8 and contains
637 * no RTL characters.
638 *
639 * # Undefined behavior
640 *
641 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
642 * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
643 * still has to be non-`NULL`.)
644 */
645 bool encoding_mem_is_utf8_bidi(const char* buffer, size_t len);
646
647 /**
648 * Checks whether the buffer is valid UTF-8 representing only code points
649 * less than or equal to U+00FF.
650 *
651 * Fails fast. (I.e. returns before having read the whole buffer if UTF-8
652 * invalidity or code points above U+00FF are discovered.
653 *
654 * # Undefined behavior
655 *
656 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
657 * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
658 * still has to be non-`NULL`.)
659 */
660 bool encoding_mem_is_utf8_latin1(const char* buffer, size_t len);
661
662 /**
663 * Returns the index of the first unpaired surrogate or, if the input is
664 * valid UTF-16 in its entirety, the length of the input.
665 *
666 * # Undefined behavior
667 *
668 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
669 * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
670 * still has to be non-`NULL` and aligned.)
671 */
672 size_t encoding_mem_utf16_valid_up_to(const char16_t* buffer, size_t len);
673
674 /**
675 * Returns the index of first byte that starts an invalid byte
676 * sequence or a non-Latin1 byte sequence, or the length of the
677 * string if there are neither.
678 *
679 * # Undefined behavior
680 *
681 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block
682 * or if `buffer` is `NULL`. (If `buffer_len` is `0`, `buffer` may be bogus but
683 * still has to be non-`NULL` and aligned.)
684 */
685 size_t encoding_mem_utf8_latin1_up_to(const char* buffer, size_t len);
686
687 /**
688 * Returns the index of first byte that starts a non-Latin1 byte
689 * sequence, or the length of the string if there are none.
690 *
691 * # Undefined behavior
692 *
693 * UB ensues if `buffer` and `buffer_len` don't designate a valid memory block,
694 * if `buffer` is `NULL`, or if the memory block does not contain valid UTF-8.
695 * (If `buffer_len` is `0`, `buffer` may be bogus but still has to be non-`NULL`
696 * and aligned.)
697 */
698 size_t encoding_mem_str_latin1_up_to(const char* buffer, size_t len);
699
700 #ifdef __cplusplus
701 } // extern "C"
702 #endif // __cplusplus
703
704 #endif // encoding_rs_mem_h_