Backed out changeset 9d8b4c0b99ed (bug 1945683) for causing btime failures. CLOSED...
[gecko.git] / dom / script / ScriptDecoding.h
blobea4c4aeef3a915c15df8de83ce39d13ec9b23eb2
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 /* Script decoding templates for processing byte data as UTF-8 or UTF-16. */
9 #ifndef mozilla_dom_ScriptDecoding_h
10 #define mozilla_dom_ScriptDecoding_h
12 #include "mozilla/Assertions.h" // MOZ_ASSERT
13 #include "mozilla/CheckedInt.h" // mozilla::CheckedInt
14 #include "mozilla/Encoding.h" // mozilla::Decoder
15 #include "mozilla/Span.h" // mozilla::Span
16 #include "mozilla/UniquePtr.h" // mozilla::UniquePtr
18 #include <stddef.h> // size_t
19 #include <stdint.h> // uint8_t, uint32_t
20 #include <type_traits> // std::is_same
22 namespace mozilla::dom {
24 template <typename Unit>
25 struct ScriptDecoding {
26 static_assert(std::is_same<Unit, char16_t>::value ||
27 std::is_same<Unit, Utf8Unit>::value,
28 "must be either UTF-8 or UTF-16");
31 template <>
32 struct ScriptDecoding<char16_t> {
33 static CheckedInt<size_t> MaxBufferLength(const UniquePtr<Decoder>& aDecoder,
34 uint32_t aByteLength) {
35 return aDecoder->MaxUTF16BufferLength(aByteLength);
38 static size_t DecodeInto(const UniquePtr<Decoder>& aDecoder,
39 const Span<const uint8_t>& aSrc,
40 Span<char16_t> aDest, bool aEndOfSource) {
41 uint32_t result;
42 size_t read;
43 size_t written;
44 std::tie(result, read, written, std::ignore) =
45 aDecoder->DecodeToUTF16(aSrc, aDest, aEndOfSource);
46 MOZ_ASSERT(result == kInputEmpty);
47 MOZ_ASSERT(read == aSrc.Length());
48 MOZ_ASSERT(written <= aDest.Length());
50 return written;
54 template <>
55 struct ScriptDecoding<Utf8Unit> {
56 static CheckedInt<size_t> MaxBufferLength(const UniquePtr<Decoder>& aDecoder,
57 uint32_t aByteLength) {
58 return aDecoder->MaxUTF8BufferLength(aByteLength);
61 static size_t DecodeInto(const UniquePtr<Decoder>& aDecoder,
62 const Span<const uint8_t>& aSrc,
63 Span<Utf8Unit> aDest, bool aEndOfSource) {
64 uint32_t result;
65 size_t read;
66 size_t written;
67 // Until C++ char8_t happens, our decoder APIs deal in |uint8_t| while
68 // |Utf8Unit| internally deals with |char|, so there's inevitable impedance
69 // mismatch between |aDest| as |Utf8Unit| and |AsWritableBytes(aDest)| as
70 // |Span<uint8_t>|. :-( The written memory will be interpreted through
71 // |char Utf8Unit::mValue| which is *permissible* because any object's
72 // memory can be interpreted as |char|. Unfortunately, until
73 // twos-complement is mandated, we have to play fast and loose and *hope*
74 // interpreting memory storing |uint8_t| as |char| will pick up the desired
75 // wrapped-around value. ¯\_(ツ)_/¯
76 std::tie(result, read, written, std::ignore) =
77 aDecoder->DecodeToUTF8(aSrc, AsWritableBytes(aDest), aEndOfSource);
78 MOZ_ASSERT(result == kInputEmpty);
79 MOZ_ASSERT(read == aSrc.Length());
80 MOZ_ASSERT(written <= aDest.Length());
82 return written;
86 } // namespace mozilla::dom
88 #endif // mozilla_dom_ScriptDecoding_h