Coverage Report

Created: 2026-12-01 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/include/simdutf/implementation.h
Line
Count
Source
1
#ifndef SIMDUTF_IMPLEMENTATION_H
2
#define SIMDUTF_IMPLEMENTATION_H
3
#if !defined(SIMDUTF_NO_THREADS)
4
  #include <atomic>
5
#endif
6
#ifdef SIMDUTF_INTERNAL_TESTS
7
  #include <vector>
8
#endif
9
#include "simdutf/common_defs.h"
10
#include "simdutf/compiler_check.h"
11
#include "simdutf/encoding_types.h"
12
#include "simdutf/error.h"
13
#include "simdutf/internal/isadetection.h"
14
15
#include <string_view>
16
#if SIMDUTF_SPAN
17
  #include <concepts>
18
  #include <type_traits>
19
  #include <span>
20
  #include <tuple>
21
  #include <utility> // for std::unreachable
22
#endif
23
// The following defines are conditionally enabled/disabled during amalgamation.
24
// By default all features are enabled, regular code shouldn't check them. Only
25
// when user code really relies of a selected subset, it's good to verify these
26
// flags, like:
27
//
28
//      #if !SIMDUTF_FEATURE_UTF16
29
//      #   error("Please amalgamate simdutf with UTF-16 support")
30
//      #endif
31
//
32
#ifndef SIMDUTF_FEATURE_DETECT_ENCODING
33
  #define SIMDUTF_FEATURE_DETECT_ENCODING 1
34
#endif
35
#ifndef SIMDUTF_FEATURE_ASCII
36
  #define SIMDUTF_FEATURE_ASCII 1
37
#endif
38
#ifndef SIMDUTF_FEATURE_LATIN1
39
  #define SIMDUTF_FEATURE_LATIN1 1
40
#endif
41
#ifndef SIMDUTF_FEATURE_UTF8
42
  #define SIMDUTF_FEATURE_UTF8 1
43
#endif
44
#ifndef SIMDUTF_FEATURE_UTF16
45
  #define SIMDUTF_FEATURE_UTF16 1
46
#endif
47
#ifndef SIMDUTF_FEATURE_UTF32
48
  #define SIMDUTF_FEATURE_UTF32 1
49
#endif
50
#ifndef SIMDUTF_FEATURE_BASE64
51
  #define SIMDUTF_FEATURE_BASE64 1
52
#endif
53
54
/// helpers placed in namespace detail are not a part of the public API
55
namespace simdutf {
56
namespace detail {
57
namespace {
58
// this is to avoid including <algorithm> just for min
59
0
constexpr std::size_t min(std::size_t a, std::size_t b) {
60
0
  return a < b ? a : b;
61
0
}
62
template <typename T, typename U>
63
constexpr std::size_t min(const T &a, const U &b) = delete;
64
} // namespace
65
} // namespace detail
66
} // namespace simdutf
67
68
#if SIMDUTF_CPLUSPLUS23
69
  #include <simdutf/constexpr_ptr.h>
70
#endif
71
72
#if SIMDUTF_SPAN
73
/// helpers placed in namespace detail are not a part of the public API
74
namespace simdutf {
75
namespace detail {
76
/**
77
 * matches a byte, in the many ways C++ allows. note that these
78
 * are all distinct types.
79
 */
80
template <typename T>
81
concept byte_like = std::is_same_v<T, std::byte> ||     //
82
                    std::is_same_v<T, char> ||          //
83
                    std::is_same_v<T, signed char> ||   //
84
                    std::is_same_v<T, unsigned char> || //
85
                    std::is_same_v<T, char8_t>;
86
87
template <typename T>
88
concept is_byte_like = byte_like<std::remove_cvref_t<T>>;
89
90
template <typename T>
91
concept is_pointer = std::is_pointer_v<T>;
92
93
/**
94
 * matches anything that behaves like std::span and points to character-like
95
 * data such as: std::byte, char, unsigned char, signed char, std::int8_t,
96
 * std::uint8_t
97
 */
98
template <typename T>
99
concept input_span_of_byte_like = requires(const T &t) {
100
  { t.size() } noexcept -> std::convertible_to<std::size_t>;
101
  { t.data() } noexcept -> is_pointer;
102
  { *t.data() } noexcept -> is_byte_like;
103
};
104
105
template <typename T>
106
concept is_mutable = !std::is_const_v<std::remove_reference_t<T>>;
107
108
/**
109
 * like span_of_byte_like, but for an output span (intended to be written to)
110
 */
111
template <typename T>
112
concept output_span_of_byte_like = requires(T &t) {
113
  { t.size() } noexcept -> std::convertible_to<std::size_t>;
114
  { t.data() } noexcept -> is_pointer;
115
  { *t.data() } noexcept -> is_byte_like;
116
  { *t.data() } noexcept -> is_mutable;
117
};
118
119
  #if SIMDUTF_CPLUSPLUS23
120
// Give the scalar base64 code a char copy of a non-char span (constexpr only).
121
template <typename F> constexpr auto with_char_input(const auto &input, F &&f) {
122
  using T = std::remove_cvref_t<decltype(*input.data())>;
123
  if constexpr (std::is_same_v<T, char>) {
124
    return f(input.data());
125
  } else {
126
    char *copy = new char[input.size()];
127
    for (std::size_t i = 0; i < input.size(); i++) {
128
      copy[i] = static_cast<char>(input.data()[i]);
129
    }
130
    auto r = f(static_cast<const char *>(copy));
131
    delete[] copy;
132
    return r;
133
  }
134
}
135
136
// Copy back only the written(r) bytes, leaving the rest of the output alone.
137
template <typename F, typename W>
138
constexpr auto with_char_output(auto &&output, F &&f, W &&written) {
139
  using T = std::remove_cvref_t<decltype(*output.data())>;
140
  if constexpr (std::is_same_v<T, char>) {
141
    return f(output.data());
142
  } else {
143
    char *copy = new char[output.size()];
144
    auto r = f(copy);
145
    const std::size_t n = written(r);
146
    for (std::size_t i = 0; i < n; i++) {
147
      output.data()[i] = static_cast<T>(copy[i]);
148
    }
149
    delete[] copy;
150
    return r;
151
  }
152
}
153
  #endif // SIMDUTF_CPLUSPLUS23
154
155
/**
156
 * a pointer like object, when indexed, results in a byte like result.
157
 * valid examples: char*, const char*, std::array<char,10>
158
 * invalid examples: int*, std::array<int,10>
159
 */
160
template <class InputPtr>
161
concept indexes_into_byte_like = requires(InputPtr p) {
162
  { std::decay_t<decltype(p[0])>{} } -> simdutf::detail::byte_like;
163
};
164
template <class InputPtr>
165
concept indexes_into_utf16 = requires(InputPtr p) {
166
  { std::decay_t<decltype(p[0])>{} } -> std::same_as<char16_t>;
167
};
168
template <class InputPtr>
169
concept indexes_into_utf32 = requires(InputPtr p) {
170
  { std::decay_t<decltype(p[0])>{} } -> std::same_as<char32_t>;
171
};
172
173
template <class InputPtr>
174
concept index_assignable_from_char = requires(InputPtr p, char s) {
175
  { p[0] = s };
176
};
177
178
/**
179
 * a pointer like object that results in a uint32_t when indexed.
180
 * valid examples: uint32_t*
181
 */
182
template <class InputPtr>
183
concept indexes_into_uint32 = requires(InputPtr p) {
184
  { std::decay_t<decltype(p[0])>{} } -> std::same_as<std::uint32_t>;
185
};
186
} // namespace detail
187
} // namespace simdutf
188
#endif // SIMDUTF_SPAN
189
190
// these includes are needed for constexpr support. they are
191
// not part of the public api.
192
#include <simdutf/scalar/swap_bytes.h>
193
#include <simdutf/scalar/ascii.h>
194
#include <simdutf/scalar/atomic_util.h>
195
#include <simdutf/scalar/latin1.h>
196
#include <simdutf/scalar/latin1_to_utf16/latin1_to_utf16.h>
197
#include <simdutf/scalar/latin1_to_utf32/latin1_to_utf32.h>
198
#include <simdutf/scalar/latin1_to_utf8/latin1_to_utf8.h>
199
#include <simdutf/scalar/utf16.h>
200
#include <simdutf/scalar/utf16_to_latin1/utf16_to_latin1.h>
201
#include <simdutf/scalar/utf16_to_latin1/valid_utf16_to_latin1.h>
202
#include <simdutf/scalar/utf16_to_utf32/utf16_to_utf32.h>
203
#include <simdutf/scalar/utf16_to_utf32/valid_utf16_to_utf32.h>
204
#include <simdutf/scalar/utf16_to_utf8/utf16_to_utf8.h>
205
#include <simdutf/scalar/utf16_to_utf8/valid_utf16_to_utf8.h>
206
#include <simdutf/scalar/utf32.h>
207
#include <simdutf/scalar/utf32_to_latin1/utf32_to_latin1.h>
208
#include <simdutf/scalar/utf32_to_latin1/valid_utf32_to_latin1.h>
209
#include <simdutf/scalar/utf32_to_utf16/utf32_to_utf16.h>
210
#include <simdutf/scalar/utf32_to_utf16/valid_utf32_to_utf16.h>
211
#include <simdutf/scalar/utf32_to_utf8/utf32_to_utf8.h>
212
#include <simdutf/scalar/utf32_to_utf8/valid_utf32_to_utf8.h>
213
#include <simdutf/scalar/utf8.h>
214
#include <simdutf/scalar/utf8_to_latin1/utf8_to_latin1.h>
215
#include <simdutf/scalar/utf8_to_latin1/valid_utf8_to_latin1.h>
216
#include <simdutf/scalar/utf8_to_utf16/utf8_to_utf16.h>
217
#include <simdutf/scalar/utf8_to_utf16/utf8_to_utf16_with_replacement.h>
218
#include <simdutf/scalar/utf8_to_utf16/valid_utf8_to_utf16.h>
219
#include <simdutf/scalar/utf8_to_utf32/utf8_to_utf32.h>
220
#include <simdutf/scalar/utf8_to_utf32/valid_utf8_to_utf32.h>
221
222
namespace simdutf {
223
224
constexpr size_t default_line_length =
225
    76; ///< default line length for base64 encoding with lines
226
227
#if SIMDUTF_FEATURE_DETECT_ENCODING
228
/**
229
 * Autodetect the encoding of the input, a single encoding is recommended.
230
 * E.g., the function might return simdutf::encoding_type::UTF8,
231
 * simdutf::encoding_type::UTF16_LE, simdutf::encoding_type::UTF16_BE, or
232
 * simdutf::encoding_type::UTF32_LE.
233
 *
234
 * @param input the string to analyze.
235
 * @param length the length of the string in bytes.
236
 * @return the detected encoding type
237
 */
238
simdutf_warn_unused simdutf::encoding_type
239
autodetect_encoding(const char *input, size_t length) noexcept;
240
simdutf_really_inline simdutf_warn_unused simdutf::encoding_type
241
0
autodetect_encoding(const uint8_t *input, size_t length) noexcept {
242
0
  return autodetect_encoding(reinterpret_cast<const char *>(input), length);
243
0
}
244
  #if SIMDUTF_SPAN
245
/**
246
 * Autodetect the encoding of the input, a single encoding is recommended.
247
 * E.g., the function might return simdutf::encoding_type::UTF8,
248
 * simdutf::encoding_type::UTF16_LE, simdutf::encoding_type::UTF16_BE, or
249
 * simdutf::encoding_type::UTF32_LE.
250
 *
251
 * @param input the string to analyze. can be a anything span-like that has a
252
 * data() and size() that points to character data: std::string,
253
 * std::string_view, std::vector<char>, std::span<const std::byte> etc.
254
 * @return the detected encoding type
255
 */
256
simdutf_really_inline simdutf_warn_unused simdutf::encoding_type
257
autodetect_encoding(
258
    const detail::input_span_of_byte_like auto &input) noexcept {
259
  return autodetect_encoding(reinterpret_cast<const char *>(input.data()),
260
                             input.size());
261
}
262
  #endif // SIMDUTF_SPAN
263
264
/**
265
 * Autodetect the possible encodings of the input in one pass.
266
 * E.g., if the input might be UTF-16LE or UTF-8, this function returns
267
 * the value (simdutf::encoding_type::UTF8 | simdutf::encoding_type::UTF16_LE).
268
 *
269
 * Overridden by each implementation.
270
 *
271
 * @param input the string to analyze.
272
 * @param length the length of the string in bytes.
273
 * @return the detected encoding type
274
 */
275
simdutf_warn_unused int detect_encodings(const char *input,
276
                                         size_t length) noexcept;
277
simdutf_really_inline simdutf_warn_unused int
278
0
detect_encodings(const uint8_t *input, size_t length) noexcept {
279
0
  return detect_encodings(reinterpret_cast<const char *>(input), length);
280
0
}
281
  #if SIMDUTF_SPAN
282
simdutf_really_inline simdutf_warn_unused int
283
detect_encodings(const detail::input_span_of_byte_like auto &input) noexcept {
284
  return detect_encodings(reinterpret_cast<const char *>(input.data()),
285
                          input.size());
286
}
287
  #endif // SIMDUTF_SPAN
288
#endif   // SIMDUTF_FEATURE_DETECT_ENCODING
289
290
#if SIMDUTF_FEATURE_UTF8 || SIMDUTF_FEATURE_DETECT_ENCODING
291
/**
292
 * Validate the UTF-8 string. This function may be best when you expect
293
 * the input to be almost always valid. Otherwise, consider using
294
 * validate_utf8_with_errors.
295
 *
296
 * Overridden by each implementation.
297
 *
298
 * @param buf the UTF-8 string to validate.
299
 * @param len the length of the string in bytes.
300
 * @return true if and only if the string is valid UTF-8.
301
 */
302
simdutf_warn_unused bool validate_utf8(const char *buf, size_t len) noexcept;
303
  #if SIMDUTF_SPAN
304
simdutf_constexpr23 simdutf_really_inline simdutf_warn_unused bool
305
validate_utf8(const detail::input_span_of_byte_like auto &input) noexcept {
306
    #if SIMDUTF_CPLUSPLUS23
307
  if consteval {
308
    return scalar::utf8::validate(
309
        detail::constexpr_cast_ptr<uint8_t>(input.data()), input.size());
310
  } else
311
    #endif
312
  {
313
    return validate_utf8(reinterpret_cast<const char *>(input.data()),
314
                         input.size());
315
  }
316
}
317
  #endif // SIMDUTF_SPAN
318
#endif   // SIMDUTF_FEATURE_UTF8 || SIMDUTF_FEATURE_DETECT_ENCODING
319
320
#if SIMDUTF_FEATURE_UTF8
321
/**
322
 * Validate the UTF-8 string and stop on error.
323
 *
324
 * Overridden by each implementation.
325
 *
326
 * @param buf the UTF-8 string to validate.
327
 * @param len the length of the string in bytes.
328
 * @return a result pair struct (of type simdutf::result containing the two
329
 * fields error and count) with an error code and either position of the error
330
 * (in the input in code units) if any, or the number of code units validated if
331
 * successful.
332
 */
333
simdutf_warn_unused result validate_utf8_with_errors(const char *buf,
334
                                                     size_t len) noexcept;
335
  #if SIMDUTF_SPAN
336
simdutf_really_inline simdutf_constexpr23 simdutf_warn_unused result
337
validate_utf8_with_errors(
338
    const detail::input_span_of_byte_like auto &input) noexcept {
339
    #if SIMDUTF_CPLUSPLUS23
340
  if consteval {
341
    return scalar::utf8::validate_with_errors(
342
        detail::constexpr_cast_ptr<uint8_t>(input.data()), input.size());
343
  } else
344
    #endif
345
  {
346
    return validate_utf8_with_errors(
347
        reinterpret_cast<const char *>(input.data()), input.size());
348
  }
349
}
350
  #endif // SIMDUTF_SPAN
351
#endif   // SIMDUTF_FEATURE_UTF8
352
353
#if SIMDUTF_FEATURE_UTF8
354
/**
355
 * Validate the UTF-8 string and stop on error, while counting the number of
356
 * continuation bytes and of four-byte sequences in the valid prefix.
357
 *
358
 * Overridden by each implementation.
359
 *
360
 * These two counts are enough to derive, without a second pass over the input,
361
 * both the number of code points (input_count - continuation_count) and the
362
 * number of UTF-16 code units (input_count - continuation_count +
363
 * four_byte_count) of the valid prefix. The utf32_length() and utf16_length()
364
 * helpers return them.
365
 *
366
 * @param buf the UTF-8 string to validate.
367
 * @param len the length of the string in bytes.
368
 * @return a utf8_result struct with an error code, the number of bytes in the
369
 * valid prefix (input_count), and the counts of continuation bytes and of
370
 * four-byte sequences within it.
371
 */
372
simdutf_warn_unused utf8_result validate_utf8_with_counts(const char *buf,
373
                                                          size_t len) noexcept;
374
  #if SIMDUTF_SPAN
375
simdutf_really_inline simdutf_constexpr23 simdutf_warn_unused utf8_result
376
validate_utf8_with_counts(
377
    const detail::input_span_of_byte_like auto &input) noexcept {
378
    #if SIMDUTF_CPLUSPLUS23
379
  if consteval {
380
    return scalar::utf8::validate_with_counts(
381
        detail::constexpr_cast_ptr<uint8_t>(input.data()), input.size());
382
  } else
383
    #endif
384
  {
385
    return validate_utf8_with_counts(
386
        reinterpret_cast<const char *>(input.data()), input.size());
387
  }
388
}
389
  #endif // SIMDUTF_SPAN
390
#endif   // SIMDUTF_FEATURE_UTF8
391
392
#if SIMDUTF_FEATURE_ASCII
393
/**
394
 * Validate the ASCII string.
395
 *
396
 * Overridden by each implementation.
397
 *
398
 * @param buf the ASCII string to validate.
399
 * @param len the length of the string in bytes.
400
 * @return true if and only if the string is valid ASCII.
401
 */
402
simdutf_warn_unused bool validate_ascii(const char *buf, size_t len) noexcept;
403
  #if SIMDUTF_SPAN
404
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 bool
405
validate_ascii(const detail::input_span_of_byte_like auto &input) noexcept {
406
    #if SIMDUTF_CPLUSPLUS23
407
  if consteval {
408
    return scalar::ascii::validate(
409
        detail::constexpr_cast_ptr<std::uint8_t>(input.data()), input.size());
410
  } else
411
    #endif
412
  {
413
    return validate_ascii(reinterpret_cast<const char *>(input.data()),
414
                          input.size());
415
  }
416
}
417
  #endif // SIMDUTF_SPAN
418
419
/**
420
 * Validate the ASCII string and stop on error. It might be faster than
421
 * validate_utf8 when an error is expected to occur early.
422
 *
423
 * Overridden by each implementation.
424
 *
425
 * @param buf the ASCII string to validate.
426
 * @param len the length of the string in bytes.
427
 * @return a result pair struct (of type simdutf::result containing the two
428
 * fields error and count) with an error code and either position of the error
429
 * (in the input in code units) if any, or the number of code units validated if
430
 * successful.
431
 */
432
simdutf_warn_unused result validate_ascii_with_errors(const char *buf,
433
                                                      size_t len) noexcept;
434
  #if SIMDUTF_SPAN
435
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
436
validate_ascii_with_errors(
437
    const detail::input_span_of_byte_like auto &input) noexcept {
438
    #if SIMDUTF_CPLUSPLUS23
439
  if consteval {
440
    return scalar::ascii::validate_with_errors(
441
        detail::constexpr_cast_ptr<std::uint8_t>(input.data()), input.size());
442
  } else
443
    #endif
444
  {
445
    return validate_ascii_with_errors(
446
        reinterpret_cast<const char *>(input.data()), input.size());
447
  }
448
}
449
  #endif // SIMDUTF_SPAN
450
#endif   // SIMDUTF_FEATURE_ASCII
451
452
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_ASCII
453
/**
454
 * Validate the ASCII string as a UTF-16 sequence.
455
 * An UTF-16 sequence is considered an ASCII sequence
456
 * if it could be converted to an ASCII string losslessly.
457
 *
458
 * Overridden by each implementation.
459
 *
460
 * @param buf the UTF-16 string to validate.
461
 * @param len the length of the string in bytes.
462
 * @return true if and only if the string is valid ASCII.
463
 */
464
simdutf_warn_unused bool validate_utf16_as_ascii(const char16_t *buf,
465
                                                 size_t len) noexcept;
466
  #if SIMDUTF_SPAN
467
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 bool
468
0
validate_utf16_as_ascii(std::span<const char16_t> input) noexcept {
469
0
    #if SIMDUTF_CPLUSPLUS23
470
0
  if consteval {
471
0
    return scalar::utf16::validate_as_ascii<endianness::NATIVE>(input.data(),
472
0
                                                                input.size());
473
0
  } else
474
0
    #endif
475
0
  {
476
0
    return validate_utf16_as_ascii(input.data(), input.size());
477
0
  }
478
0
}
479
  #endif // SIMDUTF_SPAN
480
481
/**
482
 * Validate the ASCII string as a UTF-16BE sequence.
483
 * An UTF-16 sequence is considered an ASCII sequence
484
 * if it could be converted to an ASCII string losslessly.
485
 *
486
 * Overridden by each implementation.
487
 *
488
 * @param buf the UTF-16BE string to validate.
489
 * @param len the length of the string in bytes.
490
 * @return true if and only if the string is valid ASCII.
491
 */
492
simdutf_warn_unused bool validate_utf16be_as_ascii(const char16_t *buf,
493
                                                   size_t len) noexcept;
494
  #if SIMDUTF_SPAN
495
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 bool
496
0
validate_utf16be_as_ascii(std::span<const char16_t> input) noexcept {
497
0
    #if SIMDUTF_CPLUSPLUS23
498
0
  if consteval {
499
0
    return scalar::utf16::validate_as_ascii<endianness::BIG>(input.data(),
500
0
                                                             input.size());
501
0
  } else
502
0
    #endif
503
0
  {
504
0
    return validate_utf16be_as_ascii(input.data(), input.size());
505
0
  }
506
0
}
507
  #endif // SIMDUTF_SPAN
508
509
/**
510
 * Validate the ASCII string as a UTF-16LE sequence.
511
 * An UTF-16 sequence is considered an ASCII sequence
512
 * if it could be converted to an ASCII string losslessly.
513
 *
514
 * Overridden by each implementation.
515
 *
516
 * @param buf the UTF-16LE string to validate.
517
 * @param len the length of the string in bytes.
518
 * @return true if and only if the string is valid ASCII.
519
 */
520
simdutf_warn_unused bool validate_utf16le_as_ascii(const char16_t *buf,
521
                                                   size_t len) noexcept;
522
  #if SIMDUTF_SPAN
523
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 bool
524
0
validate_utf16le_as_ascii(std::span<const char16_t> input) noexcept {
525
0
    #if SIMDUTF_CPLUSPLUS23
526
0
  if consteval {
527
0
    return scalar::utf16::validate_as_ascii<endianness::LITTLE>(input.data(),
528
0
                                                                input.size());
529
0
  } else
530
0
    #endif
531
0
  {
532
0
    return validate_utf16le_as_ascii(input.data(), input.size());
533
0
  }
534
0
}
535
  #endif // SIMDUTF_SPAN
536
#endif   // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_ASCII
537
538
#if SIMDUTF_FEATURE_UTF16
539
/**
540
 * Using native endianness; Validate the UTF-16 string.
541
 * This function may be best when you expect the input to be almost always
542
 * valid. Otherwise, consider using validate_utf16_with_errors.
543
 *
544
 * Overridden by each implementation.
545
 *
546
 * This function is not BOM-aware.
547
 *
548
 * @param buf the UTF-16 string to validate.
549
 * @param len the length of the string in number of 2-byte code units
550
 * (char16_t).
551
 * @return true if and only if the string is valid UTF-16.
552
 */
553
simdutf_warn_unused bool validate_utf16(const char16_t *buf,
554
                                        size_t len) noexcept;
555
  #if SIMDUTF_SPAN
556
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 bool
557
0
validate_utf16(std::span<const char16_t> input) noexcept {
558
0
    #if SIMDUTF_CPLUSPLUS23
559
0
  if consteval {
560
0
    return scalar::utf16::validate<endianness::NATIVE>(input.data(),
561
0
                                                       input.size());
562
0
  } else
563
0
    #endif
564
0
  {
565
0
    return validate_utf16(input.data(), input.size());
566
0
  }
567
0
}
568
  #endif // SIMDUTF_SPAN
569
#endif   // SIMDUTF_FEATURE_UTF16
570
571
#if SIMDUTF_FEATURE_UTF16 || SIMDUTF_FEATURE_DETECT_ENCODING
572
/**
573
 * Validate the UTF-16LE string. This function may be best when you expect
574
 * the input to be almost always valid. Otherwise, consider using
575
 * validate_utf16le_with_errors.
576
 *
577
 * Overridden by each implementation.
578
 *
579
 * This function is not BOM-aware.
580
 *
581
 * @param buf the UTF-16LE string to validate.
582
 * @param len the length of the string in number of 2-byte code units
583
 * (char16_t).
584
 * @return true if and only if the string is valid UTF-16LE.
585
 */
586
simdutf_warn_unused bool validate_utf16le(const char16_t *buf,
587
                                          size_t len) noexcept;
588
  #if SIMDUTF_SPAN
589
simdutf_really_inline simdutf_constexpr23 simdutf_warn_unused bool
590
0
validate_utf16le(std::span<const char16_t> input) noexcept {
591
0
    #if SIMDUTF_CPLUSPLUS23
592
0
  if consteval {
593
0
    return scalar::utf16::validate<endianness::LITTLE>(input.data(),
594
0
                                                       input.size());
595
0
  } else
596
0
    #endif
597
0
  {
598
0
    return validate_utf16le(input.data(), input.size());
599
0
  }
600
0
}
601
  #endif // SIMDUTF_SPAN
602
#endif   // SIMDUTF_FEATURE_UTF16 || SIMDUTF_FEATURE_DETECT_ENCODING
603
604
#if SIMDUTF_FEATURE_UTF16
605
/**
606
 * Validate the UTF-16BE string. This function may be best when you expect
607
 * the input to be almost always valid. Otherwise, consider using
608
 * validate_utf16be_with_errors.
609
 *
610
 * Overridden by each implementation.
611
 *
612
 * This function is not BOM-aware.
613
 *
614
 * @param buf the UTF-16BE string to validate.
615
 * @param len the length of the string in number of 2-byte code units
616
 * (char16_t).
617
 * @return true if and only if the string is valid UTF-16BE.
618
 */
619
simdutf_warn_unused bool validate_utf16be(const char16_t *buf,
620
                                          size_t len) noexcept;
621
  #if SIMDUTF_SPAN
622
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 bool
623
0
validate_utf16be(std::span<const char16_t> input) noexcept {
624
0
    #if SIMDUTF_CPLUSPLUS23
625
0
  if consteval {
626
0
    return scalar::utf16::validate<endianness::BIG>(input.data(), input.size());
627
0
  } else
628
0
    #endif
629
0
  {
630
0
    return validate_utf16be(input.data(), input.size());
631
0
  }
632
0
}
633
  #endif // SIMDUTF_SPAN
634
635
/**
636
 * Using native endianness; Validate the UTF-16 string and stop on error.
637
 * It might be faster than validate_utf16 when an error is expected to occur
638
 * early.
639
 *
640
 * Overridden by each implementation.
641
 *
642
 * This function is not BOM-aware.
643
 *
644
 * @param buf the UTF-16 string to validate.
645
 * @param len the length of the string in number of 2-byte code units
646
 * (char16_t).
647
 * @return a result pair struct (of type simdutf::result containing the two
648
 * fields error and count) with an error code and either position of the error
649
 * (in the input in code units) if any, or the number of code units validated if
650
 * successful.
651
 */
652
simdutf_warn_unused result validate_utf16_with_errors(const char16_t *buf,
653
                                                      size_t len) noexcept;
654
  #if SIMDUTF_SPAN
655
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
656
0
validate_utf16_with_errors(std::span<const char16_t> input) noexcept {
657
0
    #if SIMDUTF_CPLUSPLUS23
658
0
  if consteval {
659
0
    return scalar::utf16::validate_with_errors<endianness::NATIVE>(
660
0
        input.data(), input.size());
661
0
  } else
662
0
    #endif
663
0
  {
664
0
    return validate_utf16_with_errors(input.data(), input.size());
665
0
  }
666
0
}
667
  #endif // SIMDUTF_SPAN
668
669
/**
670
 * Validate the UTF-16LE string and stop on error. It might be faster than
671
 * validate_utf16le when an error is expected to occur early.
672
 *
673
 * Overridden by each implementation.
674
 *
675
 * This function is not BOM-aware.
676
 *
677
 * @param buf the UTF-16LE string to validate.
678
 * @param len the length of the string in number of 2-byte code units
679
 * (char16_t).
680
 * @return a result pair struct (of type simdutf::result containing the two
681
 * fields error and count) with an error code and either position of the error
682
 * (in the input in code units) if any, or the number of code units validated if
683
 * successful.
684
 */
685
simdutf_warn_unused result validate_utf16le_with_errors(const char16_t *buf,
686
                                                        size_t len) noexcept;
687
  #if SIMDUTF_SPAN
688
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
689
0
validate_utf16le_with_errors(std::span<const char16_t> input) noexcept {
690
0
    #if SIMDUTF_CPLUSPLUS23
691
0
  if consteval {
692
0
    return scalar::utf16::validate_with_errors<endianness::LITTLE>(
693
0
        input.data(), input.size());
694
0
  } else
695
0
    #endif
696
0
  {
697
0
    return validate_utf16le_with_errors(input.data(), input.size());
698
0
  }
699
0
}
700
  #endif // SIMDUTF_SPAN
701
702
/**
703
 * Validate the UTF-16BE string and stop on error. It might be faster than
704
 * validate_utf16be when an error is expected to occur early.
705
 *
706
 * Overridden by each implementation.
707
 *
708
 * This function is not BOM-aware.
709
 *
710
 * @param buf the UTF-16BE string to validate.
711
 * @param len the length of the string in number of 2-byte code units
712
 * (char16_t).
713
 * @return a result pair struct (of type simdutf::result containing the two
714
 * fields error and count) with an error code and either position of the error
715
 * (in the input in code units) if any, or the number of code units validated if
716
 * successful.
717
 */
718
simdutf_warn_unused result validate_utf16be_with_errors(const char16_t *buf,
719
                                                        size_t len) noexcept;
720
  #if SIMDUTF_SPAN
721
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
722
0
validate_utf16be_with_errors(std::span<const char16_t> input) noexcept {
723
0
    #if SIMDUTF_CPLUSPLUS23
724
0
  if consteval {
725
0
    return scalar::utf16::validate_with_errors<endianness::BIG>(input.data(),
726
0
                                                                input.size());
727
0
  } else
728
0
    #endif
729
0
  {
730
0
    return validate_utf16be_with_errors(input.data(), input.size());
731
0
  }
732
0
}
733
  #endif // SIMDUTF_SPAN
734
735
/**
736
 * Fixes an ill-formed UTF-16LE string by replacing mismatched surrogates with
737
 * the Unicode replacement character U+FFFD. If input and output points to
738
 * different memory areas, the procedure copies string, and it's expected that
739
 * output memory is at least as big as the input. It's also possible to set
740
 * input equal output, that makes replacements an in-place operation.
741
 *
742
 * @param input the UTF-16LE string to correct.
743
 * @param len the length of the string in number of 2-byte code units
744
 * (char16_t).
745
 * @param output the output buffer.
746
 */
747
void to_well_formed_utf16le(const char16_t *input, size_t len,
748
                            char16_t *output) noexcept;
749
  #if SIMDUTF_SPAN
750
simdutf_really_inline simdutf_constexpr23 void
751
to_well_formed_utf16le(std::span<const char16_t> input,
752
0
                       std::span<char16_t> output) noexcept {
753
0
    #if SIMDUTF_CPLUSPLUS23
754
0
  if consteval {
755
0
    scalar::utf16::to_well_formed_utf16<endianness::LITTLE>(
756
0
        input.data(), input.size(), output.data());
757
0
  } else
758
0
    #endif
759
0
  {
760
0
    to_well_formed_utf16le(input.data(), input.size(), output.data());
761
0
  }
762
0
}
763
  #endif // SIMDUTF_SPAN
764
765
/**
766
 * Fixes an ill-formed UTF-16BE string by replacing mismatched surrogates with
767
 * the Unicode replacement character U+FFFD. If input and output points to
768
 * different memory areas, the procedure copies string, and it's expected that
769
 * output memory is at least as big as the input. It's also possible to set
770
 * input equal output, that makes replacements an in-place operation.
771
 *
772
 * @param input the UTF-16BE string to correct.
773
 * @param len the length of the string in number of 2-byte code units
774
 * (char16_t).
775
 * @param output the output buffer.
776
 */
777
void to_well_formed_utf16be(const char16_t *input, size_t len,
778
                            char16_t *output) noexcept;
779
  #if SIMDUTF_SPAN
780
simdutf_really_inline simdutf_constexpr23 void
781
to_well_formed_utf16be(std::span<const char16_t> input,
782
0
                       std::span<char16_t> output) noexcept {
783
0
    #if SIMDUTF_CPLUSPLUS23
784
0
  if consteval {
785
0
    scalar::utf16::to_well_formed_utf16<endianness::BIG>(
786
0
        input.data(), input.size(), output.data());
787
0
  } else
788
0
    #endif
789
0
  {
790
0
    to_well_formed_utf16be(input.data(), input.size(), output.data());
791
0
  }
792
0
}
793
  #endif // SIMDUTF_SPAN
794
795
/**
796
 * Fixes an ill-formed UTF-16 string by replacing mismatched surrogates with the
797
 * Unicode replacement character U+FFFD. If input and output points to different
798
 * memory areas, the procedure copies string, and it's expected that output
799
 * memory is at least as big as the input. It's also possible to set input equal
800
 * output, that makes replacements an in-place operation.
801
 *
802
 * @param input the UTF-16 string to correct.
803
 * @param len the length of the string in number of 2-byte code units
804
 * (char16_t).
805
 * @param output the output buffer.
806
 */
807
void to_well_formed_utf16(const char16_t *input, size_t len,
808
                          char16_t *output) noexcept;
809
  #if SIMDUTF_SPAN
810
simdutf_really_inline simdutf_constexpr23 void
811
to_well_formed_utf16(std::span<const char16_t> input,
812
0
                     std::span<char16_t> output) noexcept {
813
0
    #if SIMDUTF_CPLUSPLUS23
814
0
  if consteval {
815
0
    scalar::utf16::to_well_formed_utf16<endianness::NATIVE>(
816
0
        input.data(), input.size(), output.data());
817
0
  } else
818
0
    #endif
819
0
  {
820
0
    to_well_formed_utf16(input.data(), input.size(), output.data());
821
0
  }
822
0
}
823
  #endif // SIMDUTF_SPAN
824
825
#endif // SIMDUTF_FEATURE_UTF16
826
827
#if SIMDUTF_FEATURE_UTF32 || SIMDUTF_FEATURE_DETECT_ENCODING
828
/**
829
 * Validate the UTF-32 string. This function may be best when you expect
830
 * the input to be almost always valid. Otherwise, consider using
831
 * validate_utf32_with_errors.
832
 *
833
 * Overridden by each implementation.
834
 *
835
 * This function is not BOM-aware.
836
 *
837
 * @param buf the UTF-32 string to validate.
838
 * @param len the length of the string in number of 4-byte code units
839
 * (char32_t).
840
 * @return true if and only if the string is valid UTF-32.
841
 */
842
simdutf_warn_unused bool validate_utf32(const char32_t *buf,
843
                                        size_t len) noexcept;
844
  #if SIMDUTF_SPAN
845
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 bool
846
0
validate_utf32(std::span<const char32_t> input) noexcept {
847
0
    #if SIMDUTF_CPLUSPLUS23
848
0
  if consteval {
849
0
    return scalar::utf32::validate(
850
0
        detail::constexpr_cast_ptr<std::uint32_t>(input.data()), input.size());
851
0
  } else
852
0
    #endif
853
0
  {
854
0
    return validate_utf32(input.data(), input.size());
855
0
  }
856
0
}
857
  #endif // SIMDUTF_SPAN
858
#endif   // SIMDUTF_FEATURE_UTF32 || SIMDUTF_FEATURE_DETECT_ENCODING
859
860
#if SIMDUTF_FEATURE_UTF32
861
/**
862
 * Validate the UTF-32 string and stop on error. It might be faster than
863
 * validate_utf32 when an error is expected to occur early.
864
 *
865
 * Overridden by each implementation.
866
 *
867
 * This function is not BOM-aware.
868
 *
869
 * @param buf the UTF-32 string to validate.
870
 * @param len the length of the string in number of 4-byte code units
871
 * (char32_t).
872
 * @return a result pair struct (of type simdutf::result containing the two
873
 * fields error and count) with an error code and either position of the error
874
 * (in the input in code units) if any, or the number of code units validated if
875
 * successful.
876
 */
877
simdutf_warn_unused result validate_utf32_with_errors(const char32_t *buf,
878
                                                      size_t len) noexcept;
879
  #if SIMDUTF_SPAN
880
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
881
0
validate_utf32_with_errors(std::span<const char32_t> input) noexcept {
882
0
    #if SIMDUTF_CPLUSPLUS23
883
0
  if consteval {
884
0
    return scalar::utf32::validate_with_errors(
885
0
        detail::constexpr_cast_ptr<std::uint32_t>(input.data()), input.size());
886
0
  } else
887
0
    #endif
888
0
  {
889
0
    return validate_utf32_with_errors(input.data(), input.size());
890
0
  }
891
0
}
892
  #endif // SIMDUTF_SPAN
893
#endif   // SIMDUTF_FEATURE_UTF32
894
895
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
896
/**
897
 * Convert Latin1 string into UTF-8 string.
898
 *
899
 * This function is suitable to work with inputs from untrusted sources.
900
 *
901
 * @param input         the Latin1 string to convert
902
 * @param length        the length of the string in bytes
903
 * @param utf8_output   the pointer to buffer that can hold conversion result
904
 * @return the number of written char; 0 if conversion is not possible
905
 */
906
simdutf_warn_unused size_t convert_latin1_to_utf8(const char *input,
907
                                                  size_t length,
908
                                                  char *utf8_output) noexcept;
909
  #if SIMDUTF_SPAN
910
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
911
convert_latin1_to_utf8(
912
    const detail::input_span_of_byte_like auto &latin1_input,
913
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
914
    #if SIMDUTF_CPLUSPLUS23
915
  if consteval {
916
    return scalar::latin1_to_utf8::convert(
917
        detail::constexpr_cast_ptr<char>(latin1_input.data()),
918
        latin1_input.size(),
919
        detail::constexpr_cast_writeptr<char>(utf8_output.data()));
920
  } else
921
    #endif
922
  {
923
    return convert_latin1_to_utf8(
924
        reinterpret_cast<const char *>(latin1_input.data()),
925
        latin1_input.size(), reinterpret_cast<char *>(utf8_output.data()));
926
  }
927
}
928
  #endif // SIMDUTF_SPAN
929
930
/**
931
 * Convert Latin1 string into UTF-8 string with output limit.
932
 *
933
 * This function is suitable to work with inputs from untrusted sources.
934
 *
935
 * We write as many characters as possible.
936
 *
937
 * Using convert_latin1_to_utf8_safe instead of convert_latin1_to_utf8 comes
938
 * with a significant penalty in some cases, being up to four times slower,
939
 * especially on short inputs. If you have allocated the output buffer so that
940
 * it contains utf8_length_from_latin1(input, length) bytes, then prefer
941
 * convert_latin1_to_utf8.
942
 *
943
 * @param input         the Latin1 string to convert
944
 * @param length        the length of the string in bytes
945
 * @param utf8_output   the pointer to buffer that can hold conversion result
946
 * @param utf8_len      the maximum output length
947
 * @return the number of written char; 0 if conversion is not possible
948
 */
949
simdutf_warn_unused size_t
950
convert_latin1_to_utf8_safe(const char *input, size_t length, char *utf8_output,
951
                            size_t utf8_len) noexcept;
952
  #if SIMDUTF_SPAN
953
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
954
convert_latin1_to_utf8_safe(
955
    const detail::input_span_of_byte_like auto &input,
956
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
957
      // implementation note: outputspan is a forwarding ref to avoid copying
958
      // and allow both lvalues and rvalues. std::span can be copied without
959
      // problems, but std::vector should not, and this function should accept
960
      // both. it will allow using an owning rvalue ref (example: passing a
961
      // temporary std::string) as output, but the user will quickly find out
962
      // that he has no way of getting the data out of the object in that case.
963
    #if SIMDUTF_CPLUSPLUS23
964
  if consteval {
965
    return scalar::latin1_to_utf8::convert_safe_constexpr(
966
        input.data(), input.size(), utf8_output.data(), utf8_output.size());
967
  } else
968
    #endif
969
  {
970
    return convert_latin1_to_utf8_safe(
971
        reinterpret_cast<const char *>(input.data()), input.size(),
972
        reinterpret_cast<char *>(utf8_output.data()), utf8_output.size());
973
  }
974
}
975
  #endif // SIMDUTF_SPAN
976
977
/**
978
 * Convert a Latin1 string into a size-limited UTF-8 buffer and report how much
979
 * input was consumed and output was written.
980
 *
981
 * We write as many complete characters as possible. The returned error is
982
 * SUCCESS if all input was consumed, or OUTPUT_BUFFER_TOO_SMALL otherwise.
983
 *
984
 * @param input         the Latin1 string to convert
985
 * @param length        the length of the string in bytes
986
 * @param utf8_output   the pointer to the output buffer
987
 * @param utf8_len      the maximum output length
988
 * @return a full_result with error, input_count and output_count
989
 */
990
simdutf_warn_unused full_result convert_latin1_to_utf8_safe_with_details(
991
    const char *input, size_t length, char *utf8_output,
992
    size_t utf8_len) noexcept;
993
  #if SIMDUTF_SPAN
994
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 full_result
995
convert_latin1_to_utf8_safe_with_details(
996
    const detail::input_span_of_byte_like auto &input,
997
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
998
    #if SIMDUTF_CPLUSPLUS23
999
  if consteval {
1000
    return scalar::latin1_to_utf8::convert_safe_with_details_constexpr(
1001
        input.data(), input.size(), utf8_output.data(), utf8_output.size());
1002
  } else
1003
    #endif
1004
  {
1005
    return convert_latin1_to_utf8_safe_with_details(
1006
        reinterpret_cast<const char *>(input.data()), input.size(),
1007
        reinterpret_cast<char *>(utf8_output.data()), utf8_output.size());
1008
  }
1009
}
1010
  #endif // SIMDUTF_SPAN
1011
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
1012
1013
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
1014
/**
1015
 * Convert possibly Latin1 string into UTF-16LE string.
1016
 *
1017
 * This function is suitable to work with inputs from untrusted sources.
1018
 *
1019
 * @param input         the Latin1 string to convert
1020
 * @param length        the length of the string in bytes
1021
 * @param utf16_output  the pointer to buffer that can hold conversion result
1022
 * @return the number of written char16_t; 0 if conversion is not possible
1023
 */
1024
simdutf_warn_unused size_t convert_latin1_to_utf16le(
1025
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1026
  #if SIMDUTF_SPAN
1027
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1028
convert_latin1_to_utf16le(
1029
    const detail::input_span_of_byte_like auto &latin1_input,
1030
    std::span<char16_t> utf16_output) noexcept {
1031
    #if SIMDUTF_CPLUSPLUS23
1032
  if consteval {
1033
    return scalar::latin1_to_utf16::convert<endianness::LITTLE>(
1034
        latin1_input.data(), latin1_input.size(), utf16_output.data());
1035
  } else
1036
    #endif
1037
  {
1038
    return convert_latin1_to_utf16le(
1039
        reinterpret_cast<const char *>(latin1_input.data()),
1040
        latin1_input.size(), utf16_output.data());
1041
  }
1042
}
1043
  #endif // SIMDUTF_SPAN
1044
1045
/**
1046
 * Convert Latin1 string into UTF-16BE string.
1047
 *
1048
 * This function is suitable to work with inputs from untrusted sources.
1049
 *
1050
 * @param input         the Latin1 string to convert
1051
 * @param length        the length of the string in bytes
1052
 * @param utf16_output  the pointer to buffer that can hold conversion result
1053
 * @return the number of written char16_t; 0 if conversion is not possible
1054
 */
1055
simdutf_warn_unused size_t convert_latin1_to_utf16be(
1056
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1057
  #if SIMDUTF_SPAN
1058
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1059
convert_latin1_to_utf16be(const detail::input_span_of_byte_like auto &input,
1060
                          std::span<char16_t> output) noexcept {
1061
    #if SIMDUTF_CPLUSPLUS23
1062
  if consteval {
1063
    return scalar::latin1_to_utf16::convert<endianness::BIG>(
1064
        input.data(), input.size(), output.data());
1065
  } else
1066
    #endif
1067
  {
1068
    return convert_latin1_to_utf16be(
1069
        reinterpret_cast<const char *>(input.data()), input.size(),
1070
        output.data());
1071
  }
1072
}
1073
  #endif // SIMDUTF_SPAN
1074
/**
1075
 * Compute the number of bytes that this UTF-16 string would require in Latin1
1076
 * format.
1077
 *
1078
 * @param length        the length of the string in Latin1 code units (char)
1079
 * @return the length of the string in Latin1 code units (char) required to
1080
 * encode the UTF-16 string as Latin1
1081
 */
1082
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1083
latin1_length_from_utf16(size_t length) noexcept {
1084
  return length;
1085
}
1086
1087
/**
1088
 * Compute the number of code units that this Latin1 string would require in
1089
 * UTF-16 format.
1090
 *
1091
 * @param length        the length of the string in Latin1 code units (char)
1092
 * @return the length of the string in 2-byte code units (char16_t) required to
1093
 * encode the Latin1 string as UTF-16
1094
 */
1095
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1096
0
utf16_length_from_latin1(size_t length) noexcept {
1097
0
  return length;
1098
0
}
1099
#endif // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
1100
1101
#if SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
1102
/**
1103
 * Convert Latin1 string into UTF-32 string.
1104
 *
1105
 * This function is suitable to work with inputs from untrusted sources.
1106
 *
1107
 * @param input         the Latin1 string to convert
1108
 * @param length        the length of the string in bytes
1109
 * @param utf32_buffer  the pointer to buffer that can hold conversion result
1110
 * @return the number of written char32_t; 0 if conversion is not possible
1111
 */
1112
simdutf_warn_unused size_t convert_latin1_to_utf32(
1113
    const char *input, size_t length, char32_t *utf32_buffer) noexcept;
1114
  #if SIMDUTF_SPAN
1115
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1116
convert_latin1_to_utf32(
1117
    const detail::input_span_of_byte_like auto &latin1_input,
1118
    std::span<char32_t> utf32_output) noexcept {
1119
    #if SIMDUTF_CPLUSPLUS23
1120
  if consteval {
1121
    return scalar::latin1_to_utf32::convert(
1122
        latin1_input.data(), latin1_input.size(), utf32_output.data());
1123
  } else
1124
    #endif
1125
  {
1126
    return convert_latin1_to_utf32(
1127
        reinterpret_cast<const char *>(latin1_input.data()),
1128
        latin1_input.size(), utf32_output.data());
1129
  }
1130
}
1131
  #endif // SIMDUTF_SPAN
1132
#endif   // SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
1133
1134
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
1135
/**
1136
 * Convert possibly broken UTF-8 string into latin1 string.
1137
 *
1138
 * During the conversion also validation of the input string is done.
1139
 * This function is suitable to work with inputs from untrusted sources.
1140
 *
1141
 * @param input         the UTF-8 string to convert
1142
 * @param length        the length of the string in bytes
1143
 * @param latin1_output  the pointer to buffer that can hold conversion result
1144
 * @return the number of written char; 0 if the input was not valid UTF-8 string
1145
 * or if it cannot be represented as Latin1
1146
 */
1147
simdutf_warn_unused size_t convert_utf8_to_latin1(const char *input,
1148
                                                  size_t length,
1149
                                                  char *latin1_output) noexcept;
1150
  #if SIMDUTF_SPAN
1151
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1152
convert_utf8_to_latin1(
1153
    const detail::input_span_of_byte_like auto &input,
1154
    detail::output_span_of_byte_like auto &&output) noexcept {
1155
    #if SIMDUTF_CPLUSPLUS23
1156
  if consteval {
1157
    return scalar::utf8_to_latin1::convert(input.data(), input.size(),
1158
                                           output.data());
1159
  } else
1160
    #endif
1161
  {
1162
    return convert_utf8_to_latin1(reinterpret_cast<const char *>(input.data()),
1163
                                  input.size(),
1164
                                  reinterpret_cast<char *>(output.data()));
1165
  }
1166
}
1167
  #endif // SIMDUTF_SPAN
1168
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
1169
1170
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
1171
/**
1172
 * Using native endianness, convert possibly broken UTF-8 string into a UTF-16
1173
 * string.
1174
 *
1175
 * During the conversion also validation of the input string is done.
1176
 * This function is suitable to work with inputs from untrusted sources.
1177
 *
1178
 * @param input         the UTF-8 string to convert
1179
 * @param length        the length of the string in bytes
1180
 * @param utf16_output  the pointer to buffer that can hold conversion result
1181
 * @return the number of written char16_t; 0 if the input was not valid UTF-8
1182
 * string
1183
 */
1184
simdutf_warn_unused size_t convert_utf8_to_utf16(
1185
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1186
  #if SIMDUTF_SPAN
1187
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1188
convert_utf8_to_utf16(const detail::input_span_of_byte_like auto &input,
1189
                      std::span<char16_t> output) noexcept {
1190
    #if SIMDUTF_CPLUSPLUS23
1191
  if consteval {
1192
    return scalar::utf8_to_utf16::convert<endianness::NATIVE>(
1193
        input.data(), input.size(), output.data());
1194
  } else
1195
    #endif
1196
  {
1197
    return convert_utf8_to_utf16(reinterpret_cast<const char *>(input.data()),
1198
                                 input.size(), output.data());
1199
  }
1200
}
1201
  #endif // SIMDUTF_SPAN
1202
1203
/**
1204
 * Compute the number of bytes that this UTF-16LE string would require in UTF-8
1205
 * format even when the UTF-16LE content contains mismatched surrogates
1206
 * that have to be replaced by the replacement character (0xFFFD).
1207
 *
1208
 * @param input         the UTF-16LE string to convert
1209
 * @param length        the length of the string in 2-byte code units (char16_t)
1210
 * @return a result pair struct (of type simdutf::result containing the two
1211
 * fields error and count) where the count is the number of bytes required to
1212
 * encode the UTF-16LE string as UTF-8, and the error code is either SUCCESS or
1213
 * SURROGATE. The count is correct regardless of the error field.
1214
 * When SURROGATE is returned, it does not indicate an error in the case of this
1215
 * function: it indicates that at least one surrogate has been encountered: the
1216
 * surrogates may be matched or not (thus this function does not validate). If
1217
 * the returned error code is SUCCESS, then the input contains no surrogate, is
1218
 * in the Basic Multilingual Plane, and is necessarily valid.
1219
 */
1220
simdutf_warn_unused result utf8_length_from_utf16le_with_replacement(
1221
    const char16_t *input, size_t length) noexcept;
1222
  #if SIMDUTF_SPAN
1223
simdutf_really_inline simdutf_constexpr23 simdutf_warn_unused result
1224
utf8_length_from_utf16le_with_replacement(
1225
0
    std::span<const char16_t> valid_utf16_input) noexcept {
1226
0
    #if SIMDUTF_CPLUSPLUS23
1227
0
  if consteval {
1228
0
    return scalar::utf16::utf8_length_from_utf16_with_replacement<
1229
0
        endianness::LITTLE>(valid_utf16_input.data(), valid_utf16_input.size());
1230
0
  } else
1231
0
    #endif
1232
0
  {
1233
0
    return utf8_length_from_utf16le_with_replacement(valid_utf16_input.data(),
1234
0
                                                     valid_utf16_input.size());
1235
0
  }
1236
0
}
1237
  #endif // SIMDUTF_SPAN
1238
1239
/**
1240
 * Compute the number of bytes that this UTF-16BE string would require in UTF-8
1241
 * format even when the UTF-16BE content contains mismatched surrogates
1242
 * that have to be replaced by the replacement character (0xFFFD).
1243
 *
1244
 * @param input         the UTF-16BE string to convert
1245
 * @param length        the length of the string in 2-byte code units (char16_t)
1246
 * @return a result pair struct (of type simdutf::result containing the two
1247
 * fields error and count) where the count is the number of bytes required to
1248
 * encode the UTF-16BE string as UTF-8, and the error code is either SUCCESS or
1249
 * SURROGATE. The count is correct regardless of the error field.
1250
 * When SURROGATE is returned, it does not indicate an error in the case of this
1251
 * function: it indicates that at least one surrogate has been encountered: the
1252
 * surrogates may be matched or not (thus this function does not validate). If
1253
 * the returned error code is SUCCESS, then the input contains no surrogate, is
1254
 * in the Basic Multilingual Plane, and is necessarily valid.
1255
 */
1256
simdutf_warn_unused result utf8_length_from_utf16be_with_replacement(
1257
    const char16_t *input, size_t length) noexcept;
1258
  #if SIMDUTF_SPAN
1259
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
1260
utf8_length_from_utf16be_with_replacement(
1261
0
    std::span<const char16_t> valid_utf16_input) noexcept {
1262
0
    #if SIMDUTF_CPLUSPLUS23
1263
0
  if consteval {
1264
0
    return scalar::utf16::utf8_length_from_utf16_with_replacement<
1265
0
        endianness::BIG>(valid_utf16_input.data(), valid_utf16_input.size());
1266
0
  } else
1267
0
    #endif
1268
0
  {
1269
0
    return utf8_length_from_utf16be_with_replacement(valid_utf16_input.data(),
1270
0
                                                     valid_utf16_input.size());
1271
0
  }
1272
0
}
1273
  #endif // SIMDUTF_SPAN
1274
1275
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
1276
1277
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
1278
/**
1279
 * Using native endianness, convert a Latin1 string into a UTF-16 string.
1280
 *
1281
 * @param input         the Latin1 string to convert
1282
 * @param length        the length of the string in bytes
1283
 * @param utf16_output  the pointer to buffer that can hold conversion result
1284
 * @return the number of written char16_t.
1285
 */
1286
simdutf_warn_unused size_t convert_latin1_to_utf16(
1287
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1288
  #if SIMDUTF_SPAN
1289
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1290
convert_latin1_to_utf16(const detail::input_span_of_byte_like auto &input,
1291
                        std::span<char16_t> output) noexcept {
1292
    #if SIMDUTF_CPLUSPLUS23
1293
  if consteval {
1294
    return scalar::latin1_to_utf16::convert<endianness::NATIVE>(
1295
        input.data(), input.size(), output.data());
1296
  } else
1297
    #endif
1298
  {
1299
    return convert_latin1_to_utf16(reinterpret_cast<const char *>(input.data()),
1300
                                   input.size(), output.data());
1301
  }
1302
}
1303
  #endif // SIMDUTF_SPAN
1304
#endif   // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
1305
1306
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
1307
/**
1308
 * Convert possibly broken UTF-8 string into UTF-16LE string.
1309
 *
1310
 * During the conversion also validation of the input string is done.
1311
 * This function is suitable to work with inputs from untrusted sources.
1312
 *
1313
 * @param input         the UTF-8 string to convert
1314
 * @param length        the length of the string in bytes
1315
 * @param utf16_output  the pointer to buffer that can hold conversion result
1316
 * @return the number of written char16_t; 0 if the input was not valid UTF-8
1317
 * string
1318
 */
1319
simdutf_warn_unused size_t convert_utf8_to_utf16le(
1320
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1321
  #if SIMDUTF_SPAN
1322
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1323
convert_utf8_to_utf16le(const detail::input_span_of_byte_like auto &utf8_input,
1324
                        std::span<char16_t> utf16_output) noexcept {
1325
    #if SIMDUTF_CPLUSPLUS23
1326
  if consteval {
1327
    return scalar::utf8_to_utf16::convert<endianness::LITTLE>(
1328
        utf8_input.data(), utf8_input.size(), utf16_output.data());
1329
  } else
1330
    #endif
1331
  {
1332
    return convert_utf8_to_utf16le(
1333
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1334
        utf16_output.data());
1335
  }
1336
}
1337
  #endif // SIMDUTF_SPAN
1338
1339
/**
1340
 * Convert possibly broken UTF-8 string into UTF-16BE string.
1341
 *
1342
 * During the conversion also validation of the input string is done.
1343
 * This function is suitable to work with inputs from untrusted sources.
1344
 *
1345
 * @param input         the UTF-8 string to convert
1346
 * @param length        the length of the string in bytes
1347
 * @param utf16_output  the pointer to buffer that can hold conversion result
1348
 * @return the number of written char16_t; 0 if the input was not valid UTF-8
1349
 * string
1350
 */
1351
simdutf_warn_unused size_t convert_utf8_to_utf16be(
1352
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1353
  #if SIMDUTF_SPAN
1354
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1355
convert_utf8_to_utf16be(const detail::input_span_of_byte_like auto &utf8_input,
1356
                        std::span<char16_t> utf16_output) noexcept {
1357
1358
    #if SIMDUTF_CPLUSPLUS23
1359
  if consteval {
1360
    return scalar::utf8_to_utf16::convert<endianness::BIG>(
1361
        utf8_input.data(), utf8_input.size(), utf16_output.data());
1362
  } else
1363
    #endif
1364
  {
1365
    return convert_utf8_to_utf16be(
1366
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1367
        utf16_output.data());
1368
  }
1369
}
1370
  #endif // SIMDUTF_SPAN
1371
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
1372
1373
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
1374
/**
1375
 * Convert possibly broken UTF-8 string into latin1 string with errors.
1376
 * If the string cannot be represented as Latin1, an error
1377
 * code is returned.
1378
 *
1379
 * During the conversion also validation of the input string is done.
1380
 * This function is suitable to work with inputs from untrusted sources.
1381
 *
1382
 * @param input         the UTF-8 string to convert
1383
 * @param length        the length of the string in bytes
1384
 * @param latin1_output  the pointer to buffer that can hold conversion result
1385
 * @return a result pair struct (of type simdutf::result containing the two
1386
 * fields error and count) with an error code and either position of the error
1387
 * (in the input in code units) if any, or the number of bytes written if
1388
 * successful.
1389
 */
1390
simdutf_warn_unused result convert_utf8_to_latin1_with_errors(
1391
    const char *input, size_t length, char *latin1_output) noexcept;
1392
  #if SIMDUTF_SPAN
1393
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
1394
convert_utf8_to_latin1_with_errors(
1395
    const detail::input_span_of_byte_like auto &utf8_input,
1396
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
1397
    #if SIMDUTF_CPLUSPLUS23
1398
  if consteval {
1399
    return scalar::utf8_to_latin1::convert_with_errors(
1400
        utf8_input.data(), utf8_input.size(), latin1_output.data());
1401
  } else
1402
    #endif
1403
  {
1404
    return convert_utf8_to_latin1_with_errors(
1405
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1406
        reinterpret_cast<char *>(latin1_output.data()));
1407
  }
1408
}
1409
  #endif // SIMDUTF_SPAN
1410
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
1411
1412
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
1413
/**
1414
 * Using native endianness, convert possibly broken UTF-8 string into UTF-16
1415
 * string and stop on error.
1416
 *
1417
 * During the conversion also validation of the input string is done.
1418
 * This function is suitable to work with inputs from untrusted sources.
1419
 *
1420
 * @param input         the UTF-8 string to convert
1421
 * @param length        the length of the string in bytes
1422
 * @param utf16_output  the pointer to buffer that can hold conversion result
1423
 * @return a result pair struct (of type simdutf::result containing the two
1424
 * fields error and count) with an error code and either position of the error
1425
 * (in the input in code units) if any, or the number of char16_t written if
1426
 * successful.
1427
 */
1428
simdutf_warn_unused result convert_utf8_to_utf16_with_errors(
1429
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1430
  #if SIMDUTF_SPAN
1431
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
1432
convert_utf8_to_utf16_with_errors(
1433
    const detail::input_span_of_byte_like auto &utf8_input,
1434
    std::span<char16_t> utf16_output) noexcept {
1435
    #if SIMDUTF_CPLUSPLUS23
1436
  if consteval {
1437
    return scalar::utf8_to_utf16::convert_with_errors<endianness::NATIVE>(
1438
        utf8_input.data(), utf8_input.size(), utf16_output.data());
1439
  } else
1440
    #endif
1441
  {
1442
    return convert_utf8_to_utf16_with_errors(
1443
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1444
        utf16_output.data());
1445
  }
1446
}
1447
  #endif // SIMDUTF_SPAN
1448
1449
/**
1450
 * Convert possibly broken UTF-8 string into UTF-16LE string and stop on error.
1451
 *
1452
 * During the conversion also validation of the input string is done.
1453
 * This function is suitable to work with inputs from untrusted sources.
1454
 *
1455
 * @param input         the UTF-8 string to convert
1456
 * @param length        the length of the string in bytes
1457
 * @param utf16_output  the pointer to buffer that can hold conversion result
1458
 * @return a result pair struct (of type simdutf::result containing the two
1459
 * fields error and count) with an error code and either position of the error
1460
 * (in the input in code units) if any, or the number of char16_t written if
1461
 * successful.
1462
 */
1463
simdutf_warn_unused result convert_utf8_to_utf16le_with_errors(
1464
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1465
  #if SIMDUTF_SPAN
1466
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
1467
convert_utf8_to_utf16le_with_errors(
1468
    const detail::input_span_of_byte_like auto &utf8_input,
1469
    std::span<char16_t> utf16_output) noexcept {
1470
    #if SIMDUTF_CPLUSPLUS23
1471
  if consteval {
1472
    return scalar::utf8_to_utf16::convert_with_errors<endianness::LITTLE>(
1473
        utf8_input.data(), utf8_input.size(), utf16_output.data());
1474
  } else
1475
    #endif
1476
  {
1477
    return convert_utf8_to_utf16le_with_errors(
1478
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1479
        utf16_output.data());
1480
  }
1481
}
1482
  #endif // SIMDUTF_SPAN
1483
1484
/**
1485
 * Convert possibly broken UTF-8 string into UTF-16BE string and stop on error.
1486
 *
1487
 * During the conversion also validation of the input string is done.
1488
 * This function is suitable to work with inputs from untrusted sources.
1489
 *
1490
 * @param input         the UTF-8 string to convert
1491
 * @param length        the length of the string in bytes
1492
 * @param utf16_output  the pointer to buffer that can hold conversion result
1493
 * @return a result pair struct (of type simdutf::result containing the two
1494
 * fields error and count) with an error code and either position of the error
1495
 * (in the input in code units) if any, or the number of char16_t written if
1496
 * successful.
1497
 */
1498
simdutf_warn_unused result convert_utf8_to_utf16be_with_errors(
1499
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1500
  #if SIMDUTF_SPAN
1501
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
1502
convert_utf8_to_utf16be_with_errors(
1503
    const detail::input_span_of_byte_like auto &utf8_input,
1504
    std::span<char16_t> utf16_output) noexcept {
1505
    #if SIMDUTF_CPLUSPLUS23
1506
  if consteval {
1507
    return scalar::utf8_to_utf16::convert_with_errors<endianness::BIG>(
1508
        utf8_input.data(), utf8_input.size(), utf16_output.data());
1509
  } else
1510
    #endif
1511
  {
1512
    return convert_utf8_to_utf16be_with_errors(
1513
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1514
        utf16_output.data());
1515
  }
1516
}
1517
  #endif // SIMDUTF_SPAN
1518
1519
/**
1520
 * Convert a possibly ill-formed UTF-8 string into a UTF-16 string (native
1521
 * endianness), replacing each ill-formed subsequence with U+FFFD.
1522
 *
1523
 * Substitution follows Unicode maximal subparts (the WHATWG rule): each
1524
 * maximal subpart of an ill-formed sequence becomes one U+FFFD code unit.
1525
 * The function always succeeds. The output buffer must hold
1526
 * utf16_length_from_utf8_with_replacement(...).count char16_t. A buffer of
1527
 * `length` char16_t is always large enough. Pass that result to the overload
1528
 * that takes it, so the recorded error locations are not discovered again.
1529
 *
1530
 * This function is not BOM-aware.
1531
 *
1532
 * @param input         the UTF-8 string to convert
1533
 * @param length        the length of the string in bytes
1534
 * @param utf16_output  the pointer to buffer that can hold the conversion
1535
 * result
1536
 * @return the number of written char16_t
1537
 */
1538
simdutf_warn_unused size_t convert_utf8_to_utf16_with_replacement(
1539
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1540
  #if SIMDUTF_SPAN
1541
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1542
convert_utf8_to_utf16_with_replacement(
1543
    const detail::input_span_of_byte_like auto &utf8_input,
1544
    std::span<char16_t> utf16_output) noexcept {
1545
    #if SIMDUTF_CPLUSPLUS23
1546
  if consteval {
1547
    return scalar::utf8_to_utf16::convert_with_replacement<endianness::NATIVE>(
1548
        utf8_input.data(), utf8_input.size(), utf16_output.data());
1549
  } else
1550
    #endif
1551
  {
1552
    return convert_utf8_to_utf16_with_replacement(
1553
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1554
        utf16_output.data());
1555
  }
1556
}
1557
  #endif // SIMDUTF_SPAN
1558
1559
/**
1560
 * Convert a possibly ill-formed UTF-8 string into a UTF-16 string (native
1561
 * endianness), replacing each ill-formed subsequence with U+FFFD, using error
1562
 * locations already computed for this same input.
1563
 *
1564
 * locations must be the value returned by
1565
 * utf16_length_from_utf8_with_replacement for these bytes. When
1566
 * locations.more_errors is false, every ill-formed subsequence is in
1567
 * locations.error_offset and the gaps are valid UTF-8. When it is true, the
1568
 * input has further ill-formed subsequences, and the tail past the stored
1569
 * locations is scanned during conversion. The function always succeeds.
1570
 *
1571
 * @param input         the UTF-8 string to convert
1572
 * @param length        the length of the string in bytes
1573
 * @param utf16_output  the pointer to buffer that can hold the conversion
1574
 * result
1575
 * @param locations     result of utf16_length_from_utf8_with_replacement
1576
 * @return the number of written char16_t, equal to locations.count when
1577
 * locations describes this input
1578
 */
1579
simdutf_warn_unused size_t convert_utf8_to_utf16_with_replacement(
1580
    const char *input, size_t length, char16_t *utf16_output,
1581
    const utf8_to_utf16_result &locations) noexcept;
1582
  #if SIMDUTF_SPAN
1583
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1584
convert_utf8_to_utf16_with_replacement(
1585
    const detail::input_span_of_byte_like auto &utf8_input,
1586
    std::span<char16_t> utf16_output,
1587
    const utf8_to_utf16_result &locations) noexcept {
1588
    #if SIMDUTF_CPLUSPLUS23
1589
  if consteval {
1590
    (void)locations;
1591
    return scalar::utf8_to_utf16::convert_with_replacement<endianness::NATIVE>(
1592
        utf8_input.data(), utf8_input.size(), utf16_output.data());
1593
  } else
1594
    #endif
1595
  {
1596
    return convert_utf8_to_utf16_with_replacement(
1597
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1598
        utf16_output.data(), locations);
1599
  }
1600
}
1601
  #endif // SIMDUTF_SPAN
1602
1603
/**
1604
 * Convert a possibly ill-formed UTF-8 string into a UTF-16LE string, replacing
1605
 * each ill-formed subsequence with U+FFFD.
1606
 *
1607
 * Substitution follows Unicode maximal subparts (the WHATWG rule): each
1608
 * maximal subpart of an ill-formed sequence becomes one U+FFFD code unit.
1609
 * The function always succeeds. The output buffer must hold
1610
 * utf16_length_from_utf8_with_replacement(...).count char16_t. A buffer of
1611
 * `length` char16_t is always large enough. Pass that result to the overload
1612
 * that takes it, so the recorded error locations are not discovered again.
1613
 *
1614
 * This function is not BOM-aware.
1615
 *
1616
 * @param input         the UTF-8 string to convert
1617
 * @param length        the length of the string in bytes
1618
 * @param utf16_output  the pointer to buffer that can hold the conversion
1619
 * result
1620
 * @return the number of written char16_t
1621
 */
1622
simdutf_warn_unused size_t convert_utf8_to_utf16le_with_replacement(
1623
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1624
  #if SIMDUTF_SPAN
1625
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1626
convert_utf8_to_utf16le_with_replacement(
1627
    const detail::input_span_of_byte_like auto &utf8_input,
1628
    std::span<char16_t> utf16_output) noexcept {
1629
    #if SIMDUTF_CPLUSPLUS23
1630
  if consteval {
1631
    return scalar::utf8_to_utf16::convert_with_replacement<endianness::LITTLE>(
1632
        utf8_input.data(), utf8_input.size(), utf16_output.data());
1633
  } else
1634
    #endif
1635
  {
1636
    return convert_utf8_to_utf16le_with_replacement(
1637
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1638
        utf16_output.data());
1639
  }
1640
}
1641
  #endif // SIMDUTF_SPAN
1642
1643
/**
1644
 * Convert a possibly ill-formed UTF-8 string into a UTF-16LE string, using
1645
 * error locations already computed for this same input. See
1646
 * convert_utf8_to_utf16_with_replacement.
1647
 *
1648
 * @param input         the UTF-8 string to convert
1649
 * @param length        the length of the string in bytes
1650
 * @param utf16_output  the pointer to buffer that can hold the conversion
1651
 * result
1652
 * @param locations     result of utf16_length_from_utf8_with_replacement
1653
 * @return the number of written char16_t
1654
 */
1655
simdutf_warn_unused size_t convert_utf8_to_utf16le_with_replacement(
1656
    const char *input, size_t length, char16_t *utf16_output,
1657
    const utf8_to_utf16_result &locations) noexcept;
1658
  #if SIMDUTF_SPAN
1659
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1660
convert_utf8_to_utf16le_with_replacement(
1661
    const detail::input_span_of_byte_like auto &utf8_input,
1662
    std::span<char16_t> utf16_output,
1663
    const utf8_to_utf16_result &locations) noexcept {
1664
    #if SIMDUTF_CPLUSPLUS23
1665
  if consteval {
1666
    (void)locations;
1667
    return scalar::utf8_to_utf16::convert_with_replacement<endianness::LITTLE>(
1668
        utf8_input.data(), utf8_input.size(), utf16_output.data());
1669
  } else
1670
    #endif
1671
  {
1672
    return convert_utf8_to_utf16le_with_replacement(
1673
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1674
        utf16_output.data(), locations);
1675
  }
1676
}
1677
  #endif // SIMDUTF_SPAN
1678
1679
/**
1680
 * Convert a possibly ill-formed UTF-8 string into a UTF-16BE string, replacing
1681
 * each ill-formed subsequence with U+FFFD.
1682
 *
1683
 * Substitution follows Unicode maximal subparts (the WHATWG rule): each
1684
 * maximal subpart of an ill-formed sequence becomes one U+FFFD code unit.
1685
 * The function always succeeds. The output buffer must hold
1686
 * utf16_length_from_utf8_with_replacement(...).count char16_t. A buffer of
1687
 * `length` char16_t is always large enough. Pass that result to the overload
1688
 * that takes it, so the recorded error locations are not discovered again.
1689
 *
1690
 * This function is not BOM-aware.
1691
 *
1692
 * @param input         the UTF-8 string to convert
1693
 * @param length        the length of the string in bytes
1694
 * @param utf16_output  the pointer to buffer that can hold the conversion
1695
 * result
1696
 * @return the number of written char16_t
1697
 */
1698
simdutf_warn_unused size_t convert_utf8_to_utf16be_with_replacement(
1699
    const char *input, size_t length, char16_t *utf16_output) noexcept;
1700
  #if SIMDUTF_SPAN
1701
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1702
convert_utf8_to_utf16be_with_replacement(
1703
    const detail::input_span_of_byte_like auto &utf8_input,
1704
    std::span<char16_t> utf16_output) noexcept {
1705
    #if SIMDUTF_CPLUSPLUS23
1706
  if consteval {
1707
    return scalar::utf8_to_utf16::convert_with_replacement<endianness::BIG>(
1708
        utf8_input.data(), utf8_input.size(), utf16_output.data());
1709
  } else
1710
    #endif
1711
  {
1712
    return convert_utf8_to_utf16be_with_replacement(
1713
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1714
        utf16_output.data());
1715
  }
1716
}
1717
  #endif // SIMDUTF_SPAN
1718
1719
/**
1720
 * Convert a possibly ill-formed UTF-8 string into a UTF-16BE string, using
1721
 * error locations already computed for this same input. See
1722
 * convert_utf8_to_utf16_with_replacement.
1723
 *
1724
 * @param input         the UTF-8 string to convert
1725
 * @param length        the length of the string in bytes
1726
 * @param utf16_output  the pointer to buffer that can hold the conversion
1727
 * result
1728
 * @param locations     result of utf16_length_from_utf8_with_replacement
1729
 * @return the number of written char16_t
1730
 */
1731
simdutf_warn_unused size_t convert_utf8_to_utf16be_with_replacement(
1732
    const char *input, size_t length, char16_t *utf16_output,
1733
    const utf8_to_utf16_result &locations) noexcept;
1734
  #if SIMDUTF_SPAN
1735
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1736
convert_utf8_to_utf16be_with_replacement(
1737
    const detail::input_span_of_byte_like auto &utf8_input,
1738
    std::span<char16_t> utf16_output,
1739
    const utf8_to_utf16_result &locations) noexcept {
1740
    #if SIMDUTF_CPLUSPLUS23
1741
  if consteval {
1742
    (void)locations;
1743
    return scalar::utf8_to_utf16::convert_with_replacement<endianness::BIG>(
1744
        utf8_input.data(), utf8_input.size(), utf16_output.data());
1745
  } else
1746
    #endif
1747
  {
1748
    return convert_utf8_to_utf16be_with_replacement(
1749
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1750
        utf16_output.data(), locations);
1751
  }
1752
}
1753
  #endif // SIMDUTF_SPAN
1754
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
1755
1756
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
1757
/**
1758
 * Convert possibly broken UTF-8 string into UTF-32 string.
1759
 *
1760
 * During the conversion also validation of the input string is done.
1761
 * This function is suitable to work with inputs from untrusted sources.
1762
 *
1763
 * @param input         the UTF-8 string to convert
1764
 * @param length        the length of the string in bytes
1765
 * @param utf32_output  the pointer to buffer that can hold conversion result
1766
 * @return the number of written char32_t; 0 if the input was not valid UTF-8
1767
 * string
1768
 */
1769
simdutf_warn_unused size_t convert_utf8_to_utf32(
1770
    const char *input, size_t length, char32_t *utf32_output) noexcept;
1771
  #if SIMDUTF_SPAN
1772
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1773
convert_utf8_to_utf32(const detail::input_span_of_byte_like auto &utf8_input,
1774
                      std::span<char32_t> utf32_output) noexcept {
1775
    #if SIMDUTF_CPLUSPLUS23
1776
  if consteval {
1777
    return scalar::utf8_to_utf32::convert(utf8_input.data(), utf8_input.size(),
1778
                                          utf32_output.data());
1779
  } else
1780
    #endif
1781
  {
1782
    return convert_utf8_to_utf32(
1783
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1784
        utf32_output.data());
1785
  }
1786
}
1787
  #endif // SIMDUTF_SPAN
1788
1789
/**
1790
 * Convert possibly broken UTF-8 string into UTF-32 string and stop on error.
1791
 *
1792
 * During the conversion also validation of the input string is done.
1793
 * This function is suitable to work with inputs from untrusted sources.
1794
 *
1795
 * @param input         the UTF-8 string to convert
1796
 * @param length        the length of the string in bytes
1797
 * @param utf32_output  the pointer to buffer that can hold conversion result
1798
 * @return a result pair struct (of type simdutf::result containing the two
1799
 * fields error and count) with an error code and either position of the error
1800
 * (in the input in code units) if any, or the number of char32_t written if
1801
 * successful.
1802
 */
1803
simdutf_warn_unused result convert_utf8_to_utf32_with_errors(
1804
    const char *input, size_t length, char32_t *utf32_output) noexcept;
1805
  #if SIMDUTF_SPAN
1806
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
1807
convert_utf8_to_utf32_with_errors(
1808
    const detail::input_span_of_byte_like auto &utf8_input,
1809
    std::span<char32_t> utf32_output) noexcept {
1810
    #if SIMDUTF_CPLUSPLUS23
1811
  if consteval {
1812
    return scalar::utf8_to_utf32::convert_with_errors(
1813
        utf8_input.data(), utf8_input.size(), utf32_output.data());
1814
  } else
1815
    #endif
1816
  {
1817
    return convert_utf8_to_utf32_with_errors(
1818
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size(),
1819
        utf32_output.data());
1820
  }
1821
}
1822
  #endif // SIMDUTF_SPAN
1823
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
1824
1825
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
1826
/**
1827
 * Convert valid UTF-8 string into latin1 string.
1828
 *
1829
 * This function assumes that the input string is valid UTF-8 and that it can be
1830
 * represented as Latin1. If you violate this assumption, the result is
1831
 * implementation defined and may include system-dependent behavior such as
1832
 * crashes.
1833
 *
1834
 * This function is for expert users only and not part of our public API. Use
1835
 * convert_utf8_to_latin1 instead. The function may be removed from the library
1836
 * in the future.
1837
 *
1838
 * This function is not BOM-aware.
1839
 *
1840
 * @param input         the UTF-8 string to convert
1841
 * @param length        the length of the string in bytes
1842
 * @param latin1_output  the pointer to buffer that can hold conversion result
1843
 * @return the number of written char; 0 if the input was not valid UTF-8 string
1844
 */
1845
simdutf_warn_unused size_t convert_valid_utf8_to_latin1(
1846
    const char *input, size_t length, char *latin1_output) noexcept;
1847
  #if SIMDUTF_SPAN
1848
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1849
convert_valid_utf8_to_latin1(
1850
    const detail::input_span_of_byte_like auto &valid_utf8_input,
1851
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
1852
    #if SIMDUTF_CPLUSPLUS23
1853
  if consteval {
1854
    return scalar::utf8_to_latin1::convert_valid(
1855
        valid_utf8_input.data(), valid_utf8_input.size(), latin1_output.data());
1856
  } else
1857
    #endif
1858
  {
1859
    return convert_valid_utf8_to_latin1(
1860
        reinterpret_cast<const char *>(valid_utf8_input.data()),
1861
        valid_utf8_input.size(), latin1_output.data());
1862
  }
1863
}
1864
  #endif // SIMDUTF_SPAN
1865
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
1866
1867
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
1868
/**
1869
 * Using native endianness, convert valid UTF-8 string into a UTF-16 string.
1870
 *
1871
 * This function assumes that the input string is valid UTF-8.
1872
 *
1873
 * @param input         the UTF-8 string to convert
1874
 * @param length        the length of the string in bytes
1875
 * @param utf16_buffer  the pointer to buffer that can hold conversion result
1876
 * @return the number of written char16_t
1877
 */
1878
simdutf_warn_unused size_t convert_valid_utf8_to_utf16(
1879
    const char *input, size_t length, char16_t *utf16_buffer) noexcept;
1880
  #if SIMDUTF_SPAN
1881
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1882
convert_valid_utf8_to_utf16(
1883
    const detail::input_span_of_byte_like auto &valid_utf8_input,
1884
    std::span<char16_t> utf16_output) noexcept {
1885
    #if SIMDUTF_CPLUSPLUS23
1886
  if consteval {
1887
    return scalar::utf8_to_utf16::convert_valid<endianness::NATIVE>(
1888
        valid_utf8_input.data(), valid_utf8_input.size(), utf16_output.data());
1889
  } else
1890
    #endif
1891
  {
1892
    return convert_valid_utf8_to_utf16(
1893
        reinterpret_cast<const char *>(valid_utf8_input.data()),
1894
        valid_utf8_input.size(), utf16_output.data());
1895
  }
1896
}
1897
  #endif // SIMDUTF_SPAN
1898
1899
/**
1900
 * Convert valid UTF-8 string into UTF-16LE string.
1901
 *
1902
 * This function assumes that the input string is valid UTF-8.
1903
 *
1904
 * @param input         the UTF-8 string to convert
1905
 * @param length        the length of the string in bytes
1906
 * @param utf16_buffer  the pointer to buffer that can hold conversion result
1907
 * @return the number of written char16_t
1908
 */
1909
simdutf_warn_unused size_t convert_valid_utf8_to_utf16le(
1910
    const char *input, size_t length, char16_t *utf16_buffer) noexcept;
1911
  #if SIMDUTF_SPAN
1912
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1913
convert_valid_utf8_to_utf16le(
1914
    const detail::input_span_of_byte_like auto &valid_utf8_input,
1915
    std::span<char16_t> utf16_output) noexcept {
1916
1917
    #if SIMDUTF_CPLUSPLUS23
1918
  if consteval {
1919
    return scalar::utf8_to_utf16::convert_valid<endianness::LITTLE>(
1920
        valid_utf8_input.data(), valid_utf8_input.size(), utf16_output.data());
1921
  } else
1922
    #endif
1923
  {
1924
    return convert_valid_utf8_to_utf16le(
1925
        reinterpret_cast<const char *>(valid_utf8_input.data()),
1926
        valid_utf8_input.size(), utf16_output.data());
1927
  }
1928
}
1929
  #endif // SIMDUTF_SPAN
1930
1931
/**
1932
 * Convert valid UTF-8 string into UTF-16BE string.
1933
 *
1934
 * This function assumes that the input string is valid UTF-8.
1935
 *
1936
 * @param input         the UTF-8 string to convert
1937
 * @param length        the length of the string in bytes
1938
 * @param utf16_buffer  the pointer to buffer that can hold conversion result
1939
 * @return the number of written char16_t
1940
 */
1941
simdutf_warn_unused size_t convert_valid_utf8_to_utf16be(
1942
    const char *input, size_t length, char16_t *utf16_buffer) noexcept;
1943
  #if SIMDUTF_SPAN
1944
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1945
convert_valid_utf8_to_utf16be(
1946
    const detail::input_span_of_byte_like auto &valid_utf8_input,
1947
    std::span<char16_t> utf16_output) noexcept {
1948
    #if SIMDUTF_CPLUSPLUS23
1949
  if consteval {
1950
    return scalar::utf8_to_utf16::convert_valid<endianness::BIG>(
1951
        valid_utf8_input.data(), valid_utf8_input.size(), utf16_output.data());
1952
  } else
1953
    #endif
1954
  {
1955
    return convert_valid_utf8_to_utf16be(
1956
        reinterpret_cast<const char *>(valid_utf8_input.data()),
1957
        valid_utf8_input.size(), utf16_output.data());
1958
  }
1959
}
1960
  #endif // SIMDUTF_SPAN
1961
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
1962
1963
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
1964
/**
1965
 * Convert valid UTF-8 string into UTF-32 string.
1966
 *
1967
 * This function assumes that the input string is valid UTF-8.
1968
 *
1969
 * @param input         the UTF-8 string to convert
1970
 * @param length        the length of the string in bytes
1971
 * @param utf32_buffer  the pointer to buffer that can hold conversion result
1972
 * @return the number of written char32_t
1973
 */
1974
simdutf_warn_unused size_t convert_valid_utf8_to_utf32(
1975
    const char *input, size_t length, char32_t *utf32_buffer) noexcept;
1976
  #if SIMDUTF_SPAN
1977
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
1978
convert_valid_utf8_to_utf32(
1979
    const detail::input_span_of_byte_like auto &valid_utf8_input,
1980
    std::span<char32_t> utf32_output) noexcept {
1981
    #if SIMDUTF_CPLUSPLUS23
1982
  if consteval {
1983
    return scalar::utf8_to_utf32::convert_valid(
1984
        valid_utf8_input.data(), valid_utf8_input.size(), utf32_output.data());
1985
  } else
1986
    #endif
1987
  {
1988
    return convert_valid_utf8_to_utf32(
1989
        reinterpret_cast<const char *>(valid_utf8_input.data()),
1990
        valid_utf8_input.size(), utf32_output.data());
1991
  }
1992
}
1993
  #endif // SIMDUTF_SPAN
1994
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
1995
1996
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
1997
/**
1998
 * Return the number of bytes that this Latin1 string would require in UTF-8
1999
 * format.
2000
 *
2001
 * @param input         the Latin1 string to convert
2002
 * @param length        the length of the string bytes
2003
 * @return the number of bytes required to encode the Latin1 string as UTF-8
2004
 */
2005
simdutf_warn_unused size_t utf8_length_from_latin1(const char *input,
2006
                                                   size_t length) noexcept;
2007
  #if SIMDUTF_SPAN
2008
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2009
utf8_length_from_latin1(
2010
    const detail::input_span_of_byte_like auto &latin1_input) noexcept {
2011
    #if SIMDUTF_CPLUSPLUS23
2012
  if consteval {
2013
    return scalar::latin1_to_utf8::utf8_length_from_latin1(latin1_input.data(),
2014
                                                           latin1_input.size());
2015
  } else
2016
    #endif
2017
  {
2018
    return utf8_length_from_latin1(
2019
        reinterpret_cast<const char *>(latin1_input.data()),
2020
        latin1_input.size());
2021
  }
2022
}
2023
  #endif // SIMDUTF_SPAN
2024
2025
/**
2026
 * Compute the number of bytes that this UTF-8 string would require in Latin1
2027
 * format.
2028
 *
2029
 * This function does not validate the input. It is acceptable to pass invalid
2030
 * UTF-8 strings. The result is then implementation defined, but it remains a
2031
 * safe buffer size: convert_utf8_to_latin1 and
2032
 * convert_utf8_to_latin1_with_errors never write more bytes than this
2033
 * function returns, even when they reject the input.
2034
 *
2035
 * This function is not BOM-aware.
2036
 *
2037
 * @param input         the UTF-8 string to convert
2038
 * @param length        the length of the string in byte
2039
 * @return the number of bytes required to encode the UTF-8 string as Latin1
2040
 */
2041
simdutf_warn_unused size_t latin1_length_from_utf8(const char *input,
2042
                                                   size_t length) noexcept;
2043
  #if SIMDUTF_SPAN
2044
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2045
latin1_length_from_utf8(
2046
    const detail::input_span_of_byte_like auto &valid_utf8_input) noexcept {
2047
    #if SIMDUTF_CPLUSPLUS23
2048
  if consteval {
2049
    return scalar::utf8::count_code_points(valid_utf8_input.data(),
2050
                                           valid_utf8_input.size());
2051
  } else
2052
    #endif
2053
  {
2054
    return latin1_length_from_utf8(
2055
        reinterpret_cast<const char *>(valid_utf8_input.data()),
2056
        valid_utf8_input.size());
2057
  }
2058
}
2059
  #endif // SIMDUTF_SPAN
2060
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
2061
2062
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
2063
/**
2064
 * Compute the number of 2-byte code units that this UTF-8 string would require
2065
 * in UTF-16LE format.
2066
 *
2067
 * This function does not validate the input. It is acceptable to pass invalid
2068
 * UTF-8 strings. The result is then implementation defined, but it remains a
2069
 * safe buffer size: convert_utf8_to_utf16, convert_utf8_to_utf16le,
2070
 * convert_utf8_to_utf16be and their _with_errors variants never write more
2071
 * char16_t than this function returns, even when they reject the input. To
2072
 * transcode invalid input instead of rejecting it, use
2073
 * utf16_length_from_utf8_with_replacement and
2074
 * convert_utf8_to_utf16_with_replacement.
2075
 *
2076
 * This function is not BOM-aware.
2077
 *
2078
 * @param input         the UTF-8 string to process
2079
 * @param length        the length of the string in bytes
2080
 * @return the number of char16_t code units required to encode the UTF-8 string
2081
 * as UTF-16LE
2082
 */
2083
simdutf_warn_unused size_t utf16_length_from_utf8(const char *input,
2084
                                                  size_t length) noexcept;
2085
  #if SIMDUTF_SPAN
2086
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2087
utf16_length_from_utf8(
2088
    const detail::input_span_of_byte_like auto &valid_utf8_input) noexcept {
2089
    #if SIMDUTF_CPLUSPLUS23
2090
  if consteval {
2091
    return scalar::utf8::utf16_length_from_utf8(valid_utf8_input.data(),
2092
                                                valid_utf8_input.size());
2093
  } else
2094
    #endif
2095
  {
2096
    return utf16_length_from_utf8(
2097
        reinterpret_cast<const char *>(valid_utf8_input.data()),
2098
        valid_utf8_input.size());
2099
  }
2100
}
2101
  #endif // SIMDUTF_SPAN
2102
2103
/**
2104
 * Compute the number of char16_t that this UTF-8 string requires in UTF-16
2105
 * when ill-formed subsequences are replaced by U+FFFD, and record where those
2106
 * subsequences are.
2107
 *
2108
 * Substitution follows Unicode maximal subparts (the WHATWG rule): each
2109
 * maximal subpart becomes one U+FFFD. count is always the number of char16_t
2110
 * that convert_utf8_to_utf16_with_replacement writes, and it is at most
2111
 * `length`. The count does not depend on endianness.
2112
 *
2113
 * error is SUCCESS when the input is valid UTF-8. Otherwise it is the error
2114
 * code of the first ill-formed sequence (HEADER_BITS, TOO_SHORT, TOO_LONG,
2115
 * OVERLONG, TOO_LARGE, or SURROGATE). count stays correct either way.
2116
 *
2117
 * error_offset holds the first error_count byte indexes, at most
2118
 * utf8_to_utf16_result::max_errors (16), counting from the start of input.
2119
 * more_errors is true when the input has further ill-formed subsequences.
2120
 * Pass the whole result to convert_utf8_to_utf16_with_replacement on these
2121
 * same bytes. A complete list (more_errors false) lets that conversion skip a
2122
 * second scan.
2123
 *
2124
 * When the input is known to be valid, utf16_length_from_utf8 is the faster
2125
 * length function: it only counts, whereas this one validates.
2126
 *
2127
 * This function is not BOM-aware.
2128
 *
2129
 * @param input         the UTF-8 string to process
2130
 * @param length        the length of the string in bytes
2131
 * @return a utf8_to_utf16_result
2132
 */
2133
simdutf_warn_unused utf8_to_utf16_result
2134
utf16_length_from_utf8_with_replacement(const char *input,
2135
                                        size_t length) noexcept;
2136
  #if SIMDUTF_SPAN
2137
simdutf_really_inline
2138
    simdutf_warn_unused simdutf_constexpr23 utf8_to_utf16_result
2139
    utf16_length_from_utf8_with_replacement(
2140
        const detail::input_span_of_byte_like auto &utf8_input) noexcept {
2141
    #if SIMDUTF_CPLUSPLUS23
2142
  if consteval {
2143
    return scalar::utf8_to_utf16::utf16_length_from_utf8_with_replacement(
2144
        utf8_input.data(), utf8_input.size());
2145
  } else
2146
    #endif
2147
  {
2148
    return utf16_length_from_utf8_with_replacement(
2149
        reinterpret_cast<const char *>(utf8_input.data()), utf8_input.size());
2150
  }
2151
}
2152
  #endif // SIMDUTF_SPAN
2153
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
2154
2155
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
2156
/**
2157
 * Compute the number of 4-byte code units that this UTF-8 string would require
2158
 * in UTF-32 format.
2159
 *
2160
 * This function is equivalent to count_utf8
2161
 *
2162
 * This function does not validate the input. It is acceptable to pass invalid
2163
 * UTF-8 strings. The result is then implementation defined, but it remains a
2164
 * safe buffer size: convert_utf8_to_utf32 and convert_utf8_to_utf32_with_errors
2165
 * never write more char32_t than this function returns, even when they reject
2166
 * the input.
2167
 *
2168
 * This function is not BOM-aware.
2169
 *
2170
 * @param input         the UTF-8 string to process
2171
 * @param length        the length of the string in bytes
2172
 * @return the number of char32_t code units required to encode the UTF-8 string
2173
 * as UTF-32
2174
 */
2175
simdutf_warn_unused size_t utf32_length_from_utf8(const char *input,
2176
                                                  size_t length) noexcept;
2177
  #if SIMDUTF_SPAN
2178
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2179
utf32_length_from_utf8(
2180
    const detail::input_span_of_byte_like auto &valid_utf8_input) noexcept {
2181
2182
    #if SIMDUTF_CPLUSPLUS23
2183
  if consteval {
2184
    return scalar::utf8::count_code_points(valid_utf8_input.data(),
2185
                                           valid_utf8_input.size());
2186
  } else
2187
    #endif
2188
  {
2189
    return utf32_length_from_utf8(
2190
        reinterpret_cast<const char *>(valid_utf8_input.data()),
2191
        valid_utf8_input.size());
2192
  }
2193
}
2194
  #endif // SIMDUTF_SPAN
2195
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
2196
2197
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
2198
/**
2199
 * Using native endianness, convert possibly broken UTF-16 string into UTF-8
2200
 * string.
2201
 *
2202
 * During the conversion also validation of the input string is done.
2203
 * This function is suitable to work with inputs from untrusted sources.
2204
 *
2205
 * This function is not BOM-aware.
2206
 *
2207
 * @param input         the UTF-16 string to convert
2208
 * @param length        the length of the string in 2-byte code units (char16_t)
2209
 * @param utf8_buffer   the pointer to buffer that can hold conversion result
2210
 * @return number of written code units; 0 if input is not a valid UTF-16LE
2211
 * string
2212
 */
2213
simdutf_warn_unused size_t convert_utf16_to_utf8(const char16_t *input,
2214
                                                 size_t length,
2215
                                                 char *utf8_buffer) noexcept;
2216
  #if SIMDUTF_SPAN
2217
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2218
convert_utf16_to_utf8(
2219
    std::span<const char16_t> utf16_input,
2220
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2221
    #if SIMDUTF_CPLUSPLUS23
2222
  if consteval {
2223
    return scalar::utf16_to_utf8::convert<endianness::NATIVE>(
2224
        utf16_input.data(), utf16_input.size(), utf8_output.data());
2225
  } else
2226
    #endif
2227
  {
2228
    return convert_utf16_to_utf8(utf16_input.data(), utf16_input.size(),
2229
                                 reinterpret_cast<char *>(utf8_output.data()));
2230
  }
2231
}
2232
  #endif // SIMDUTF_SPAN
2233
2234
/**
2235
 * Using native endianness, convert possibly broken UTF-16 string into UTF-8
2236
 * string with output limit.
2237
 *
2238
 * We write as many characters as possible into the output buffer,
2239
 *
2240
 * During the conversion also validation of the input string is done.
2241
 * This function is suitable to work with inputs from untrusted sources.
2242
 *
2243
 * This function is not BOM-aware.
2244
 *
2245
 * Using convert_utf16_to_utf8_safe instead of convert_utf16_to_utf8 comes with
2246
 * a significant penalty in some cases, being up to three times slower,
2247
 * especially on short inputs. If you have allocated the output buffer so that
2248
 * it contains utf8_length_from_utf16(input, length) bytes, then prefer
2249
 * convert_utf16_to_utf8.
2250
 *
2251
 * @param input         the UTF-16 string to convert
2252
 * @param length        the length of the string in 16-bit code units (char16_t)
2253
 * @param utf8_output   the pointer to buffer that can hold conversion result
2254
 * @param utf8_len      the maximum output length
2255
 * @return the number of written char; 0 if conversion is not possible
2256
 */
2257
simdutf_warn_unused size_t convert_utf16_to_utf8_safe(const char16_t *input,
2258
                                                      size_t length,
2259
                                                      char *utf8_output,
2260
                                                      size_t utf8_len) noexcept;
2261
  #if SIMDUTF_SPAN
2262
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2263
convert_utf16_to_utf8_safe(
2264
    std::span<const char16_t> utf16_input,
2265
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2266
      // implementation note: outputspan is a forwarding ref to avoid copying
2267
      // and allow both lvalues and rvalues. std::span can be copied without
2268
      // problems, but std::vector should not, and this function should accept
2269
      // both. it will allow using an owning rvalue ref (example: passing a
2270
      // temporary std::string) as output, but the user will quickly find out
2271
      // that he has no way of getting the data out of the object in that case.
2272
    #if SIMDUTF_CPLUSPLUS23
2273
  if consteval {
2274
    const full_result r =
2275
        scalar::utf16_to_utf8::convert_with_errors<endianness::NATIVE, true>(
2276
            utf16_input.data(), utf16_input.size(), utf8_output.data(),
2277
            utf8_output.size());
2278
    if (r.error != error_code::SUCCESS &&
2279
        r.error != error_code::OUTPUT_BUFFER_TOO_SMALL) {
2280
      return 0;
2281
    }
2282
    return r.output_count;
2283
  } else
2284
    #endif
2285
  {
2286
    return convert_utf16_to_utf8_safe(
2287
        utf16_input.data(), utf16_input.size(),
2288
        reinterpret_cast<char *>(utf8_output.data()), utf8_output.size());
2289
  }
2290
}
2291
  #endif // SIMDUTF_SPAN
2292
2293
/**
2294
 * Convert a possibly broken UTF-16 string into a size-limited UTF-8 buffer and
2295
 * report how much input was consumed and output was written.
2296
 *
2297
 * We write as many complete characters as possible while validating the input.
2298
 * The returned error is SUCCESS if all input was consumed,
2299
 * OUTPUT_BUFFER_TOO_SMALL if the next character does not fit, or SURROGATE if
2300
 * an unpaired surrogate was found.
2301
 *
2302
 * @param input         the UTF-16 string to convert
2303
 * @param length        the length in 16-bit code units
2304
 * @param utf8_output   the pointer to the output buffer
2305
 * @param utf8_len      the maximum output length
2306
 * @return a full_result with error, input_count and output_count
2307
 */
2308
simdutf_warn_unused full_result convert_utf16_to_utf8_safe_with_details(
2309
    const char16_t *input, size_t length, char *utf8_output,
2310
    size_t utf8_len) noexcept;
2311
  #if SIMDUTF_SPAN
2312
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 full_result
2313
convert_utf16_to_utf8_safe_with_details(
2314
    std::span<const char16_t> utf16_input,
2315
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2316
    #if SIMDUTF_CPLUSPLUS23
2317
  if consteval {
2318
    if (utf16_input.empty()) {
2319
      return full_result(error_code::SUCCESS, 0, 0);
2320
    }
2321
    return scalar::utf16_to_utf8::convert_with_errors<endianness::NATIVE, true>(
2322
        utf16_input.data(), utf16_input.size(), utf8_output.data(),
2323
        utf8_output.size());
2324
  } else
2325
    #endif
2326
  {
2327
    return convert_utf16_to_utf8_safe_with_details(
2328
        utf16_input.data(), utf16_input.size(),
2329
        reinterpret_cast<char *>(utf8_output.data()), utf8_output.size());
2330
  }
2331
}
2332
  #endif // SIMDUTF_SPAN
2333
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
2334
2335
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
2336
/**
2337
 * Using native endianness, convert possibly broken UTF-16 string into Latin1
2338
 * string.
2339
 *
2340
 * During the conversion also validation of the input string is done.
2341
 * This function is suitable to work with inputs from untrusted sources.
2342
 *
2343
 * This function is not BOM-aware.
2344
 *
2345
 * @param input         the UTF-16 string to convert
2346
 * @param length        the length of the string in 2-byte code units (char16_t)
2347
 * @param latin1_buffer   the pointer to buffer that can hold conversion result
2348
 * @return number of written code units; 0 if input is not a valid UTF-16 string
2349
 * or if it cannot be represented as Latin1
2350
 */
2351
simdutf_warn_unused size_t convert_utf16_to_latin1(
2352
    const char16_t *input, size_t length, char *latin1_buffer) noexcept;
2353
  #if SIMDUTF_SPAN
2354
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2355
convert_utf16_to_latin1(
2356
    std::span<const char16_t> utf16_input,
2357
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
2358
    #if SIMDUTF_CPLUSPLUS23
2359
  if consteval {
2360
    return scalar::utf16_to_latin1::convert<endianness::NATIVE>(
2361
        utf16_input.data(), utf16_input.size(), latin1_output.data());
2362
  } else
2363
    #endif
2364
  {
2365
    return convert_utf16_to_latin1(
2366
        utf16_input.data(), utf16_input.size(),
2367
        reinterpret_cast<char *>(latin1_output.data()));
2368
  }
2369
}
2370
  #endif // SIMDUTF_SPAN
2371
2372
/**
2373
 * Convert possibly broken UTF-16LE string into Latin1 string.
2374
 * If the string cannot be represented as Latin1, an error
2375
 * is returned.
2376
 *
2377
 * During the conversion also validation of the input string is done.
2378
 * This function is suitable to work with inputs from untrusted sources.
2379
 *
2380
 * This function is not BOM-aware.
2381
 *
2382
 * @param input         the UTF-16LE string to convert
2383
 * @param length        the length of the string in 2-byte code units (char16_t)
2384
 * @param latin1_buffer   the pointer to buffer that can hold conversion result
2385
 * @return number of written code units; 0 if input is not a valid UTF-16LE
2386
 * string or if it cannot be represented as Latin1
2387
 */
2388
simdutf_warn_unused size_t convert_utf16le_to_latin1(
2389
    const char16_t *input, size_t length, char *latin1_buffer) noexcept;
2390
  #if SIMDUTF_SPAN
2391
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2392
convert_utf16le_to_latin1(
2393
    std::span<const char16_t> utf16_input,
2394
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
2395
    #if SIMDUTF_CPLUSPLUS23
2396
  if consteval {
2397
    return scalar::utf16_to_latin1::convert<endianness::LITTLE>(
2398
        utf16_input.data(), utf16_input.size(), latin1_output.data());
2399
  } else
2400
    #endif
2401
  {
2402
    return convert_utf16le_to_latin1(
2403
        utf16_input.data(), utf16_input.size(),
2404
        reinterpret_cast<char *>(latin1_output.data()));
2405
  }
2406
}
2407
  #endif // SIMDUTF_SPAN
2408
2409
/**
2410
 * Convert possibly broken UTF-16BE string into Latin1 string.
2411
 *
2412
 * During the conversion also validation of the input string is done.
2413
 * This function is suitable to work with inputs from untrusted sources.
2414
 *
2415
 * This function is not BOM-aware.
2416
 *
2417
 * @param input         the UTF-16BE string to convert
2418
 * @param length        the length of the string in 2-byte code units (char16_t)
2419
 * @param latin1_buffer   the pointer to buffer that can hold conversion result
2420
 * @return number of written code units; 0 if input is not a valid UTF-16BE
2421
 * string or if it cannot be represented as Latin1
2422
 */
2423
simdutf_warn_unused size_t convert_utf16be_to_latin1(
2424
    const char16_t *input, size_t length, char *latin1_buffer) noexcept;
2425
  #if SIMDUTF_SPAN
2426
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2427
convert_utf16be_to_latin1(
2428
    std::span<const char16_t> utf16_input,
2429
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
2430
    #if SIMDUTF_CPLUSPLUS23
2431
  if consteval {
2432
    return scalar::utf16_to_latin1::convert<endianness::BIG>(
2433
        utf16_input.data(), utf16_input.size(), latin1_output.data());
2434
  } else
2435
    #endif
2436
  {
2437
    return convert_utf16be_to_latin1(
2438
        utf16_input.data(), utf16_input.size(),
2439
        reinterpret_cast<char *>(latin1_output.data()));
2440
  }
2441
}
2442
  #endif // SIMDUTF_SPAN
2443
#endif   // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
2444
2445
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
2446
/**
2447
 * Convert possibly broken UTF-16LE string into UTF-8 string.
2448
 *
2449
 * During the conversion also validation of the input string is done.
2450
 * This function is suitable to work with inputs from untrusted sources.
2451
 *
2452
 * This function is not BOM-aware.
2453
 *
2454
 * @param input         the UTF-16LE string to convert
2455
 * @param length        the length of the string in 2-byte code units (char16_t)
2456
 * @param utf8_buffer   the pointer to buffer that can hold conversion result
2457
 * @return number of written code units; 0 if input is not a valid UTF-16LE
2458
 * string
2459
 */
2460
simdutf_warn_unused size_t convert_utf16le_to_utf8(const char16_t *input,
2461
                                                   size_t length,
2462
                                                   char *utf8_buffer) noexcept;
2463
  #if SIMDUTF_SPAN
2464
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2465
convert_utf16le_to_utf8(
2466
    std::span<const char16_t> utf16_input,
2467
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2468
    #if SIMDUTF_CPLUSPLUS23
2469
  if consteval {
2470
    return scalar::utf16_to_utf8::convert<endianness::LITTLE>(
2471
        utf16_input.data(), utf16_input.size(), utf8_output.data());
2472
  } else
2473
    #endif
2474
  {
2475
    return convert_utf16le_to_utf8(
2476
        utf16_input.data(), utf16_input.size(),
2477
        reinterpret_cast<char *>(utf8_output.data()));
2478
  }
2479
}
2480
  #endif // SIMDUTF_SPAN
2481
2482
/**
2483
 * Convert possibly broken UTF-16BE string into UTF-8 string.
2484
 *
2485
 * During the conversion also validation of the input string is done.
2486
 * This function is suitable to work with inputs from untrusted sources.
2487
 *
2488
 * This function is not BOM-aware.
2489
 *
2490
 * @param input         the UTF-16BE string to convert
2491
 * @param length        the length of the string in 2-byte code units (char16_t)
2492
 * @param utf8_buffer   the pointer to buffer that can hold conversion result
2493
 * @return number of written code units; 0 if input is not a valid UTF-16LE
2494
 * string
2495
 */
2496
simdutf_warn_unused size_t convert_utf16be_to_utf8(const char16_t *input,
2497
                                                   size_t length,
2498
                                                   char *utf8_buffer) noexcept;
2499
  #if SIMDUTF_SPAN
2500
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2501
convert_utf16be_to_utf8(
2502
    std::span<const char16_t> utf16_input,
2503
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2504
    #if SIMDUTF_CPLUSPLUS23
2505
  if consteval {
2506
    return scalar::utf16_to_utf8::convert<endianness::BIG>(
2507
        utf16_input.data(), utf16_input.size(), utf8_output.data());
2508
  } else
2509
    #endif
2510
  {
2511
    return convert_utf16be_to_utf8(
2512
        utf16_input.data(), utf16_input.size(),
2513
        reinterpret_cast<char *>(utf8_output.data()));
2514
  }
2515
}
2516
  #endif // SIMDUTF_SPAN
2517
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
2518
2519
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
2520
/**
2521
 * Using native endianness, convert possibly broken UTF-16 string into Latin1
2522
 * string.
2523
 *
2524
 * During the conversion also validation of the input string is done.
2525
 * This function is suitable to work with inputs from untrusted sources.
2526
 * This function is not BOM-aware.
2527
 *
2528
 * @param input         the UTF-16 string to convert
2529
 * @param length        the length of the string in 2-byte code units (char16_t)
2530
 * @param latin1_buffer   the pointer to buffer that can hold conversion result
2531
 * @return a result pair struct (of type simdutf::result containing the two
2532
 * fields error and count) with an error code and either position of the error
2533
 * (in the input in code units) if any, or the number of char written if
2534
 * successful.
2535
 */
2536
simdutf_warn_unused result convert_utf16_to_latin1_with_errors(
2537
    const char16_t *input, size_t length, char *latin1_buffer) noexcept;
2538
  #if SIMDUTF_SPAN
2539
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
2540
convert_utf16_to_latin1_with_errors(
2541
    std::span<const char16_t> utf16_input,
2542
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
2543
    #if SIMDUTF_CPLUSPLUS23
2544
  if consteval {
2545
    return scalar::utf16_to_latin1::convert_with_errors<endianness::NATIVE>(
2546
        utf16_input.data(), utf16_input.size(), latin1_output.data());
2547
  } else
2548
    #endif
2549
  {
2550
    return convert_utf16_to_latin1_with_errors(
2551
        utf16_input.data(), utf16_input.size(),
2552
        reinterpret_cast<char *>(latin1_output.data()));
2553
  }
2554
}
2555
  #endif // SIMDUTF_SPAN
2556
2557
/**
2558
 * Convert possibly broken UTF-16LE string into Latin1 string.
2559
 *
2560
 * During the conversion also validation of the input string is done.
2561
 * This function is suitable to work with inputs from untrusted sources.
2562
 * This function is not BOM-aware.
2563
 *
2564
 * @param input         the UTF-16LE string to convert
2565
 * @param length        the length of the string in 2-byte code units (char16_t)
2566
 * @param latin1_buffer   the pointer to buffer that can hold conversion result
2567
 * @return a result pair struct (of type simdutf::result containing the two
2568
 * fields error and count) with an error code and either position of the error
2569
 * (in the input in code units) if any, or the number of char written if
2570
 * successful.
2571
 */
2572
simdutf_warn_unused result convert_utf16le_to_latin1_with_errors(
2573
    const char16_t *input, size_t length, char *latin1_buffer) noexcept;
2574
  #if SIMDUTF_SPAN
2575
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
2576
convert_utf16le_to_latin1_with_errors(
2577
    std::span<const char16_t> utf16_input,
2578
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
2579
    #if SIMDUTF_CPLUSPLUS23
2580
  if consteval {
2581
    return scalar::utf16_to_latin1::convert_with_errors<endianness::LITTLE>(
2582
        utf16_input.data(), utf16_input.size(), latin1_output.data());
2583
  } else
2584
    #endif
2585
  {
2586
    return convert_utf16le_to_latin1_with_errors(
2587
        utf16_input.data(), utf16_input.size(),
2588
        reinterpret_cast<char *>(latin1_output.data()));
2589
  }
2590
}
2591
  #endif // SIMDUTF_SPAN
2592
2593
/**
2594
 * Convert possibly broken UTF-16BE string into Latin1 string.
2595
 * If the string cannot be represented as Latin1, an error
2596
 * is returned.
2597
 *
2598
 * During the conversion also validation of the input string is done.
2599
 * This function is suitable to work with inputs from untrusted sources.
2600
 * This function is not BOM-aware.
2601
 *
2602
 * @param input         the UTF-16BE string to convert
2603
 * @param length        the length of the string in 2-byte code units (char16_t)
2604
 * @param latin1_buffer   the pointer to buffer that can hold conversion result
2605
 * @return a result pair struct (of type simdutf::result containing the two
2606
 * fields error and count) with an error code and either position of the error
2607
 * (in the input in code units) if any, or the number of char written if
2608
 * successful.
2609
 */
2610
simdutf_warn_unused result convert_utf16be_to_latin1_with_errors(
2611
    const char16_t *input, size_t length, char *latin1_buffer) noexcept;
2612
  #if SIMDUTF_SPAN
2613
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
2614
convert_utf16be_to_latin1_with_errors(
2615
    std::span<const char16_t> utf16_input,
2616
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
2617
    #if SIMDUTF_CPLUSPLUS23
2618
  if consteval {
2619
    return scalar::utf16_to_latin1::convert_with_errors<endianness::BIG>(
2620
        utf16_input.data(), utf16_input.size(), latin1_output.data());
2621
  } else
2622
    #endif
2623
  {
2624
    return convert_utf16be_to_latin1_with_errors(
2625
        utf16_input.data(), utf16_input.size(),
2626
        reinterpret_cast<char *>(latin1_output.data()));
2627
  }
2628
}
2629
  #endif // SIMDUTF_SPAN
2630
#endif   // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
2631
2632
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
2633
/**
2634
 * Using native endianness, convert possibly broken UTF-16 string into UTF-8
2635
 * string and stop on error.
2636
 *
2637
 * During the conversion also validation of the input string is done.
2638
 * This function is suitable to work with inputs from untrusted sources.
2639
 *
2640
 * This function is not BOM-aware.
2641
 *
2642
 * @param input         the UTF-16 string to convert
2643
 * @param length        the length of the string in 2-byte code units (char16_t)
2644
 * @param utf8_buffer   the pointer to buffer that can hold conversion result
2645
 * @return a result pair struct (of type simdutf::result containing the two
2646
 * fields error and count) with an error code and either position of the error
2647
 * (in the input in code units) if any, or the number of char written if
2648
 * successful.
2649
 */
2650
simdutf_warn_unused result convert_utf16_to_utf8_with_errors(
2651
    const char16_t *input, size_t length, char *utf8_buffer) noexcept;
2652
  #if SIMDUTF_SPAN
2653
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
2654
convert_utf16_to_utf8_with_errors(
2655
    std::span<const char16_t> utf16_input,
2656
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2657
    #if SIMDUTF_CPLUSPLUS23
2658
  if consteval {
2659
    return scalar::utf16_to_utf8::convert_with_errors<endianness::NATIVE>(
2660
        utf16_input.data(), utf16_input.size(), utf8_output.data());
2661
  } else
2662
    #endif
2663
  {
2664
    return convert_utf16_to_utf8_with_errors(
2665
        utf16_input.data(), utf16_input.size(),
2666
        reinterpret_cast<char *>(utf8_output.data()));
2667
  }
2668
}
2669
  #endif // SIMDUTF_SPAN
2670
2671
/**
2672
 * Convert possibly broken UTF-16LE string into UTF-8 string and stop on error.
2673
 *
2674
 * During the conversion also validation of the input string is done.
2675
 * This function is suitable to work with inputs from untrusted sources.
2676
 *
2677
 * This function is not BOM-aware.
2678
 *
2679
 * @param input         the UTF-16LE string to convert
2680
 * @param length        the length of the string in 2-byte code units (char16_t)
2681
 * @param utf8_buffer   the pointer to buffer that can hold conversion result
2682
 * @return a result pair struct (of type simdutf::result containing the two
2683
 * fields error and count) with an error code and either position of the error
2684
 * (in the input in code units) if any, or the number of char written if
2685
 * successful.
2686
 */
2687
simdutf_warn_unused result convert_utf16le_to_utf8_with_errors(
2688
    const char16_t *input, size_t length, char *utf8_buffer) noexcept;
2689
  #if SIMDUTF_SPAN
2690
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
2691
convert_utf16le_to_utf8_with_errors(
2692
    std::span<const char16_t> utf16_input,
2693
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2694
    #if SIMDUTF_CPLUSPLUS23
2695
  if consteval {
2696
    return scalar::utf16_to_utf8::convert_with_errors<endianness::LITTLE>(
2697
        utf16_input.data(), utf16_input.size(), utf8_output.data());
2698
  } else
2699
    #endif
2700
  {
2701
    return convert_utf16le_to_utf8_with_errors(
2702
        utf16_input.data(), utf16_input.size(),
2703
        reinterpret_cast<char *>(utf8_output.data()));
2704
  }
2705
}
2706
  #endif // SIMDUTF_SPAN
2707
2708
/**
2709
 * Convert possibly broken UTF-16BE string into UTF-8 string and stop on error.
2710
 *
2711
 * During the conversion also validation of the input string is done.
2712
 * This function is suitable to work with inputs from untrusted sources.
2713
 *
2714
 * This function is not BOM-aware.
2715
 *
2716
 * @param input         the UTF-16BE string to convert
2717
 * @param length        the length of the string in 2-byte code units (char16_t)
2718
 * @param utf8_buffer   the pointer to buffer that can hold conversion result
2719
 * @return a result pair struct (of type simdutf::result containing the two
2720
 * fields error and count) with an error code and either position of the error
2721
 * (in the input in code units) if any, or the number of char written if
2722
 * successful.
2723
 */
2724
simdutf_warn_unused result convert_utf16be_to_utf8_with_errors(
2725
    const char16_t *input, size_t length, char *utf8_buffer) noexcept;
2726
  #if SIMDUTF_SPAN
2727
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
2728
convert_utf16be_to_utf8_with_errors(
2729
    std::span<const char16_t> utf16_input,
2730
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2731
    #if SIMDUTF_CPLUSPLUS23
2732
  if consteval {
2733
    return scalar::utf16_to_utf8::convert_with_errors<endianness::BIG>(
2734
        utf16_input.data(), utf16_input.size(), utf8_output.data());
2735
  } else
2736
    #endif
2737
  {
2738
    return convert_utf16be_to_utf8_with_errors(
2739
        utf16_input.data(), utf16_input.size(),
2740
        reinterpret_cast<char *>(utf8_output.data()));
2741
  }
2742
}
2743
  #endif // SIMDUTF_SPAN
2744
2745
/**
2746
 * Convert possibly broken UTF-16LE string into UTF-8 string, replacing
2747
 * unpaired surrogates with the Unicode replacement character U+FFFD.
2748
 *
2749
 * This function always succeeds: unpaired surrogates are replaced with
2750
 * U+FFFD (3 bytes in UTF-8: 0xEF 0xBF 0xBD).
2751
 *
2752
 * This function is not BOM-aware.
2753
 *
2754
 * @param input         the UTF-16LE string to convert
2755
 * @param length        the length of the string in 2-byte code units (char16_t)
2756
 * @param utf8_buffer   the pointer to buffer that can hold conversion result
2757
 * @return number of written code units
2758
 */
2759
simdutf_warn_unused size_t convert_utf16le_to_utf8_with_replacement(
2760
    const char16_t *input, size_t length, char *utf8_buffer) noexcept;
2761
  #if SIMDUTF_SPAN
2762
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2763
convert_utf16le_to_utf8_with_replacement(
2764
    std::span<const char16_t> utf16_input,
2765
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2766
    #if SIMDUTF_CPLUSPLUS23
2767
  if consteval {
2768
    return scalar::utf16_to_utf8::convert_with_replacement<endianness::LITTLE>(
2769
        utf16_input.data(), utf16_input.size(), utf8_output.data());
2770
  } else
2771
    #endif
2772
  {
2773
    return convert_utf16le_to_utf8_with_replacement(
2774
        utf16_input.data(), utf16_input.size(),
2775
        reinterpret_cast<char *>(utf8_output.data()));
2776
  }
2777
}
2778
  #endif // SIMDUTF_SPAN
2779
2780
/**
2781
 * Convert possibly broken UTF-16BE string into UTF-8 string, replacing
2782
 * unpaired surrogates with the Unicode replacement character U+FFFD.
2783
 *
2784
 * This function always succeeds: unpaired surrogates are replaced with
2785
 * U+FFFD (3 bytes in UTF-8: 0xEF 0xBF 0xBD).
2786
 *
2787
 * This function is not BOM-aware.
2788
 *
2789
 * @param input         the UTF-16BE string to convert
2790
 * @param length        the length of the string in 2-byte code units (char16_t)
2791
 * @param utf8_buffer   the pointer to buffer that can hold conversion result
2792
 * @return number of written code units
2793
 */
2794
simdutf_warn_unused size_t convert_utf16be_to_utf8_with_replacement(
2795
    const char16_t *input, size_t length, char *utf8_buffer) noexcept;
2796
  #if SIMDUTF_SPAN
2797
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2798
convert_utf16be_to_utf8_with_replacement(
2799
    std::span<const char16_t> utf16_input,
2800
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2801
    #if SIMDUTF_CPLUSPLUS23
2802
  if consteval {
2803
    return scalar::utf16_to_utf8::convert_with_replacement<endianness::BIG>(
2804
        utf16_input.data(), utf16_input.size(), utf8_output.data());
2805
  } else
2806
    #endif
2807
  {
2808
    return convert_utf16be_to_utf8_with_replacement(
2809
        utf16_input.data(), utf16_input.size(),
2810
        reinterpret_cast<char *>(utf8_output.data()));
2811
  }
2812
}
2813
  #endif // SIMDUTF_SPAN
2814
2815
/**
2816
 * Convert possibly broken UTF-16 string (native endianness) into UTF-8 string,
2817
 * replacing unpaired surrogates with the Unicode replacement character U+FFFD.
2818
 *
2819
 * This function always succeeds: unpaired surrogates are replaced with
2820
 * U+FFFD (3 bytes in UTF-8: 0xEF 0xBF 0xBD).
2821
 *
2822
 * This function is not BOM-aware.
2823
 *
2824
 * @param input         the UTF-16 string to convert
2825
 * @param length        the length of the string in 2-byte code units (char16_t)
2826
 * @param utf8_buffer   the pointer to buffer that can hold conversion result
2827
 * @return number of written code units
2828
 */
2829
simdutf_warn_unused size_t convert_utf16_to_utf8_with_replacement(
2830
    const char16_t *input, size_t length, char *utf8_buffer) noexcept;
2831
  #if SIMDUTF_SPAN
2832
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2833
convert_utf16_to_utf8_with_replacement(
2834
    std::span<const char16_t> utf16_input,
2835
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2836
    #if SIMDUTF_CPLUSPLUS23
2837
  if consteval {
2838
    return scalar::utf16_to_utf8::convert_with_replacement<endianness::NATIVE>(
2839
        utf16_input.data(), utf16_input.size(), utf8_output.data());
2840
  } else
2841
    #endif
2842
  {
2843
    return convert_utf16_to_utf8_with_replacement(
2844
        utf16_input.data(), utf16_input.size(),
2845
        reinterpret_cast<char *>(utf8_output.data()));
2846
  }
2847
}
2848
  #endif // SIMDUTF_SPAN
2849
2850
/**
2851
 * Convert a possibly broken UTF-16 string into a size-limited UTF-8 buffer,
2852
 * replacing unpaired surrogates with U+FFFD and reporting how much input was
2853
 * consumed and output was written.
2854
 *
2855
 * We write as many complete characters as possible. The returned error is
2856
 * SUCCESS if all input was consumed, or OUTPUT_BUFFER_TOO_SMALL if the next
2857
 * character or replacement does not fit.
2858
 *
2859
 * @param input         the UTF-16 string to convert
2860
 * @param length        the length in 16-bit code units
2861
 * @param utf8_output   the pointer to the output buffer
2862
 * @param utf8_len      the maximum output length
2863
 * @return a full_result with error, input_count and output_count
2864
 */
2865
simdutf_warn_unused full_result convert_utf16_to_utf8_with_replacement_safe(
2866
    const char16_t *input, size_t length, char *utf8_output,
2867
    size_t utf8_len) noexcept;
2868
  #if SIMDUTF_SPAN
2869
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 full_result
2870
convert_utf16_to_utf8_with_replacement_safe(
2871
    std::span<const char16_t> utf16_input,
2872
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2873
    #if SIMDUTF_CPLUSPLUS23
2874
  if consteval {
2875
    return scalar::utf16_to_utf8::convert_with_replacement_safe<
2876
        endianness::NATIVE>(utf16_input.data(), utf16_input.size(),
2877
                            utf8_output.data(), utf8_output.size());
2878
  } else
2879
    #endif
2880
  {
2881
    return convert_utf16_to_utf8_with_replacement_safe(
2882
        utf16_input.data(), utf16_input.size(),
2883
        reinterpret_cast<char *>(utf8_output.data()), utf8_output.size());
2884
  }
2885
}
2886
  #endif // SIMDUTF_SPAN
2887
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
2888
2889
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
2890
/**
2891
 * Using native endianness, convert valid UTF-16 string into UTF-8 string.
2892
 *
2893
 * This function assumes that the input string is valid UTF-16.
2894
 *
2895
 * This function is not BOM-aware.
2896
 *
2897
 * @param input         the UTF-16 string to convert
2898
 * @param length        the length of the string in 2-byte code units (char16_t)
2899
 * @param utf8_buffer   the pointer to a buffer that can hold the conversion
2900
 * result
2901
 * @return number of written code units; 0 if conversion is not possible
2902
 */
2903
simdutf_warn_unused size_t convert_valid_utf16_to_utf8(
2904
    const char16_t *input, size_t length, char *utf8_buffer) noexcept;
2905
  #if SIMDUTF_SPAN
2906
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2907
convert_valid_utf16_to_utf8(
2908
    std::span<const char16_t> valid_utf16_input,
2909
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
2910
    #if SIMDUTF_CPLUSPLUS23
2911
  if consteval {
2912
    return scalar::utf16_to_utf8::convert_valid<endianness::NATIVE>(
2913
        valid_utf16_input.data(), valid_utf16_input.size(), utf8_output.data());
2914
  } else
2915
    #endif
2916
  {
2917
    return convert_valid_utf16_to_utf8(
2918
        valid_utf16_input.data(), valid_utf16_input.size(),
2919
        reinterpret_cast<char *>(utf8_output.data()));
2920
  }
2921
}
2922
  #endif // SIMDUTF_SPAN
2923
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
2924
2925
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
2926
/**
2927
 * Using native endianness, convert UTF-16 string into Latin1 string.
2928
 *
2929
 * This function assumes that the input string is valid UTF-16 and that it can
2930
 * be represented as Latin1. If you violate this assumption, the result is
2931
 * implementation defined and may include system-dependent behavior such as
2932
 * crashes.
2933
 *
2934
 * This function is for expert users only and not part of our public API. Use
2935
 * convert_utf16_to_latin1 instead. The function may be removed from the library
2936
 * in the future.
2937
 *
2938
 * This function is not BOM-aware.
2939
 *
2940
 * @param input         the UTF-16 string to convert
2941
 * @param length        the length of the string in 2-byte code units (char16_t)
2942
 * @param latin1_buffer   the pointer to buffer that can hold conversion result
2943
 * @return number of written code units; 0 if conversion is not possible
2944
 */
2945
simdutf_warn_unused size_t convert_valid_utf16_to_latin1(
2946
    const char16_t *input, size_t length, char *latin1_buffer) noexcept;
2947
  #if SIMDUTF_SPAN
2948
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
2949
convert_valid_utf16_to_latin1(
2950
    std::span<const char16_t> valid_utf16_input,
2951
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
2952
    #if SIMDUTF_CPLUSPLUS23
2953
  if consteval {
2954
    return scalar::utf16_to_latin1::convert_valid_impl<endianness::NATIVE>(
2955
        detail::constexpr_cast_ptr<uint16_t>(valid_utf16_input.data()),
2956
        valid_utf16_input.size(),
2957
        detail::constexpr_cast_writeptr<char>(latin1_output.data()));
2958
  } else
2959
    #endif
2960
  {
2961
    return convert_valid_utf16_to_latin1(
2962
        valid_utf16_input.data(), valid_utf16_input.size(),
2963
        reinterpret_cast<char *>(latin1_output.data()));
2964
  }
2965
}
2966
  #endif // SIMDUTF_SPAN
2967
2968
/**
2969
 * Convert valid UTF-16LE string into Latin1 string.
2970
 *
2971
 * This function assumes that the input string is valid UTF-16LE and that it can
2972
 * be represented as Latin1. If you violate this assumption, the result is
2973
 * implementation defined and may include system-dependent behavior such as
2974
 * crashes.
2975
 *
2976
 * This function is for expert users only and not part of our public API. Use
2977
 * convert_utf16le_to_latin1 instead. The function may be removed from the
2978
 * library in the future.
2979
 *
2980
 * This function is not BOM-aware.
2981
 *
2982
 * @param input         the UTF-16LE string to convert
2983
 * @param length        the length of the string in 2-byte code units (char16_t)
2984
 * @param latin1_buffer   the pointer to buffer that can hold conversion result
2985
 * @return number of written code units; 0 if conversion is not possible
2986
 */
2987
simdutf_warn_unused size_t convert_valid_utf16le_to_latin1(
2988
    const char16_t *input, size_t length, char *latin1_buffer) noexcept;
2989
  #if SIMDUTF_SPAN
2990
simdutf_really_inline simdutf_constexpr23 simdutf_warn_unused size_t
2991
convert_valid_utf16le_to_latin1(
2992
    std::span<const char16_t> valid_utf16_input,
2993
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
2994
    #if SIMDUTF_CPLUSPLUS23
2995
  if consteval {
2996
    return scalar::utf16_to_latin1::convert_valid_impl<endianness::LITTLE>(
2997
        detail::constexpr_cast_ptr<uint16_t>(valid_utf16_input.data()),
2998
        valid_utf16_input.size(),
2999
        detail::constexpr_cast_writeptr<char>(latin1_output.data()));
3000
  } else
3001
    #endif
3002
  {
3003
    return convert_valid_utf16le_to_latin1(
3004
        valid_utf16_input.data(), valid_utf16_input.size(),
3005
        reinterpret_cast<char *>(latin1_output.data()));
3006
  }
3007
}
3008
  #endif // SIMDUTF_SPAN
3009
3010
/**
3011
 * Convert valid UTF-16BE string into Latin1 string.
3012
 *
3013
 * This function assumes that the input string is valid UTF-16BE and that it can
3014
 * be represented as Latin1. If you violate this assumption, the result is
3015
 * implementation defined and may include system-dependent behavior such as
3016
 * crashes.
3017
 *
3018
 * This function is for expert users only and not part of our public API. Use
3019
 * convert_utf16be_to_latin1 instead. The function may be removed from the
3020
 * library in the future.
3021
 *
3022
 * This function is not BOM-aware.
3023
 *
3024
 * @param input         the UTF-16BE string to convert
3025
 * @param length        the length of the string in 2-byte code units (char16_t)
3026
 * @param latin1_buffer   the pointer to buffer that can hold conversion result
3027
 * @return number of written code units; 0 if conversion is not possible
3028
 */
3029
simdutf_warn_unused size_t convert_valid_utf16be_to_latin1(
3030
    const char16_t *input, size_t length, char *latin1_buffer) noexcept;
3031
  #if SIMDUTF_SPAN
3032
simdutf_really_inline simdutf_constexpr23 simdutf_warn_unused size_t
3033
convert_valid_utf16be_to_latin1(
3034
    std::span<const char16_t> valid_utf16_input,
3035
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
3036
    #if SIMDUTF_CPLUSPLUS23
3037
  if consteval {
3038
    return scalar::utf16_to_latin1::convert_valid_impl<endianness::BIG>(
3039
        detail::constexpr_cast_ptr<uint16_t>(valid_utf16_input.data()),
3040
        valid_utf16_input.size(),
3041
        detail::constexpr_cast_writeptr<char>(latin1_output.data()));
3042
  } else
3043
    #endif
3044
  {
3045
    return convert_valid_utf16be_to_latin1(
3046
        valid_utf16_input.data(), valid_utf16_input.size(),
3047
        reinterpret_cast<char *>(latin1_output.data()));
3048
  }
3049
}
3050
  #endif // SIMDUTF_SPAN
3051
#endif   // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
3052
3053
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
3054
/**
3055
 * Convert valid UTF-16LE string into UTF-8 string.
3056
 *
3057
 * This function assumes that the input string is valid UTF-16LE
3058
 *
3059
 * This function is not BOM-aware.
3060
 *
3061
 * @param input         the UTF-16LE string to convert
3062
 * @param length        the length of the string in 2-byte code units (char16_t)
3063
 * @param utf8_buffer   the pointer to a buffer that can hold the conversion
3064
 * result
3065
 * @return number of written code units; 0 if conversion is not possible
3066
 */
3067
simdutf_warn_unused size_t convert_valid_utf16le_to_utf8(
3068
    const char16_t *input, size_t length, char *utf8_buffer) noexcept;
3069
  #if SIMDUTF_SPAN
3070
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3071
convert_valid_utf16le_to_utf8(
3072
    std::span<const char16_t> valid_utf16_input,
3073
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
3074
    #if SIMDUTF_CPLUSPLUS23
3075
  if consteval {
3076
    return scalar::utf16_to_utf8::convert_valid<endianness::NATIVE>(
3077
        valid_utf16_input.data(), valid_utf16_input.size(), utf8_output.data());
3078
  } else
3079
    #endif
3080
  {
3081
    return convert_valid_utf16le_to_utf8(
3082
        valid_utf16_input.data(), valid_utf16_input.size(),
3083
        reinterpret_cast<char *>(utf8_output.data()));
3084
  }
3085
}
3086
  #endif // SIMDUTF_SPAN
3087
3088
/**
3089
 * Convert valid UTF-16BE string into UTF-8 string.
3090
 *
3091
 * This function assumes that the input string is valid UTF-16BE.
3092
 *
3093
 * This function is not BOM-aware.
3094
 *
3095
 * @param input         the UTF-16BE string to convert
3096
 * @param length        the length of the string in 2-byte code units (char16_t)
3097
 * @param utf8_buffer   the pointer to a buffer that can hold the conversion
3098
 * result
3099
 * @return number of written code units; 0 if conversion is not possible
3100
 */
3101
simdutf_warn_unused size_t convert_valid_utf16be_to_utf8(
3102
    const char16_t *input, size_t length, char *utf8_buffer) noexcept;
3103
  #if SIMDUTF_SPAN
3104
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3105
convert_valid_utf16be_to_utf8(
3106
    std::span<const char16_t> valid_utf16_input,
3107
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
3108
    #if SIMDUTF_CPLUSPLUS23
3109
  if consteval {
3110
    return scalar::utf16_to_utf8::convert_valid<endianness::BIG>(
3111
        valid_utf16_input.data(), valid_utf16_input.size(), utf8_output.data());
3112
  } else
3113
    #endif
3114
  {
3115
    return convert_valid_utf16be_to_utf8(
3116
        valid_utf16_input.data(), valid_utf16_input.size(),
3117
        reinterpret_cast<char *>(utf8_output.data()));
3118
  }
3119
}
3120
  #endif // SIMDUTF_SPAN
3121
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
3122
3123
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
3124
/**
3125
 * Using native endianness, convert possibly broken UTF-16 string into UTF-32
3126
 * string.
3127
 *
3128
 * During the conversion also validation of the input string is done.
3129
 * This function is suitable to work with inputs from untrusted sources.
3130
 *
3131
 * This function is not BOM-aware.
3132
 *
3133
 * @param input         the UTF-16 string to convert
3134
 * @param length        the length of the string in 2-byte code units (char16_t)
3135
 * @param utf32_buffer   the pointer to buffer that can hold conversion result
3136
 * @return number of written code units; 0 if input is not a valid UTF-16LE
3137
 * string
3138
 */
3139
simdutf_warn_unused size_t convert_utf16_to_utf32(
3140
    const char16_t *input, size_t length, char32_t *utf32_buffer) noexcept;
3141
  #if SIMDUTF_SPAN
3142
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3143
convert_utf16_to_utf32(std::span<const char16_t> utf16_input,
3144
0
                       std::span<char32_t> utf32_output) noexcept {
3145
0
3146
0
    #if SIMDUTF_CPLUSPLUS23
3147
0
  if consteval {
3148
0
    return scalar::utf16_to_utf32::convert<endianness::NATIVE>(
3149
0
        utf16_input.data(), utf16_input.size(), utf32_output.data());
3150
0
  } else
3151
0
    #endif
3152
0
  {
3153
0
    return convert_utf16_to_utf32(utf16_input.data(), utf16_input.size(),
3154
0
                                  utf32_output.data());
3155
0
  }
3156
0
}
3157
  #endif // SIMDUTF_SPAN
3158
3159
/**
3160
 * Convert possibly broken UTF-16LE string into UTF-32 string.
3161
 *
3162
 * During the conversion also validation of the input string is done.
3163
 * This function is suitable to work with inputs from untrusted sources.
3164
 *
3165
 * This function is not BOM-aware.
3166
 *
3167
 * @param input         the UTF-16LE string to convert
3168
 * @param length        the length of the string in 2-byte code units (char16_t)
3169
 * @param utf32_buffer   the pointer to buffer that can hold conversion result
3170
 * @return number of written code units; 0 if input is not a valid UTF-16LE
3171
 * string
3172
 */
3173
simdutf_warn_unused size_t convert_utf16le_to_utf32(
3174
    const char16_t *input, size_t length, char32_t *utf32_buffer) noexcept;
3175
  #if SIMDUTF_SPAN
3176
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3177
convert_utf16le_to_utf32(std::span<const char16_t> utf16_input,
3178
0
                         std::span<char32_t> utf32_output) noexcept {
3179
0
    #if SIMDUTF_CPLUSPLUS23
3180
0
  if consteval {
3181
0
    return scalar::utf16_to_utf32::convert<endianness::LITTLE>(
3182
0
        utf16_input.data(), utf16_input.size(), utf32_output.data());
3183
0
  } else
3184
0
    #endif
3185
0
  {
3186
0
    return convert_utf16le_to_utf32(utf16_input.data(), utf16_input.size(),
3187
0
                                    utf32_output.data());
3188
0
  }
3189
0
}
3190
  #endif // SIMDUTF_SPAN
3191
3192
/**
3193
 * Convert possibly broken UTF-16BE string into UTF-32 string.
3194
 *
3195
 * During the conversion also validation of the input string is done.
3196
 * This function is suitable to work with inputs from untrusted sources.
3197
 *
3198
 * This function is not BOM-aware.
3199
 *
3200
 * @param input         the UTF-16BE string to convert
3201
 * @param length        the length of the string in 2-byte code units (char16_t)
3202
 * @param utf32_buffer   the pointer to buffer that can hold conversion result
3203
 * @return number of written code units; 0 if input is not a valid UTF-16LE
3204
 * string
3205
 */
3206
simdutf_warn_unused size_t convert_utf16be_to_utf32(
3207
    const char16_t *input, size_t length, char32_t *utf32_buffer) noexcept;
3208
  #if SIMDUTF_SPAN
3209
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3210
convert_utf16be_to_utf32(std::span<const char16_t> utf16_input,
3211
0
                         std::span<char32_t> utf32_output) noexcept {
3212
0
    #if SIMDUTF_CPLUSPLUS23
3213
0
  if consteval {
3214
0
    return scalar::utf16_to_utf32::convert<endianness::BIG>(
3215
0
        utf16_input.data(), utf16_input.size(), utf32_output.data());
3216
0
  } else
3217
0
    #endif
3218
0
  {
3219
0
    return convert_utf16be_to_utf32(utf16_input.data(), utf16_input.size(),
3220
0
                                    utf32_output.data());
3221
0
  }
3222
0
}
3223
  #endif // SIMDUTF_SPAN
3224
3225
/**
3226
 * Using native endianness, convert possibly broken UTF-16 string into
3227
 * UTF-32 string and stop on error.
3228
 *
3229
 * During the conversion also validation of the input string is done.
3230
 * This function is suitable to work with inputs from untrusted sources.
3231
 *
3232
 * This function is not BOM-aware.
3233
 *
3234
 * @param input         the UTF-16 string to convert
3235
 * @param length        the length of the string in 2-byte code units (char16_t)
3236
 * @param utf32_buffer   the pointer to buffer that can hold conversion result
3237
 * @return a result pair struct (of type simdutf::result containing the two
3238
 * fields error and count) with an error code and either position of the error
3239
 * (in the input in code units) if any, or the number of char32_t written if
3240
 * successful.
3241
 */
3242
simdutf_warn_unused result convert_utf16_to_utf32_with_errors(
3243
    const char16_t *input, size_t length, char32_t *utf32_buffer) noexcept;
3244
  #if SIMDUTF_SPAN
3245
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
3246
convert_utf16_to_utf32_with_errors(std::span<const char16_t> utf16_input,
3247
0
                                   std::span<char32_t> utf32_output) noexcept {
3248
0
    #if SIMDUTF_CPLUSPLUS23
3249
0
  if consteval {
3250
0
    return scalar::utf16_to_utf32::convert_with_errors<endianness::NATIVE>(
3251
0
        utf16_input.data(), utf16_input.size(), utf32_output.data());
3252
0
  } else
3253
0
    #endif
3254
0
  {
3255
0
    return convert_utf16_to_utf32_with_errors(
3256
0
        utf16_input.data(), utf16_input.size(), utf32_output.data());
3257
0
  }
3258
0
}
3259
  #endif // SIMDUTF_SPAN
3260
3261
/**
3262
 * Convert possibly broken UTF-16LE string into UTF-32 string and stop on error.
3263
 *
3264
 * During the conversion also validation of the input string is done.
3265
 * This function is suitable to work with inputs from untrusted sources.
3266
 *
3267
 * This function is not BOM-aware.
3268
 *
3269
 * @param input         the UTF-16LE string to convert
3270
 * @param length        the length of the string in 2-byte code units (char16_t)
3271
 * @param utf32_buffer   the pointer to buffer that can hold conversion result
3272
 * @return a result pair struct (of type simdutf::result containing the two
3273
 * fields error and count) with an error code and either position of the error
3274
 * (in the input in code units) if any, or the number of char32_t written if
3275
 * successful.
3276
 */
3277
simdutf_warn_unused result convert_utf16le_to_utf32_with_errors(
3278
    const char16_t *input, size_t length, char32_t *utf32_buffer) noexcept;
3279
  #if SIMDUTF_SPAN
3280
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
3281
convert_utf16le_to_utf32_with_errors(
3282
    std::span<const char16_t> utf16_input,
3283
0
    std::span<char32_t> utf32_output) noexcept {
3284
0
    #if SIMDUTF_CPLUSPLUS23
3285
0
  if consteval {
3286
0
    return scalar::utf16_to_utf32::convert_with_errors<endianness::LITTLE>(
3287
0
        utf16_input.data(), utf16_input.size(), utf32_output.data());
3288
0
  } else
3289
0
    #endif
3290
0
  {
3291
0
    return convert_utf16le_to_utf32_with_errors(
3292
0
        utf16_input.data(), utf16_input.size(), utf32_output.data());
3293
0
  }
3294
0
}
3295
  #endif // SIMDUTF_SPAN
3296
3297
/**
3298
 * Convert possibly broken UTF-16BE string into UTF-32 string and stop on error.
3299
 *
3300
 * During the conversion also validation of the input string is done.
3301
 * This function is suitable to work with inputs from untrusted sources.
3302
 *
3303
 * This function is not BOM-aware.
3304
 *
3305
 * @param input         the UTF-16BE string to convert
3306
 * @param length        the length of the string in 2-byte code units (char16_t)
3307
 * @param utf32_buffer   the pointer to buffer that can hold conversion result
3308
 * @return a result pair struct (of type simdutf::result containing the two
3309
 * fields error and count) with an error code and either position of the error
3310
 * (in the input in code units) if any, or the number of char32_t written if
3311
 * successful.
3312
 */
3313
simdutf_warn_unused result convert_utf16be_to_utf32_with_errors(
3314
    const char16_t *input, size_t length, char32_t *utf32_buffer) noexcept;
3315
  #if SIMDUTF_SPAN
3316
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
3317
convert_utf16be_to_utf32_with_errors(
3318
    std::span<const char16_t> utf16_input,
3319
0
    std::span<char32_t> utf32_output) noexcept {
3320
0
    #if SIMDUTF_CPLUSPLUS23
3321
0
  if consteval {
3322
0
    return scalar::utf16_to_utf32::convert_with_errors<endianness::BIG>(
3323
0
        utf16_input.data(), utf16_input.size(), utf32_output.data());
3324
0
  } else
3325
0
    #endif
3326
0
  {
3327
0
    return convert_utf16be_to_utf32_with_errors(
3328
0
        utf16_input.data(), utf16_input.size(), utf32_output.data());
3329
0
  }
3330
0
}
3331
  #endif // SIMDUTF_SPAN
3332
3333
/**
3334
 * Using native endianness, convert valid UTF-16 string into UTF-32 string.
3335
 *
3336
 * This function assumes that the input string is valid UTF-16 (native
3337
 * endianness).
3338
 *
3339
 * This function is not BOM-aware.
3340
 *
3341
 * @param input         the UTF-16 string to convert
3342
 * @param length        the length of the string in 2-byte code units (char16_t)
3343
 * @param utf32_buffer   the pointer to a buffer that can hold the conversion
3344
 * result
3345
 * @return number of written code units; 0 if conversion is not possible
3346
 */
3347
simdutf_warn_unused size_t convert_valid_utf16_to_utf32(
3348
    const char16_t *input, size_t length, char32_t *utf32_buffer) noexcept;
3349
  #if SIMDUTF_SPAN
3350
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3351
convert_valid_utf16_to_utf32(std::span<const char16_t> valid_utf16_input,
3352
0
                             std::span<char32_t> utf32_output) noexcept {
3353
0
    #if SIMDUTF_CPLUSPLUS23
3354
0
  if consteval {
3355
0
    return scalar::utf16_to_utf32::convert_valid<endianness::NATIVE>(
3356
0
        valid_utf16_input.data(), valid_utf16_input.size(),
3357
0
        utf32_output.data());
3358
0
  } else
3359
0
    #endif
3360
0
  {
3361
0
    return convert_valid_utf16_to_utf32(valid_utf16_input.data(),
3362
0
                                        valid_utf16_input.size(),
3363
0
                                        utf32_output.data());
3364
0
  }
3365
0
}
3366
  #endif // SIMDUTF_SPAN
3367
3368
/**
3369
 * Convert valid UTF-16LE string into UTF-32 string.
3370
 *
3371
 * This function assumes that the input string is valid UTF-16LE.
3372
 *
3373
 * This function is not BOM-aware.
3374
 *
3375
 * @param input         the UTF-16LE string to convert
3376
 * @param length        the length of the string in 2-byte code units (char16_t)
3377
 * @param utf32_buffer   the pointer to a buffer that can hold the conversion
3378
 * result
3379
 * @return number of written code units; 0 if conversion is not possible
3380
 */
3381
simdutf_warn_unused size_t convert_valid_utf16le_to_utf32(
3382
    const char16_t *input, size_t length, char32_t *utf32_buffer) noexcept;
3383
  #if SIMDUTF_SPAN
3384
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3385
convert_valid_utf16le_to_utf32(std::span<const char16_t> valid_utf16_input,
3386
0
                               std::span<char32_t> utf32_output) noexcept {
3387
0
    #if SIMDUTF_CPLUSPLUS23
3388
0
  if consteval {
3389
0
    return scalar::utf16_to_utf32::convert_valid<endianness::LITTLE>(
3390
0
        valid_utf16_input.data(), valid_utf16_input.size(),
3391
0
        utf32_output.data());
3392
0
  } else
3393
0
    #endif
3394
0
  {
3395
0
    return convert_valid_utf16le_to_utf32(valid_utf16_input.data(),
3396
0
                                          valid_utf16_input.size(),
3397
0
                                          utf32_output.data());
3398
0
  }
3399
0
}
3400
  #endif // SIMDUTF_SPAN
3401
3402
/**
3403
 * Convert valid UTF-16BE string into UTF-32 string.
3404
 *
3405
 * This function assumes that the input string is valid UTF-16LE.
3406
 *
3407
 * This function is not BOM-aware.
3408
 *
3409
 * @param input         the UTF-16BE string to convert
3410
 * @param length        the length of the string in 2-byte code units (char16_t)
3411
 * @param utf32_buffer   the pointer to a buffer that can hold the conversion
3412
 * result
3413
 * @return number of written code units; 0 if conversion is not possible
3414
 */
3415
simdutf_warn_unused size_t convert_valid_utf16be_to_utf32(
3416
    const char16_t *input, size_t length, char32_t *utf32_buffer) noexcept;
3417
  #if SIMDUTF_SPAN
3418
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3419
convert_valid_utf16be_to_utf32(std::span<const char16_t> valid_utf16_input,
3420
0
                               std::span<char32_t> utf32_output) noexcept {
3421
0
    #if SIMDUTF_CPLUSPLUS23
3422
0
  if consteval {
3423
0
    return scalar::utf16_to_utf32::convert_valid<endianness::BIG>(
3424
0
        valid_utf16_input.data(), valid_utf16_input.size(),
3425
0
        utf32_output.data());
3426
0
  } else
3427
0
    #endif
3428
0
  {
3429
0
    return convert_valid_utf16be_to_utf32(valid_utf16_input.data(),
3430
0
                                          valid_utf16_input.size(),
3431
0
                                          utf32_output.data());
3432
0
  }
3433
0
}
3434
  #endif // SIMDUTF_SPAN
3435
#endif   // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
3436
3437
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
3438
/**
3439
 * Using native endianness; Compute the number of bytes that this UTF-16
3440
 * string would require in UTF-8 format.
3441
 *
3442
 * This function does not validate the input. It is acceptable to pass invalid
3443
 * UTF-16 strings. The result is then implementation defined, but it remains a
3444
 * safe buffer size: convert_utf16_to_utf8 and convert_utf16_to_utf8_with_errors
3445
 * never write more bytes than this function returns, even when they reject
3446
 * the input.
3447
 *
3448
 * @param input         the UTF-16 string to convert
3449
 * @param length        the length of the string in 2-byte code units (char16_t)
3450
 * @return the number of bytes required to encode the UTF-16LE string as UTF-8
3451
 */
3452
simdutf_warn_unused size_t utf8_length_from_utf16(const char16_t *input,
3453
                                                  size_t length) noexcept;
3454
  #if SIMDUTF_SPAN
3455
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3456
0
utf8_length_from_utf16(std::span<const char16_t> valid_utf16_input) noexcept {
3457
0
    #if SIMDUTF_CPLUSPLUS23
3458
0
  if consteval {
3459
0
    return scalar::utf16::utf8_length_from_utf16<endianness::NATIVE>(
3460
0
        valid_utf16_input.data(), valid_utf16_input.size());
3461
0
  } else
3462
0
    #endif
3463
0
  {
3464
0
    return utf8_length_from_utf16(valid_utf16_input.data(),
3465
0
                                  valid_utf16_input.size());
3466
0
  }
3467
0
}
3468
  #endif // SIMDUTF_SPAN
3469
3470
/**
3471
 * Using native endianness; compute the number of bytes that this UTF-16
3472
 * string would require in UTF-8 format even when the UTF-16LE content contains
3473
 * mismatched surrogates that have to be replaced by the replacement character
3474
 * (0xFFFD).
3475
 *
3476
 * @param input         the UTF-16 string to convert
3477
 * @param length        the length of the string in 2-byte code units (char16_t)
3478
 * @return a result pair struct (of type simdutf::result containing the two
3479
 * fields error and count) where the count is the number of bytes required to
3480
 * encode the UTF-16 string as UTF-8, and the error code is either SUCCESS or
3481
 * SURROGATE. The count is correct regardless of the error field.
3482
 * When SURROGATE is returned, it does not indicate an error in the case of this
3483
 * function: it indicates that at least one surrogate has been encountered: the
3484
 * surrogates may be matched or not (thus this function does not validate). If
3485
 * the returned error code is SUCCESS, then the input contains no surrogate, is
3486
 * in the Basic Multilingual Plane, and is necessarily valid.
3487
 */
3488
simdutf_warn_unused result utf8_length_from_utf16_with_replacement(
3489
    const char16_t *input, size_t length) noexcept;
3490
  #if SIMDUTF_SPAN
3491
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
3492
utf8_length_from_utf16_with_replacement(
3493
0
    std::span<const char16_t> valid_utf16_input) noexcept {
3494
0
    #if SIMDUTF_CPLUSPLUS23
3495
0
  if consteval {
3496
0
    return scalar::utf16::utf8_length_from_utf16_with_replacement<
3497
0
        endianness::NATIVE>(valid_utf16_input.data(), valid_utf16_input.size());
3498
0
  } else
3499
0
    #endif
3500
0
  {
3501
0
    return utf8_length_from_utf16_with_replacement(valid_utf16_input.data(),
3502
0
                                                   valid_utf16_input.size());
3503
0
  }
3504
0
}
3505
  #endif // SIMDUTF_SPAN
3506
3507
/**
3508
 * Compute the number of bytes that this UTF-16LE string would require in UTF-8
3509
 * format.
3510
 *
3511
 * This function does not validate the input. It is acceptable to pass invalid
3512
 * UTF-16 strings. The result is then implementation defined, but it remains a
3513
 * safe buffer size: convert_utf16le_to_utf8 and
3514
 * convert_utf16le_to_utf8_with_errors never write more bytes than this function
3515
 * returns, even when they reject the input.
3516
 *
3517
 * @param input         the UTF-16LE string to convert
3518
 * @param length        the length of the string in 2-byte code units (char16_t)
3519
 * @return the number of bytes required to encode the UTF-16LE string as UTF-8
3520
 */
3521
simdutf_warn_unused size_t utf8_length_from_utf16le(const char16_t *input,
3522
                                                    size_t length) noexcept;
3523
  #if SIMDUTF_SPAN
3524
simdutf_really_inline simdutf_constexpr23 simdutf_warn_unused size_t
3525
0
utf8_length_from_utf16le(std::span<const char16_t> valid_utf16_input) noexcept {
3526
0
    #if SIMDUTF_CPLUSPLUS23
3527
0
  if consteval {
3528
0
    return scalar::utf16::utf8_length_from_utf16<endianness::LITTLE>(
3529
0
        valid_utf16_input.data(), valid_utf16_input.size());
3530
0
  } else
3531
0
    #endif
3532
0
  {
3533
0
    return utf8_length_from_utf16le(valid_utf16_input.data(),
3534
0
                                    valid_utf16_input.size());
3535
0
  }
3536
0
}
3537
  #endif // SIMDUTF_SPAN
3538
3539
/**
3540
 * Compute the number of bytes that this UTF-16BE string would require in UTF-8
3541
 * format.
3542
 *
3543
 * This function does not validate the input. It is acceptable to pass invalid
3544
 * UTF-16 strings. The result is then implementation defined, but it remains a
3545
 * safe buffer size: convert_utf16be_to_utf8 and
3546
 * convert_utf16be_to_utf8_with_errors never write more bytes than this function
3547
 * returns, even when they reject the input.
3548
 *
3549
 * @param input         the UTF-16BE string to convert
3550
 * @param length        the length of the string in 2-byte code units (char16_t)
3551
 * @return the number of bytes required to encode the UTF-16BE string as UTF-8
3552
 */
3553
simdutf_warn_unused size_t utf8_length_from_utf16be(const char16_t *input,
3554
                                                    size_t length) noexcept;
3555
  #if SIMDUTF_SPAN
3556
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3557
0
utf8_length_from_utf16be(std::span<const char16_t> valid_utf16_input) noexcept {
3558
0
    #if SIMDUTF_CPLUSPLUS23
3559
0
  if consteval {
3560
0
    return scalar::utf16::utf8_length_from_utf16<endianness::BIG>(
3561
0
        valid_utf16_input.data(), valid_utf16_input.size());
3562
0
  } else
3563
0
    #endif
3564
0
  {
3565
0
    return utf8_length_from_utf16be(valid_utf16_input.data(),
3566
0
                                    valid_utf16_input.size());
3567
0
  }
3568
0
}
3569
  #endif // SIMDUTF_SPAN
3570
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
3571
3572
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
3573
/**
3574
 * Convert possibly broken UTF-32 string into UTF-8 string.
3575
 *
3576
 * During the conversion also validation of the input string is done.
3577
 * This function is suitable to work with inputs from untrusted sources.
3578
 *
3579
 * This function is not BOM-aware.
3580
 *
3581
 * @param input         the UTF-32 string to convert
3582
 * @param length        the length of the string in 4-byte code units (char32_t)
3583
 * @param utf8_buffer   the pointer to buffer that can hold conversion result
3584
 * @return number of written code units; 0 if input is not a valid UTF-32 string
3585
 */
3586
simdutf_warn_unused size_t convert_utf32_to_utf8(const char32_t *input,
3587
                                                 size_t length,
3588
                                                 char *utf8_buffer) noexcept;
3589
  #if SIMDUTF_SPAN
3590
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3591
convert_utf32_to_utf8(
3592
    std::span<const char32_t> utf32_input,
3593
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
3594
    #if SIMDUTF_CPLUSPLUS23
3595
  if consteval {
3596
    return scalar::utf32_to_utf8::convert(
3597
        utf32_input.data(), utf32_input.size(), utf8_output.data());
3598
  } else
3599
    #endif
3600
  {
3601
    return convert_utf32_to_utf8(utf32_input.data(), utf32_input.size(),
3602
                                 reinterpret_cast<char *>(utf8_output.data()));
3603
  }
3604
}
3605
  #endif // SIMDUTF_SPAN
3606
3607
/**
3608
 * Convert possibly broken UTF-32 string into UTF-8 string and stop on error.
3609
 *
3610
 * During the conversion also validation of the input string is done.
3611
 * This function is suitable to work with inputs from untrusted sources.
3612
 *
3613
 * This function is not BOM-aware.
3614
 *
3615
 * @param input         the UTF-32 string to convert
3616
 * @param length        the length of the string in 4-byte code units (char32_t)
3617
 * @param utf8_buffer   the pointer to buffer that can hold conversion result
3618
 * @return a result pair struct (of type simdutf::result containing the two
3619
 * fields error and count) with an error code and either position of the error
3620
 * (in the input in code units) if any, or the number of char written if
3621
 * successful.
3622
 */
3623
simdutf_warn_unused result convert_utf32_to_utf8_with_errors(
3624
    const char32_t *input, size_t length, char *utf8_buffer) noexcept;
3625
  #if SIMDUTF_SPAN
3626
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
3627
convert_utf32_to_utf8_with_errors(
3628
    std::span<const char32_t> utf32_input,
3629
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
3630
    #if SIMDUTF_CPLUSPLUS23
3631
  if consteval {
3632
    return scalar::utf32_to_utf8::convert_with_errors(
3633
        utf32_input.data(), utf32_input.size(), utf8_output.data());
3634
  } else
3635
    #endif
3636
  {
3637
    return convert_utf32_to_utf8_with_errors(
3638
        utf32_input.data(), utf32_input.size(),
3639
        reinterpret_cast<char *>(utf8_output.data()));
3640
  }
3641
}
3642
  #endif // SIMDUTF_SPAN
3643
3644
/**
3645
 * Convert valid UTF-32 string into UTF-8 string.
3646
 *
3647
 * This function assumes that the input string is valid UTF-32.
3648
 *
3649
 * This function is not BOM-aware.
3650
 *
3651
 * @param input         the UTF-32 string to convert
3652
 * @param length        the length of the string in 4-byte code units (char32_t)
3653
 * @param utf8_buffer   the pointer to a buffer that can hold the conversion
3654
 * result
3655
 * @return number of written code units; 0 if conversion is not possible
3656
 */
3657
simdutf_warn_unused size_t convert_valid_utf32_to_utf8(
3658
    const char32_t *input, size_t length, char *utf8_buffer) noexcept;
3659
  #if SIMDUTF_SPAN
3660
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3661
convert_valid_utf32_to_utf8(
3662
    std::span<const char32_t> valid_utf32_input,
3663
    detail::output_span_of_byte_like auto &&utf8_output) noexcept {
3664
    #if SIMDUTF_CPLUSPLUS23
3665
  if consteval {
3666
    return scalar::utf32_to_utf8::convert_valid(
3667
        valid_utf32_input.data(), valid_utf32_input.size(), utf8_output.data());
3668
  } else
3669
    #endif
3670
  {
3671
    return convert_valid_utf32_to_utf8(
3672
        valid_utf32_input.data(), valid_utf32_input.size(),
3673
        reinterpret_cast<char *>(utf8_output.data()));
3674
  }
3675
}
3676
  #endif // SIMDUTF_SPAN
3677
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
3678
3679
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
3680
/**
3681
 * Using native endianness, convert possibly broken UTF-32 string into a UTF-16
3682
 * string.
3683
 *
3684
 * During the conversion also validation of the input string is done.
3685
 * This function is suitable to work with inputs from untrusted sources.
3686
 *
3687
 * This function is not BOM-aware.
3688
 *
3689
 * @param input         the UTF-32 string to convert
3690
 * @param length        the length of the string in 4-byte code units (char32_t)
3691
 * @param utf16_buffer   the pointer to buffer that can hold conversion result
3692
 * @return number of written code units; 0 if input is not a valid UTF-32 string
3693
 */
3694
simdutf_warn_unused size_t convert_utf32_to_utf16(
3695
    const char32_t *input, size_t length, char16_t *utf16_buffer) noexcept;
3696
  #if SIMDUTF_SPAN
3697
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3698
convert_utf32_to_utf16(std::span<const char32_t> utf32_input,
3699
0
                       std::span<char16_t> utf16_output) noexcept {
3700
0
    #if SIMDUTF_CPLUSPLUS23
3701
0
  if consteval {
3702
0
    return scalar::utf32_to_utf16::convert<endianness::NATIVE>(
3703
0
        utf32_input.data(), utf32_input.size(), utf16_output.data());
3704
0
  } else
3705
0
    #endif
3706
0
  {
3707
0
    return convert_utf32_to_utf16(utf32_input.data(), utf32_input.size(),
3708
0
                                  utf16_output.data());
3709
0
  }
3710
0
}
3711
  #endif // SIMDUTF_SPAN
3712
3713
/**
3714
 * Convert possibly broken UTF-32 string into UTF-16LE string.
3715
 *
3716
 * During the conversion also validation of the input string is done.
3717
 * This function is suitable to work with inputs from untrusted sources.
3718
 *
3719
 * This function is not BOM-aware.
3720
 *
3721
 * @param input         the UTF-32 string to convert
3722
 * @param length        the length of the string in 4-byte code units (char32_t)
3723
 * @param utf16_buffer   the pointer to buffer that can hold conversion result
3724
 * @return number of written code units; 0 if input is not a valid UTF-32 string
3725
 */
3726
simdutf_warn_unused size_t convert_utf32_to_utf16le(
3727
    const char32_t *input, size_t length, char16_t *utf16_buffer) noexcept;
3728
  #if SIMDUTF_SPAN
3729
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3730
convert_utf32_to_utf16le(std::span<const char32_t> utf32_input,
3731
0
                         std::span<char16_t> utf16_output) noexcept {
3732
0
    #if SIMDUTF_CPLUSPLUS23
3733
0
  if consteval {
3734
0
    return scalar::utf32_to_utf16::convert<endianness::LITTLE>(
3735
0
        utf32_input.data(), utf32_input.size(), utf16_output.data());
3736
0
  } else
3737
0
    #endif
3738
0
  {
3739
0
    return convert_utf32_to_utf16le(utf32_input.data(), utf32_input.size(),
3740
0
                                    utf16_output.data());
3741
0
  }
3742
0
}
3743
  #endif // SIMDUTF_SPAN
3744
#endif   // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
3745
3746
#if SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
3747
/**
3748
 * Convert possibly broken UTF-32 string into Latin1 string.
3749
 *
3750
 * During the conversion also validation of the input string is done.
3751
 * This function is suitable to work with inputs from untrusted sources.
3752
 *
3753
 * This function is not BOM-aware.
3754
 *
3755
 * @param input         the UTF-32 string to convert
3756
 * @param length        the length of the string in 4-byte code units (char32_t)
3757
 * @param latin1_buffer   the pointer to buffer that can hold conversion result
3758
 * @return number of written code units; 0 if input is not a valid UTF-32 string
3759
 * or if it cannot be represented as Latin1
3760
 */
3761
simdutf_warn_unused size_t convert_utf32_to_latin1(
3762
    const char32_t *input, size_t length, char *latin1_buffer) noexcept;
3763
  #if SIMDUTF_SPAN
3764
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3765
convert_utf32_to_latin1(
3766
    std::span<const char32_t> utf32_input,
3767
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
3768
    #if SIMDUTF_CPLUSPLUS23
3769
  if consteval {
3770
    return scalar::utf32_to_latin1::convert(
3771
        utf32_input.data(), utf32_input.size(), latin1_output.data());
3772
  } else
3773
    #endif
3774
  {
3775
    return convert_utf32_to_latin1(
3776
        utf32_input.data(), utf32_input.size(),
3777
        reinterpret_cast<char *>(latin1_output.data()));
3778
  }
3779
}
3780
  #endif // SIMDUTF_SPAN
3781
3782
/**
3783
 * Convert possibly broken UTF-32 string into Latin1 string and stop on error.
3784
 * If the string cannot be represented as Latin1, an error is returned.
3785
 *
3786
 * During the conversion also validation of the input string is done.
3787
 * This function is suitable to work with inputs from untrusted sources.
3788
 *
3789
 * This function is not BOM-aware.
3790
 *
3791
 * @param input         the UTF-32 string to convert
3792
 * @param length        the length of the string in 4-byte code units (char32_t)
3793
 * @param latin1_buffer   the pointer to buffer that can hold conversion result
3794
 * @return a result pair struct (of type simdutf::result containing the two
3795
 * fields error and count) with an error code and either position of the error
3796
 * (in the input in code units) if any, or the number of char written if
3797
 * successful.
3798
 */
3799
simdutf_warn_unused result convert_utf32_to_latin1_with_errors(
3800
    const char32_t *input, size_t length, char *latin1_buffer) noexcept;
3801
  #if SIMDUTF_SPAN
3802
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
3803
convert_utf32_to_latin1_with_errors(
3804
    std::span<const char32_t> utf32_input,
3805
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
3806
    #if SIMDUTF_CPLUSPLUS23
3807
  if consteval {
3808
    return scalar::utf32_to_latin1::convert_with_errors(
3809
        utf32_input.data(), utf32_input.size(), latin1_output.data());
3810
  } else
3811
    #endif
3812
  {
3813
    return convert_utf32_to_latin1_with_errors(
3814
        utf32_input.data(), utf32_input.size(),
3815
        reinterpret_cast<char *>(latin1_output.data()));
3816
  }
3817
}
3818
  #endif // SIMDUTF_SPAN
3819
3820
/**
3821
 * Convert valid UTF-32 string into Latin1 string.
3822
 *
3823
 * This function assumes that the input string is valid UTF-32 and that it can
3824
 * be represented as Latin1. If you violate this assumption, the result is
3825
 * implementation defined and may include system-dependent behavior such as
3826
 * crashes.
3827
 *
3828
 * This function is for expert users only and not part of our public API. Use
3829
 * convert_utf32_to_latin1 instead. The function may be removed from the library
3830
 * in the future.
3831
 *
3832
 * This function is not BOM-aware.
3833
 *
3834
 * @param input         the UTF-32 string to convert
3835
 * @param length        the length of the string in 4-byte code units (char32_t)
3836
 * @param latin1_buffer   the pointer to a buffer that can hold the conversion
3837
 * result
3838
 * @return number of written code units; 0 if conversion is not possible
3839
 */
3840
simdutf_warn_unused size_t convert_valid_utf32_to_latin1(
3841
    const char32_t *input, size_t length, char *latin1_buffer) noexcept;
3842
  #if SIMDUTF_SPAN
3843
simdutf_really_inline simdutf_constexpr23 simdutf_warn_unused size_t
3844
convert_valid_utf32_to_latin1(
3845
    std::span<const char32_t> valid_utf32_input,
3846
    detail::output_span_of_byte_like auto &&latin1_output) noexcept {
3847
    #if SIMDUTF_CPLUSPLUS23
3848
  if consteval {
3849
    return scalar::utf32_to_latin1::convert_valid(
3850
        detail::constexpr_cast_ptr<uint32_t>(valid_utf32_input.data()),
3851
        valid_utf32_input.size(),
3852
        detail::constexpr_cast_writeptr<char>(latin1_output.data()));
3853
  }
3854
    #endif
3855
  {
3856
    return convert_valid_utf32_to_latin1(
3857
        valid_utf32_input.data(), valid_utf32_input.size(),
3858
        reinterpret_cast<char *>(latin1_output.data()));
3859
  }
3860
}
3861
  #endif // SIMDUTF_SPAN
3862
3863
/**
3864
 * Compute the number of bytes that this UTF-32 string would require in Latin1
3865
 * format.
3866
 *
3867
 * This function does not validate the input. It is acceptable to pass invalid
3868
 * UTF-32 strings but in such cases the result is implementation defined.
3869
 *
3870
 * This function is not BOM-aware.
3871
 *
3872
 * @param length        the length of the string in 4-byte code units (char32_t)
3873
 * @return the number of bytes required to encode the UTF-32 string as Latin1
3874
 */
3875
simdutf_warn_unused simdutf_really_inline simdutf_constexpr23 size_t
3876
latin1_length_from_utf32(size_t length) noexcept {
3877
  return length;
3878
}
3879
3880
/**
3881
 * Compute the number of bytes that this Latin1 string would require in UTF-32
3882
 * format.
3883
 *
3884
 * @param length        the length of the string in Latin1 code units (char)
3885
 * @return the length of the string in 4-byte code units (char32_t) required to
3886
 * encode the Latin1 string as UTF-32
3887
 */
3888
simdutf_warn_unused simdutf_really_inline simdutf_constexpr23 size_t
3889
0
utf32_length_from_latin1(size_t length) noexcept {
3890
0
  return length;
3891
0
}
3892
#endif // SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
3893
3894
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
3895
/**
3896
 * Convert possibly broken UTF-32 string into UTF-16BE string.
3897
 *
3898
 * During the conversion also validation of the input string is done.
3899
 * This function is suitable to work with inputs from untrusted sources.
3900
 *
3901
 * This function is not BOM-aware.
3902
 *
3903
 * @param input         the UTF-32 string to convert
3904
 * @param length        the length of the string in 4-byte code units (char32_t)
3905
 * @param utf16_buffer   the pointer to buffer that can hold conversion result
3906
 * @return number of written code units; 0 if input is not a valid UTF-32 string
3907
 */
3908
simdutf_warn_unused size_t convert_utf32_to_utf16be(
3909
    const char32_t *input, size_t length, char16_t *utf16_buffer) noexcept;
3910
  #if SIMDUTF_SPAN
3911
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
3912
convert_utf32_to_utf16be(std::span<const char32_t> utf32_input,
3913
0
                         std::span<char16_t> utf16_output) noexcept {
3914
0
    #if SIMDUTF_CPLUSPLUS23
3915
0
  if consteval {
3916
0
    return scalar::utf32_to_utf16::convert<endianness::BIG>(
3917
0
        utf32_input.data(), utf32_input.size(), utf16_output.data());
3918
0
  } else
3919
0
    #endif
3920
0
  {
3921
0
    return convert_utf32_to_utf16be(utf32_input.data(), utf32_input.size(),
3922
0
                                    utf16_output.data());
3923
0
  }
3924
0
}
3925
  #endif // SIMDUTF_SPAN
3926
3927
/**
3928
 * Using native endianness, convert possibly broken UTF-32 string into UTF-16
3929
 * string and stop on error.
3930
 *
3931
 * During the conversion also validation of the input string is done.
3932
 * This function is suitable to work with inputs from untrusted sources.
3933
 *
3934
 * This function is not BOM-aware.
3935
 *
3936
 * @param input         the UTF-32 string to convert
3937
 * @param length        the length of the string in 4-byte code units (char32_t)
3938
 * @param utf16_buffer   the pointer to buffer that can hold conversion result
3939
 * @return a result pair struct (of type simdutf::result containing the two
3940
 * fields error and count) with an error code and either position of the error
3941
 * (in the input in code units) if any, or the number of char16_t written if
3942
 * successful.
3943
 */
3944
simdutf_warn_unused result convert_utf32_to_utf16_with_errors(
3945
    const char32_t *input, size_t length, char16_t *utf16_buffer) noexcept;
3946
  #if SIMDUTF_SPAN
3947
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
3948
convert_utf32_to_utf16_with_errors(std::span<const char32_t> utf32_input,
3949
0
                                   std::span<char16_t> utf16_output) noexcept {
3950
0
    #if SIMDUTF_CPLUSPLUS23
3951
0
  if consteval {
3952
0
    return scalar::utf32_to_utf16::convert_with_errors<endianness::NATIVE>(
3953
0
        utf32_input.data(), utf32_input.size(), utf16_output.data());
3954
0
  } else
3955
0
    #endif
3956
0
  {
3957
0
    return convert_utf32_to_utf16_with_errors(
3958
0
        utf32_input.data(), utf32_input.size(), utf16_output.data());
3959
0
  }
3960
0
}
3961
  #endif // SIMDUTF_SPAN
3962
3963
/**
3964
 * Convert possibly broken UTF-32 string into UTF-16LE string and stop on error.
3965
 *
3966
 * During the conversion also validation of the input string is done.
3967
 * This function is suitable to work with inputs from untrusted sources.
3968
 *
3969
 * This function is not BOM-aware.
3970
 *
3971
 * @param input         the UTF-32 string to convert
3972
 * @param length        the length of the string in 4-byte code units (char32_t)
3973
 * @param utf16_buffer   the pointer to buffer that can hold conversion result
3974
 * @return a result pair struct (of type simdutf::result containing the two
3975
 * fields error and count) with an error code and either position of the error
3976
 * (in the input in code units) if any, or the number of char16_t written if
3977
 * successful.
3978
 */
3979
simdutf_warn_unused result convert_utf32_to_utf16le_with_errors(
3980
    const char32_t *input, size_t length, char16_t *utf16_buffer) noexcept;
3981
  #if SIMDUTF_SPAN
3982
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
3983
convert_utf32_to_utf16le_with_errors(
3984
    std::span<const char32_t> utf32_input,
3985
0
    std::span<char16_t> utf16_output) noexcept {
3986
0
    #if SIMDUTF_CPLUSPLUS23
3987
0
  if consteval {
3988
0
    return scalar::utf32_to_utf16::convert_with_errors<endianness::LITTLE>(
3989
0
        utf32_input.data(), utf32_input.size(), utf16_output.data());
3990
0
  } else
3991
0
    #endif
3992
0
  {
3993
0
    return convert_utf32_to_utf16le_with_errors(
3994
0
        utf32_input.data(), utf32_input.size(), utf16_output.data());
3995
0
  }
3996
0
}
3997
  #endif // SIMDUTF_SPAN
3998
3999
/**
4000
 * Convert possibly broken UTF-32 string into UTF-16BE string and stop on error.
4001
 *
4002
 * During the conversion also validation of the input string is done.
4003
 * This function is suitable to work with inputs from untrusted sources.
4004
 *
4005
 * This function is not BOM-aware.
4006
 *
4007
 * @param input         the UTF-32 string to convert
4008
 * @param length        the length of the string in 4-byte code units (char32_t)
4009
 * @param utf16_buffer   the pointer to buffer that can hold conversion result
4010
 * @return a result pair struct (of type simdutf::result containing the two
4011
 * fields error and count) with an error code and either position of the error
4012
 * (in the input in code units) if any, or the number of char16_t written if
4013
 * successful.
4014
 */
4015
simdutf_warn_unused result convert_utf32_to_utf16be_with_errors(
4016
    const char32_t *input, size_t length, char16_t *utf16_buffer) noexcept;
4017
  #if SIMDUTF_SPAN
4018
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
4019
convert_utf32_to_utf16be_with_errors(
4020
    std::span<const char32_t> utf32_input,
4021
0
    std::span<char16_t> utf16_output) noexcept {
4022
0
    #if SIMDUTF_CPLUSPLUS23
4023
0
  if consteval {
4024
0
    return scalar::utf32_to_utf16::convert_with_errors<endianness::BIG>(
4025
0
        utf32_input.data(), utf32_input.size(), utf16_output.data());
4026
0
  } else
4027
0
    #endif
4028
0
  {
4029
0
    return convert_utf32_to_utf16be_with_errors(
4030
0
        utf32_input.data(), utf32_input.size(), utf16_output.data());
4031
0
  }
4032
0
}
4033
  #endif // SIMDUTF_SPAN
4034
4035
/**
4036
 * Using native endianness, convert valid UTF-32 string into a UTF-16 string.
4037
 *
4038
 * This function assumes that the input string is valid UTF-32.
4039
 *
4040
 * This function is not BOM-aware.
4041
 *
4042
 * @param input         the UTF-32 string to convert
4043
 * @param length        the length of the string in 4-byte code units (char32_t)
4044
 * @param utf16_buffer   the pointer to a buffer that can hold the conversion
4045
 * result
4046
 * @return number of written code units; 0 if conversion is not possible
4047
 */
4048
simdutf_warn_unused size_t convert_valid_utf32_to_utf16(
4049
    const char32_t *input, size_t length, char16_t *utf16_buffer) noexcept;
4050
  #if SIMDUTF_SPAN
4051
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4052
convert_valid_utf32_to_utf16(std::span<const char32_t> valid_utf32_input,
4053
0
                             std::span<char16_t> utf16_output) noexcept {
4054
0
4055
0
    #if SIMDUTF_CPLUSPLUS23
4056
0
  if consteval {
4057
0
    return scalar::utf32_to_utf16::convert_valid<endianness::NATIVE>(
4058
0
        valid_utf32_input.data(), valid_utf32_input.size(),
4059
0
        utf16_output.data());
4060
0
  } else
4061
0
    #endif
4062
0
  {
4063
0
    return convert_valid_utf32_to_utf16(valid_utf32_input.data(),
4064
0
                                        valid_utf32_input.size(),
4065
0
                                        utf16_output.data());
4066
0
  }
4067
0
}
4068
  #endif // SIMDUTF_SPAN
4069
4070
/**
4071
 * Convert valid UTF-32 string into UTF-16LE string.
4072
 *
4073
 * This function assumes that the input string is valid UTF-32.
4074
 *
4075
 * This function is not BOM-aware.
4076
 *
4077
 * @param input         the UTF-32 string to convert
4078
 * @param length        the length of the string in 4-byte code units (char32_t)
4079
 * @param utf16_buffer   the pointer to a buffer that can hold the conversion
4080
 * result
4081
 * @return number of written code units; 0 if conversion is not possible
4082
 */
4083
simdutf_warn_unused size_t convert_valid_utf32_to_utf16le(
4084
    const char32_t *input, size_t length, char16_t *utf16_buffer) noexcept;
4085
  #if SIMDUTF_SPAN
4086
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4087
convert_valid_utf32_to_utf16le(std::span<const char32_t> valid_utf32_input,
4088
0
                               std::span<char16_t> utf16_output) noexcept {
4089
0
    #if SIMDUTF_CPLUSPLUS23
4090
0
  if consteval {
4091
0
    return scalar::utf32_to_utf16::convert_valid<endianness::LITTLE>(
4092
0
        valid_utf32_input.data(), valid_utf32_input.size(),
4093
0
        utf16_output.data());
4094
0
  } else
4095
0
    #endif
4096
0
  {
4097
0
    return convert_valid_utf32_to_utf16le(valid_utf32_input.data(),
4098
0
                                          valid_utf32_input.size(),
4099
0
                                          utf16_output.data());
4100
0
  }
4101
0
}
4102
  #endif // SIMDUTF_SPAN
4103
4104
/**
4105
 * Convert valid UTF-32 string into UTF-16BE string.
4106
 *
4107
 * This function assumes that the input string is valid UTF-32.
4108
 *
4109
 * This function is not BOM-aware.
4110
 *
4111
 * @param input         the UTF-32 string to convert
4112
 * @param length        the length of the string in 4-byte code units (char32_t)
4113
 * @param utf16_buffer   the pointer to a buffer that can hold the conversion
4114
 * result
4115
 * @return number of written code units; 0 if conversion is not possible
4116
 */
4117
simdutf_warn_unused size_t convert_valid_utf32_to_utf16be(
4118
    const char32_t *input, size_t length, char16_t *utf16_buffer) noexcept;
4119
  #if SIMDUTF_SPAN
4120
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4121
convert_valid_utf32_to_utf16be(std::span<const char32_t> valid_utf32_input,
4122
0
                               std::span<char16_t> utf16_output) noexcept {
4123
0
    #if SIMDUTF_CPLUSPLUS23
4124
0
  if consteval {
4125
0
    return scalar::utf32_to_utf16::convert_valid<endianness::BIG>(
4126
0
        valid_utf32_input.data(), valid_utf32_input.size(),
4127
0
        utf16_output.data());
4128
0
  } else
4129
0
    #endif
4130
0
  {
4131
0
    return convert_valid_utf32_to_utf16be(valid_utf32_input.data(),
4132
0
                                          valid_utf32_input.size(),
4133
0
                                          utf16_output.data());
4134
0
  }
4135
0
}
4136
  #endif // SIMDUTF_SPAN
4137
#endif   // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
4138
4139
#if SIMDUTF_FEATURE_UTF16
4140
/**
4141
 * Change the endianness of the input. Can be used to go from UTF-16LE to
4142
 * UTF-16BE or from UTF-16BE to UTF-16LE.
4143
 *
4144
 * This function does not validate the input.
4145
 *
4146
 * This function is not BOM-aware.
4147
 *
4148
 * @param input         the UTF-16 string to process
4149
 * @param length        the length of the string in 2-byte code units (char16_t)
4150
 * @param output        the pointer to a buffer that can hold the conversion
4151
 * result
4152
 */
4153
void change_endianness_utf16(const char16_t *input, size_t length,
4154
                             char16_t *output) noexcept;
4155
  #if SIMDUTF_SPAN
4156
simdutf_really_inline simdutf_constexpr23 void
4157
change_endianness_utf16(std::span<const char16_t> utf16_input,
4158
0
                        std::span<char16_t> utf16_output) noexcept {
4159
0
    #if SIMDUTF_CPLUSPLUS23
4160
0
  if consteval {
4161
0
    return scalar::utf16::change_endianness_utf16(
4162
0
        utf16_input.data(), utf16_input.size(), utf16_output.data());
4163
0
  } else
4164
0
    #endif
4165
0
  {
4166
0
    return change_endianness_utf16(utf16_input.data(), utf16_input.size(),
4167
0
                                   utf16_output.data());
4168
0
  }
4169
0
}
4170
  #endif // SIMDUTF_SPAN
4171
#endif   // SIMDUTF_FEATURE_UTF16
4172
4173
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
4174
/**
4175
 * Compute the number of bytes that this UTF-32 string would require in UTF-8
4176
 * format.
4177
 *
4178
 * This function does not validate the input. It is acceptable to pass invalid
4179
 * UTF-32 strings but in such cases the result is implementation defined.
4180
 *
4181
 * @param input         the UTF-32 string to convert
4182
 * @param length        the length of the string in 4-byte code units (char32_t)
4183
 * @return the number of bytes required to encode the UTF-32 string as UTF-8
4184
 */
4185
simdutf_warn_unused size_t utf8_length_from_utf32(const char32_t *input,
4186
                                                  size_t length) noexcept;
4187
  #if SIMDUTF_SPAN
4188
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4189
0
utf8_length_from_utf32(std::span<const char32_t> valid_utf32_input) noexcept {
4190
0
    #if SIMDUTF_CPLUSPLUS23
4191
0
  if consteval {
4192
0
    return scalar::utf32::utf8_length_from_utf32(valid_utf32_input.data(),
4193
0
                                                 valid_utf32_input.size());
4194
0
  } else
4195
0
    #endif
4196
0
  {
4197
0
    return utf8_length_from_utf32(valid_utf32_input.data(),
4198
0
                                  valid_utf32_input.size());
4199
0
  }
4200
0
}
4201
  #endif // SIMDUTF_SPAN
4202
#endif   // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
4203
4204
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
4205
/**
4206
 * Compute the number of two-byte code units that this UTF-32 string would
4207
 * require in UTF-16 format.
4208
 *
4209
 * This function does not validate the input. It is acceptable to pass invalid
4210
 * UTF-32 strings but in such cases the result is implementation defined.
4211
 *
4212
 * @param input         the UTF-32 string to convert
4213
 * @param length        the length of the string in 4-byte code units (char32_t)
4214
 * @return the number of bytes required to encode the UTF-32 string as UTF-16
4215
 */
4216
simdutf_warn_unused size_t utf16_length_from_utf32(const char32_t *input,
4217
                                                   size_t length) noexcept;
4218
  #if SIMDUTF_SPAN
4219
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4220
0
utf16_length_from_utf32(std::span<const char32_t> valid_utf32_input) noexcept {
4221
0
    #if SIMDUTF_CPLUSPLUS23
4222
0
  if consteval {
4223
0
    return scalar::utf32::utf16_length_from_utf32(valid_utf32_input.data(),
4224
0
                                                  valid_utf32_input.size());
4225
0
  } else
4226
0
    #endif
4227
0
  {
4228
0
    return utf16_length_from_utf32(valid_utf32_input.data(),
4229
0
                                   valid_utf32_input.size());
4230
0
  }
4231
0
}
4232
  #endif // SIMDUTF_SPAN
4233
4234
/**
4235
 * Using native endianness; Compute the number of bytes that this UTF-16
4236
 * string would require in UTF-32 format.
4237
 *
4238
 * This function is equivalent to count_utf16.
4239
 *
4240
 * This function does not validate the input. It is acceptable to pass invalid
4241
 * UTF-16 strings but in such cases the result is implementation defined.
4242
 *
4243
 * This function is not BOM-aware.
4244
 *
4245
 * @param input         the UTF-16 string to convert
4246
 * @param length        the length of the string in 2-byte code units (char16_t)
4247
 * @return the number of bytes required to encode the UTF-16LE string as UTF-32
4248
 */
4249
simdutf_warn_unused size_t utf32_length_from_utf16(const char16_t *input,
4250
                                                   size_t length) noexcept;
4251
  #if SIMDUTF_SPAN
4252
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4253
0
utf32_length_from_utf16(std::span<const char16_t> valid_utf16_input) noexcept {
4254
0
    #if SIMDUTF_CPLUSPLUS23
4255
0
  if consteval {
4256
0
    return scalar::utf16::utf32_length_from_utf16<endianness::NATIVE>(
4257
0
        valid_utf16_input.data(), valid_utf16_input.size());
4258
0
  } else
4259
0
    #endif
4260
0
  {
4261
0
    return utf32_length_from_utf16(valid_utf16_input.data(),
4262
0
                                   valid_utf16_input.size());
4263
0
  }
4264
0
}
4265
  #endif // SIMDUTF_SPAN
4266
4267
/**
4268
 * Compute the number of bytes that this UTF-16LE string would require in UTF-32
4269
 * format.
4270
 *
4271
 * This function is equivalent to count_utf16le.
4272
 *
4273
 * This function does not validate the input. It is acceptable to pass invalid
4274
 * UTF-16 strings but in such cases the result is implementation defined.
4275
 *
4276
 * This function is not BOM-aware.
4277
 *
4278
 * @param input         the UTF-16LE string to convert
4279
 * @param length        the length of the string in 2-byte code units (char16_t)
4280
 * @return the number of bytes required to encode the UTF-16LE string as UTF-32
4281
 */
4282
simdutf_warn_unused size_t utf32_length_from_utf16le(const char16_t *input,
4283
                                                     size_t length) noexcept;
4284
  #if SIMDUTF_SPAN
4285
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4286
utf32_length_from_utf16le(
4287
0
    std::span<const char16_t> valid_utf16_input) noexcept {
4288
0
    #if SIMDUTF_CPLUSPLUS23
4289
0
  if consteval {
4290
0
    return scalar::utf16::utf32_length_from_utf16<endianness::LITTLE>(
4291
0
        valid_utf16_input.data(), valid_utf16_input.size());
4292
0
  } else
4293
0
    #endif
4294
0
  {
4295
0
    return utf32_length_from_utf16le(valid_utf16_input.data(),
4296
0
                                     valid_utf16_input.size());
4297
0
  }
4298
0
}
4299
  #endif // SIMDUTF_SPAN
4300
4301
/**
4302
 * Compute the number of bytes that this UTF-16BE string would require in UTF-32
4303
 * format.
4304
 *
4305
 * This function is equivalent to count_utf16be.
4306
 *
4307
 * This function does not validate the input. It is acceptable to pass invalid
4308
 * UTF-16 strings but in such cases the result is implementation defined.
4309
 *
4310
 * This function is not BOM-aware.
4311
 *
4312
 * @param input         the UTF-16BE string to convert
4313
 * @param length        the length of the string in 2-byte code units (char16_t)
4314
 * @return the number of bytes required to encode the UTF-16BE string as UTF-32
4315
 */
4316
simdutf_warn_unused size_t utf32_length_from_utf16be(const char16_t *input,
4317
                                                     size_t length) noexcept;
4318
  #if SIMDUTF_SPAN
4319
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4320
utf32_length_from_utf16be(
4321
0
    std::span<const char16_t> valid_utf16_input) noexcept {
4322
0
    #if SIMDUTF_CPLUSPLUS23
4323
0
  if consteval {
4324
0
    return scalar::utf16::utf32_length_from_utf16<endianness::BIG>(
4325
0
        valid_utf16_input.data(), valid_utf16_input.size());
4326
0
  } else
4327
0
    #endif
4328
0
  {
4329
0
    return utf32_length_from_utf16be(valid_utf16_input.data(),
4330
0
                                     valid_utf16_input.size());
4331
0
  }
4332
0
}
4333
  #endif // SIMDUTF_SPAN
4334
#endif   // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
4335
4336
#if SIMDUTF_FEATURE_UTF16
4337
/**
4338
 * Count the number of code points (characters) in the string assuming that
4339
 * it is valid.
4340
 *
4341
 * This function assumes that the input string is valid UTF-16 (native
4342
 * endianness). It is acceptable to pass invalid UTF-16 strings but in such
4343
 * cases the result is implementation defined.
4344
 *
4345
 * This function is not BOM-aware.
4346
 *
4347
 * @param input         the UTF-16 string to process
4348
 * @param length        the length of the string in 2-byte code units (char16_t)
4349
 * @return number of code points
4350
 */
4351
simdutf_warn_unused size_t count_utf16(const char16_t *input,
4352
                                       size_t length) noexcept;
4353
  #if SIMDUTF_SPAN
4354
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4355
0
count_utf16(std::span<const char16_t> valid_utf16_input) noexcept {
4356
0
    #if SIMDUTF_CPLUSPLUS23
4357
0
  if consteval {
4358
0
    return scalar::utf16::count_code_points<endianness::NATIVE>(
4359
0
        valid_utf16_input.data(), valid_utf16_input.size());
4360
0
  } else
4361
0
    #endif
4362
0
  {
4363
0
    return count_utf16(valid_utf16_input.data(), valid_utf16_input.size());
4364
0
  }
4365
0
}
4366
  #endif // SIMDUTF_SPAN
4367
4368
/**
4369
 * Count the number of code points (characters) in the string assuming that
4370
 * it is valid.
4371
 *
4372
 * This function assumes that the input string is valid UTF-16LE.
4373
 * It is acceptable to pass invalid UTF-16 strings but in such cases
4374
 * the result is implementation defined.
4375
 *
4376
 * This function is not BOM-aware.
4377
 *
4378
 * @param input         the UTF-16LE string to process
4379
 * @param length        the length of the string in 2-byte code units (char16_t)
4380
 * @return number of code points
4381
 */
4382
simdutf_warn_unused size_t count_utf16le(const char16_t *input,
4383
                                         size_t length) noexcept;
4384
  #if SIMDUTF_SPAN
4385
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4386
0
count_utf16le(std::span<const char16_t> valid_utf16_input) noexcept {
4387
0
    #if SIMDUTF_CPLUSPLUS23
4388
0
  if consteval {
4389
0
    return scalar::utf16::count_code_points<endianness::LITTLE>(
4390
0
        valid_utf16_input.data(), valid_utf16_input.size());
4391
0
  } else
4392
0
    #endif
4393
0
  {
4394
0
    return count_utf16le(valid_utf16_input.data(), valid_utf16_input.size());
4395
0
  }
4396
0
}
4397
  #endif // SIMDUTF_SPAN
4398
4399
/**
4400
 * Count the number of code points (characters) in the string assuming that
4401
 * it is valid.
4402
 *
4403
 * This function assumes that the input string is valid UTF-16BE.
4404
 * It is acceptable to pass invalid UTF-16 strings but in such cases
4405
 * the result is implementation defined.
4406
 *
4407
 * This function is not BOM-aware.
4408
 *
4409
 * @param input         the UTF-16BE string to process
4410
 * @param length        the length of the string in 2-byte code units (char16_t)
4411
 * @return number of code points
4412
 */
4413
simdutf_warn_unused size_t count_utf16be(const char16_t *input,
4414
                                         size_t length) noexcept;
4415
  #if SIMDUTF_SPAN
4416
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4417
0
count_utf16be(std::span<const char16_t> valid_utf16_input) noexcept {
4418
0
    #if SIMDUTF_CPLUSPLUS23
4419
0
  if consteval {
4420
0
    return scalar::utf16::count_code_points<endianness::BIG>(
4421
0
        valid_utf16_input.data(), valid_utf16_input.size());
4422
0
  } else
4423
0
    #endif
4424
0
  {
4425
0
    return count_utf16be(valid_utf16_input.data(), valid_utf16_input.size());
4426
0
  }
4427
0
}
4428
  #endif // SIMDUTF_SPAN
4429
#endif   // SIMDUTF_FEATURE_UTF16
4430
4431
#if SIMDUTF_FEATURE_UTF8
4432
/**
4433
 * Count the number of code points (characters) in the string assuming that
4434
 * it is valid.
4435
 *
4436
 * This function assumes that the input string is valid UTF-8.
4437
 * It is acceptable to pass invalid UTF-8 strings but in such cases
4438
 * the result is implementation defined.
4439
 *
4440
 * @param input         the UTF-8 string to process
4441
 * @param length        the length of the string in bytes
4442
 * @return number of code points
4443
 */
4444
simdutf_warn_unused size_t count_utf8(const char *input,
4445
                                      size_t length) noexcept;
4446
  #if SIMDUTF_SPAN
4447
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t count_utf8(
4448
    const detail::input_span_of_byte_like auto &valid_utf8_input) noexcept {
4449
    #if SIMDUTF_CPLUSPLUS23
4450
  if consteval {
4451
    return scalar::utf8::count_code_points(valid_utf8_input.data(),
4452
                                           valid_utf8_input.size());
4453
  } else
4454
    #endif
4455
  {
4456
    return count_utf8(reinterpret_cast<const char *>(valid_utf8_input.data()),
4457
                      valid_utf8_input.size());
4458
  }
4459
}
4460
  #endif // SIMDUTF_SPAN
4461
4462
/**
4463
 * Given a valid UTF-8 string having a possibly truncated last character,
4464
 * this function checks the end of string. If the last character is truncated
4465
 * (or partial), then it returns a shorter length (shorter by 1 to 3 bytes) so
4466
 * that the short UTF-8 strings only contain complete characters. If there is no
4467
 * truncated character, the original length is returned.
4468
 *
4469
 * This function assumes that the input string is valid UTF-8, but possibly
4470
 * truncated.
4471
 *
4472
 * @param input         the UTF-8 string to process
4473
 * @param length        the length of the string in bytes
4474
 * @return the length of the string in bytes, possibly shorter by 1 to 3 bytes
4475
 */
4476
simdutf_warn_unused size_t trim_partial_utf8(const char *input, size_t length);
4477
  #if SIMDUTF_SPAN
4478
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4479
trim_partial_utf8(
4480
    const detail::input_span_of_byte_like auto &valid_utf8_input) noexcept {
4481
    #if SIMDUTF_CPLUSPLUS23
4482
  if consteval {
4483
    return scalar::utf8::trim_partial_utf8(valid_utf8_input.data(),
4484
                                           valid_utf8_input.size());
4485
  } else
4486
    #endif
4487
  {
4488
    return trim_partial_utf8(
4489
        reinterpret_cast<const char *>(valid_utf8_input.data()),
4490
        valid_utf8_input.size());
4491
  }
4492
}
4493
  #endif // SIMDUTF_SPAN
4494
#endif   // SIMDUTF_FEATURE_UTF8
4495
4496
#if SIMDUTF_FEATURE_UTF16
4497
/**
4498
 * Given a valid UTF-16BE string having a possibly truncated last character,
4499
 * this function checks the end of string. If the last character is truncated
4500
 * (or partial), then it returns a shorter length (shorter by 1 unit) so that
4501
 * the short UTF-16BE strings only contain complete characters. If there is no
4502
 * truncated character, the original length is returned.
4503
 *
4504
 * This function assumes that the input string is valid UTF-16BE, but possibly
4505
 * truncated.
4506
 *
4507
 * @param input         the UTF-16BE string to process
4508
 * @param length        the length of the string in bytes
4509
 * @return the length of the string in bytes, possibly shorter by 1 unit
4510
 */
4511
simdutf_warn_unused size_t trim_partial_utf16be(const char16_t *input,
4512
                                                size_t length);
4513
  #if SIMDUTF_SPAN
4514
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4515
0
trim_partial_utf16be(std::span<const char16_t> valid_utf16_input) noexcept {
4516
0
    #if SIMDUTF_CPLUSPLUS23
4517
0
  if consteval {
4518
0
    return scalar::utf16::trim_partial_utf16<endianness::BIG>(
4519
0
        valid_utf16_input.data(), valid_utf16_input.size());
4520
0
  } else
4521
0
    #endif
4522
0
  {
4523
0
    return trim_partial_utf16be(valid_utf16_input.data(),
4524
0
                                valid_utf16_input.size());
4525
0
  }
4526
0
}
4527
  #endif // SIMDUTF_SPAN
4528
4529
/**
4530
 * Given a valid UTF-16LE string having a possibly truncated last character,
4531
 * this function checks the end of string. If the last character is truncated
4532
 * (or partial), then it returns a shorter length (shorter by 1 unit) so that
4533
 * the short UTF-16LE strings only contain complete characters. If there is no
4534
 * truncated character, the original length is returned.
4535
 *
4536
 * This function assumes that the input string is valid UTF-16LE, but possibly
4537
 * truncated.
4538
 *
4539
 * @param input         the UTF-16LE string to process
4540
 * @param length        the length of the string in bytes
4541
 * @return the length of the string in unit, possibly shorter by 1 unit
4542
 */
4543
simdutf_warn_unused size_t trim_partial_utf16le(const char16_t *input,
4544
                                                size_t length);
4545
  #if SIMDUTF_SPAN
4546
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4547
0
trim_partial_utf16le(std::span<const char16_t> valid_utf16_input) noexcept {
4548
0
    #if SIMDUTF_CPLUSPLUS23
4549
0
  if consteval {
4550
0
    return scalar::utf16::trim_partial_utf16<endianness::LITTLE>(
4551
0
        valid_utf16_input.data(), valid_utf16_input.size());
4552
0
  } else
4553
0
    #endif
4554
0
  {
4555
0
    return trim_partial_utf16le(valid_utf16_input.data(),
4556
0
                                valid_utf16_input.size());
4557
0
  }
4558
0
}
4559
  #endif // SIMDUTF_SPAN
4560
4561
/**
4562
 * Given a valid UTF-16 string having a possibly truncated last character,
4563
 * this function checks the end of string. If the last character is truncated
4564
 * (or partial), then it returns a shorter length (shorter by 1 unit) so that
4565
 * the short UTF-16 strings only contain complete characters. If there is no
4566
 * truncated character, the original length is returned.
4567
 *
4568
 * This function assumes that the input string is valid UTF-16, but possibly
4569
 * truncated. We use the native endianness.
4570
 *
4571
 * @param input         the UTF-16 string to process
4572
 * @param length        the length of the string in bytes
4573
 * @return the length of the string in unit, possibly shorter by 1 unit
4574
 */
4575
simdutf_warn_unused size_t trim_partial_utf16(const char16_t *input,
4576
                                              size_t length);
4577
  #if SIMDUTF_SPAN
4578
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4579
0
trim_partial_utf16(std::span<const char16_t> valid_utf16_input) noexcept {
4580
0
    #if SIMDUTF_CPLUSPLUS23
4581
0
  if consteval {
4582
0
    return scalar::utf16::trim_partial_utf16<endianness::NATIVE>(
4583
0
        valid_utf16_input.data(), valid_utf16_input.size());
4584
0
  } else
4585
0
    #endif
4586
0
  {
4587
0
    return trim_partial_utf16(valid_utf16_input.data(),
4588
0
                              valid_utf16_input.size());
4589
0
  }
4590
0
}
4591
  #endif // SIMDUTF_SPAN
4592
#endif   // SIMDUTF_FEATURE_UTF16
4593
4594
#if SIMDUTF_FEATURE_BASE64 || SIMDUTF_FEATURE_UTF16 ||                         \
4595
    SIMDUTF_FEATURE_DETECT_ENCODING
4596
  #ifndef SIMDUTF_NEED_TRAILING_ZEROES
4597
    #define SIMDUTF_NEED_TRAILING_ZEROES 1
4598
  #endif
4599
#endif // SIMDUTF_FEATURE_BASE64 || SIMDUTF_FEATURE_UTF16 ||
4600
       // SIMDUTF_FEATURE_DETECT_ENCODING
4601
4602
#if SIMDUTF_FEATURE_BASE64
4603
// base64_options are used to specify the base64 encoding options.
4604
// ASCII spaces are ' ', '\t', '\n', '\r', '\f'
4605
// garbage characters are characters that are not part of the base64 alphabet
4606
// nor ASCII spaces.
4607
constexpr uint64_t base64_reverse_padding =
4608
    2; /* modifier for base64_default and base64_url */
4609
enum base64_options : uint64_t {
4610
  base64_default = 0, /* standard base64 format (with padding) */
4611
  base64_url = 1,     /* base64url format (no padding) */
4612
  base64_default_no_padding =
4613
      base64_default |
4614
      base64_reverse_padding, /* standard base64 format without padding */
4615
  base64_url_with_padding =
4616
      base64_url | base64_reverse_padding, /* base64url with padding */
4617
  base64_default_accept_garbage =
4618
      4, /* standard base64 format accepting garbage characters, the input stops
4619
            with the first '=' if any */
4620
  base64_url_accept_garbage =
4621
      5, /* base64url format accepting garbage characters, the input stops with
4622
            the first '=' if any */
4623
  base64_default_or_url =
4624
      8, /* standard/base64url hybrid format (only meaningful for decoding!) */
4625
  base64_default_or_url_accept_garbage =
4626
      12, /* standard/base64url hybrid format accepting garbage characters
4627
             (only meaningful for decoding!), the input stops with the first '='
4628
             if any */
4629
};
4630
4631
// last_chunk_handling_options are used to specify the handling of the last
4632
// chunk in base64 decoding.
4633
// https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64
4634
enum last_chunk_handling_options : uint64_t {
4635
  loose = 0,  /* standard base64 format, decode partial final chunk */
4636
  strict = 1, /* error when the last chunk is partial, 2 or 3 chars, and
4637
                 unpadded, or non-zero bit padding */
4638
  stop_before_partial =
4639
      2, /* if the last chunk is partial, ignore it (no error) */
4640
  only_full_chunks =
4641
      3 /* only decode full blocks (4 base64 characters, no padding) */
4642
};
4643
4644
inline simdutf_constexpr23 bool
4645
is_partial(last_chunk_handling_options options) {
4646
  return (options == stop_before_partial) || (options == only_full_chunks);
4647
}
4648
4649
namespace detail {
4650
simdutf_warn_unused const char *find(const char *start, const char *end,
4651
                                     char character) noexcept;
4652
simdutf_warn_unused const char16_t *
4653
find(const char16_t *start, const char16_t *end, char16_t character) noexcept;
4654
} // namespace detail
4655
4656
/**
4657
 * Find the first occurrence of a character in a string. If the character is
4658
 * not found, return a pointer to the end of the string.
4659
 * @param start        the start of the string
4660
 * @param end          the end of the string
4661
 * @param character    the character to find
4662
 * @return a pointer to the first occurrence of the character in the string,
4663
 * or a pointer to the end of the string if the character is not found.
4664
 *
4665
 */
4666
simdutf_warn_unused simdutf_really_inline simdutf_constexpr23 const char *
4667
find(const char *start, const char *end, char character) noexcept {
4668
  #if SIMDUTF_CPLUSPLUS23
4669
  if consteval {
4670
    for (; start != end; ++start)
4671
      if (*start == character)
4672
        return start;
4673
    return end;
4674
  } else
4675
  #endif
4676
  {
4677
    return detail::find(start, end, character);
4678
  }
4679
}
4680
simdutf_warn_unused simdutf_really_inline simdutf_constexpr23 const char16_t *
4681
find(const char16_t *start, const char16_t *end, char16_t character) noexcept {
4682
    // implementation note: this is repeated instead of a template, to ensure
4683
    // the api is still a function and compiles without concepts
4684
  #if SIMDUTF_CPLUSPLUS23
4685
  if consteval {
4686
    for (; start != end; ++start)
4687
      if (*start == character)
4688
        return start;
4689
    return end;
4690
  } else
4691
  #endif
4692
  {
4693
    return detail::find(start, end, character);
4694
  }
4695
}
4696
}
4697
  // We include base64_tables once.
4698
  #include <simdutf/base64_tables.h>
4699
  #include <simdutf/scalar/base64.h>
4700
4701
namespace simdutf {
4702
4703
0
inline std::string_view to_string(base64_options options) {
4704
0
  switch (options) {
4705
0
  case base64_default:
4706
0
    return "base64_default";
4707
0
  case base64_url:
4708
0
    return "base64_url";
4709
0
  case base64_reverse_padding:
4710
0
    return "base64_reverse_padding";
4711
0
  case base64_url_with_padding:
4712
0
    return "base64_url_with_padding";
4713
0
  case base64_default_accept_garbage:
4714
0
    return "base64_default_accept_garbage";
4715
0
  case base64_url_accept_garbage:
4716
0
    return "base64_url_accept_garbage";
4717
0
  case base64_default_or_url:
4718
0
    return "base64_default_or_url";
4719
0
  case base64_default_or_url_accept_garbage:
4720
0
    return "base64_default_or_url_accept_garbage";
4721
0
  }
4722
0
  return "<unknown>";
4723
0
}
4724
4725
0
inline std::string_view to_string(last_chunk_handling_options options) {
4726
0
  switch (options) {
4727
0
  case loose:
4728
0
    return "loose";
4729
0
  case strict:
4730
0
    return "strict";
4731
0
  case stop_before_partial:
4732
0
    return "stop_before_partial";
4733
0
  case only_full_chunks:
4734
0
    return "only_full_chunks";
4735
0
  }
4736
0
  return "<unknown>";
4737
0
}
4738
4739
/**
4740
 * Provide the maximal binary length in bytes given the base64 input.
4741
 * As long as the input does not contain ignorable characters (e.g., ASCII
4742
 * spaces or linefeed characters), the result is exact. In particular, the
4743
 * function checks for padding characters.
4744
 *
4745
 * The function is fast (constant time). It checks up to two characters at
4746
 * the end of the string. The input is not otherwise validated or read.
4747
 *
4748
 * @param input         the base64 input to process
4749
 * @param length        the length of the base64 input in bytes
4750
 * @return maximum number of binary bytes
4751
 */
4752
simdutf_warn_unused size_t
4753
maximal_binary_length_from_base64(const char *input, size_t length) noexcept;
4754
  #if SIMDUTF_SPAN
4755
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4756
maximal_binary_length_from_base64(
4757
    const detail::input_span_of_byte_like auto &input) noexcept {
4758
    #if SIMDUTF_CPLUSPLUS23
4759
  if consteval {
4760
    return scalar::base64::maximal_binary_length_from_base64(
4761
        detail::constexpr_cast_ptr<uint8_t>(input.data()), input.size());
4762
  } else
4763
    #endif
4764
  {
4765
    return maximal_binary_length_from_base64(
4766
        reinterpret_cast<const char *>(input.data()), input.size());
4767
  }
4768
}
4769
  #endif // SIMDUTF_SPAN
4770
4771
/**
4772
 * Provide the maximal binary length in bytes given the base64 input.
4773
 * As long as the input does not contain ignorable characters (e.g., ASCII
4774
 * spaces or linefeed characters), the result is exact. In particular, the
4775
 * function checks for padding characters.
4776
 *
4777
 * The function is fast (constant time). It checks up to two characters at
4778
 * the end of the string. The input is not otherwise validated or read.
4779
 *
4780
 * @param input         the base64 input to process, in ASCII stored as 16-bit
4781
 * units
4782
 * @param length        the length of the base64 input in 16-bit units
4783
 * @return maximal number of binary bytes
4784
 */
4785
simdutf_warn_unused size_t maximal_binary_length_from_base64(
4786
    const char16_t *input, size_t length) noexcept;
4787
  #if SIMDUTF_SPAN
4788
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4789
0
maximal_binary_length_from_base64(std::span<const char16_t> input) noexcept {
4790
0
    #if SIMDUTF_CPLUSPLUS23
4791
0
  if consteval {
4792
0
    return scalar::base64::maximal_binary_length_from_base64(input.data(),
4793
0
                                                             input.size());
4794
0
  } else
4795
0
    #endif
4796
0
  {
4797
0
    return maximal_binary_length_from_base64(input.data(), input.size());
4798
0
  }
4799
0
}
4800
  #endif // SIMDUTF_SPAN
4801
4802
/**
4803
 * Compute the binary length from a base64 input.
4804
 * This function is useful for base64 inputs that may contain ASCII whitespaces
4805
 * (such as line breaks). For such inputs, the result is exact, and for any
4806
 * inputs the result can be used to size the output buffer passed to
4807
 * `base64_to_binary`.
4808
 *
4809
 * The function ignores whitespace and does not require padding characters
4810
 * ('=').
4811
 *
4812
 * @param input         the base64 input to process
4813
 * @param length        the length of the base64 input in bytes
4814
 * @return number of binary bytes
4815
 */
4816
simdutf_warn_unused size_t binary_length_from_base64(const char *input,
4817
                                                     size_t length) noexcept;
4818
  #if SIMDUTF_SPAN
4819
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4820
binary_length_from_base64(
4821
    const detail::input_span_of_byte_like auto &input) noexcept {
4822
    #if SIMDUTF_CPLUSPLUS23
4823
  if consteval {
4824
    return detail::with_char_input(input, [&](auto in) {
4825
      return scalar::base64::binary_length_from_base64(in, input.size());
4826
    });
4827
  } else
4828
    #endif
4829
  {
4830
    return binary_length_from_base64(
4831
        reinterpret_cast<const char *>(input.data()), input.size());
4832
  }
4833
}
4834
  #endif // SIMDUTF_SPAN
4835
4836
/**
4837
 * Compute the binary length from a base64 input.
4838
 * This function is useful for base64 inputs that may contain ASCII whitespaces
4839
 * (such as line breaks). For such inputs, the result is exact, and for any
4840
 * inputs the result can be used to size the output buffer passed to
4841
 * `base64_to_binary`.
4842
 *
4843
 * The function ignores whitespace and does not require padding characters
4844
 * ('=').
4845
 *
4846
 * @param input         the base64 input to process, in ASCII stored as 16-bit
4847
 * units
4848
 * @param length        the length of the base64 input in 16-bit units
4849
 * @return number of binary bytes
4850
 */
4851
simdutf_warn_unused size_t binary_length_from_base64(const char16_t *input,
4852
                                                     size_t length) noexcept;
4853
  #if SIMDUTF_SPAN
4854
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
4855
0
binary_length_from_base64(std::span<const char16_t> input) noexcept {
4856
0
    #if SIMDUTF_CPLUSPLUS23
4857
0
  if consteval {
4858
0
    return scalar::base64::binary_length_from_base64(input.data(),
4859
0
                                                     input.size());
4860
0
  } else
4861
0
    #endif
4862
0
  {
4863
0
    return binary_length_from_base64(input.data(), input.size());
4864
0
  }
4865
0
}
4866
  #endif // SIMDUTF_SPAN
4867
4868
/**
4869
 * Convert a base64 input to a binary output.
4870
 *
4871
 * This function follows the WHATWG forgiving-base64 format, which means that it
4872
 * will ignore any ASCII spaces in the input. You may provide a padded input
4873
 * (with one or two equal signs at the end) or an unpadded input (without any
4874
 * equal signs at the end).
4875
 *
4876
 * See https://infra.spec.whatwg.org/#forgiving-base64-decode
4877
 *
4878
 * This function will fail in case of invalid input. When last_chunk_options =
4879
 * loose, there are two possible reasons for failure: the input contains a
4880
 * number of base64 characters that when divided by 4, leaves a single remainder
4881
 * character (BASE64_INPUT_REMAINDER), or the input contains a character that is
4882
 * not a valid base64 character (INVALID_BASE64_CHARACTER).
4883
 *
4884
 * When the error is INVALID_BASE64_CHARACTER, r.count contains the index in the
4885
 * input where the invalid character was found. When the error is
4886
 * BASE64_INPUT_REMAINDER, then r.count contains the number of bytes decoded.
4887
 *
4888
 * The default option (simdutf::base64_default) expects the characters `+` and
4889
 * `/` as part of its alphabet. The URL option (simdutf::base64_url) expects the
4890
 * characters `-` and `_` as part of its alphabet.
4891
 *
4892
 * The padding (`=`) is validated if present. There may be at most two padding
4893
 * characters at the end of the input. If there are any padding characters, the
4894
 * total number of characters (excluding spaces but including padding
4895
 * characters) must be divisible by four.
4896
 *
4897
 * You should call this function with a buffer that is at least
4898
 * maximal_binary_length_from_base64(input, length) bytes long. If you fail to
4899
 * provide that much space, the function may cause a buffer overflow.
4900
 *
4901
 * Advanced users may want to tailor how the last chunk is handled. By default,
4902
 * we use a loose (forgiving) approach but we also support a strict approach
4903
 * as well as a stop_before_partial approach, as per the following proposal:
4904
 *
4905
 * https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64
4906
 *
4907
 * @param input         the base64 string to process
4908
 * @param length        the length of the string in bytes
4909
 * @param output        the pointer to a buffer that can hold the conversion
4910
 * result (should be at least maximal_binary_length_from_base64(input, length)
4911
 * bytes long).
4912
 * @param options       the base64 options to use, usually base64_default or
4913
 * base64_url, and base64_default by default.
4914
 * @param last_chunk_options the last chunk handling options,
4915
 * last_chunk_handling_options::loose by default
4916
 * but can also be last_chunk_handling_options::strict or
4917
 * last_chunk_handling_options::stop_before_partial.
4918
 * @return a result pair struct (of type simdutf::result containing the two
4919
 * fields error and count) with an error code and either position of the error
4920
 * (in the input in bytes) if any, or the number of bytes written if successful.
4921
 */
4922
simdutf_warn_unused result base64_to_binary(
4923
    const char *input, size_t length, char *output,
4924
    base64_options options = base64_default,
4925
    last_chunk_handling_options last_chunk_options = loose) noexcept;
4926
  #if SIMDUTF_SPAN
4927
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
4928
base64_to_binary(
4929
    const detail::input_span_of_byte_like auto &input,
4930
    detail::output_span_of_byte_like auto &&binary_output,
4931
    base64_options options = base64_default,
4932
    last_chunk_handling_options last_chunk_options = loose) noexcept {
4933
    #if SIMDUTF_CPLUSPLUS23
4934
  if consteval {
4935
    return detail::with_char_input(input, [&](auto in) {
4936
      return detail::with_char_output(
4937
          binary_output,
4938
          [&](char *out) {
4939
            return scalar::base64::base64_to_binary_details_impl(
4940
                in, input.size(), out, options, last_chunk_options);
4941
          },
4942
          [](const full_result &r) { return r.output_count; });
4943
    });
4944
  } else
4945
    #endif
4946
  {
4947
    return base64_to_binary(reinterpret_cast<const char *>(input.data()),
4948
                            input.size(),
4949
                            reinterpret_cast<char *>(binary_output.data()),
4950
                            options, last_chunk_options);
4951
  }
4952
}
4953
  #endif // SIMDUTF_SPAN
4954
4955
/**
4956
 * Provide the base64 length in bytes given the length of a binary input.
4957
 *
4958
 * @param length        the length of the input in bytes
4959
 * @param options       the base64 options to use (default: base64_default)
4960
 * @return number of base64 bytes
4961
 */
4962
inline simdutf_warn_unused simdutf_constexpr23 size_t base64_length_from_binary(
4963
    size_t length, base64_options options = base64_default) noexcept {
4964
  return scalar::base64::base64_length_from_binary(length, options);
4965
}
4966
4967
/**
4968
 * Provide the base64 length in bytes given the length of a binary input,
4969
 * taking into account line breaks.
4970
 *
4971
 * @param length        the length of the input in bytes
4972
 * @param options       the base64 options to use (default: base64_default)
4973
 * @param line_length   the length of lines, must be at least 4 (otherwise it is
4974
 * interpreted as 4),
4975
 * @return number of base64 bytes
4976
 */
4977
inline simdutf_warn_unused simdutf_constexpr23 size_t
4978
base64_length_from_binary_with_lines(
4979
    size_t length, base64_options options = base64_default,
4980
2.88k
    size_t line_length = default_line_length) noexcept {
4981
2.88k
  return scalar::base64::base64_length_from_binary_with_lines(length, options,
4982
2.88k
                                                              line_length);
4983
2.88k
}
4984
4985
/**
4986
 * Convert a binary input to a base64 output.
4987
 *
4988
 * The default option (simdutf::base64_default) uses the characters `+` and `/`
4989
 * as part of its alphabet. Further, it adds padding (`=`) at the end of the
4990
 * output to ensure that the output length is a multiple of four.
4991
 *
4992
 * The URL option (simdutf::base64_url) uses the characters `-` and `_` as part
4993
 * of its alphabet. No padding is added at the end of the output.
4994
 *
4995
 * This function always succeeds.
4996
 *
4997
 * @param input         the binary to process
4998
 * @param length        the length of the input in bytes
4999
 * @param output        the pointer to a buffer that can hold the conversion
5000
 * result (should be at least base64_length_from_binary(length) bytes long)
5001
 * @param options       the base64 options to use, can be base64_default or
5002
 * base64_url, is base64_default by default.
5003
 * @return number of written bytes, will be equal to
5004
 * base64_length_from_binary(length, options)
5005
 */
5006
size_t binary_to_base64(const char *input, size_t length, char *output,
5007
                        base64_options options = base64_default) noexcept;
5008
  #if SIMDUTF_SPAN
5009
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
5010
binary_to_base64(const detail::input_span_of_byte_like auto &input,
5011
                 detail::output_span_of_byte_like auto &&binary_output,
5012
                 base64_options options = base64_default) noexcept {
5013
    #if SIMDUTF_CPLUSPLUS23
5014
  if consteval {
5015
    return detail::with_char_input(input, [&](const char *in) {
5016
      return detail::with_char_output(
5017
          binary_output,
5018
          [&](char *out) {
5019
            return scalar::base64::tail_encode_base64(out, in, input.size(),
5020
                                                      options);
5021
          },
5022
          [](std::size_t n) { return n; });
5023
    });
5024
  } else
5025
    #endif
5026
  {
5027
    return binary_to_base64(
5028
        reinterpret_cast<const char *>(input.data()), input.size(),
5029
        reinterpret_cast<char *>(binary_output.data()), options);
5030
  }
5031
}
5032
  #endif // SIMDUTF_SPAN
5033
5034
/**
5035
 * Convert a binary input to a base64 output with line breaks.
5036
 *
5037
 * The default option (simdutf::base64_default) uses the characters `+` and `/`
5038
 * as part of its alphabet. Further, it adds padding (`=`) at the end of the
5039
 * output to ensure that the output length is a multiple of four.
5040
 *
5041
 * The URL option (simdutf::base64_url) uses the characters `-` and `_` as part
5042
 * of its alphabet. No padding is added at the end of the output.
5043
 *
5044
 * This function always succeeds.
5045
 *
5046
 * @param input         the binary to process
5047
 * @param length        the length of the input in bytes
5048
 * @param output        the pointer to a buffer that can hold the conversion
5049
 * result (should be at least base64_length_from_binary_with_lines(length,
5050
 * options, line_length) bytes long)
5051
 * @param line_length   the length of lines, must be at least 4 (otherwise it is
5052
 * interpreted as 4),
5053
 * @param options       the base64 options to use, can be base64_default or
5054
 * base64_url, is base64_default by default.
5055
 * @return number of written bytes, will be equal to
5056
 * base64_length_from_binary_with_lines(length, options)
5057
 */
5058
size_t
5059
binary_to_base64_with_lines(const char *input, size_t length, char *output,
5060
                            size_t line_length = simdutf::default_line_length,
5061
                            base64_options options = base64_default) noexcept;
5062
  #if SIMDUTF_SPAN
5063
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 size_t
5064
binary_to_base64_with_lines(
5065
    const detail::input_span_of_byte_like auto &input,
5066
    detail::output_span_of_byte_like auto &&binary_output,
5067
    size_t line_length = simdutf::default_line_length,
5068
    base64_options options = base64_default) noexcept {
5069
    #if SIMDUTF_CPLUSPLUS23
5070
  if consteval {
5071
    return detail::with_char_input(input, [&](const char *in) {
5072
      return detail::with_char_output(
5073
          binary_output,
5074
          [&](char *out) {
5075
            return scalar::base64::tail_encode_base64_impl<true>(
5076
                out, in, input.size(), options, line_length);
5077
          },
5078
          [](std::size_t n) { return n; });
5079
    });
5080
  } else
5081
    #endif
5082
  {
5083
    return binary_to_base64_with_lines(
5084
        reinterpret_cast<const char *>(input.data()), input.size(),
5085
        reinterpret_cast<char *>(binary_output.data()), line_length, options);
5086
  }
5087
}
5088
  #endif // SIMDUTF_SPAN
5089
5090
  #if SIMDUTF_ATOMIC_REF
5091
/**
5092
 * Convert a binary input to a base64 output, using atomic accesses.
5093
 * This function comes with a potentially significant performance
5094
 * penalty, but it may be useful in some cases where the input
5095
 * buffers are shared between threads, to avoid undefined
5096
 * behavior in case of data races.
5097
 *
5098
 * The function is for advanced users. Its main use case is when
5099
 * to silence sanitizer warnings. We have no documented use case
5100
 * where this function is actually necessary in terms of practical correctness.
5101
 *
5102
 * This function is only available when simdutf is compiled with
5103
 * C++20 support and __cpp_lib_atomic_ref >= 201806L. You may check
5104
 * the availability of this function by checking the macro
5105
 * SIMDUTF_ATOMIC_REF.
5106
 *
5107
 * The default option (simdutf::base64_default) uses the characters `+` and `/`
5108
 * as part of its alphabet. Further, it adds padding (`=`) at the end of the
5109
 * output to ensure that the output length is a multiple of four.
5110
 *
5111
 * The URL option (simdutf::base64_url) uses the characters `-` and `_` as part
5112
 * of its alphabet. No padding is added at the end of the output.
5113
 *
5114
 * This function always succeeds.
5115
 *
5116
 * This function is considered experimental. It is not tested by default
5117
 * (see the CMake option SIMDUTF_ATOMIC_BASE64_TESTS) nor is it fuzz tested.
5118
 * It is not documented in the public API documentation (README). It is
5119
 * offered on a best effort basis. We rely on the community for further
5120
 * testing and feedback.
5121
 *
5122
 * @brief atomic_binary_to_base64
5123
 * @param input         the binary to process
5124
 * @param length        the length of the input in bytes
5125
 * @param output        the pointer to a buffer that can hold the conversion
5126
 * result (should be at least base64_length_from_binary(length) bytes long)
5127
 * @param options       the base64 options to use, can be base64_default or
5128
 * base64_url, is base64_default by default.
5129
 * @return number of written bytes, will be equal to
5130
 * base64_length_from_binary(length, options)
5131
 */
5132
size_t
5133
atomic_binary_to_base64(const char *input, size_t length, char *output,
5134
                        base64_options options = base64_default) noexcept;
5135
    #if SIMDUTF_SPAN
5136
simdutf_really_inline simdutf_warn_unused size_t
5137
atomic_binary_to_base64(const detail::input_span_of_byte_like auto &input,
5138
                        detail::output_span_of_byte_like auto &&binary_output,
5139
                        base64_options options = base64_default) noexcept {
5140
  return atomic_binary_to_base64(
5141
      reinterpret_cast<const char *>(input.data()), input.size(),
5142
      reinterpret_cast<char *>(binary_output.data()), options);
5143
}
5144
    #endif // SIMDUTF_SPAN
5145
  #endif   // SIMDUTF_ATOMIC_REF
5146
5147
/**
5148
 * Convert a base64 input to a binary output.
5149
 *
5150
 * This function follows the WHATWG forgiving-base64 format, which means that it
5151
 * will ignore any ASCII spaces in the input. You may provide a padded input
5152
 * (with one or two equal signs at the end) or an unpadded input (without any
5153
 * equal signs at the end).
5154
 *
5155
 * See https://infra.spec.whatwg.org/#forgiving-base64-decode
5156
 *
5157
 * This function will fail in case of invalid input. When last_chunk_options =
5158
 * loose, there are two possible reasons for failure: the input contains a
5159
 * number of base64 characters that when divided by 4, leaves a single remainder
5160
 * character (BASE64_INPUT_REMAINDER), or the input contains a character that is
5161
 * not a valid base64 character (INVALID_BASE64_CHARACTER).
5162
 *
5163
 * When the error is INVALID_BASE64_CHARACTER, r.count contains the index in the
5164
 * input where the invalid character was found. When the error is
5165
 * BASE64_INPUT_REMAINDER, then r.count contains the number of bytes decoded.
5166
 *
5167
 * The default option (simdutf::base64_default) expects the characters `+` and
5168
 * `/` as part of its alphabet. The URL option (simdutf::base64_url) expects the
5169
 * characters `-` and `_` as part of its alphabet.
5170
 *
5171
 * The padding (`=`) is validated if present. There may be at most two padding
5172
 * characters at the end of the input. If there are any padding characters, the
5173
 * total number of characters (excluding spaces but including padding
5174
 * characters) must be divisible by four.
5175
 *
5176
 * You should call this function with a buffer that is at least
5177
 * maximal_binary_length_from_base64(input, length) bytes long. If you fail
5178
 * to provide that much space, the function may cause a buffer overflow.
5179
 *
5180
 * Advanced users may want to tailor how the last chunk is handled. By default,
5181
 * we use a loose (forgiving) approach but we also support a strict approach
5182
 * as well as a stop_before_partial approach, as per the following proposal:
5183
 *
5184
 * https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64
5185
 *
5186
 * @param input         the base64 string to process, in ASCII stored as 16-bit
5187
 * units
5188
 * @param length        the length of the string in 16-bit units
5189
 * @param output        the pointer to a buffer that can hold the conversion
5190
 * result (should be at least maximal_binary_length_from_base64(input, length)
5191
 * bytes long).
5192
 * @param options       the base64 options to use, can be base64_default or
5193
 * base64_url, is base64_default by default.
5194
 * @param last_chunk_options the last chunk handling options,
5195
 * last_chunk_handling_options::loose by default
5196
 * but can also be last_chunk_handling_options::strict or
5197
 * last_chunk_handling_options::stop_before_partial.
5198
 * @return a result pair struct (of type simdutf::result containing the two
5199
 * fields error and count) with an error code and position of the
5200
 * INVALID_BASE64_CHARACTER error (in the input in units) if any, or the number
5201
 * of bytes written if successful.
5202
 */
5203
simdutf_warn_unused result
5204
base64_to_binary(const char16_t *input, size_t length, char *output,
5205
                 base64_options options = base64_default,
5206
                 last_chunk_handling_options last_chunk_options =
5207
                     last_chunk_handling_options::loose) noexcept;
5208
  #if SIMDUTF_SPAN
5209
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 result
5210
base64_to_binary(
5211
    std::span<const char16_t> input,
5212
    detail::output_span_of_byte_like auto &&binary_output,
5213
    base64_options options = base64_default,
5214
    last_chunk_handling_options last_chunk_options = loose) noexcept {
5215
    #if SIMDUTF_CPLUSPLUS23
5216
  if consteval {
5217
    return detail::with_char_output(
5218
        binary_output,
5219
        [&](auto out) {
5220
          return scalar::base64::base64_to_binary_details_impl(
5221
              input.data(), input.size(), out, options, last_chunk_options);
5222
        },
5223
        [](const full_result &r) { return r.output_count; });
5224
  } else
5225
    #endif
5226
  {
5227
    return base64_to_binary(input.data(), input.size(),
5228
                            reinterpret_cast<char *>(binary_output.data()),
5229
                            options, last_chunk_options);
5230
  }
5231
}
5232
  #endif // SIMDUTF_SPAN
5233
5234
/**
5235
 * Convert a base64 input to a binary output while returning more details
5236
 * than base64_to_binary.
5237
 *
5238
 * This function follows the WHATWG forgiving-base64 format, which means that it
5239
 * will ignore any ASCII spaces in the input. You may provide a padded input
5240
 * (with one or two equal signs at the end) or an unpadded input (without any
5241
 * equal signs at the end).
5242
 *
5243
 * See https://infra.spec.whatwg.org/#forgiving-base64-decode
5244
 *
5245
 * Unlike base64_to_binary, this function returns a full_result with both
5246
 * input_count and output_count, so you always know how much input was consumed
5247
 * and how much output was written. There are three cases where the input may
5248
 * not be fully consumed:
5249
 *
5250
 * 1. stop_before_partial: When last_chunk_options is set to
5251
 *    stop_before_partial, any incomplete 4-character group at the end of the
5252
 *    input is left unconsumed. This is useful for streaming/chunked decoding
5253
 *    where you can carry over the unconsumed input to the next chunk.
5254
 *
5255
 * 2. INVALID_BASE64_CHARACTER: The input contains a character that is not a
5256
 *    valid base64 character. In this case, input_count indicates where the
5257
 *    invalid character was found.
5258
 *
5259
 * 3. BASE64_INPUT_REMAINDER: When last_chunk_options is loose, the input
5260
 *    contains a number of base64 characters that, when divided by 4, leaves
5261
 *    a single remainder character (which cannot encode any bytes).
5262
 *
5263
 * You should call this function with a buffer that is at least
5264
 * maximal_binary_length_from_base64(input, length) bytes long. If you fail to
5265
 * provide that much space, the function may cause a buffer overflow.
5266
 *
5267
 * @param input         the base64 string to process
5268
 * @param length        the length of the string in bytes
5269
 * @param output        the pointer to a buffer that can hold the conversion
5270
 * result (should be at least maximal_binary_length_from_base64(input, length)
5271
 * bytes long).
5272
 * @param options       the base64 options to use, can be base64_default or
5273
 * base64_url, is base64_default by default.
5274
 * @param last_chunk_options the last chunk handling options,
5275
 * last_chunk_handling_options::loose by default
5276
 * but can also be last_chunk_handling_options::strict or
5277
 * last_chunk_handling_options::stop_before_partial.
5278
 * @return a full_result struct (of type simdutf::full_result containing the
5279
 * three fields error, input_count and output_count).
5280
 */
5281
simdutf_warn_unused full_result
5282
base64_to_binary_details(const char *input, size_t length, char *output,
5283
                         base64_options options = base64_default,
5284
                         last_chunk_handling_options last_chunk_options =
5285
                             last_chunk_handling_options::loose) noexcept;
5286
  #if SIMDUTF_SPAN
5287
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 full_result
5288
base64_to_binary_details(
5289
    const detail::input_span_of_byte_like auto &input,
5290
    detail::output_span_of_byte_like auto &&binary_output,
5291
    base64_options options = base64_default,
5292
    last_chunk_handling_options last_chunk_options = loose) noexcept {
5293
    #if SIMDUTF_CPLUSPLUS23
5294
  if consteval {
5295
    return detail::with_char_input(input, [&](auto in) {
5296
      return detail::with_char_output(
5297
          binary_output,
5298
          [&](char *out) {
5299
            return scalar::base64::base64_to_binary_details_impl(
5300
                in, input.size(), out, options, last_chunk_options);
5301
          },
5302
          [](const full_result &r) { return r.output_count; });
5303
    });
5304
  } else
5305
    #endif
5306
  {
5307
    return base64_to_binary_details(
5308
        reinterpret_cast<const char *>(input.data()), input.size(),
5309
        reinterpret_cast<char *>(binary_output.data()), options,
5310
        last_chunk_options);
5311
  }
5312
}
5313
  #endif // SIMDUTF_SPAN
5314
5315
/**
5316
 * Convert a base64 input to a binary output while returning more details
5317
 * than base64_to_binary.
5318
 *
5319
 * This function follows the WHATWG forgiving-base64 format, which means that it
5320
 * will ignore any ASCII spaces in the input. You may provide a padded input
5321
 * (with one or two equal signs at the end) or an unpadded input (without any
5322
 * equal signs at the end).
5323
 *
5324
 * See https://infra.spec.whatwg.org/#forgiving-base64-decode
5325
 *
5326
 * Unlike base64_to_binary, this function returns a full_result with both
5327
 * input_count and output_count, so you always know how much input was consumed
5328
 * and how much output was written. There are three cases where the input may
5329
 * not be fully consumed:
5330
 *
5331
 * 1. stop_before_partial: When last_chunk_options is set to
5332
 *    stop_before_partial, any incomplete 4-character group at the end of the
5333
 *    input is left unconsumed. This is useful for streaming/chunked decoding
5334
 *    where you can carry over the unconsumed input to the next chunk.
5335
 *
5336
 * 2. INVALID_BASE64_CHARACTER: The input contains a character that is not a
5337
 *    valid base64 character. In this case, input_count indicates where the
5338
 *    invalid character was found.
5339
 *
5340
 * 3. BASE64_INPUT_REMAINDER: When last_chunk_options is loose, the input
5341
 *    contains a number of base64 characters that, when divided by 4, leaves
5342
 *    a single remainder character (which cannot encode any bytes).
5343
 *
5344
 * You should call this function with a buffer that is at least
5345
 * maximal_binary_length_from_base64(input, length) bytes long. If you fail to
5346
 * provide that much space, the function may cause a buffer overflow.
5347
 *
5348
 * @param input         the base64 string to process, in ASCII stored as 16-bit
5349
 * units
5350
 * @param length        the length of the string in 16-bit units
5351
 * @param output        the pointer to a buffer that can hold the conversion
5352
 * result (should be at least maximal_binary_length_from_base64(input, length)
5353
 * bytes long).
5354
 * @param options       the base64 options to use, can be base64_default or
5355
 * base64_url, is base64_default by default.
5356
 * @param last_chunk_options the last chunk handling options,
5357
 * last_chunk_handling_options::loose by default
5358
 * but can also be last_chunk_handling_options::strict or
5359
 * last_chunk_handling_options::stop_before_partial.
5360
 * @return a full_result struct (of type simdutf::full_result containing the
5361
 * three fields error, input_count and output_count).
5362
 */
5363
simdutf_warn_unused full_result
5364
base64_to_binary_details(const char16_t *input, size_t length, char *output,
5365
                         base64_options options = base64_default,
5366
                         last_chunk_handling_options last_chunk_options =
5367
                             last_chunk_handling_options::loose) noexcept;
5368
  #if SIMDUTF_SPAN
5369
simdutf_really_inline simdutf_warn_unused simdutf_constexpr23 full_result
5370
base64_to_binary_details(
5371
    std::span<const char16_t> input,
5372
    detail::output_span_of_byte_like auto &&binary_output,
5373
    base64_options options = base64_default,
5374
    last_chunk_handling_options last_chunk_options = loose) noexcept {
5375
    #if SIMDUTF_CPLUSPLUS23
5376
  if consteval {
5377
    return detail::with_char_output(
5378
        binary_output,
5379
        [&](auto out) {
5380
          return scalar::base64::base64_to_binary_details_impl(
5381
              input.data(), input.size(), out, options, last_chunk_options);
5382
        },
5383
        [](const full_result &r) { return r.output_count; });
5384
  } else
5385
    #endif
5386
  {
5387
    return base64_to_binary_details(
5388
        input.data(), input.size(),
5389
        reinterpret_cast<char *>(binary_output.data()), options,
5390
        last_chunk_options);
5391
  }
5392
}
5393
  #endif // SIMDUTF_SPAN
5394
5395
/**
5396
 * Check if a character is an ignorable base64 character.
5397
 * Checking a large input, character by character, is not computationally
5398
 * efficient.
5399
 *
5400
 * @param input         the character to check
5401
 * @param options       the base64 options to use, is base64_default by default.
5402
 * @return true if the character is an ignorable base64 character, false
5403
 * otherwise.
5404
 */
5405
simdutf_warn_unused simdutf_really_inline simdutf_constexpr23 bool
5406
base64_ignorable(char input, base64_options options = base64_default) noexcept {
5407
  return scalar::base64::is_ignorable(input, options);
5408
}
5409
simdutf_warn_unused simdutf_really_inline simdutf_constexpr23 bool
5410
base64_ignorable(char16_t input,
5411
                 base64_options options = base64_default) noexcept {
5412
  return scalar::base64::is_ignorable(input, options);
5413
}
5414
5415
/**
5416
 * Check if a character is a valid base64 character.
5417
 * Checking a large input, character by character, is not computationally
5418
 * efficient.
5419
 * Note that padding characters are not considered valid base64 characters in
5420
 * this context, nor are spaces.
5421
 *
5422
 * @param input         the character to check
5423
 * @param options       the base64 options to use, is base64_default by default.
5424
 * @return true if the character is a base64 character, false otherwise.
5425
 */
5426
simdutf_warn_unused simdutf_really_inline simdutf_constexpr23 bool
5427
base64_valid(char input, base64_options options = base64_default) noexcept {
5428
  return scalar::base64::is_base64(input, options);
5429
}
5430
simdutf_warn_unused simdutf_really_inline simdutf_constexpr23 bool
5431
base64_valid(char16_t input, base64_options options = base64_default) noexcept {
5432
  return scalar::base64::is_base64(input, options);
5433
}
5434
5435
/**
5436
 * Check if a character is a valid base64 character or the padding character
5437
 * ('='). Checking a large input, character by character, is not computationally
5438
 * efficient.
5439
 *
5440
 * @param input         the character to check
5441
 * @param options       the base64 options to use, is base64_default by default.
5442
 * @return true if the character is a base64 character, false otherwise.
5443
 */
5444
simdutf_warn_unused simdutf_really_inline simdutf_constexpr23 bool
5445
base64_valid_or_padding(char input,
5446
0
                        base64_options options = base64_default) noexcept {
5447
0
  return scalar::base64::is_base64_or_padding(input, options);
5448
0
}
5449
simdutf_warn_unused simdutf_really_inline simdutf_constexpr23 bool
5450
base64_valid_or_padding(char16_t input,
5451
0
                        base64_options options = base64_default) noexcept {
5452
0
  return scalar::base64::is_base64_or_padding(input, options);
5453
0
}
5454
5455
/**
5456
 * Convert a base64 input to a binary output.
5457
 *
5458
 * This function follows the WHATWG forgiving-base64 format, which means that it
5459
 * will ignore any ASCII spaces in the input. You may provide a padded input
5460
 * (with one or two equal signs at the end) or an unpadded input (without any
5461
 * equal signs at the end).
5462
 *
5463
 * See https://infra.spec.whatwg.org/#forgiving-base64-decode
5464
 *
5465
 * This function will fail in case of invalid input. When last_chunk_options =
5466
 * loose, there are three possible reasons for failure: the input contains a
5467
 * number of base64 characters that when divided by 4, leaves a single remainder
5468
 * character (BASE64_INPUT_REMAINDER), the input contains a character that is
5469
 * not a valid base64 character (INVALID_BASE64_CHARACTER), or the output buffer
5470
 * is too small (OUTPUT_BUFFER_TOO_SMALL).
5471
 *
5472
 * When OUTPUT_BUFFER_TOO_SMALL, we return both the number of bytes written
5473
 * and the number of units processed, see description of the parameters and
5474
 * returned value.
5475
 *
5476
 * When the error is INVALID_BASE64_CHARACTER, r.count contains the index in the
5477
 * input where the invalid character was found. When the error is
5478
 * BASE64_INPUT_REMAINDER, then r.count contains the number of bytes decoded.
5479
 *
5480
 * The default option (simdutf::base64_default) expects the characters `+` and
5481
 * `/` as part of its alphabet. The URL option (simdutf::base64_url) expects the
5482
 * characters `-` and `_` as part of its alphabet.
5483
 *
5484
 * The padding (`=`) is validated if present. There may be at most two padding
5485
 * characters at the end of the input. If there are any padding characters, the
5486
 * total number of characters (excluding spaces but including padding
5487
 * characters) must be divisible by four.
5488
 *
5489
 * The INVALID_BASE64_CHARACTER cases are considered fatal and you are expected
5490
 * to discard the output unless the parameter decode_up_to_bad_char is set to
5491
 * true. In that case, the function will decode up to the first invalid
5492
 * character. Extra padding characters ('=') are considered invalid characters.
5493
 *
5494
 * Advanced users may want to tailor how the last chunk is handled. By default,
5495
 * we use a loose (forgiving) approach but we also support a strict approach
5496
 * as well as a stop_before_partial approach, as per the following proposal:
5497
 *
5498
 * https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64
5499
 *
5500
 * The base64_to_binary_safe function has negligible overhead compared with
5501
 * base64_to_binary in the absence of ignorable characters; however, on short
5502
 * inputs containing ignorable characters, it can be up to three times slower.
5503
 *
5504
 * @param input         the base64 string to process, in ASCII stored as 8-bit
5505
 * or 16-bit units
5506
 * @param length        the length of the string in 8-bit or 16-bit units.
5507
 * @param output        the pointer to a buffer that can hold the conversion
5508
 * result.
5509
 * @param outlen        the number of bytes that can be written in the output
5510
 * buffer. Upon return, it is modified to reflect how many bytes were written.
5511
 * @param options       the base64 options to use, can be base64_default or
5512
 * base64_url, is base64_default by default.
5513
 * @param last_chunk_options the last chunk handling options,
5514
 * last_chunk_handling_options::loose by default
5515
 * but can also be last_chunk_handling_options::strict or
5516
 * last_chunk_handling_options::stop_before_partial.
5517
 * @param decode_up_to_bad_char if true, the function will decode up to the
5518
 * first invalid character. By default (false), it is assumed that the output
5519
 * buffer is to be discarded. When there are multiple errors in the input,
5520
 * using decode_up_to_bad_char might trigger a different error.
5521
 * @return a result pair struct (of type simdutf::result containing the two
5522
 * fields error and count) with an error code and position of the
5523
 * INVALID_BASE64_CHARACTER error (in the input in units) if any, or the number
5524
 * of units processed if successful.
5525
 */
5526
simdutf_warn_unused result
5527
base64_to_binary_safe(const char *input, size_t length, char *output,
5528
                      size_t &outlen, base64_options options = base64_default,
5529
                      last_chunk_handling_options last_chunk_options =
5530
                          last_chunk_handling_options::loose,
5531
                      bool decode_up_to_bad_char = false) noexcept;
5532
// the span overload has moved to the bottom of the file
5533
5534
simdutf_warn_unused result
5535
base64_to_binary_safe(const char16_t *input, size_t length, char *output,
5536
                      size_t &outlen, base64_options options = base64_default,
5537
                      last_chunk_handling_options last_chunk_options =
5538
                          last_chunk_handling_options::loose,
5539
                      bool decode_up_to_bad_char = false) noexcept;
5540
  // span overload moved to bottom of file
5541
5542
  #if SIMDUTF_ATOMIC_REF
5543
/**
5544
 * Convert a base64 input to a binary output with a size limit and using atomic
5545
 * operations.
5546
 *
5547
 * Like `base64_to_binary_safe` but using atomic operations, this function is
5548
 * thread-safe for concurrent memory access, allowing the output
5549
 * buffers to be shared between threads without undefined behavior in case of
5550
 * data races.
5551
 *
5552
 * This function comes with a potentially significant performance penalty, but
5553
 * is useful when thread safety is needed during base64 decoding.
5554
 *
5555
 * This function is only available when simdutf is compiled with
5556
 * C++20 support and __cpp_lib_atomic_ref >= 201806L. You may check
5557
 * the availability of this function by checking the macro
5558
 * SIMDUTF_ATOMIC_REF.
5559
 *
5560
 * This function is considered experimental. It is not tested by default
5561
 * (see the CMake option SIMDUTF_ATOMIC_BASE64_TESTS) nor is it fuzz tested.
5562
 * It is not documented in the public API documentation (README). It is
5563
 * offered on a best effort basis. We rely on the community for further
5564
 * testing and feedback.
5565
 *
5566
 * @param input         the base64 input to decode
5567
 * @param length        the length of the input in bytes
5568
 * @param output        the pointer to buffer that can hold the conversion
5569
 * result
5570
 * @param outlen        the number of bytes that can be written in the output
5571
 * buffer. Upon return, it is modified to reflect how many bytes were written.
5572
 * @param options       the base64 options to use (default, url, etc.)
5573
 * @param last_chunk_options the last chunk handling options (loose, strict,
5574
 * stop_before_partial)
5575
 * @param decode_up_to_bad_char if true, the function will decode up to the
5576
 * first invalid character. By default (false), it is assumed that the output
5577
 * buffer is to be discarded. When there are multiple errors in the input,
5578
 * using decode_up_to_bad_char might trigger a different error.
5579
 * @return a result struct with an error code and count indicating error
5580
 * position or success
5581
 */
5582
simdutf_warn_unused result atomic_base64_to_binary_safe(
5583
    const char *input, size_t length, char *output, size_t &outlen,
5584
    base64_options options = base64_default,
5585
    last_chunk_handling_options last_chunk_options =
5586
        last_chunk_handling_options::loose,
5587
    bool decode_up_to_bad_char = false) noexcept;
5588
simdutf_warn_unused result atomic_base64_to_binary_safe(
5589
    const char16_t *input, size_t length, char *output, size_t &outlen,
5590
    base64_options options = base64_default,
5591
    last_chunk_handling_options last_chunk_options = loose,
5592
    bool decode_up_to_bad_char = false) noexcept;
5593
    #if SIMDUTF_SPAN
5594
/**
5595
 * @brief span overload
5596
 * @return a tuple of result and outlen
5597
 */
5598
simdutf_really_inline simdutf_warn_unused std::tuple<result, std::size_t>
5599
atomic_base64_to_binary_safe(
5600
    const detail::input_span_of_byte_like auto &binary_input,
5601
    detail::output_span_of_byte_like auto &&output,
5602
    base64_options options = base64_default,
5603
    last_chunk_handling_options last_chunk_options =
5604
        last_chunk_handling_options::loose,
5605
    bool decode_up_to_bad_char = false) noexcept {
5606
  size_t outlen = output.size();
5607
  auto ret = atomic_base64_to_binary_safe(
5608
      reinterpret_cast<const char *>(binary_input.data()), binary_input.size(),
5609
      reinterpret_cast<char *>(output.data()), outlen, options,
5610
      last_chunk_options, decode_up_to_bad_char);
5611
  return {ret, outlen};
5612
}
5613
/**
5614
 * @brief span overload
5615
 * @return a tuple of result and outlen
5616
 */
5617
simdutf_warn_unused std::tuple<result, std::size_t>
5618
atomic_base64_to_binary_safe(
5619
    std::span<const char16_t> base64_input,
5620
    detail::output_span_of_byte_like auto &&binary_output,
5621
    base64_options options = base64_default,
5622
    last_chunk_handling_options last_chunk_options = loose,
5623
    bool decode_up_to_bad_char = false) noexcept {
5624
  size_t outlen = binary_output.size();
5625
  auto ret = atomic_base64_to_binary_safe(
5626
      base64_input.data(), base64_input.size(),
5627
      reinterpret_cast<char *>(binary_output.data()), outlen, options,
5628
      last_chunk_options, decode_up_to_bad_char);
5629
  return {ret, outlen};
5630
}
5631
    #endif // SIMDUTF_SPAN
5632
  #endif   // SIMDUTF_ATOMIC_REF
5633
5634
#endif // SIMDUTF_FEATURE_BASE64
5635
5636
/**
5637
 * An implementation of simdutf for a particular CPU architecture.
5638
 *
5639
 * Also used to maintain the currently active implementation. The active
5640
 * implementation is automatically initialized on first use to the most advanced
5641
 * implementation supported by the host.
5642
 */
5643
class implementation {
5644
public:
5645
  /**
5646
   * The name of this implementation.
5647
   *
5648
   *     const implementation *impl = simdutf::active_implementation;
5649
   *     cout << "simdutf is optimized for " << impl->name() << "(" <<
5650
   * impl->description() << ")" << endl;
5651
   *
5652
   * @return the name of the implementation, e.g. "haswell", "westmere", "arm64"
5653
   */
5654
  virtual std::string_view name() const noexcept { return _name; }
5655
5656
  /**
5657
   * The description of this implementation.
5658
   *
5659
   *     const implementation *impl = simdutf::active_implementation;
5660
   *     cout << "simdutf is optimized for " << impl->name() << "(" <<
5661
   * impl->description() << ")" << endl;
5662
   *
5663
   * @return the name of the implementation, e.g. "haswell", "westmere", "arm64"
5664
   */
5665
  virtual std::string_view description() const noexcept { return _description; }
5666
5667
  /**
5668
   * The instruction sets this implementation is compiled against
5669
   * and the current CPU match. This function may poll the current CPU/system
5670
   * and should therefore not be called too often if performance is a concern.
5671
   *
5672
   *
5673
   * @return true if the implementation can be safely used on the current system
5674
   * (determined at runtime)
5675
   */
5676
  bool supported_by_runtime_system() const;
5677
5678
#if SIMDUTF_FEATURE_DETECT_ENCODING
5679
  /**
5680
   * This function will try to detect the encoding
5681
   * @param input the string to identify
5682
   * @param length the length of the string in bytes.
5683
   * @return the encoding type detected
5684
   */
5685
  virtual encoding_type autodetect_encoding(const char *input,
5686
                                            size_t length) const noexcept;
5687
5688
  /**
5689
   * This function will try to detect the possible encodings in one pass
5690
   * @param input the string to identify
5691
   * @param length the length of the string in bytes.
5692
   * @return the encoding type detected
5693
   */
5694
  virtual int detect_encodings(const char *input,
5695
                               size_t length) const noexcept = 0;
5696
#endif // SIMDUTF_FEATURE_DETECT_ENCODING
5697
5698
  /**
5699
   * @private For internal implementation use
5700
   *
5701
   * The instruction sets this implementation is compiled against.
5702
   *
5703
   * @return a mask of all required `internal::instruction_set::` values
5704
   */
5705
  virtual uint32_t required_instruction_sets() const {
5706
    return _required_instruction_sets;
5707
  }
5708
5709
#if SIMDUTF_FEATURE_UTF8 || SIMDUTF_FEATURE_DETECT_ENCODING
5710
  /**
5711
   * Validate the UTF-8 string.
5712
   *
5713
   * Overridden by each implementation.
5714
   *
5715
   * @param buf the UTF-8 string to validate.
5716
   * @param len the length of the string in bytes.
5717
   * @return true if and only if the string is valid UTF-8.
5718
   */
5719
  simdutf_warn_unused virtual bool validate_utf8(const char *buf,
5720
                                                 size_t len) const noexcept = 0;
5721
#endif // SIMDUTF_FEATURE_UTF8 || SIMDUTF_FEATURE_DETECT_ENCODING
5722
5723
#if SIMDUTF_FEATURE_UTF8
5724
  /**
5725
   * Validate the UTF-8 string and stop on errors.
5726
   *
5727
   * Overridden by each implementation.
5728
   *
5729
   * @param buf the UTF-8 string to validate.
5730
   * @param len the length of the string in bytes.
5731
   * @return a result pair struct (of type simdutf::result containing the two
5732
   * fields error and count) with an error code and either position of the error
5733
   * (in the input in code units) if any, or the number of code units validated
5734
   * if successful.
5735
   */
5736
  simdutf_warn_unused virtual result
5737
  validate_utf8_with_errors(const char *buf, size_t len) const noexcept = 0;
5738
  /**
5739
   * Finds the pointer to the first byte of invalid utf8 while counting
5740
   continuation bytes and four-byte sequences.
5741
5742
   * @param buf the UTF-8 string to validate.
5743
   * @param len the length of the string in bytes.
5744
   * @return a utf8_result struct. It contains the length of the valid utf8
5745
   segment, the error code and the count of continuation bytes and four-byte
5746
   sequences.
5747
   *
5748
   * Returns a utf8_result
5749
   */
5750
  virtual simdutf_warn_unused utf8_result
5751
  validate_utf8_with_counts(const char *buf, size_t len) const noexcept = 0;
5752
5753
#endif // SIMDUTF_FEATURE_UTF8
5754
5755
#if SIMDUTF_FEATURE_ASCII
5756
  /**
5757
   * Validate the ASCII string.
5758
   *
5759
   * Overridden by each implementation.
5760
   *
5761
   * @param buf the ASCII string to validate.
5762
   * @param len the length of the string in bytes.
5763
   * @return true if and only if the string is valid ASCII.
5764
   */
5765
  simdutf_warn_unused virtual bool
5766
  validate_ascii(const char *buf, size_t len) const noexcept = 0;
5767
5768
  /**
5769
   * Validate the ASCII string and stop on error.
5770
   *
5771
   * Overridden by each implementation.
5772
   *
5773
   * @param buf the ASCII string to validate.
5774
   * @param len the length of the string in bytes.
5775
   * @return a result pair struct (of type simdutf::result containing the two
5776
   * fields error and count) with an error code and either position of the error
5777
   * (in the input in code units) if any, or the number of code units validated
5778
   * if successful.
5779
   */
5780
  simdutf_warn_unused virtual result
5781
  validate_ascii_with_errors(const char *buf, size_t len) const noexcept = 0;
5782
5783
#endif // SIMDUTF_FEATURE_ASCII
5784
5785
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_ASCII
5786
  /**
5787
   * Validate the ASCII string as a UTF-16BE sequence.
5788
   * An UTF-16 sequence is considered an ASCII sequence
5789
   * if it could be converted to an ASCII string losslessly.
5790
   *
5791
   * Overridden by each implementation.
5792
   *
5793
   * @param buf the UTF-16BE string to validate.
5794
   * @param len the length of the string in bytes.
5795
   * @return true if and only if the string is valid ASCII.
5796
   */
5797
  simdutf_warn_unused virtual bool
5798
  validate_utf16be_as_ascii(const char16_t *buf, size_t len) const noexcept = 0;
5799
5800
  /**
5801
   * Validate the ASCII string as a UTF-16LE sequence.
5802
   * An UTF-16 sequence is considered an ASCII sequence
5803
   * if it could be converted to an ASCII string losslessly.
5804
   *
5805
   * Overridden by each implementation.
5806
   *
5807
   * @param buf the UTF-16LE string to validate.
5808
   * @param len the length of the string in bytes.
5809
   * @return true if and only if the string is valid ASCII.
5810
   */
5811
  simdutf_warn_unused virtual bool
5812
  validate_utf16le_as_ascii(const char16_t *buf, size_t len) const noexcept = 0;
5813
#endif // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_ASCII
5814
5815
#if SIMDUTF_FEATURE_UTF16 || SIMDUTF_FEATURE_DETECT_ENCODING
5816
  /**
5817
   * Validate the UTF-16LE string.This function may be best when you expect
5818
   * the input to be almost always valid. Otherwise, consider using
5819
   * validate_utf16le_with_errors.
5820
   *
5821
   * Overridden by each implementation.
5822
   *
5823
   * This function is not BOM-aware.
5824
   *
5825
   * @param buf the UTF-16LE string to validate.
5826
   * @param len the length of the string in number of 2-byte code units
5827
   * (char16_t).
5828
   * @return true if and only if the string is valid UTF-16LE.
5829
   */
5830
  simdutf_warn_unused virtual bool
5831
  validate_utf16le(const char16_t *buf, size_t len) const noexcept = 0;
5832
#endif // SIMDUTF_FEATURE_UTF16 || SIMDUTF_FEATURE_DETECT_ENCODING
5833
5834
#if SIMDUTF_FEATURE_UTF16
5835
  /**
5836
   * Validate the UTF-16BE string. This function may be best when you expect
5837
   * the input to be almost always valid. Otherwise, consider using
5838
   * validate_utf16be_with_errors.
5839
   *
5840
   * Overridden by each implementation.
5841
   *
5842
   * This function is not BOM-aware.
5843
   *
5844
   * @param buf the UTF-16BE string to validate.
5845
   * @param len the length of the string in number of 2-byte code units
5846
   * (char16_t).
5847
   * @return true if and only if the string is valid UTF-16BE.
5848
   */
5849
  simdutf_warn_unused virtual bool
5850
  validate_utf16be(const char16_t *buf, size_t len) const noexcept = 0;
5851
5852
  /**
5853
   * Validate the UTF-16LE string and stop on error.  It might be faster than
5854
   * validate_utf16le when an error is expected to occur early.
5855
   *
5856
   * Overridden by each implementation.
5857
   *
5858
   * This function is not BOM-aware.
5859
   *
5860
   * @param buf the UTF-16LE string to validate.
5861
   * @param len the length of the string in number of 2-byte code units
5862
   * (char16_t).
5863
   * @return a result pair struct (of type simdutf::result containing the two
5864
   * fields error and count) with an error code and either position of the error
5865
   * (in the input in code units) if any, or the number of code units validated
5866
   * if successful.
5867
   */
5868
  simdutf_warn_unused virtual result
5869
  validate_utf16le_with_errors(const char16_t *buf,
5870
                               size_t len) const noexcept = 0;
5871
5872
  /**
5873
   * Validate the UTF-16BE string and stop on error. It might be faster than
5874
   * validate_utf16be when an error is expected to occur early.
5875
   *
5876
   * Overridden by each implementation.
5877
   *
5878
   * This function is not BOM-aware.
5879
   *
5880
   * @param buf the UTF-16BE string to validate.
5881
   * @param len the length of the string in number of 2-byte code units
5882
   * (char16_t).
5883
   * @return a result pair struct (of type simdutf::result containing the two
5884
   * fields error and count) with an error code and either position of the error
5885
   * (in the input in code units) if any, or the number of code units validated
5886
   * if successful.
5887
   */
5888
  simdutf_warn_unused virtual result
5889
  validate_utf16be_with_errors(const char16_t *buf,
5890
                               size_t len) const noexcept = 0;
5891
  /**
5892
   * Copies the UTF-16LE string while replacing mismatched surrogates with the
5893
   * Unicode replacement character U+FFFD. We allow the input and output to be
5894
   * the same buffer so that the correction is done in-place.
5895
   *
5896
   * Overridden by each implementation.
5897
   *
5898
   * @param input the UTF-16LE string to correct.
5899
   * @param len the length of the string in number of 2-byte code units
5900
   * (char16_t).
5901
   * @param output the output buffer.
5902
   */
5903
  virtual void to_well_formed_utf16le(const char16_t *input, size_t len,
5904
                                      char16_t *output) const noexcept = 0;
5905
  /**
5906
   * Copies the UTF-16BE string while replacing mismatched surrogates with the
5907
   * Unicode replacement character U+FFFD. We allow the input and output to be
5908
   * the same buffer so that the correction is done in-place.
5909
   *
5910
   * Overridden by each implementation.
5911
   *
5912
   * @param input the UTF-16BE string to correct.
5913
   * @param len the length of the string in number of 2-byte code units
5914
   * (char16_t).
5915
   * @param output the output buffer.
5916
   */
5917
  virtual void to_well_formed_utf16be(const char16_t *input, size_t len,
5918
                                      char16_t *output) const noexcept = 0;
5919
#endif // SIMDUTF_FEATURE_UTF16
5920
5921
#if SIMDUTF_FEATURE_UTF32 || SIMDUTF_FEATURE_DETECT_ENCODING
5922
  /**
5923
   * Validate the UTF-32 string.
5924
   *
5925
   * Overridden by each implementation.
5926
   *
5927
   * This function is not BOM-aware.
5928
   *
5929
   * @param buf the UTF-32 string to validate.
5930
   * @param len the length of the string in number of 4-byte code units
5931
   * (char32_t).
5932
   * @return true if and only if the string is valid UTF-32.
5933
   */
5934
  simdutf_warn_unused virtual bool
5935
  validate_utf32(const char32_t *buf, size_t len) const noexcept = 0;
5936
#endif // SIMDUTF_FEATURE_UTF32 || SIMDUTF_FEATURE_DETECT_ENCODING
5937
5938
#if SIMDUTF_FEATURE_UTF32
5939
  /**
5940
   * Validate the UTF-32 string and stop on error.
5941
   *
5942
   * Overridden by each implementation.
5943
   *
5944
   * This function is not BOM-aware.
5945
   *
5946
   * @param buf the UTF-32 string to validate.
5947
   * @param len the length of the string in number of 4-byte code units
5948
   * (char32_t).
5949
   * @return a result pair struct (of type simdutf::result containing the two
5950
   * fields error and count) with an error code and either position of the error
5951
   * (in the input in code units) if any, or the number of code units validated
5952
   * if successful.
5953
   */
5954
  simdutf_warn_unused virtual result
5955
  validate_utf32_with_errors(const char32_t *buf,
5956
                             size_t len) const noexcept = 0;
5957
#endif // SIMDUTF_FEATURE_UTF32
5958
5959
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
5960
  /**
5961
   * Convert Latin1 string into UTF-8 string.
5962
   *
5963
   * This function is suitable to work with inputs from untrusted sources.
5964
   *
5965
   * @param input         the Latin1 string to convert
5966
   * @param length        the length of the string in bytes
5967
   * @param utf8_output  the pointer to buffer that can hold conversion result
5968
   * @return the number of written char; 0 if conversion is not possible
5969
   */
5970
  simdutf_warn_unused virtual size_t
5971
  convert_latin1_to_utf8(const char *input, size_t length,
5972
                         char *utf8_output) const noexcept = 0;
5973
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
5974
5975
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
5976
  /**
5977
   * Convert possibly Latin1 string into UTF-16LE string.
5978
   *
5979
   * This function is suitable to work with inputs from untrusted sources.
5980
   *
5981
   * @param input         the Latin1  string to convert
5982
   * @param length        the length of the string in bytes
5983
   * @param utf16_output  the pointer to buffer that can hold conversion result
5984
   * @return the number of written char16_t; 0 if conversion is not possible
5985
   */
5986
  simdutf_warn_unused virtual size_t
5987
  convert_latin1_to_utf16le(const char *input, size_t length,
5988
                            char16_t *utf16_output) const noexcept = 0;
5989
5990
  /**
5991
   * Convert Latin1 string into UTF-16BE string.
5992
   *
5993
   * This function is suitable to work with inputs from untrusted sources.
5994
   *
5995
   * @param input         the Latin1 string to convert
5996
   * @param length        the length of the string in bytes
5997
   * @param utf16_output  the pointer to buffer that can hold conversion result
5998
   * @return the number of written char16_t; 0 if conversion is not possible
5999
   */
6000
  simdutf_warn_unused virtual size_t
6001
  convert_latin1_to_utf16be(const char *input, size_t length,
6002
                            char16_t *utf16_output) const noexcept = 0;
6003
#endif // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
6004
6005
#if SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
6006
  /**
6007
   * Convert Latin1 string into UTF-32 string.
6008
   *
6009
   * This function is suitable to work with inputs from untrusted sources.
6010
   *
6011
   * @param input         the Latin1 string to convert
6012
   * @param length        the length of the string in bytes
6013
   * @param utf32_buffer  the pointer to buffer that can hold conversion result
6014
   * @return the number of written char32_t; 0 if conversion is not possible
6015
   */
6016
  simdutf_warn_unused virtual size_t
6017
  convert_latin1_to_utf32(const char *input, size_t length,
6018
                          char32_t *utf32_buffer) const noexcept = 0;
6019
#endif // SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
6020
6021
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
6022
  /**
6023
   * Convert possibly broken UTF-8 string into latin1 string.
6024
   *
6025
   * During the conversion also validation of the input string is done.
6026
   * This function is suitable to work with inputs from untrusted sources.
6027
   *
6028
   * @param input         the UTF-8 string to convert
6029
   * @param length        the length of the string in bytes
6030
   * @param latin1_output  the pointer to buffer that can hold conversion result
6031
   * @return the number of written char; 0 if the input was not valid UTF-8
6032
   * string or if it cannot be represented as Latin1
6033
   */
6034
  simdutf_warn_unused virtual size_t
6035
  convert_utf8_to_latin1(const char *input, size_t length,
6036
                         char *latin1_output) const noexcept = 0;
6037
6038
  /**
6039
   * Convert possibly broken UTF-8 string into latin1 string with errors.
6040
   * If the string cannot be represented as Latin1, an error
6041
   * code is returned.
6042
   *
6043
   * During the conversion also validation of the input string is done.
6044
   * This function is suitable to work with inputs from untrusted sources.
6045
   *
6046
   * @param input         the UTF-8 string to convert
6047
   * @param length        the length of the string in bytes
6048
   * @param latin1_output  the pointer to buffer that can hold conversion result
6049
   * @return a result pair struct (of type simdutf::result containing the two
6050
   * fields error and count) with an error code and either position of the error
6051
   * (in the input in code units) if any, or the number of bytes written
6052
   * if successful.
6053
   */
6054
  simdutf_warn_unused virtual result
6055
  convert_utf8_to_latin1_with_errors(const char *input, size_t length,
6056
                                     char *latin1_output) const noexcept = 0;
6057
6058
  /**
6059
   * Convert valid UTF-8 string into latin1 string.
6060
   *
6061
   * This function assumes that the input string is valid UTF-8 and that it can
6062
   * be represented as Latin1. If you violate this assumption, the result is
6063
   * implementation defined and may include system-dependent behavior such as
6064
   * crashes.
6065
   *
6066
   * This function is for expert users only and not part of our public API. Use
6067
   * convert_utf8_to_latin1 instead.
6068
   *
6069
   * This function is not BOM-aware.
6070
   *
6071
   * @param input         the UTF-8 string to convert
6072
   * @param length        the length of the string in bytes
6073
   * @param latin1_output  the pointer to buffer that can hold conversion result
6074
   * @return the number of written char; 0 if the input was not valid UTF-8
6075
   * string
6076
   */
6077
  simdutf_warn_unused virtual size_t
6078
  convert_valid_utf8_to_latin1(const char *input, size_t length,
6079
                               char *latin1_output) const noexcept = 0;
6080
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
6081
6082
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
6083
  /**
6084
   * Convert possibly broken UTF-8 string into UTF-16LE string.
6085
   *
6086
   * During the conversion also validation of the input string is done.
6087
   * This function is suitable to work with inputs from untrusted sources.
6088
   *
6089
   * @param input         the UTF-8 string to convert
6090
   * @param length        the length of the string in bytes
6091
   * @param utf16_output  the pointer to buffer that can hold conversion result
6092
   * @return the number of written char16_t; 0 if the input was not valid UTF-8
6093
   * string
6094
   */
6095
  simdutf_warn_unused virtual size_t
6096
  convert_utf8_to_utf16le(const char *input, size_t length,
6097
                          char16_t *utf16_output) const noexcept = 0;
6098
6099
  /**
6100
   * Convert possibly broken UTF-8 string into UTF-16BE string.
6101
   *
6102
   * During the conversion also validation of the input string is done.
6103
   * This function is suitable to work with inputs from untrusted sources.
6104
   *
6105
   * @param input         the UTF-8 string to convert
6106
   * @param length        the length of the string in bytes
6107
   * @param utf16_output  the pointer to buffer that can hold conversion result
6108
   * @return the number of written char16_t; 0 if the input was not valid UTF-8
6109
   * string
6110
   */
6111
  simdutf_warn_unused virtual size_t
6112
  convert_utf8_to_utf16be(const char *input, size_t length,
6113
                          char16_t *utf16_output) const noexcept = 0;
6114
6115
  /**
6116
   * Convert possibly broken UTF-8 string into UTF-16LE string and stop on
6117
   * error.
6118
   *
6119
   * During the conversion also validation of the input string is done.
6120
   * This function is suitable to work with inputs from untrusted sources.
6121
   *
6122
   * @param input         the UTF-8 string to convert
6123
   * @param length        the length of the string in bytes
6124
   * @param utf16_output  the pointer to buffer that can hold conversion result
6125
   * @return a result pair struct (of type simdutf::result containing the two
6126
   * fields error and count) with an error code and either position of the error
6127
   * (in the input in code units) if any, or the number of char16_t written
6128
   * if successful.
6129
   */
6130
  simdutf_warn_unused virtual result convert_utf8_to_utf16le_with_errors(
6131
      const char *input, size_t length,
6132
      char16_t *utf16_output) const noexcept = 0;
6133
6134
  /**
6135
   * Convert possibly broken UTF-8 string into UTF-16BE string and stop on
6136
   * error.
6137
   *
6138
   * During the conversion also validation of the input string is done.
6139
   * This function is suitable to work with inputs from untrusted sources.
6140
   *
6141
   * @param input         the UTF-8 string to convert
6142
   * @param length        the length of the string in bytes
6143
   * @param utf16_output  the pointer to buffer that can hold conversion result
6144
   * @return a result pair struct (of type simdutf::result containing the two
6145
   * fields error and count) with an error code and either position of the error
6146
   * (in the input in code units) if any, or the number of char16_t written
6147
   * if successful.
6148
   */
6149
  simdutf_warn_unused virtual result convert_utf8_to_utf16be_with_errors(
6150
      const char *input, size_t length,
6151
      char16_t *utf16_output) const noexcept = 0;
6152
  /**
6153
   * Compute the number of bytes that this UTF-16LE string would require in
6154
   * UTF-8 format even when the UTF-16LE content contains mismatched
6155
   * surrogates that have to be replaced by the replacement character (0xFFFD).
6156
   *
6157
   * @param input         the UTF-16LE string to convert
6158
   * @param length        the length of the string in 2-byte code units
6159
   * (char16_t)
6160
   * @return a result pair struct (of type simdutf::result containing the two
6161
   * fields error and count) where the count is the number of bytes required to
6162
   * encode the UTF-16LE string as UTF-8, and the error code is either SUCCESS
6163
   * or SURROGATE. The count is correct regardless of the error field.
6164
   * When SURROGATE is returned, it does not indicate an error in the case of
6165
   * this function: it indicates that at least one surrogate has been
6166
   * encountered: the surrogates may be matched or not (thus this function does
6167
   * not validate). If the returned error code is SUCCESS, then the input
6168
   * contains no surrogate, is in the Basic Multilingual Plane, and is
6169
   * necessarily valid.
6170
   */
6171
  virtual simdutf_warn_unused result utf8_length_from_utf16le_with_replacement(
6172
      const char16_t *input, size_t length) const noexcept = 0;
6173
6174
  /**
6175
   * Compute the number of bytes that this UTF-16BE string would require in
6176
   * UTF-8 format even when the UTF-16BE content contains mismatched
6177
   * surrogates that have to be replaced by the replacement character (0xFFFD).
6178
   *
6179
   * @param input         the UTF-16BE string to convert
6180
   * @param length        the length of the string in 2-byte code units
6181
   * (char16_t)
6182
   * @return a result pair struct (of type simdutf::result containing the two
6183
   * fields error and count) where the count is the number of bytes required to
6184
   * encode the UTF-16BE string as UTF-8, and the error code is either SUCCESS
6185
   * or SURROGATE. The count is correct regardless of the error field.
6186
   * When SURROGATE is returned, it does not indicate an error in the case of
6187
   * this function: it indicates that at least one surrogate has been
6188
   * encountered: the surrogates may be matched or not (thus this function does
6189
   * not validate). If the returned error code is SUCCESS, then the input
6190
   * contains no surrogate, is in the Basic Multilingual Plane, and is
6191
   * necessarily valid.
6192
   */
6193
  virtual simdutf_warn_unused result utf8_length_from_utf16be_with_replacement(
6194
      const char16_t *input, size_t length) const noexcept = 0;
6195
6196
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
6197
6198
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
6199
  /**
6200
   * Convert possibly broken UTF-8 string into UTF-32 string.
6201
   *
6202
   * During the conversion also validation of the input string is done.
6203
   * This function is suitable to work with inputs from untrusted sources.
6204
   *
6205
   * @param input         the UTF-8 string to convert
6206
   * @param length        the length of the string in bytes
6207
   * @param utf32_output  the pointer to buffer that can hold conversion result
6208
   * @return the number of written char16_t; 0 if the input was not valid UTF-8
6209
   * string
6210
   */
6211
  simdutf_warn_unused virtual size_t
6212
  convert_utf8_to_utf32(const char *input, size_t length,
6213
                        char32_t *utf32_output) const noexcept = 0;
6214
6215
  /**
6216
   * Convert possibly broken UTF-8 string into UTF-32 string and stop on error.
6217
   *
6218
   * During the conversion also validation of the input string is done.
6219
   * This function is suitable to work with inputs from untrusted sources.
6220
   *
6221
   * @param input         the UTF-8 string to convert
6222
   * @param length        the length of the string in bytes
6223
   * @param utf32_output  the pointer to buffer that can hold conversion result
6224
   * @return a result pair struct (of type simdutf::result containing the two
6225
   * fields error and count) with an error code and either position of the error
6226
   * (in the input in code units) if any, or the number of char32_t written if
6227
   * successful.
6228
   */
6229
  simdutf_warn_unused virtual result
6230
  convert_utf8_to_utf32_with_errors(const char *input, size_t length,
6231
                                    char32_t *utf32_output) const noexcept = 0;
6232
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
6233
6234
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
6235
  /**
6236
   * Convert valid UTF-8 string into UTF-16LE string.
6237
   *
6238
   * This function assumes that the input string is valid UTF-8.
6239
   *
6240
   * @param input         the UTF-8 string to convert
6241
   * @param length        the length of the string in bytes
6242
   * @param utf16_buffer  the pointer to buffer that can hold conversion result
6243
   * @return the number of written char16_t
6244
   */
6245
  simdutf_warn_unused virtual size_t
6246
  convert_valid_utf8_to_utf16le(const char *input, size_t length,
6247
                                char16_t *utf16_buffer) const noexcept = 0;
6248
6249
  /**
6250
   * Convert valid UTF-8 string into UTF-16BE string.
6251
   *
6252
   * This function assumes that the input string is valid UTF-8.
6253
   *
6254
   * @param input         the UTF-8 string to convert
6255
   * @param length        the length of the string in bytes
6256
   * @param utf16_buffer  the pointer to buffer that can hold conversion result
6257
   * @return the number of written char16_t
6258
   */
6259
  simdutf_warn_unused virtual size_t
6260
  convert_valid_utf8_to_utf16be(const char *input, size_t length,
6261
                                char16_t *utf16_buffer) const noexcept = 0;
6262
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
6263
6264
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
6265
  /**
6266
   * Convert valid UTF-8 string into UTF-32 string.
6267
   *
6268
   * This function assumes that the input string is valid UTF-8.
6269
   *
6270
   * @param input         the UTF-8 string to convert
6271
   * @param length        the length of the string in bytes
6272
   * @param utf32_buffer  the pointer to buffer that can hold conversion result
6273
   * @return the number of written char32_t
6274
   */
6275
  simdutf_warn_unused virtual size_t
6276
  convert_valid_utf8_to_utf32(const char *input, size_t length,
6277
                              char32_t *utf32_buffer) const noexcept = 0;
6278
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
6279
6280
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
6281
  /**
6282
   * Compute the number of 2-byte code units that this UTF-8 string would
6283
   * require in UTF-16LE format.
6284
   *
6285
   * This function does not validate the input. It is acceptable to pass invalid
6286
   * UTF-8 strings. The result is then implementation defined, but it remains a
6287
   * safe buffer size for convert_utf8_to_utf16le, convert_utf8_to_utf16be and
6288
   * their _with_errors variants, even when they reject the input.
6289
   *
6290
   * @param input         the UTF-8 string to process
6291
   * @param length        the length of the string in bytes
6292
   * @return the number of char16_t code units required to encode the UTF-8
6293
   * string as UTF-16LE
6294
   */
6295
  simdutf_warn_unused virtual size_t
6296
  utf16_length_from_utf8(const char *input, size_t length) const noexcept = 0;
6297
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
6298
6299
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
6300
  /**
6301
   * Compute the number of 4-byte code units that this UTF-8 string would
6302
   * require in UTF-32 format.
6303
   *
6304
   * This function is equivalent to count_utf8. It is acceptable to pass invalid
6305
   * UTF-8 strings. The result is then implementation defined, but it remains a
6306
   * safe buffer size for convert_utf8_to_utf32 and
6307
   * convert_utf8_to_utf32_with_errors, even when they reject the input.
6308
   *
6309
   * This function does not validate the input.
6310
   *
6311
   * @param input         the UTF-8 string to process
6312
   * @param length        the length of the string in bytes
6313
   * @return the number of char32_t code units required to encode the UTF-8
6314
   * string as UTF-32
6315
   */
6316
  simdutf_warn_unused virtual size_t
6317
  utf32_length_from_utf8(const char *input, size_t length) const noexcept = 0;
6318
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
6319
6320
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
6321
  /**
6322
   * Convert possibly broken UTF-16LE string into Latin1 string.
6323
   *
6324
   * During the conversion also validation of the input string is done.
6325
   * This function is suitable to work with inputs from untrusted sources.
6326
   *
6327
   * This function is not BOM-aware.
6328
   *
6329
   * @param input         the UTF-16LE string to convert
6330
   * @param length        the length of the string in 2-byte code units
6331
   * (char16_t)
6332
   * @param latin1_buffer   the pointer to buffer that can hold conversion
6333
   * result
6334
   * @return number of written code units; 0 if input is not a valid UTF-16LE
6335
   * string or if it cannot be represented as Latin1
6336
   */
6337
  simdutf_warn_unused virtual size_t
6338
  convert_utf16le_to_latin1(const char16_t *input, size_t length,
6339
                            char *latin1_buffer) const noexcept = 0;
6340
6341
  /**
6342
   * Convert possibly broken UTF-16BE string into Latin1 string.
6343
   *
6344
   * During the conversion also validation of the input string is done.
6345
   * This function is suitable to work with inputs from untrusted sources.
6346
   *
6347
   * This function is not BOM-aware.
6348
   *
6349
   * @param input         the UTF-16BE string to convert
6350
   * @param length        the length of the string in 2-byte code units
6351
   * (char16_t)
6352
   * @param latin1_buffer   the pointer to buffer that can hold conversion
6353
   * result
6354
   * @return number of written code units; 0 if input is not a valid UTF-16BE
6355
   * string or if it cannot be represented as Latin1
6356
   */
6357
  simdutf_warn_unused virtual size_t
6358
  convert_utf16be_to_latin1(const char16_t *input, size_t length,
6359
                            char *latin1_buffer) const noexcept = 0;
6360
6361
  /**
6362
   * Convert possibly broken UTF-16LE string into Latin1 string.
6363
   * If the string cannot be represented as Latin1, an error
6364
   * is returned.
6365
   *
6366
   * During the conversion also validation of the input string is done.
6367
   * This function is suitable to work with inputs from untrusted sources.
6368
   * This function is not BOM-aware.
6369
   *
6370
   * @param input         the UTF-16LE string to convert
6371
   * @param length        the length of the string in 2-byte code units
6372
   * (char16_t)
6373
   * @param latin1_buffer   the pointer to buffer that can hold conversion
6374
   * result
6375
   * @return a result pair struct (of type simdutf::result containing the two
6376
   * fields error and count) with an error code and either position of the error
6377
   * (in the input in code units) if any, or the number of char written if
6378
   * successful.
6379
   */
6380
  simdutf_warn_unused virtual result
6381
  convert_utf16le_to_latin1_with_errors(const char16_t *input, size_t length,
6382
                                        char *latin1_buffer) const noexcept = 0;
6383
6384
  /**
6385
   * Convert possibly broken UTF-16BE string into Latin1 string.
6386
   * If the string cannot be represented as Latin1, an error
6387
   * is returned.
6388
   *
6389
   * During the conversion also validation of the input string is done.
6390
   * This function is suitable to work with inputs from untrusted sources.
6391
   * This function is not BOM-aware.
6392
   *
6393
   * @param input         the UTF-16BE string to convert
6394
   * @param length        the length of the string in 2-byte code units
6395
   * (char16_t)
6396
   * @param latin1_buffer   the pointer to buffer that can hold conversion
6397
   * result
6398
   * @return a result pair struct (of type simdutf::result containing the two
6399
   * fields error and count) with an error code and either position of the error
6400
   * (in the input in code units) if any, or the number of char written if
6401
   * successful.
6402
   */
6403
  simdutf_warn_unused virtual result
6404
  convert_utf16be_to_latin1_with_errors(const char16_t *input, size_t length,
6405
                                        char *latin1_buffer) const noexcept = 0;
6406
6407
  /**
6408
   * Convert valid UTF-16LE string into Latin1 string.
6409
   *
6410
   * This function assumes that the input string is valid UTF-L16LE and that it
6411
   * can be represented as Latin1. If you violate this assumption, the result is
6412
   * implementation defined and may include system-dependent behavior such as
6413
   * crashes.
6414
   *
6415
   * This function is for expert users only and not part of our public API. Use
6416
   * convert_utf16le_to_latin1 instead.
6417
   *
6418
   * This function is not BOM-aware.
6419
   *
6420
   * @param input         the UTF-16LE string to convert
6421
   * @param length        the length of the string in 2-byte code units
6422
   * (char16_t)
6423
   * @param latin1_buffer   the pointer to buffer that can hold conversion
6424
   * result
6425
   * @return number of written code units; 0 if conversion is not possible
6426
   */
6427
  simdutf_warn_unused virtual size_t
6428
  convert_valid_utf16le_to_latin1(const char16_t *input, size_t length,
6429
                                  char *latin1_buffer) const noexcept = 0;
6430
6431
  /**
6432
   * Convert valid UTF-16BE string into Latin1 string.
6433
   *
6434
   * This function assumes that the input string is valid UTF16-BE and that it
6435
   * can be represented as Latin1. If you violate this assumption, the result is
6436
   * implementation defined and may include system-dependent behavior such as
6437
   * crashes.
6438
   *
6439
   * This function is for expert users only and not part of our public API. Use
6440
   * convert_utf16be_to_latin1 instead.
6441
   *
6442
   * This function is not BOM-aware.
6443
   *
6444
   * @param input         the UTF-16BE string to convert
6445
   * @param length        the length of the string in 2-byte code units
6446
   * (char16_t)
6447
   * @param latin1_buffer   the pointer to buffer that can hold conversion
6448
   * result
6449
   * @return number of written code units; 0 if conversion is not possible
6450
   */
6451
  simdutf_warn_unused virtual size_t
6452
  convert_valid_utf16be_to_latin1(const char16_t *input, size_t length,
6453
                                  char *latin1_buffer) const noexcept = 0;
6454
#endif // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
6455
6456
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
6457
  /**
6458
   * Convert possibly broken UTF-16LE string into UTF-8 string.
6459
   *
6460
   * During the conversion also validation of the input string is done.
6461
   * This function is suitable to work with inputs from untrusted sources.
6462
   *
6463
   * This function is not BOM-aware.
6464
   *
6465
   * @param input         the UTF-16LE string to convert
6466
   * @param length        the length of the string in 2-byte code units
6467
   * (char16_t)
6468
   * @param utf8_buffer   the pointer to buffer that can hold conversion result
6469
   * @return number of written code units; 0 if input is not a valid UTF-16LE
6470
   * string
6471
   */
6472
  simdutf_warn_unused virtual size_t
6473
  convert_utf16le_to_utf8(const char16_t *input, size_t length,
6474
                          char *utf8_buffer) const noexcept = 0;
6475
6476
  /**
6477
   * Convert possibly broken UTF-16BE string into UTF-8 string.
6478
   *
6479
   * During the conversion also validation of the input string is done.
6480
   * This function is suitable to work with inputs from untrusted sources.
6481
   *
6482
   * This function is not BOM-aware.
6483
   *
6484
   * @param input         the UTF-16BE string to convert
6485
   * @param length        the length of the string in 2-byte code units
6486
   * (char16_t)
6487
   * @param utf8_buffer   the pointer to buffer that can hold conversion result
6488
   * @return number of written code units; 0 if input is not a valid UTF-16BE
6489
   * string
6490
   */
6491
  simdutf_warn_unused virtual size_t
6492
  convert_utf16be_to_utf8(const char16_t *input, size_t length,
6493
                          char *utf8_buffer) const noexcept = 0;
6494
6495
  /**
6496
   * Convert possibly broken UTF-16LE string into UTF-8 string and stop on
6497
   * error.
6498
   *
6499
   * During the conversion also validation of the input string is done.
6500
   * This function is suitable to work with inputs from untrusted sources.
6501
   *
6502
   * This function is not BOM-aware.
6503
   *
6504
   * @param input         the UTF-16LE string to convert
6505
   * @param length        the length of the string in 2-byte code units
6506
   * (char16_t)
6507
   * @param utf8_buffer   the pointer to buffer that can hold conversion result
6508
   * @return a result pair struct (of type simdutf::result containing the two
6509
   * fields error and count) with an error code and either position of the error
6510
   * (in the input in code units) if any, or the number of char written if
6511
   * successful.
6512
   */
6513
  simdutf_warn_unused virtual result
6514
  convert_utf16le_to_utf8_with_errors(const char16_t *input, size_t length,
6515
                                      char *utf8_buffer) const noexcept = 0;
6516
6517
  /**
6518
   * Convert possibly broken UTF-16BE string into UTF-8 string and stop on
6519
   * error.
6520
   *
6521
   * During the conversion also validation of the input string is done.
6522
   * This function is suitable to work with inputs from untrusted sources.
6523
   *
6524
   * This function is not BOM-aware.
6525
   *
6526
   * @param input         the UTF-16BE string to convert
6527
   * @param length        the length of the string in 2-byte code units
6528
   * (char16_t)
6529
   * @param utf8_buffer   the pointer to buffer that can hold conversion result
6530
   * @return a result pair struct (of type simdutf::result containing the two
6531
   * fields error and count) with an error code and either position of the error
6532
   * (in the input in code units) if any, or the number of char written if
6533
   * successful.
6534
   */
6535
  simdutf_warn_unused virtual result
6536
  convert_utf16be_to_utf8_with_errors(const char16_t *input, size_t length,
6537
                                      char *utf8_buffer) const noexcept = 0;
6538
6539
  /**
6540
   * Convert possibly broken UTF-16LE string into UTF-8 string, replacing
6541
   * unpaired surrogates with the Unicode replacement character U+FFFD.
6542
   *
6543
   * This function always succeeds: unpaired surrogates are replaced with
6544
   * U+FFFD (3 bytes in UTF-8: 0xEF 0xBF 0xBD).
6545
   *
6546
   * This function is not BOM-aware.
6547
   *
6548
   * @param input         the UTF-16LE string to convert
6549
   * @param length        the length of the string in 2-byte code units
6550
   * (char16_t)
6551
   * @param utf8_buffer   the pointer to buffer that can hold conversion result
6552
   * @return number of written code units
6553
   */
6554
  simdutf_warn_unused virtual size_t convert_utf16le_to_utf8_with_replacement(
6555
      const char16_t *input, size_t length,
6556
      char *utf8_buffer) const noexcept = 0;
6557
6558
  /**
6559
   * Convert possibly broken UTF-16BE string into UTF-8 string, replacing
6560
   * unpaired surrogates with the Unicode replacement character U+FFFD.
6561
   *
6562
   * This function always succeeds: unpaired surrogates are replaced with
6563
   * U+FFFD (3 bytes in UTF-8: 0xEF 0xBF 0xBD).
6564
   *
6565
   * This function is not BOM-aware.
6566
   *
6567
   * @param input         the UTF-16BE string to convert
6568
   * @param length        the length of the string in 2-byte code units
6569
   * (char16_t)
6570
   * @param utf8_buffer   the pointer to buffer that can hold conversion result
6571
   * @return number of written code units
6572
   */
6573
  simdutf_warn_unused virtual size_t convert_utf16be_to_utf8_with_replacement(
6574
      const char16_t *input, size_t length,
6575
      char *utf8_buffer) const noexcept = 0;
6576
6577
  /**
6578
   * Convert valid UTF-16LE string into UTF-8 string.
6579
   *
6580
   * This function assumes that the input string is valid UTF-16LE.
6581
   *
6582
   * This function is not BOM-aware.
6583
   *
6584
   * @param input         the UTF-16LE string to convert
6585
   * @param length        the length of the string in 2-byte code units
6586
   * (char16_t)
6587
   * @param utf8_buffer   the pointer to a buffer that can hold the conversion
6588
   * result
6589
   * @return number of written code units; 0 if conversion is not possible
6590
   */
6591
  simdutf_warn_unused virtual size_t
6592
  convert_valid_utf16le_to_utf8(const char16_t *input, size_t length,
6593
                                char *utf8_buffer) const noexcept = 0;
6594
6595
  /**
6596
   * Convert valid UTF-16BE string into UTF-8 string.
6597
   *
6598
   * This function assumes that the input string is valid UTF-16BE.
6599
   *
6600
   * This function is not BOM-aware.
6601
   *
6602
   * @param input         the UTF-16BE string to convert
6603
   * @param length        the length of the string in 2-byte code units
6604
   * (char16_t)
6605
   * @param utf8_buffer   the pointer to a buffer that can hold the conversion
6606
   * result
6607
   * @return number of written code units; 0 if conversion is not possible
6608
   */
6609
  simdutf_warn_unused virtual size_t
6610
  convert_valid_utf16be_to_utf8(const char16_t *input, size_t length,
6611
                                char *utf8_buffer) const noexcept = 0;
6612
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
6613
6614
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
6615
  /**
6616
   * Convert possibly broken UTF-16LE string into UTF-32 string.
6617
   *
6618
   * During the conversion also validation of the input string is done.
6619
   * This function is suitable to work with inputs from untrusted sources.
6620
   *
6621
   * This function is not BOM-aware.
6622
   *
6623
   * @param input         the UTF-16LE string to convert
6624
   * @param length        the length of the string in 2-byte code units
6625
   * (char16_t)
6626
   * @param utf32_buffer   the pointer to buffer that can hold conversion result
6627
   * @return number of written code units; 0 if input is not a valid UTF-16LE
6628
   * string
6629
   */
6630
  simdutf_warn_unused virtual size_t
6631
  convert_utf16le_to_utf32(const char16_t *input, size_t length,
6632
                           char32_t *utf32_buffer) const noexcept = 0;
6633
6634
  /**
6635
   * Convert possibly broken UTF-16BE string into UTF-32 string.
6636
   *
6637
   * During the conversion also validation of the input string is done.
6638
   * This function is suitable to work with inputs from untrusted sources.
6639
   *
6640
   * This function is not BOM-aware.
6641
   *
6642
   * @param input         the UTF-16BE string to convert
6643
   * @param length        the length of the string in 2-byte code units
6644
   * (char16_t)
6645
   * @param utf32_buffer   the pointer to buffer that can hold conversion result
6646
   * @return number of written code units; 0 if input is not a valid UTF-16BE
6647
   * string
6648
   */
6649
  simdutf_warn_unused virtual size_t
6650
  convert_utf16be_to_utf32(const char16_t *input, size_t length,
6651
                           char32_t *utf32_buffer) const noexcept = 0;
6652
6653
  /**
6654
   * Convert possibly broken UTF-16LE string into UTF-32 string and stop on
6655
   * error.
6656
   *
6657
   * During the conversion also validation of the input string is done.
6658
   * This function is suitable to work with inputs from untrusted sources.
6659
   *
6660
   * This function is not BOM-aware.
6661
   *
6662
   * @param input         the UTF-16LE string to convert
6663
   * @param length        the length of the string in 2-byte code units
6664
   * (char16_t)
6665
   * @param utf32_buffer   the pointer to buffer that can hold conversion result
6666
   * @return a result pair struct (of type simdutf::result containing the two
6667
   * fields error and count) with an error code and either position of the error
6668
   * (in the input in code units) if any, or the number of char32_t written if
6669
   * successful.
6670
   */
6671
  simdutf_warn_unused virtual result convert_utf16le_to_utf32_with_errors(
6672
      const char16_t *input, size_t length,
6673
      char32_t *utf32_buffer) const noexcept = 0;
6674
6675
  /**
6676
   * Convert possibly broken UTF-16BE string into UTF-32 string and stop on
6677
   * error.
6678
   *
6679
   * During the conversion also validation of the input string is done.
6680
   * This function is suitable to work with inputs from untrusted sources.
6681
   *
6682
   * This function is not BOM-aware.
6683
   *
6684
   * @param input         the UTF-16BE string to convert
6685
   * @param length        the length of the string in 2-byte code units
6686
   * (char16_t)
6687
   * @param utf32_buffer   the pointer to buffer that can hold conversion result
6688
   * @return a result pair struct (of type simdutf::result containing the two
6689
   * fields error and count) with an error code and either position of the error
6690
   * (in the input in code units) if any, or the number of char32_t written if
6691
   * successful.
6692
   */
6693
  simdutf_warn_unused virtual result convert_utf16be_to_utf32_with_errors(
6694
      const char16_t *input, size_t length,
6695
      char32_t *utf32_buffer) const noexcept = 0;
6696
6697
  /**
6698
   * Convert valid UTF-16LE string into UTF-32 string.
6699
   *
6700
   * This function assumes that the input string is valid UTF-16LE.
6701
   *
6702
   * This function is not BOM-aware.
6703
   *
6704
   * @param input         the UTF-16LE string to convert
6705
   * @param length        the length of the string in 2-byte code units
6706
   * (char16_t)
6707
   * @param utf32_buffer   the pointer to a buffer that can hold the conversion
6708
   * result
6709
   * @return number of written code units; 0 if conversion is not possible
6710
   */
6711
  simdutf_warn_unused virtual size_t
6712
  convert_valid_utf16le_to_utf32(const char16_t *input, size_t length,
6713
                                 char32_t *utf32_buffer) const noexcept = 0;
6714
6715
  /**
6716
   * Convert valid UTF-16LE string into UTF-32BE string.
6717
   *
6718
   * This function assumes that the input string is valid UTF-16BE.
6719
   *
6720
   * This function is not BOM-aware.
6721
   *
6722
   * @param input         the UTF-16BE string to convert
6723
   * @param length        the length of the string in 2-byte code units
6724
   * (char16_t)
6725
   * @param utf32_buffer   the pointer to a buffer that can hold the conversion
6726
   * result
6727
   * @return number of written code units; 0 if conversion is not possible
6728
   */
6729
  simdutf_warn_unused virtual size_t
6730
  convert_valid_utf16be_to_utf32(const char16_t *input, size_t length,
6731
                                 char32_t *utf32_buffer) const noexcept = 0;
6732
#endif // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
6733
6734
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
6735
  /**
6736
   * Compute the number of bytes that this UTF-16LE string would require in
6737
   * UTF-8 format.
6738
   *
6739
   * This function does not validate the input. It is acceptable to pass invalid
6740
   * UTF-16 strings. The result is then implementation defined, but it remains a
6741
   * safe buffer size: convert_utf16le_to_utf8 and
6742
   * convert_utf16le_to_utf8_with_errors never write more bytes than this
6743
   * function returns, even when they reject the input.
6744
   *
6745
   * This function is not BOM-aware.
6746
   *
6747
   * @param input         the UTF-16LE string to convert
6748
   * @param length        the length of the string in 2-byte code units
6749
   * (char16_t)
6750
   * @return the number of bytes required to encode the UTF-16LE string as UTF-8
6751
   */
6752
  simdutf_warn_unused virtual size_t
6753
  utf8_length_from_utf16le(const char16_t *input,
6754
                           size_t length) const noexcept = 0;
6755
6756
  /**
6757
   * Compute the number of bytes that this UTF-16BE string would require in
6758
   * UTF-8 format.
6759
   *
6760
   * This function does not validate the input. It is acceptable to pass invalid
6761
   * UTF-16 strings. The result is then implementation defined, but it remains a
6762
   * safe buffer size: convert_utf16be_to_utf8 and
6763
   * convert_utf16be_to_utf8_with_errors never write more bytes than this
6764
   * function returns, even when they reject the input.
6765
   *
6766
   * This function is not BOM-aware.
6767
   *
6768
   * @param input         the UTF-16BE string to convert
6769
   * @param length        the length of the string in 2-byte code units
6770
   * (char16_t)
6771
   * @return the number of bytes required to encode the UTF-16BE string as UTF-8
6772
   */
6773
  simdutf_warn_unused virtual size_t
6774
  utf8_length_from_utf16be(const char16_t *input,
6775
                           size_t length) const noexcept = 0;
6776
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF16
6777
6778
#if SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
6779
  /**
6780
   * Convert possibly broken UTF-32 string into Latin1 string.
6781
   *
6782
   * During the conversion also validation of the input string is done.
6783
   * This function is suitable to work with inputs from untrusted sources.
6784
   *
6785
   * This function is not BOM-aware.
6786
   *
6787
   * @param input         the UTF-32 string to convert
6788
   * @param length        the length of the string in 4-byte code units
6789
   * (char32_t)
6790
   * @param latin1_buffer   the pointer to buffer that can hold conversion
6791
   * result
6792
   * @return number of written code units; 0 if input is not a valid UTF-32
6793
   * string
6794
   */
6795
  simdutf_warn_unused virtual size_t
6796
  convert_utf32_to_latin1(const char32_t *input, size_t length,
6797
                          char *latin1_buffer) const noexcept = 0;
6798
#endif // SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
6799
6800
#if SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
6801
  /**
6802
   * Convert possibly broken UTF-32 string into Latin1 string and stop on error.
6803
   * If the string cannot be represented as Latin1, an error is returned.
6804
   *
6805
   * During the conversion also validation of the input string is done.
6806
   * This function is suitable to work with inputs from untrusted sources.
6807
   *
6808
   * This function is not BOM-aware.
6809
   *
6810
   * @param input         the UTF-32 string to convert
6811
   * @param length        the length of the string in 4-byte code units
6812
   * (char32_t)
6813
   * @param latin1_buffer   the pointer to buffer that can hold conversion
6814
   * result
6815
   * @return a result pair struct (of type simdutf::result containing the two
6816
   * fields error and count) with an error code and either position of the error
6817
   * (in the input in code units) if any, or the number of char written if
6818
   * successful.
6819
   */
6820
  simdutf_warn_unused virtual result
6821
  convert_utf32_to_latin1_with_errors(const char32_t *input, size_t length,
6822
                                      char *latin1_buffer) const noexcept = 0;
6823
6824
  /**
6825
   * Convert valid UTF-32 string into Latin1 string.
6826
   *
6827
   * This function assumes that the input string is valid UTF-32 and can be
6828
   * represented as Latin1. If you violate this assumption, the result is
6829
   * implementation defined and may include system-dependent behavior such as
6830
   * crashes.
6831
   *
6832
   * This function is for expert users only and not part of our public API. Use
6833
   * convert_utf32_to_latin1 instead.
6834
   *
6835
   * This function is not BOM-aware.
6836
   *
6837
   * @param input         the UTF-32 string to convert
6838
   * @param length        the length of the string in 4-byte code units
6839
   * (char32_t)
6840
   * @param latin1_buffer   the pointer to a buffer that can hold the conversion
6841
   * result
6842
   * @return number of written code units; 0 if conversion is not possible
6843
   */
6844
  simdutf_warn_unused virtual size_t
6845
  convert_valid_utf32_to_latin1(const char32_t *input, size_t length,
6846
                                char *latin1_buffer) const noexcept = 0;
6847
#endif // SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
6848
6849
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
6850
  /**
6851
   * Convert possibly broken UTF-32 string into UTF-8 string.
6852
   *
6853
   * During the conversion also validation of the input string is done.
6854
   * This function is suitable to work with inputs from untrusted sources.
6855
   *
6856
   * This function is not BOM-aware.
6857
   *
6858
   * @param input         the UTF-32 string to convert
6859
   * @param length        the length of the string in 4-byte code units
6860
   * (char32_t)
6861
   * @param utf8_buffer   the pointer to buffer that can hold conversion result
6862
   * @return number of written code units; 0 if input is not a valid UTF-32
6863
   * string
6864
   */
6865
  simdutf_warn_unused virtual size_t
6866
  convert_utf32_to_utf8(const char32_t *input, size_t length,
6867
                        char *utf8_buffer) const noexcept = 0;
6868
6869
  /**
6870
   * Convert possibly broken UTF-32 string into UTF-8 string and stop on error.
6871
   *
6872
   * During the conversion also validation of the input string is done.
6873
   * This function is suitable to work with inputs from untrusted sources.
6874
   *
6875
   * This function is not BOM-aware.
6876
   *
6877
   * @param input         the UTF-32 string to convert
6878
   * @param length        the length of the string in 4-byte code units
6879
   * (char32_t)
6880
   * @param utf8_buffer   the pointer to buffer that can hold conversion result
6881
   * @return a result pair struct (of type simdutf::result containing the two
6882
   * fields error and count) with an error code and either position of the error
6883
   * (in the input in code units) if any, or the number of char written if
6884
   * successful.
6885
   */
6886
  simdutf_warn_unused virtual result
6887
  convert_utf32_to_utf8_with_errors(const char32_t *input, size_t length,
6888
                                    char *utf8_buffer) const noexcept = 0;
6889
6890
  /**
6891
   * Convert valid UTF-32 string into UTF-8 string.
6892
   *
6893
   * This function assumes that the input string is valid UTF-32.
6894
   *
6895
   * This function is not BOM-aware.
6896
   *
6897
   * @param input         the UTF-32 string to convert
6898
   * @param length        the length of the string in 4-byte code units
6899
   * (char32_t)
6900
   * @param utf8_buffer   the pointer to a buffer that can hold the conversion
6901
   * result
6902
   * @return number of written code units; 0 if conversion is not possible
6903
   */
6904
  simdutf_warn_unused virtual size_t
6905
  convert_valid_utf32_to_utf8(const char32_t *input, size_t length,
6906
                              char *utf8_buffer) const noexcept = 0;
6907
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
6908
6909
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
6910
  /**
6911
   * Return the number of bytes that this UTF-16 string would require in Latin1
6912
   * format.
6913
   *
6914
   *
6915
   * @param length        the length of the string in 2-byte code units
6916
   * (char16_t)
6917
   * @return the number of bytes required to encode the UTF-16 string as Latin1
6918
   */
6919
  simdutf_warn_unused virtual size_t
6920
  utf16_length_from_latin1(size_t length) const noexcept {
6921
    return length;
6922
  }
6923
#endif // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
6924
6925
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
6926
  /**
6927
   * Convert possibly broken UTF-32 string into UTF-16LE string.
6928
   *
6929
   * During the conversion also validation of the input string is done.
6930
   * This function is suitable to work with inputs from untrusted sources.
6931
   *
6932
   * This function is not BOM-aware.
6933
   *
6934
   * @param input         the UTF-32 string to convert
6935
   * @param length        the length of the string in 4-byte code units
6936
   * (char32_t)
6937
   * @param utf16_buffer   the pointer to buffer that can hold conversion result
6938
   * @return number of written code units; 0 if input is not a valid UTF-32
6939
   * string
6940
   */
6941
  simdutf_warn_unused virtual size_t
6942
  convert_utf32_to_utf16le(const char32_t *input, size_t length,
6943
                           char16_t *utf16_buffer) const noexcept = 0;
6944
6945
  /**
6946
   * Convert possibly broken UTF-32 string into UTF-16BE string.
6947
   *
6948
   * During the conversion also validation of the input string is done.
6949
   * This function is suitable to work with inputs from untrusted sources.
6950
   *
6951
   * This function is not BOM-aware.
6952
   *
6953
   * @param input         the UTF-32 string to convert
6954
   * @param length        the length of the string in 4-byte code units
6955
   * (char32_t)
6956
   * @param utf16_buffer   the pointer to buffer that can hold conversion result
6957
   * @return number of written code units; 0 if input is not a valid UTF-32
6958
   * string
6959
   */
6960
  simdutf_warn_unused virtual size_t
6961
  convert_utf32_to_utf16be(const char32_t *input, size_t length,
6962
                           char16_t *utf16_buffer) const noexcept = 0;
6963
6964
  /**
6965
   * Convert possibly broken UTF-32 string into UTF-16LE string and stop on
6966
   * error.
6967
   *
6968
   * During the conversion also validation of the input string is done.
6969
   * This function is suitable to work with inputs from untrusted sources.
6970
   *
6971
   * This function is not BOM-aware.
6972
   *
6973
   * @param input         the UTF-32 string to convert
6974
   * @param length        the length of the string in 4-byte code units
6975
   * (char32_t)
6976
   * @param utf16_buffer   the pointer to buffer that can hold conversion result
6977
   * @return a result pair struct (of type simdutf::result containing the two
6978
   * fields error and count) with an error code and either position of the error
6979
   * (in the input in code units) if any, or the number of char16_t written if
6980
   * successful.
6981
   */
6982
  simdutf_warn_unused virtual result convert_utf32_to_utf16le_with_errors(
6983
      const char32_t *input, size_t length,
6984
      char16_t *utf16_buffer) const noexcept = 0;
6985
6986
  /**
6987
   * Convert possibly broken UTF-32 string into UTF-16BE string and stop on
6988
   * error.
6989
   *
6990
   * During the conversion also validation of the input string is done.
6991
   * This function is suitable to work with inputs from untrusted sources.
6992
   *
6993
   * This function is not BOM-aware.
6994
   *
6995
   * @param input         the UTF-32 string to convert
6996
   * @param length        the length of the string in 4-byte code units
6997
   * (char32_t)
6998
   * @param utf16_buffer   the pointer to buffer that can hold conversion result
6999
   * @return a result pair struct (of type simdutf::result containing the two
7000
   * fields error and count) with an error code and either position of the error
7001
   * (in the input in code units) if any, or the number of char16_t written if
7002
   * successful.
7003
   */
7004
  simdutf_warn_unused virtual result convert_utf32_to_utf16be_with_errors(
7005
      const char32_t *input, size_t length,
7006
      char16_t *utf16_buffer) const noexcept = 0;
7007
7008
  /**
7009
   * Convert valid UTF-32 string into UTF-16LE string.
7010
   *
7011
   * This function assumes that the input string is valid UTF-32.
7012
   *
7013
   * This function is not BOM-aware.
7014
   *
7015
   * @param input         the UTF-32 string to convert
7016
   * @param length        the length of the string in 4-byte code units
7017
   * (char32_t)
7018
   * @param utf16_buffer   the pointer to a buffer that can hold the conversion
7019
   * result
7020
   * @return number of written code units; 0 if conversion is not possible
7021
   */
7022
  simdutf_warn_unused virtual size_t
7023
  convert_valid_utf32_to_utf16le(const char32_t *input, size_t length,
7024
                                 char16_t *utf16_buffer) const noexcept = 0;
7025
7026
  /**
7027
   * Convert valid UTF-32 string into UTF-16BE string.
7028
   *
7029
   * This function assumes that the input string is valid UTF-32.
7030
   *
7031
   * This function is not BOM-aware.
7032
   *
7033
   * @param input         the UTF-32 string to convert
7034
   * @param length        the length of the string in 4-byte code units
7035
   * (char32_t)
7036
   * @param utf16_buffer   the pointer to a buffer that can hold the conversion
7037
   * result
7038
   * @return number of written code units; 0 if conversion is not possible
7039
   */
7040
  simdutf_warn_unused virtual size_t
7041
  convert_valid_utf32_to_utf16be(const char32_t *input, size_t length,
7042
                                 char16_t *utf16_buffer) const noexcept = 0;
7043
#endif // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
7044
7045
#if SIMDUTF_FEATURE_UTF16
7046
  /**
7047
   * Change the endianness of the input. Can be used to go from UTF-16LE to
7048
   * UTF-16BE or from UTF-16BE to UTF-16LE.
7049
   *
7050
   * This function does not validate the input.
7051
   *
7052
   * This function is not BOM-aware.
7053
   *
7054
   * @param input         the UTF-16 string to process
7055
   * @param length        the length of the string in 2-byte code units
7056
   * (char16_t)
7057
   * @param output        the pointer to a buffer that can hold the conversion
7058
   * result
7059
   */
7060
  virtual void change_endianness_utf16(const char16_t *input, size_t length,
7061
                                       char16_t *output) const noexcept = 0;
7062
#endif // SIMDUTF_FEATURE_UTF16
7063
7064
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
7065
  /**
7066
   * Return the number of bytes that this Latin1 string would require in UTF-8
7067
   * format.
7068
   *
7069
   * @param input         the Latin1 string to convert
7070
   * @param length        the length of the string bytes
7071
   * @return the number of bytes required to encode the Latin1 string as UTF-8
7072
   */
7073
  simdutf_warn_unused virtual size_t
7074
  utf8_length_from_latin1(const char *input, size_t length) const noexcept = 0;
7075
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
7076
7077
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
7078
  /**
7079
   * Compute the number of bytes that this UTF-32 string would require in UTF-8
7080
   * format.
7081
   *
7082
   * This function does not validate the input. It is acceptable to pass invalid
7083
   * UTF-32 strings but in such cases the result is implementation defined.
7084
   *
7085
   * @param input         the UTF-32 string to convert
7086
   * @param length        the length of the string in 4-byte code units
7087
   * (char32_t)
7088
   * @return the number of bytes required to encode the UTF-32 string as UTF-8
7089
   */
7090
  simdutf_warn_unused virtual size_t
7091
  utf8_length_from_utf32(const char32_t *input,
7092
                         size_t length) const noexcept = 0;
7093
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_UTF32
7094
7095
#if SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
7096
  /**
7097
   * Compute the number of bytes that this UTF-32 string would require in Latin1
7098
   * format.
7099
   *
7100
   * This function does not validate the input. It is acceptable to pass invalid
7101
   * UTF-32 strings but in such cases the result is implementation defined.
7102
   *
7103
   * @param length        the length of the string in 4-byte code units
7104
   * (char32_t)
7105
   * @return the number of bytes required to encode the UTF-32 string as Latin1
7106
   */
7107
  simdutf_warn_unused virtual size_t
7108
  latin1_length_from_utf32(size_t length) const noexcept {
7109
    return length;
7110
  }
7111
#endif // SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
7112
7113
#if SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
7114
  /**
7115
   * Compute the number of bytes that this UTF-8 string would require in Latin1
7116
   * format.
7117
   *
7118
   * This function does not validate the input. It is acceptable to pass invalid
7119
   * UTF-8 strings. The result is then implementation defined, but it remains a
7120
   * safe buffer size: convert_utf8_to_latin1 and
7121
   * convert_utf8_to_latin1_with_errors never write more bytes than this
7122
   * function returns, even when they reject the input.
7123
   *
7124
   * @param input         the UTF-8 string to convert
7125
   * @param length        the length of the string in byte
7126
   * @return the number of bytes required to encode the UTF-8 string as Latin1
7127
   */
7128
  simdutf_warn_unused virtual size_t
7129
  latin1_length_from_utf8(const char *input, size_t length) const noexcept = 0;
7130
#endif // SIMDUTF_FEATURE_UTF8 && SIMDUTF_FEATURE_LATIN1
7131
7132
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
7133
  /**
7134
   * Compute the number of bytes that this UTF-16LE/BE string would require in
7135
   * Latin1 format.
7136
   *
7137
   * This function does not validate the input. It is acceptable to pass invalid
7138
   * UTF-16 strings but in such cases the result is implementation defined.
7139
   *
7140
   * This function is not BOM-aware.
7141
   *
7142
   * @param length        the length of the string in 2-byte code units
7143
   * (char16_t)
7144
   * @return the number of bytes required to encode the UTF-16LE string as
7145
   * Latin1
7146
   */
7147
  simdutf_warn_unused virtual size_t
7148
  latin1_length_from_utf16(size_t length) const noexcept {
7149
    return length;
7150
  }
7151
#endif // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_LATIN1
7152
7153
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
7154
  /**
7155
   * Compute the number of two-byte code units that this UTF-32 string would
7156
   * require in UTF-16 format.
7157
   *
7158
   * This function does not validate the input. It is acceptable to pass invalid
7159
   * UTF-32 strings but in such cases the result is implementation defined.
7160
   *
7161
   * @param input         the UTF-32 string to convert
7162
   * @param length        the length of the string in 4-byte code units
7163
   * (char32_t)
7164
   * @return the number of bytes required to encode the UTF-32 string as UTF-16
7165
   */
7166
  simdutf_warn_unused virtual size_t
7167
  utf16_length_from_utf32(const char32_t *input,
7168
                          size_t length) const noexcept = 0;
7169
#endif // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
7170
7171
#if SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
7172
  /**
7173
   * Return the number of bytes that this UTF-32 string would require in Latin1
7174
   * format.
7175
   *
7176
   * @param length        the length of the string in 4-byte code units
7177
   * (char32_t)
7178
   * @return the number of bytes required to encode the UTF-32 string as Latin1
7179
   */
7180
  simdutf_warn_unused virtual size_t
7181
  utf32_length_from_latin1(size_t length) const noexcept {
7182
    return length;
7183
  }
7184
#endif // SIMDUTF_FEATURE_UTF32 && SIMDUTF_FEATURE_LATIN1
7185
7186
#if SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
7187
  /**
7188
   * Compute the number of bytes that this UTF-16LE string would require in
7189
   * UTF-32 format.
7190
   *
7191
   * This function is equivalent to count_utf16le.
7192
   *
7193
   * This function does not validate the input. It is acceptable to pass invalid
7194
   * UTF-16 strings but in such cases the result is implementation defined.
7195
   *
7196
   * This function is not BOM-aware.
7197
   *
7198
   * @param input         the UTF-16LE string to convert
7199
   * @param length        the length of the string in 2-byte code units
7200
   * (char16_t)
7201
   * @return the number of bytes required to encode the UTF-16LE string as
7202
   * UTF-32
7203
   */
7204
  simdutf_warn_unused virtual size_t
7205
  utf32_length_from_utf16le(const char16_t *input,
7206
                            size_t length) const noexcept = 0;
7207
7208
  /**
7209
   * Compute the number of bytes that this UTF-16BE string would require in
7210
   * UTF-32 format.
7211
   *
7212
   * This function is equivalent to count_utf16be.
7213
   *
7214
   * This function does not validate the input. It is acceptable to pass invalid
7215
   * UTF-16 strings but in such cases the result is implementation defined.
7216
   *
7217
   * This function is not BOM-aware.
7218
   *
7219
   * @param input         the UTF-16BE string to convert
7220
   * @param length        the length of the string in 2-byte code units
7221
   * (char16_t)
7222
   * @return the number of bytes required to encode the UTF-16BE string as
7223
   * UTF-32
7224
   */
7225
  simdutf_warn_unused virtual size_t
7226
  utf32_length_from_utf16be(const char16_t *input,
7227
                            size_t length) const noexcept = 0;
7228
#endif // SIMDUTF_FEATURE_UTF16 && SIMDUTF_FEATURE_UTF32
7229
7230
#if SIMDUTF_FEATURE_UTF16
7231
  /**
7232
   * Count the number of code points (characters) in the string assuming that
7233
   * it is valid.
7234
   *
7235
   * This function assumes that the input string is valid UTF-16LE.
7236
   * It is acceptable to pass invalid UTF-16 strings but in such cases
7237
   * the result is implementation defined.
7238
   *
7239
   * This function is not BOM-aware.
7240
   *
7241
   * @param input         the UTF-16LE string to process
7242
   * @param length        the length of the string in 2-byte code units
7243
   * (char16_t)
7244
   * @return number of code points
7245
   */
7246
  simdutf_warn_unused virtual size_t
7247
  count_utf16le(const char16_t *input, size_t length) const noexcept = 0;
7248
7249
  /**
7250
   * Count the number of code points (characters) in the string assuming that
7251
   * it is valid.
7252
   *
7253
   * This function assumes that the input string is valid UTF-16BE.
7254
   * It is acceptable to pass invalid UTF-16 strings but in such cases
7255
   * the result is implementation defined.
7256
   *
7257
   * This function is not BOM-aware.
7258
   *
7259
   * @param input         the UTF-16BE string to process
7260
   * @param length        the length of the string in 2-byte code units
7261
   * (char16_t)
7262
   * @return number of code points
7263
   */
7264
  simdutf_warn_unused virtual size_t
7265
  count_utf16be(const char16_t *input, size_t length) const noexcept = 0;
7266
#endif // SIMDUTF_FEATURE_UTF16
7267
7268
#if SIMDUTF_FEATURE_UTF8
7269
  /**
7270
   * Count the number of code points (characters) in the string assuming that
7271
   * it is valid.
7272
   *
7273
   * This function assumes that the input string is valid UTF-8.
7274
   * It is acceptable to pass invalid UTF-8 strings but in such cases
7275
   * the result is implementation defined.
7276
   *
7277
   * @param input         the UTF-8 string to process
7278
   * @param length        the length of the string in bytes
7279
   * @return number of code points
7280
   */
7281
  simdutf_warn_unused virtual size_t
7282
  count_utf8(const char *input, size_t length) const noexcept = 0;
7283
#endif // SIMDUTF_FEATURE_UTF8
7284
7285
#if SIMDUTF_FEATURE_BASE64
7286
  /**
7287
   * Provide the maximal binary length in bytes given the base64 input.
7288
   * As long as the input does not contain ignorable characters (e.g., ASCII
7289
   * spaces or linefeed characters), the result is exact. In particular, the
7290
   * function checks for padding characters.
7291
   *
7292
   * The function is fast (constant time). It checks up to two characters at
7293
   * the end of the string. The input is not otherwise validated or read..
7294
   *
7295
   * @param input         the base64 input to process
7296
   * @param length        the length of the base64 input in bytes
7297
   * @return maximal number of binary bytes
7298
   */
7299
  simdutf_warn_unused size_t maximal_binary_length_from_base64(
7300
      const char *input, size_t length) const noexcept;
7301
7302
  /**
7303
   * Provide the maximal binary length in bytes given the base64 input.
7304
   * As long as the input does not contain ignorable characters (e.g., ASCII
7305
   * spaces or linefeed characters), the result is exact. In particular, the
7306
   * function checks for padding characters.
7307
   *
7308
   * The function is fast (constant time). It checks up to two characters at
7309
   * the end of the string. The input is not otherwise validated or read.
7310
   *
7311
   * @param input         the base64 input to process, in ASCII stored as 16-bit
7312
   * units
7313
   * @param length        the length of the base64 input in 16-bit units
7314
   * @return maximal number of binary bytes
7315
   */
7316
  simdutf_warn_unused size_t maximal_binary_length_from_base64(
7317
      const char16_t *input, size_t length) const noexcept;
7318
7319
  /**
7320
   * Compute the binary length from a base64 input with ASCII spaces.
7321
   * This function is useful for well-formed base64 inputs that may contain
7322
   * ASCII spaces (such as line breaks). For such inputs, the result is exact.
7323
   *
7324
   * The function counts non-whitespace characters (ASCII value > 0x20) and
7325
   * subtracts padding characters ('=') found at the end.
7326
   *
7327
   * @param input         the base64 input to process
7328
   * @param length        the length of the base64 input in bytes
7329
   * @return number of binary bytes
7330
   */
7331
  simdutf_warn_unused virtual size_t
7332
  binary_length_from_base64(const char *input, size_t length) const noexcept;
7333
7334
  /**
7335
   * Compute the binary length from a base64 input with ASCII spaces.
7336
   * This function is useful for well-formed base64 inputs that may contain
7337
   * ASCII spaces (such as line breaks). For such inputs, the result is exact.
7338
   *
7339
   * The function counts non-whitespace characters (ASCII value > 0x20) and
7340
   * subtracts padding characters ('=') found at the end.
7341
   *
7342
   * @param input         the base64 input to process, in ASCII stored as 16-bit
7343
   * units
7344
   * @param length        the length of the base64 input in 16-bit units
7345
   * @return number of binary bytes
7346
   */
7347
  simdutf_warn_unused virtual size_t
7348
  binary_length_from_base64(const char16_t *input,
7349
                            size_t length) const noexcept;
7350
7351
  /**
7352
   * Convert a base64 input to a binary output.
7353
   *
7354
   * This function follows the WHATWG forgiving-base64 format, which means that
7355
   * it will ignore any ASCII spaces in the input. You may provide a padded
7356
   * input (with one or two equal signs at the end) or an unpadded input
7357
   * (without any equal signs at the end).
7358
   *
7359
   * See https://infra.spec.whatwg.org/#forgiving-base64-decode
7360
   *
7361
   * This function will fail in case of invalid input. When last_chunk_options =
7362
   * loose, there are two possible reasons for failure: the input contains a
7363
   * number of base64 characters that when divided by 4, leaves a single
7364
   * remainder character (BASE64_INPUT_REMAINDER), or the input contains a
7365
   * character that is not a valid base64 character (INVALID_BASE64_CHARACTER).
7366
   *
7367
   * You should call this function with a buffer that is at least
7368
   * maximal_binary_length_from_base64(input, length) bytes long. If you fail to
7369
   * provide that much space, the function may cause a buffer overflow.
7370
   *
7371
   * @param input         the base64 string to process
7372
   * @param length        the length of the string in bytes
7373
   * @param output        the pointer to a buffer that can hold the conversion
7374
   * result (should be at least maximal_binary_length_from_base64(input, length)
7375
   * bytes long).
7376
   * @param options       the base64 options to use, can be base64_default or
7377
   * base64_url, is base64_default by default.
7378
   * @param last_chunk_options the handling of the last chunk (default: loose)
7379
   * @return a result pair struct (of type simdutf::result containing the two
7380
   * fields error and count) with an error code and either position of the error
7381
   * (in the input in bytes) if any, or the number of bytes written if
7382
   * successful.
7383
   */
7384
  simdutf_warn_unused virtual result
7385
  base64_to_binary(const char *input, size_t length, char *output,
7386
                   base64_options options = base64_default,
7387
                   last_chunk_handling_options last_chunk_options =
7388
                       last_chunk_handling_options::loose) const noexcept = 0;
7389
7390
  /**
7391
   * Convert a base64 input to a binary output while returning more details
7392
   * than base64_to_binary.
7393
   *
7394
   * This function follows the WHATWG forgiving-base64 format, which means that
7395
   * it will ignore any ASCII spaces in the input. You may provide a padded
7396
   * input (with one or two equal signs at the end) or an unpadded input
7397
   * (without any equal signs at the end).
7398
   *
7399
   * See https://infra.spec.whatwg.org/#forgiving-base64-decode
7400
   *
7401
   * This function will fail in case of invalid input. When last_chunk_options =
7402
   * loose, there are two possible reasons for failure: the input contains a
7403
   * number of base64 characters that when divided by 4, leaves a single
7404
   * remainder character (BASE64_INPUT_REMAINDER), or the input contains a
7405
   * character that is not a valid base64 character (INVALID_BASE64_CHARACTER).
7406
   *
7407
   * You should call this function with a buffer that is at least
7408
   * maximal_binary_length_from_base64(input, length) bytes long. If you fail to
7409
   * provide that much space, the function may cause a buffer overflow.
7410
   *
7411
   * @param input         the base64 string to process
7412
   * @param length        the length of the string in bytes
7413
   * @param output        the pointer to a buffer that can hold the conversion
7414
   * result (should be at least maximal_binary_length_from_base64(input, length)
7415
   * bytes long).
7416
   * @param options       the base64 options to use, can be base64_default or
7417
   * base64_url, is base64_default by default.
7418
   * @param last_chunk_options the handling of the last chunk (default: loose)
7419
   * @return a full_result pair struct (of type simdutf::result containing the
7420
   * three fields error, input_count and output_count).
7421
   */
7422
  simdutf_warn_unused virtual full_result base64_to_binary_details(
7423
      const char *input, size_t length, char *output,
7424
      base64_options options = base64_default,
7425
      last_chunk_handling_options last_chunk_options =
7426
          last_chunk_handling_options::loose) const noexcept = 0;
7427
7428
  /**
7429
   * Convert a base64 input to a binary output.
7430
   *
7431
   * This function follows the WHATWG forgiving-base64 format, which means that
7432
   * it will ignore any ASCII spaces in the input. You may provide a padded
7433
   * input (with one or two equal signs at the end) or an unpadded input
7434
   * (without any equal signs at the end).
7435
   *
7436
   * See https://infra.spec.whatwg.org/#forgiving-base64-decode
7437
   *
7438
   * This function will fail in case of invalid input. When last_chunk_options =
7439
   * loose, there are two possible reasons for failure: the input contains a
7440
   * number of base64 characters that when divided by 4, leaves a single
7441
   * remainder character (BASE64_INPUT_REMAINDER), or the input contains a
7442
   * character that is not a valid base64 character (INVALID_BASE64_CHARACTER).
7443
   *
7444
   * You should call this function with a buffer that is at least
7445
   * maximal_binary_length_from_base64(input, length) bytes long. If you
7446
   * fail to provide that much space, the function may cause a buffer overflow.
7447
   *
7448
   * @param input         the base64 string to process, in ASCII stored as
7449
   * 16-bit units
7450
   * @param length        the length of the string in 16-bit units
7451
   * @param output        the pointer to a buffer that can hold the conversion
7452
   * result (should be at least maximal_binary_length_from_base64(input, length)
7453
   * bytes long).
7454
   * @param options       the base64 options to use, can be base64_default or
7455
   * base64_url, is base64_default by default.
7456
   * @param last_chunk_options the handling of the last chunk (default: loose)
7457
   * @return a result pair struct (of type simdutf::result containing the two
7458
   * fields error and count) with an error code and position of the
7459
   * INVALID_BASE64_CHARACTER error (in the input in units) if any, or the
7460
   * number of bytes written if successful.
7461
   */
7462
  simdutf_warn_unused virtual result
7463
  base64_to_binary(const char16_t *input, size_t length, char *output,
7464
                   base64_options options = base64_default,
7465
                   last_chunk_handling_options last_chunk_options =
7466
                       last_chunk_handling_options::loose) const noexcept = 0;
7467
7468
  /**
7469
   * Convert a base64 input to a binary output while returning more details
7470
   * than base64_to_binary.
7471
   *
7472
   * This function follows the WHATWG forgiving-base64 format, which means that
7473
   * it will ignore any ASCII spaces in the input. You may provide a padded
7474
   * input (with one or two equal signs at the end) or an unpadded input
7475
   * (without any equal signs at the end).
7476
   *
7477
   * See https://infra.spec.whatwg.org/#forgiving-base64-decode
7478
   *
7479
   * This function will fail in case of invalid input. When last_chunk_options =
7480
   * loose, there are two possible reasons for failure: the input contains a
7481
   * number of base64 characters that when divided by 4, leaves a single
7482
   * remainder character (BASE64_INPUT_REMAINDER), or the input contains a
7483
   * character that is not a valid base64 character (INVALID_BASE64_CHARACTER).
7484
   *
7485
   * You should call this function with a buffer that is at least
7486
   * maximal_binary_length_from_base64(input, length) bytes long. If you fail to
7487
   * provide that much space, the function may cause a buffer overflow.
7488
   *
7489
   * @param input         the base64 string to process
7490
   * @param length        the length of the string in bytes
7491
   * @param output        the pointer to a buffer that can hold the conversion
7492
   * result (should be at least maximal_binary_length_from_base64(input, length)
7493
   * bytes long).
7494
   * @param options       the base64 options to use, can be base64_default or
7495
   * base64_url, is base64_default by default.
7496
   * @param last_chunk_options the handling of the last chunk (default: loose)
7497
   * @return a full_result pair struct (of type simdutf::result containing the
7498
   * three fields error, input_count and output_count).
7499
   */
7500
  simdutf_warn_unused virtual full_result base64_to_binary_details(
7501
      const char16_t *input, size_t length, char *output,
7502
      base64_options options = base64_default,
7503
      last_chunk_handling_options last_chunk_options =
7504
          last_chunk_handling_options::loose) const noexcept = 0;
7505
7506
  /**
7507
   * Provide the base64 length in bytes given the length of a binary input.
7508
   *
7509
   * @param length        the length of the input in bytes
7510
   * @param options       the base64 options to use, can be base64_default or
7511
   * base64_url, is base64_default by default.
7512
   * @return number of base64 bytes
7513
   */
7514
  simdutf_warn_unused size_t base64_length_from_binary(
7515
      size_t length, base64_options options = base64_default) const noexcept;
7516
7517
  /**
7518
   * Convert a binary input to a base64 output.
7519
   *
7520
   * The default option (simdutf::base64_default) uses the characters `+` and
7521
   * `/` as part of its alphabet. Further, it adds padding (`=`) at the end of
7522
   * the output to ensure that the output length is a multiple of four.
7523
   *
7524
   * The URL option (simdutf::base64_url) uses the characters `-` and `_` as
7525
   * part of its alphabet. No padding is added at the end of the output.
7526
   *
7527
   * This function always succeeds.
7528
   *
7529
   * @param input         the binary to process
7530
   * @param length        the length of the input in bytes
7531
   * @param output        the pointer to a buffer that can hold the conversion
7532
   * result (should be at least base64_length_from_binary(length) bytes long)
7533
   * @param options       the base64 options to use, can be base64_default or
7534
   * base64_url, is base64_default by default.
7535
   * @return number of written bytes, will be equal to
7536
   * base64_length_from_binary(length, options)
7537
   */
7538
  virtual size_t
7539
  binary_to_base64(const char *input, size_t length, char *output,
7540
                   base64_options options = base64_default) const noexcept = 0;
7541
7542
  /**
7543
   * Convert a binary input to a base64 output with lines of given length.
7544
   * Lines are separated by a single linefeed character.
7545
   *
7546
   * The default option (simdutf::base64_default) uses the characters `+` and
7547
   * `/` as part of its alphabet. Further, it adds padding (`=`) at the end of
7548
   * the output to ensure that the output length is a multiple of four.
7549
   *
7550
   * The URL option (simdutf::base64_url) uses the characters `-` and `_` as
7551
   * part of its alphabet. No padding is added at the end of the output.
7552
   *
7553
   * This function always succeeds.
7554
   *
7555
   * @param input         the binary to process
7556
   * @param length        the length of the input in bytes
7557
   * @param output        the pointer to a buffer that can hold the conversion
7558
   * result (should be at least base64_length_from_binary_with_lines(length,
7559
   * options, line_length) bytes long)
7560
   * @param line_length   the length of each line, values smaller than 4 are
7561
   * interpreted as 4
7562
   * @param options       the base64 options to use, can be base64_default or
7563
   * base64_url, is base64_default by default.
7564
   * @return number of written bytes, will be equal to
7565
   * base64_length_from_binary_with_lines(length, options, line_length)
7566
   */
7567
  virtual size_t binary_to_base64_with_lines(
7568
      const char *input, size_t length, char *output,
7569
      size_t line_length = simdutf::default_line_length,
7570
      base64_options options = base64_default) const noexcept = 0;
7571
7572
  /**
7573
   * Find the first occurrence of a character in a string. If the character is
7574
   * not found, return a pointer to the end of the string.
7575
   * @param start        the start of the string
7576
   * @param end          the end of the string
7577
   * @param character    the character to find
7578
   * @return a pointer to the first occurrence of the character in the string,
7579
   * or a pointer to the end of the string if the character is not found.
7580
   *
7581
   */
7582
  virtual const char *find(const char *start, const char *end,
7583
                           char character) const noexcept = 0;
7584
  virtual const char16_t *find(const char16_t *start, const char16_t *end,
7585
                               char16_t character) const noexcept = 0;
7586
#endif // SIMDUTF_FEATURE_BASE64
7587
7588
#ifdef SIMDUTF_INTERNAL_TESTS
7589
  // This method is exported only in developer mode, its purpose
7590
  // is to expose some internal test procedures from the given
7591
  // implementation and then use them through our standard test
7592
  // framework.
7593
  //
7594
  // Regular users should not use it, the tests of the public
7595
  // API are enough.
7596
7597
  struct TestProcedure {
7598
    // display name
7599
    std::string_view name;
7600
7601
    // procedure should return whether given test pass or not
7602
    void (*procedure)(const implementation &);
7603
  };
7604
7605
  virtual std::vector<TestProcedure> internal_tests() const;
7606
#endif
7607
7608
protected:
7609
  /** @private Construct an implementation with the given name and description.
7610
   * For subclasses.
7611
   * @param name the name of this implementation
7612
   * @param description a description of this implementation
7613
   * @param required_instruction_sets the instruction sets this implementation
7614
   * requires
7615
   */
7616
  simdutf_really_inline implementation(const char *name,
7617
                                       const char *description,
7618
                                       uint32_t required_instruction_sets)
7619
      : _name(name), _description(description),
7620
        _required_instruction_sets(required_instruction_sets) {}
7621
7622
protected:
7623
  ~implementation() = default;
7624
7625
private:
7626
  /**
7627
   * The name of this implementation.
7628
   */
7629
  const char *_name;
7630
7631
  /**
7632
   * The description of this implementation.
7633
   */
7634
  const char *_description;
7635
7636
  /**
7637
   * Instruction sets required for this implementation.
7638
   */
7639
  const uint32_t _required_instruction_sets;
7640
};
7641
7642
/** @private */
7643
namespace internal {
7644
7645
/**
7646
 * The list of available implementations compiled into simdutf.
7647
 */
7648
class available_implementation_list {
7649
public:
7650
  /** Get the list of available implementations compiled into simdutf */
7651
  simdutf_really_inline available_implementation_list() {}
7652
  /** Number of implementations */
7653
  size_t size() const noexcept;
7654
  /** STL const begin() iterator */
7655
  const implementation *const *begin() const noexcept;
7656
  /** STL const end() iterator */
7657
  const implementation *const *end() const noexcept;
7658
7659
  /**
7660
   * Get the implementation with the given name.
7661
   *
7662
   * Case sensitive.
7663
   *
7664
   *     const implementation *impl =
7665
   * simdutf::available_implementations["westmere"]; if (!impl) { exit(1); } if
7666
   * (!imp->supported_by_runtime_system()) { exit(1); }
7667
   *     simdutf::active_implementation = impl;
7668
   *
7669
   * @param name the implementation to find, e.g. "westmere", "haswell", "arm64"
7670
   * @return the implementation, or nullptr if the parse failed.
7671
   */
7672
  const implementation *operator[](std::string_view name) const noexcept {
7673
    for (const implementation *impl : *this) {
7674
      if (impl->name() == name) {
7675
        return impl;
7676
      }
7677
    }
7678
    return nullptr;
7679
  }
7680
7681
  /**
7682
   * Detect the most advanced implementation supported by the current host.
7683
   *
7684
   * This is used to initialize the implementation on startup.
7685
   *
7686
   *     const implementation *impl =
7687
   * simdutf::available_implementation::detect_best_supported();
7688
   *     simdutf::active_implementation = impl;
7689
   *
7690
   * @return the most advanced supported implementation for the current host, or
7691
   * an implementation that returns UNSUPPORTED_ARCHITECTURE if there is no
7692
   * supported implementation. Will never return nullptr.
7693
   */
7694
  const implementation *detect_best_supported() const noexcept;
7695
};
7696
7697
template <typename T> class atomic_ptr {
7698
public:
7699
  atomic_ptr(T *_ptr) : ptr{_ptr} {}
7700
7701
#if defined(SIMDUTF_NO_THREADS)
7702
  operator const T *() const { return ptr; }
7703
  const T &operator*() const { return *ptr; }
7704
  const T *operator->() const { return ptr; }
7705
7706
  operator T *() { return ptr; }
7707
  T &operator*() { return *ptr; }
7708
  T *operator->() { return ptr; }
7709
  atomic_ptr &operator=(T *_ptr) {
7710
    ptr = _ptr;
7711
    return *this;
7712
  }
7713
7714
#else
7715
  operator const T *() const { return ptr.load(); }
7716
  const T &operator*() const { return *ptr; }
7717
  const T *operator->() const { return ptr.load(); }
7718
7719
  operator T *() { return ptr.load(); }
7720
  T &operator*() { return *ptr; }
7721
  T *operator->() { return ptr.load(); }
7722
  atomic_ptr &operator=(T *_ptr) {
7723
    ptr = _ptr;
7724
    return *this;
7725
  }
7726
7727
#endif
7728
7729
private:
7730
#if defined(SIMDUTF_NO_THREADS)
7731
  T *ptr;
7732
#else
7733
  std::atomic<T *> ptr;
7734
#endif
7735
};
7736
7737
class detect_best_supported_implementation_on_first_use;
7738
7739
} // namespace internal
7740
7741
/**
7742
 * The list of available implementations compiled into simdutf.
7743
 */
7744
extern SIMDUTF_DLLIMPORTEXPORT const internal::available_implementation_list &
7745
get_available_implementations();
7746
7747
/**
7748
 * The active implementation.
7749
 *
7750
 * Automatically initialized on first use to the most advanced implementation
7751
 * supported by this hardware.
7752
 */
7753
extern SIMDUTF_DLLIMPORTEXPORT internal::atomic_ptr<const implementation> &
7754
get_active_implementation();
7755
7756
} // namespace simdutf
7757
7758
#if SIMDUTF_FEATURE_BASE64
7759
  // this header is not part of the public api
7760
  #include <simdutf/base64_implementation.h>
7761
7762
namespace simdutf {
7763
  #if SIMDUTF_SPAN
7764
/**
7765
 * @brief span overload
7766
 * @return a tuple of result and outlen
7767
 */
7768
simdutf_really_inline
7769
    simdutf_constexpr23 simdutf_warn_unused std::tuple<result, std::size_t>
7770
    base64_to_binary_safe(
7771
        const detail::input_span_of_byte_like auto &input,
7772
        detail::output_span_of_byte_like auto &&binary_output,
7773
        base64_options options = base64_default,
7774
        last_chunk_handling_options last_chunk_options = loose,
7775
        bool decode_up_to_bad_char = false) noexcept {
7776
  size_t outlen = binary_output.size();
7777
    #if SIMDUTF_CPLUSPLUS23
7778
  if consteval {
7779
    auto r = detail::with_char_input(input, [&](auto in) {
7780
      return detail::with_char_output(
7781
          binary_output,
7782
          [&](char *out) {
7783
            return base64_to_binary_safe_impl(in, input.size(), out, outlen,
7784
                                              options, last_chunk_options,
7785
                                              decode_up_to_bad_char);
7786
          },
7787
          [&](const auto &) { return outlen; });
7788
    });
7789
    return {r, outlen};
7790
  } else
7791
    #endif
7792
  {
7793
    auto r = base64_to_binary_safe_impl<char>(
7794
        reinterpret_cast<const char *>(input.data()), input.size(),
7795
        reinterpret_cast<char *>(binary_output.data()), outlen, options,
7796
        last_chunk_options, decode_up_to_bad_char);
7797
    return {r, outlen};
7798
  }
7799
}
7800
7801
    #if SIMDUTF_SPAN
7802
/**
7803
 * @brief span overload
7804
 * @return a tuple of result and outlen
7805
 */
7806
simdutf_really_inline
7807
    simdutf_warn_unused simdutf_constexpr23 std::tuple<result, std::size_t>
7808
    base64_to_binary_safe(
7809
        std::span<const char16_t> input,
7810
        detail::output_span_of_byte_like auto &&binary_output,
7811
        base64_options options = base64_default,
7812
        last_chunk_handling_options last_chunk_options = loose,
7813
        bool decode_up_to_bad_char = false) noexcept {
7814
  size_t outlen = binary_output.size();
7815
      #if SIMDUTF_CPLUSPLUS23
7816
  if consteval {
7817
    auto r = detail::with_char_output(
7818
        binary_output,
7819
        [&](auto out) {
7820
          return base64_to_binary_safe_impl(input.data(), input.size(), out,
7821
                                            outlen, options, last_chunk_options,
7822
                                            decode_up_to_bad_char);
7823
        },
7824
        [&](const auto &) { return outlen; });
7825
    return {r, outlen};
7826
  } else
7827
      #endif
7828
  {
7829
    auto r = base64_to_binary_safe(
7830
        input.data(), input.size(),
7831
        reinterpret_cast<char *>(binary_output.data()), outlen, options,
7832
        last_chunk_options, decode_up_to_bad_char);
7833
    return {r, outlen};
7834
  }
7835
}
7836
    #endif // SIMDUTF_SPAN
7837
7838
  #endif // SIMDUTF_SPAN
7839
} // namespace simdutf
7840
7841
#endif // SIMDUTF_FEATURE_BASE64
7842
7843
#if SIMDUTF_CPLUSPLUS23 && SIMDUTF_FEATURE_BASE64
7844
7845
namespace simdutf {
7846
namespace literals {
7847
7848
namespace detail {
7849
7850
// the detail namespace is not part of the public api
7851
7852
template <std::size_t N> struct base64_literal_helper {
7853
  std::array<char, N - 1> storage{};
7854
  static constexpr std::size_t size() noexcept { return N - 1; }
7855
  consteval base64_literal_helper(const char (&str)[N]) {
7856
    for (std::size_t i = 0; i < size(); i++) {
7857
      storage[i] = str[i];
7858
    }
7859
  }
7860
};
7861
7862
template <std::size_t InputLen> struct base64_decode_result {
7863
  static constexpr std::size_t max_out = (InputLen + 3) / 4 * 3;
7864
  std::array<char, max_out> buffer{};
7865
  std::size_t output_count{};
7866
};
7867
7868
template <std::size_t InputLen>
7869
consteval auto base64_decode_literal(const char *str) {
7870
  base64_decode_result<InputLen> result{};
7871
  auto r = scalar::base64::base64_to_binary_details_impl(
7872
      str, InputLen, result.buffer.data(), base64_default, loose);
7873
  if (r.error != error_code::SUCCESS) {
7874
  #if __cpp_lib_unreachable >= 202202L
7875
    std::unreachable(); // invalid base64 input in _base64 literal
7876
  #else
7877
    // workaround for older stdlib
7878
    throw "invalid base64 input in _base64 literal";
7879
  #endif
7880
  }
7881
  result.output_count = r.output_count;
7882
  return result;
7883
}
7884
7885
template <base64_literal_helper a> consteval auto base64_make_array() {
7886
  constexpr auto decoded = base64_decode_literal<a.size()>(a.storage.data());
7887
  std::array<char, decoded.output_count> ret{};
7888
  for (std::size_t i = 0; i < decoded.output_count; i++) {
7889
    ret[i] = decoded.buffer[i];
7890
  }
7891
  return ret;
7892
}
7893
7894
} // namespace detail
7895
7896
/**
7897
 * User-defined literal for compile-time base64 decoding.
7898
 *
7899
 * Usage:
7900
 *   using namespace simdutf::literals;
7901
 *   constexpr auto decoded = "SGVsbG8gV29ybGQh"_base64;
7902
 *   // decoded is a std::array<char, 12> containing "Hello World!"
7903
 *
7904
 * The input must be valid base64. Whitepace is allowed and ignored.
7905
 * A compilation error occurs if the input is invalid.
7906
 */
7907
template <detail::base64_literal_helper a> consteval auto operator""_base64() {
7908
  return detail::base64_make_array<a>();
7909
}
7910
7911
} // namespace literals
7912
} // namespace simdutf
7913
7914
#endif // SIMDUTF_CPLUSPLUS23 && SIMDUTF_FEATURE_BASE64
7915
7916
#endif // SIMDUTF_IMPLEMENTATION_H