Debian Codegraph @

js/src/builtin/intl/FormatBuffer.h

raw
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/. */
6
7 #ifndef builtin_intl_FormatBuffer_h
8 #define builtin_intl_FormatBuffer_h
9
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Span.h"
12 #include "mozilla/TextUtils.h"
13
14 #include <stddef.h>
15 #include <stdint.h>
16
17 #include "js/AllocPolicy.h"
18 #include "js/TypeDecls.h"
19 #include "js/UniquePtr.h"
20 #include "js/Vector.h"
21 #include "vm/StringType.h"
22
23 namespace js::intl {
24
25 /**
26 * A buffer for formatting unified intl data.
27 */
28 template <typename CharT, size_t MinInlineCapacity = 0,
29 class AllocPolicy = TempAllocPolicy>
30 class FormatBuffer {
31 public:
32 using CharType = CharT;
33
34 // Allow move constructors, but not copy constructors, as this class owns a
35 // js::Vector.
36 FormatBuffer(FormatBuffer&& other) noexcept = default;
37 FormatBuffer& operator=(FormatBuffer&& other) noexcept = default;
38
39 explicit FormatBuffer(AllocPolicy aP = AllocPolicy())
40 : buffer_(std::move(aP)) {
41 // The initial capacity matches the requested minimum inline capacity, as
42 // long as it doesn't exceed |Vector::kMaxInlineBytes / sizeof(CharT)|. If
43 // this assertion should ever fail, either reduce |MinInlineCapacity| or
44 // make the FormatBuffer initialization fallible.
45 MOZ_ASSERT(buffer_.capacity() == MinInlineCapacity);
46 if constexpr (MinInlineCapacity > 0) {
47 // Ensure the full capacity is marked as reserved.
48 //
49 // Reserving the minimum inline capacity can never fail, even when
50 // simulating OOM.
51 MOZ_ALWAYS_TRUE(buffer_.reserve(MinInlineCapacity));
52 }
53 }
54
55 // Implicitly convert to a Span.
56 operator mozilla::Span<CharType>() { return buffer_; }
57 operator mozilla::Span<const CharType>() const { return buffer_; }
58
59 /**
60 * Ensures the buffer has enough space to accommodate |size| elements.
61 */
62 [[nodiscard]] bool reserve(size_t size) {
63 // Call |reserve| a second time to ensure its full capacity is marked as
64 // reserved.
65 return buffer_.reserve(size) && buffer_.reserve(buffer_.capacity());
66 }
67
68 /**
69 * Returns the raw data inside the buffer.
70 */
71 CharType* data() { return buffer_.begin(); }
72
73 /**
74 * Returns the count of elements written into the buffer.
75 */
76 size_t length() const { return buffer_.length(); }
77
78 /**
79 * Returns the buffer's overall capacity.
80 */
81 size_t capacity() const { return buffer_.capacity(); }
82
83 /**
84 * Resizes the buffer to the given amount of written elements.
85 */
86 void written(size_t amount) {
87 MOZ_ASSERT(amount <= buffer_.capacity());
88 // This sets |buffer_|'s internal size so that it matches how much was
89 // written. This is necessary because the write happens across FFI
90 // boundaries.
91 size_t curLength = length();
92 if (amount > curLength) {
93 buffer_.infallibleGrowByUninitialized(amount - curLength);
94 } else {
95 buffer_.shrinkBy(curLength - amount);
96 }
97 }
98
99 /**
100 * Copies the buffer's data to a JSString.
101 */
102 JSLinearString* toString(JSContext* cx) const {
103 static_assert(std::is_same_v<CharT, char16_t>);
104 return NewStringCopyN<CanGC>(cx, buffer_.begin(), buffer_.length());
105 }
106
107 /**
108 * Copies the buffer's data to a JSString. The buffer must contain only
109 * ASCII characters.
110 */
111 JSLinearString* toAsciiString(JSContext* cx) const {
112 static_assert(std::is_same_v<CharT, char>);
113
114 MOZ_ASSERT(mozilla::IsAscii(buffer_));
115 return NewStringCopyN<CanGC>(cx, buffer_.begin(), buffer_.length());
116 }
117
118 /**
119 * Extract this buffer's content as a null-terminated string.
120 */
121 UniquePtr<CharType[], JS::FreePolicy> extractStringZ() {
122 // Adding the NUL character on an already null-terminated string is likely
123 // an error. If there's ever a valid use case which triggers this assertion,
124 // we should change the below code to only conditionally add '\0'.
125 MOZ_ASSERT_IF(!buffer_.empty(), buffer_.end()[-1] != '\0');
126
127 if (!buffer_.append('\0')) {
128 return nullptr;
129 }
130 return UniquePtr<CharType[], JS::FreePolicy>(
131 buffer_.extractOrCopyRawBuffer());
132 }
133
134 private:
135 js::Vector<CharT, MinInlineCapacity, AllocPolicy> buffer_;
136 };
137
138 } // namespace js::intl
139
140 #endif /* builtin_intl_FormatBuffer_h */