Bug 1826905 [wpt PR 39420] - view-transition: Add WPTs for damage tracking when new...
[gecko.git] / mfbt / Utf8.h
blob29b1883084174235891683f6af8e9e4f0496cc4e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 * UTF-8-related functionality, including a type-safe structure representing a
9 * UTF-8 code unit.
12 #ifndef mozilla_Utf8_h
13 #define mozilla_Utf8_h
15 #include "mozilla/Casting.h" // for mozilla::AssertedCast
16 #include "mozilla/Likely.h" // for MOZ_UNLIKELY
17 #include "mozilla/Maybe.h" // for mozilla::Maybe
18 #include "mozilla/Span.h" // for mozilla::Span
19 #include "mozilla/TextUtils.h" // for mozilla::IsAscii and via Latin1.h for
20 // encoding_rs_mem.h and MOZ_HAS_JSRUST.
21 #include "mozilla/Types.h" // for MFBT_API
23 #include <limits> // for std::numeric_limits
24 #include <limits.h> // for CHAR_BIT
25 #include <stddef.h> // for size_t
26 #include <stdint.h> // for uint8_t
28 #if MOZ_HAS_JSRUST()
29 // Can't include mozilla/Encoding.h here.
30 extern "C" {
31 // Declared as uint8_t instead of char to match declaration in another header.
32 size_t encoding_utf8_valid_up_to(uint8_t const* buffer, size_t buffer_len);
34 #else
35 namespace mozilla {
36 namespace detail {
37 extern MFBT_API bool IsValidUtf8(const void* aCodeUnits, size_t aCount);
38 }; // namespace detail
39 }; // namespace mozilla
40 #endif // MOZ_HAS_JSRUST
42 namespace mozilla {
44 union Utf8Unit;
46 static_assert(CHAR_BIT == 8,
47 "Utf8Unit won't work so well with non-octet chars");
49 /**
50 * A code unit within a UTF-8 encoded string. (A code unit is the smallest
51 * unit within the Unicode encoding of a string. For UTF-8 this is an 8-bit
52 * number; for UTF-16 it would be a 16-bit number.)
54 * This is *not* the same as a single code point: in UTF-8, non-ASCII code
55 * points are constituted by multiple code units.
57 union Utf8Unit {
58 private:
59 // Utf8Unit is a union wrapping a raw |char|. The C++ object model and C++
60 // requirements as to how objects may be accessed with respect to their actual
61 // types (almost?) uniquely compel this choice.
63 // Our requirements for a UTF-8 code unit representation are:
65 // 1. It must be "compatible" with C++ character/string literals that use
66 // the UTF-8 encoding. Given a properly encoded C++ literal, you should
67 // be able to use |Utf8Unit| and friends to access it; given |Utf8Unit|
68 // and friends (particularly UnicodeData), you should be able to access
69 // C++ character types for their contents.
70 // 2. |Utf8Unit| and friends must convert to/from |char| and |char*| only by
71 // explicit operation.
72 // 3. |Utf8Unit| must participate in overload resolution and template type
73 // equivalence (that is, given |template<class> class X|, when |X<T>| and
74 // |X<U>| are the same type) distinctly from the C++ character types.
76 // And a few nice-to-haves (at least for the moment):
78 // 4. The representation should use unsigned numbers, to avoid undefined
79 // behavior that can arise with signed types, and because Unicode code
80 // points and code units are unsigned.
81 // 5. |Utf8Unit| and friends should be convertible to/from |unsigned char|
82 // and |unsigned char*|, for APIs that (because of #4 above) use those
83 // types as the "natural" choice for UTF-8 data.
85 // #1 requires that |Utf8Unit| "incorporate" a C++ character type: one of
86 // |{,{un,}signed} char|.[0] |uint8_t| won't work because it might not be a
87 // C++ character type.
89 // #2 and #3 mean that |Utf8Unit| can't *be* such a type (or a typedef to one:
90 // typedefs don't generate *new* types, just type aliases). This requires a
91 // compound type.
93 // The ultimate representation (and character type in it) is constrained by
94 // C++14 [basic.lval]p10 that defines how objects may be accessed, with
95 // respect to the dynamic type in memory and the actual type used to access
96 // them. It reads:
98 // If a program attempts to access the stored value of an object
99 // through a glvalue of other than one of the following types the
100 // behavior is undefined:
102 // 1. the dynamic type of the object,
103 // 2. a cv-qualified version of the dynamic type of the object,
104 // ...other types irrelevant here...
105 // 3. an aggregate or union type that includes one of the
106 // aforementioned types among its elements or non-static data
107 // members (including, recursively, an element or non-static
108 // data member of a subaggregate or contained union),
109 // ...more irrelevant types...
110 // 4. a char or unsigned char type.
112 // Accessing (wrapped) UTF-8 data as |char|/|unsigned char| is allowed no
113 // matter the representation by #4. (Briefly set aside what values are seen.)
114 // (And #2 allows |const| on either the dynamic type or the accessing type.)
115 // (|signed char| is really only useful for small signed numbers, not
116 // characters, so we ignore it.)
118 // If we interpret contents as |char|/|unsigned char| contrary to the actual
119 // type stored there, what happens? C++14 [basic.fundamental]p1 requires
120 // character types be identically aligned/sized; C++14 [basic.fundamental]p3
121 // requires |signed char| and |unsigned char| have the same value
122 // representation. C++ doesn't require identical bitwise representation, tho.
123 // Practically we could assume it, but this verges on C++ spec bits best not
124 // *relied* on for correctness, if possible.
126 // So we don't expose |Utf8Unit|'s contents as |unsigned char*|: only |char|
127 // and |char*|. Instead we safely expose |unsigned char| by fully-defined
128 // *integral conversion* (C++14 [conv.integral]p2). Integral conversion from
129 // |unsigned char| → |char| has only implementation-defined behavior. It'd be
130 // better not to depend on that, but given twos-complement won, it should be
131 // okay. (Also |unsigned char*| is awkward enough to work with for strings
132 // that it probably doesn't appear in string manipulation much anyway, only in
133 // places that should really use |Utf8Unit| directly.)
135 // The opposite direction -- interpreting |char| or |char*| data through
136 // |Utf8Unit| -- isn't tricky as long as |Utf8Unit| contains a |char| as
137 // decided above, using #3. An "aggregate or union" will work that contains a
138 // |char|. Oddly, an aggregate won't work: C++14 [dcl.init.aggr]p1 says
139 // aggregates must have "no private or protected non-static data members", and
140 // we want to keep the inner |char| hidden. So a |struct| is out, and only
141 // |union| remains.
143 // (Enums are not "an aggregate or union type", so [maybe surprisingly] we
144 // can't make |Utf8Unit| an enum class with |char| underlying type, because we
145 // are given no license to treat |char| memory as such an |enum|'s memory.)
147 // Therefore |Utf8Unit| is a union type with a |char| non-static data member.
148 // This satisfies all our requirements. It also supports the nice-to-haves of
149 // creating a |Utf8Unit| from an |unsigned char|, and being convertible to
150 // |unsigned char|. It doesn't satisfy the nice-to-haves of using an
151 // |unsigned char| internally, nor of letting us wrap an existing
152 // |unsigned char| or pointer to one. We probably *could* do these, if we
153 // were willing to rely harder on implementation-defined behaviors, but for
154 // now we privilege C++'s main character type over some conceptual purity.
156 // 0. There's a proposal for a UTF-8 character type distinct from the existing
157 // C++ narrow character types:
159 // http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0482r0.html
161 // but it hasn't been standardized (and might never be), and none of the
162 // compilers we really care about have implemented it. Maybe someday we
163 // can change our implementation to it without too much trouble, if we're
164 // lucky...
165 char mValue = '\0';
167 public:
168 Utf8Unit() = default;
170 explicit constexpr Utf8Unit(char aUnit) : mValue(aUnit) {}
172 explicit constexpr Utf8Unit(unsigned char aUnit)
173 : mValue(static_cast<char>(aUnit)) {
174 // Per the above comment, the prior cast is integral conversion with
175 // implementation-defined semantics, and we regretfully but unavoidably
176 // assume the conversion does what we want it to.
179 constexpr bool operator==(const Utf8Unit& aOther) const {
180 return mValue == aOther.mValue;
183 constexpr bool operator!=(const Utf8Unit& aOther) const {
184 return !(*this == aOther);
187 /** Convert a UTF-8 code unit to a raw char. */
188 constexpr char toChar() const {
189 // Only a |char| is ever permitted to be written into this location, so this
190 // is both permissible and returns the desired value.
191 return mValue;
194 /** Convert a UTF-8 code unit to a raw unsigned char. */
195 constexpr unsigned char toUnsignedChar() const {
196 // Per the above comment, this is well-defined integral conversion.
197 return static_cast<unsigned char>(mValue);
200 /** Convert a UTF-8 code unit to a uint8_t. */
201 constexpr uint8_t toUint8() const {
202 // Per the above comment, this is well-defined integral conversion.
203 return static_cast<uint8_t>(mValue);
206 // We currently don't expose |&mValue|. |UnicodeData| sort of does, but
207 // that's a somewhat separate concern, justified in different comments in
208 // that other code.
212 * Reinterpret the address of a UTF-8 code unit as |const unsigned char*|.
214 * Assuming proper backing has been set up, the resulting |const unsigned char*|
215 * may validly be dereferenced.
217 * No access is provided to mutate this underlying memory as |unsigned char|.
218 * Presently memory inside |Utf8Unit| is *only* stored as |char|, and we are
219 * loath to offer a way to write non-|char| data until absolutely necessary.
221 inline const unsigned char* Utf8AsUnsignedChars(const Utf8Unit* aUnits) {
222 static_assert(sizeof(Utf8Unit) == sizeof(unsigned char),
223 "sizes must match to permissibly reinterpret_cast<>");
224 static_assert(alignof(Utf8Unit) == alignof(unsigned char),
225 "alignment must match to permissibly reinterpret_cast<>");
227 // The static_asserts above only enable the reinterpret_cast<> to occur.
229 // Dereferencing the resulting pointer is a separate question. Any object's
230 // memory may be interpreted as |unsigned char| per C++11 [basic.lval]p10, but
231 // this doesn't guarantee what values will be observed. If |char| is
232 // implemented to act like |unsigned char|, we're good to go: memory for the
233 // |char| in |Utf8Unit| acts as we need. But if |char| is implemented to act
234 // like |signed char|, dereferencing produces the right value only if the
235 // |char| types all use two's-complement representation. Every modern
236 // compiler does this, and there's a C++ proposal to standardize it.
237 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html So
238 // *technically* this is implementation-defined -- but everyone does it and
239 // this behavior is being standardized.
240 return reinterpret_cast<const unsigned char*>(aUnits);
243 /** Returns true iff |aUnit| is an ASCII value. */
244 constexpr bool IsAscii(Utf8Unit aUnit) {
245 return IsAscii(aUnit.toUnsignedChar());
249 * Return true if the given span of memory consists of a valid UTF-8
250 * string and false otherwise.
252 * The string *may* contain U+0000 NULL code points.
254 inline bool IsUtf8(mozilla::Span<const char> aString) {
255 #if MOZ_HAS_JSRUST()
256 size_t length = aString.Length();
257 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(aString.Elements());
258 // For short strings, the function call is a pessimization, and the SIMD
259 // code won't have a chance to kick in anyway.
260 if (length < 16) {
261 for (size_t i = 0; i < length; i++) {
262 if (ptr[i] >= 0x80U) {
263 ptr += i;
264 length -= i;
265 goto end;
268 return true;
270 end:
271 return length == encoding_utf8_valid_up_to(ptr, length);
272 #else
273 return detail::IsValidUtf8(aString.Elements(), aString.Length());
274 #endif
277 #if MOZ_HAS_JSRUST()
279 // See Latin1.h for conversions between Latin1 and UTF-8.
282 * Returns the index of the start of the first malformed byte
283 * sequence or the length of the string if there are none.
285 inline size_t Utf8ValidUpTo(mozilla::Span<const char> aString) {
286 return encoding_utf8_valid_up_to(
287 reinterpret_cast<const uint8_t*>(aString.Elements()), aString.Length());
291 * Converts potentially-invalid UTF-16 to UTF-8 replacing lone surrogates
292 * with the REPLACEMENT CHARACTER.
294 * The length of aDest must be at least the length of aSource times three.
296 * Returns the number of code units written.
298 inline size_t ConvertUtf16toUtf8(mozilla::Span<const char16_t> aSource,
299 mozilla::Span<char> aDest) {
300 return encoding_mem_convert_utf16_to_utf8(
301 aSource.Elements(), aSource.Length(), aDest.Elements(), aDest.Length());
305 * Converts potentially-invalid UTF-8 to UTF-16 replacing malformed byte
306 * sequences with the REPLACEMENT CHARACTER with potentially insufficient
307 * output space.
309 * Returns the number of code units read and the number of bytes written.
311 * If the output isn't large enough, not all input is consumed.
313 * The conversion is guaranteed to be complete if the length of aDest is
314 * at least the length of aSource times three.
316 * The output is always valid UTF-8 ending on scalar value boundary
317 * even in the case of partial conversion.
319 * The semantics of this function match the semantics of
320 * TextEncoder.encodeInto.
321 * https://encoding.spec.whatwg.org/#dom-textencoder-encodeinto
323 inline std::tuple<size_t, size_t> ConvertUtf16toUtf8Partial(
324 mozilla::Span<const char16_t> aSource, mozilla::Span<char> aDest) {
325 size_t srcLen = aSource.Length();
326 size_t dstLen = aDest.Length();
327 encoding_mem_convert_utf16_to_utf8_partial(aSource.Elements(), &srcLen,
328 aDest.Elements(), &dstLen);
329 return std::make_tuple(srcLen, dstLen);
333 * Converts potentially-invalid UTF-8 to UTF-16 replacing malformed byte
334 * sequences with the REPLACEMENT CHARACTER.
336 * Returns the number of code units written.
338 * The length of aDest must be at least one greater than the length of aSource
339 * even though the last slot isn't written to.
341 * If you know that the input is valid for sure, use
342 * UnsafeConvertValidUtf8toUtf16() instead.
344 inline size_t ConvertUtf8toUtf16(mozilla::Span<const char> aSource,
345 mozilla::Span<char16_t> aDest) {
346 return encoding_mem_convert_utf8_to_utf16(
347 aSource.Elements(), aSource.Length(), aDest.Elements(), aDest.Length());
351 * Converts known-valid UTF-8 to UTF-16. If the input might be invalid,
352 * use ConvertUtf8toUtf16() or ConvertUtf8toUtf16WithoutReplacement() instead.
354 * Returns the number of code units written.
356 * The length of aDest must be at least the length of aSource.
358 inline size_t UnsafeConvertValidUtf8toUtf16(mozilla::Span<const char> aSource,
359 mozilla::Span<char16_t> aDest) {
360 return encoding_mem_convert_str_to_utf16(aSource.Elements(), aSource.Length(),
361 aDest.Elements(), aDest.Length());
365 * Converts potentially-invalid UTF-8 to valid UTF-16 signaling on error.
367 * Returns the number of code units written or `mozilla::Nothing` if the
368 * input was invalid.
370 * The length of the destination buffer must be at least the length of the
371 * source buffer.
373 * When the input was invalid, some output may have been written.
375 * If you know that the input is valid for sure, use
376 * UnsafeConvertValidUtf8toUtf16() instead.
378 inline mozilla::Maybe<size_t> ConvertUtf8toUtf16WithoutReplacement(
379 mozilla::Span<const char> aSource, mozilla::Span<char16_t> aDest) {
380 size_t written = encoding_mem_convert_utf8_to_utf16_without_replacement(
381 aSource.Elements(), aSource.Length(), aDest.Elements(), aDest.Length());
382 if (MOZ_UNLIKELY(written == std::numeric_limits<size_t>::max())) {
383 return mozilla::Nothing();
385 return mozilla::Some(written);
388 #endif // MOZ_HAS_JSRUST
391 * Returns true iff |aUnit| is a UTF-8 trailing code unit matching the pattern
392 * 0b10xx'xxxx.
394 inline bool IsTrailingUnit(Utf8Unit aUnit) {
395 return (aUnit.toUint8() & 0b1100'0000) == 0b1000'0000;
399 * Given |aLeadUnit| that is a non-ASCII code unit, a pointer to an |Iter aIter|
400 * that (initially) itself points one unit past |aLeadUnit|, and
401 * |const EndIter& aEnd| that denotes the end of the UTF-8 data when compared
402 * against |*aIter| using |aEnd - *aIter|:
404 * If |aLeadUnit| and subsequent code units computed using |*aIter| (up to
405 * |aEnd|) encode a valid code point -- not exceeding Unicode's range, not a
406 * surrogate, in shortest form -- then return Some(that code point) and advance
407 * |*aIter| past those code units.
409 * Otherwise decrement |*aIter| (so that it points at |aLeadUnit|) and return
410 * Nothing().
412 * |Iter| and |EndIter| are generalized concepts most easily understood as if
413 * they were |const char*|, |const unsigned char*|, or |const Utf8Unit*|:
414 * iterators that when dereferenced can be used to construct a |Utf8Unit| and
415 * that can be compared and modified in certain limited ways. (Carefully note
416 * that this function mutates |*aIter|.) |Iter| and |EndIter| are template
417 * parameters to support more-complicated adaptor iterators.
419 * The template parameters after |Iter| allow users to implement custom handling
420 * for various forms of invalid UTF-8. A version of this function that defaults
421 * all such handling to no-ops is defined below this function. To learn how to
422 * define your own custom handling, consult the implementation of that function,
423 * which documents exactly how custom handler functors are invoked.
425 * This function is MOZ_ALWAYS_INLINE: if you don't need that, use the version
426 * of this function without the "Inline" suffix on the name.
428 template <typename Iter, typename EndIter, class OnBadLeadUnit,
429 class OnNotEnoughUnits, class OnBadTrailingUnit, class OnBadCodePoint,
430 class OnNotShortestForm>
431 MOZ_ALWAYS_INLINE Maybe<char32_t> DecodeOneUtf8CodePointInline(
432 const Utf8Unit aLeadUnit, Iter* aIter, const EndIter& aEnd,
433 OnBadLeadUnit aOnBadLeadUnit, OnNotEnoughUnits aOnNotEnoughUnits,
434 OnBadTrailingUnit aOnBadTrailingUnit, OnBadCodePoint aOnBadCodePoint,
435 OnNotShortestForm aOnNotShortestForm) {
436 MOZ_ASSERT(Utf8Unit((*aIter)[-1]) == aLeadUnit);
438 char32_t n = aLeadUnit.toUint8();
439 MOZ_ASSERT(!IsAscii(n));
441 // |aLeadUnit| determines the number of trailing code units in the code point
442 // and the bits of |aLeadUnit| that contribute to the code point's value.
443 uint8_t remaining;
444 uint32_t min;
445 if ((n & 0b1110'0000) == 0b1100'0000) {
446 remaining = 1;
447 min = 0x80;
448 n &= 0b0001'1111;
449 } else if ((n & 0b1111'0000) == 0b1110'0000) {
450 remaining = 2;
451 min = 0x800;
452 n &= 0b0000'1111;
453 } else if ((n & 0b1111'1000) == 0b1111'0000) {
454 remaining = 3;
455 min = 0x10000;
456 n &= 0b0000'0111;
457 } else {
458 *aIter -= 1;
459 aOnBadLeadUnit();
460 return Nothing();
463 // If the code point would require more code units than remain, the encoding
464 // is invalid.
465 auto actual = aEnd - *aIter;
466 if (MOZ_UNLIKELY(actual < remaining)) {
467 *aIter -= 1;
468 aOnNotEnoughUnits(AssertedCast<uint8_t>(actual + 1), remaining + 1);
469 return Nothing();
472 for (uint8_t i = 0; i < remaining; i++) {
473 const Utf8Unit unit(*(*aIter)++);
475 // Every non-leading code unit in properly encoded UTF-8 has its high
476 // bit set and the next-highest bit unset.
477 if (MOZ_UNLIKELY(!IsTrailingUnit(unit))) {
478 uint8_t unitsObserved = i + 1 + 1;
479 *aIter -= unitsObserved;
480 aOnBadTrailingUnit(unitsObserved);
481 return Nothing();
484 // The code point being encoded is the concatenation of all the
485 // unconstrained bits.
486 n = (n << 6) | (unit.toUint8() & 0b0011'1111);
489 // UTF-16 surrogates and values outside the Unicode range are invalid.
490 if (MOZ_UNLIKELY(n > 0x10FFFF || (0xD800 <= n && n <= 0xDFFF))) {
491 uint8_t unitsObserved = remaining + 1;
492 *aIter -= unitsObserved;
493 aOnBadCodePoint(n, unitsObserved);
494 return Nothing();
497 // Overlong code points are also invalid.
498 if (MOZ_UNLIKELY(n < min)) {
499 uint8_t unitsObserved = remaining + 1;
500 *aIter -= unitsObserved;
501 aOnNotShortestForm(n, unitsObserved);
502 return Nothing();
505 return Some(n);
509 * Identical to the above function, but not forced to be instantiated inline --
510 * the compiler is permitted to common up separate invocations if it chooses.
512 template <typename Iter, typename EndIter, class OnBadLeadUnit,
513 class OnNotEnoughUnits, class OnBadTrailingUnit, class OnBadCodePoint,
514 class OnNotShortestForm>
515 inline Maybe<char32_t> DecodeOneUtf8CodePoint(
516 const Utf8Unit aLeadUnit, Iter* aIter, const EndIter& aEnd,
517 OnBadLeadUnit aOnBadLeadUnit, OnNotEnoughUnits aOnNotEnoughUnits,
518 OnBadTrailingUnit aOnBadTrailingUnit, OnBadCodePoint aOnBadCodePoint,
519 OnNotShortestForm aOnNotShortestForm) {
520 return DecodeOneUtf8CodePointInline(aLeadUnit, aIter, aEnd, aOnBadLeadUnit,
521 aOnNotEnoughUnits, aOnBadTrailingUnit,
522 aOnBadCodePoint, aOnNotShortestForm);
526 * Like the always-inlined function above, but with no-op behavior from all
527 * trailing if-invalid notifier functors.
529 * This function is MOZ_ALWAYS_INLINE: if you don't need that, use the version
530 * of this function without the "Inline" suffix on the name.
532 template <typename Iter, typename EndIter>
533 MOZ_ALWAYS_INLINE Maybe<char32_t> DecodeOneUtf8CodePointInline(
534 const Utf8Unit aLeadUnit, Iter* aIter, const EndIter& aEnd) {
535 // aOnBadLeadUnit is called when |aLeadUnit| itself is an invalid lead unit in
536 // a multi-unit code point. It is passed no arguments: the caller already has
537 // |aLeadUnit| on hand, so no need to provide it again.
538 auto onBadLeadUnit = []() {};
540 // aOnNotEnoughUnits is called when |aLeadUnit| properly indicates a code
541 // point length, but there aren't enough units from |*aIter| to |aEnd| to
542 // satisfy that length. It is passed the number of code units actually
543 // available (according to |aEnd - *aIter|) and the number of code units that
544 // |aLeadUnit| indicates are needed. Both numbers include the contribution
545 // of |aLeadUnit| itself: so |aUnitsAvailable <= 3|, |aUnitsNeeded <= 4|, and
546 // |aUnitsAvailable < aUnitsNeeded|. As above, it also is not passed the lead
547 // code unit.
548 auto onNotEnoughUnits = [](uint8_t aUnitsAvailable, uint8_t aUnitsNeeded) {};
550 // aOnBadTrailingUnit is called when one of the trailing code units implied by
551 // |aLeadUnit| doesn't match the 0b10xx'xxxx bit pattern that all UTF-8
552 // trailing code units must satisfy. It is passed the total count of units
553 // observed (including |aLeadUnit|). The bad trailing code unit will
554 // conceptually be at |(*aIter)[aUnitsObserved - 1]| if this functor is
555 // called, and so |aUnitsObserved <= 4|.
556 auto onBadTrailingUnit = [](uint8_t aUnitsObserved) {};
558 // aOnBadCodePoint is called when a structurally-correct code point encoding
559 // is found, but the *value* that is encoded is not a valid code point: either
560 // because it exceeded the U+10FFFF Unicode maximum code point, or because it
561 // was a UTF-16 surrogate. It is passed the non-code point value and the
562 // number of code units used to encode it.
563 auto onBadCodePoint = [](char32_t aBadCodePoint, uint8_t aUnitsObserved) {};
565 // aOnNotShortestForm is called when structurally-correct encoding is found,
566 // but the encoded value should have been encoded in fewer code units (e.g.
567 // mis-encoding U+0000 as 0b1100'0000 0b1000'0000 in two code units instead of
568 // as 0b0000'0000). It is passed the mis-encoded code point (which will be
569 // valid and not a surrogate) and the count of code units that mis-encoded it.
570 auto onNotShortestForm = [](char32_t aBadCodePoint, uint8_t aUnitsObserved) {
573 return DecodeOneUtf8CodePointInline(aLeadUnit, aIter, aEnd, onBadLeadUnit,
574 onNotEnoughUnits, onBadTrailingUnit,
575 onBadCodePoint, onNotShortestForm);
579 * Identical to the above function, but not forced to be instantiated inline --
580 * the compiler/linker are allowed to common up separate invocations.
582 template <typename Iter, typename EndIter>
583 inline Maybe<char32_t> DecodeOneUtf8CodePoint(const Utf8Unit aLeadUnit,
584 Iter* aIter,
585 const EndIter& aEnd) {
586 return DecodeOneUtf8CodePointInline(aLeadUnit, aIter, aEnd);
589 } // namespace mozilla
591 #endif /* mozilla_Utf8_h */