1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
* Copyright (C) 2012 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include <algorithm>
#include <optional>
#include "base/strings/span_printf.h"
#include "third_party/blink/renderer/platform/wtf/dtoa.h"
#include "third_party/blink/renderer/platform/wtf/text/integer_to_string_conversion.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace WTF {
String StringBuilder::ReleaseString() {
if (!length_)
return g_empty_string;
if (string_.IsNull())
BuildString<String>();
String string = std::move(string_);
Clear();
return string;
}
String StringBuilder::ToString() {
if (!length_)
return g_empty_string;
if (string_.IsNull())
BuildString<String>();
return string_;
}
AtomicString StringBuilder::ToAtomicString() {
if (!length_)
return g_empty_atom;
if (string_.IsNull())
BuildString<AtomicString>();
return AtomicString(string_);
}
String StringBuilder::Substring(unsigned start, unsigned length) const {
if (start >= length_)
return g_empty_string;
if (!string_.IsNull())
return string_.Substring(start, length);
length = std::min(length, length_ - start);
if (is_8bit_)
return String(Span8().subspan(start, length));
return String(Span16().subspan(start, length));
}
StringView StringBuilder::SubstringView(unsigned start, unsigned length) const {
if (start >= length_)
return StringView();
if (!string_.IsNull())
return StringView(string_, start, length);
length = std::min(length, length_ - start);
if (is_8bit_)
return StringView(Span8().subspan(start, length));
return StringView(Span16().subspan(start, length));
}
void StringBuilder::Swap(StringBuilder& builder) {
std::optional<Buffer8> buffer8;
std::optional<Buffer16> buffer16;
if (has_buffer_) {
if (is_8bit_) {
buffer8 = std::move(buffer8_);
buffer8_.~Buffer8();
} else {
buffer16 = std::move(buffer16_);
buffer16_.~Buffer16();
}
}
if (builder.has_buffer_) {
if (builder.is_8bit_) {
new (&buffer8_) Buffer8(std::move(builder.buffer8_));
builder.buffer8_.~Buffer8();
} else {
new (&buffer16_) Buffer16(std::move(builder.buffer16_));
builder.buffer16_.~Buffer16();
}
}
if (buffer8)
new (&builder.buffer8_) Buffer8(std::move(*buffer8));
else if (buffer16)
new (&builder.buffer16_) Buffer16(std::move(*buffer16));
std::swap(string_, builder.string_);
std::swap(length_, builder.length_);
std::swap(is_8bit_, builder.is_8bit_);
std::swap(has_buffer_, builder.has_buffer_);
}
void StringBuilder::ClearBuffer() {
if (!has_buffer_)
return;
if (is_8bit_)
buffer8_.~Buffer8();
else
buffer16_.~Buffer16();
has_buffer_ = false;
}
void StringBuilder::Ensure16Bit() {
EnsureBuffer16(0);
}
void StringBuilder::Clear() {
ClearBuffer();
string_ = String();
length_ = 0;
is_8bit_ = true;
}
unsigned StringBuilder::Capacity() const {
if (!HasBuffer())
return 0;
if (is_8bit_)
return buffer8_.capacity();
return buffer16_.capacity();
}
void StringBuilder::ReserveCapacity(unsigned new_capacity) {
if (!HasBuffer()) {
if (is_8bit_)
CreateBuffer8(new_capacity);
else
CreateBuffer16(new_capacity);
return;
}
if (is_8bit_)
buffer8_.reserve(new_capacity);
else
buffer16_.reserve(new_capacity);
}
void StringBuilder::Reserve16BitCapacity(unsigned new_capacity) {
if (is_8bit_ || !HasBuffer())
CreateBuffer16(new_capacity);
else
buffer16_.reserve(new_capacity);
}
void StringBuilder::Resize(unsigned new_size) {
DCHECK_LE(new_size, length_);
string_ = string_.Left(new_size);
length_ = new_size;
if (HasBuffer()) {
if (is_8bit_)
buffer8_.resize(new_size);
else
buffer16_.resize(new_size);
}
}
void StringBuilder::CreateBuffer8(unsigned added_size) {
DCHECK(!HasBuffer());
DCHECK(is_8bit_);
new (&buffer8_) Buffer8;
has_buffer_ = true;
// createBuffer is called right before appending addedSize more bytes. We
// want to ensure we have enough space to fit m_string plus the added
// size.
//
// We also ensure that we have at least the initialBufferSize of extra space
// for appending new bytes to avoid future mallocs for appending short
// strings or single characters. This is a no-op if m_length == 0 since
// initialBufferSize() is the same as the inline capacity of the vector.
// This allows doing append(string); append('\0') without extra mallocs.
buffer8_.ReserveInitialCapacity(length_ +
std::max(added_size, InitialBufferSize()));
length_ = 0;
Append(string_);
string_ = String();
}
void StringBuilder::CreateBuffer16(unsigned added_size) {
DCHECK(is_8bit_ || !HasBuffer());
Buffer8 buffer8;
unsigned length = length_;
wtf_size_t capacity = 0;
if (has_buffer_) {
buffer8 = std::move(buffer8_);
buffer8_.~Buffer8();
capacity = buffer8.capacity();
}
new (&buffer16_) Buffer16;
has_buffer_ = true;
capacity = std::max<wtf_size_t>(
capacity, length_ + std::max<unsigned>(
added_size, InitialBufferSize() / sizeof(UChar)));
// See CreateBuffer8's call to ReserveInitialCapacity for why we do this.
buffer16_.ReserveInitialCapacity(capacity);
is_8bit_ = false;
length_ = 0;
if (!buffer8.empty()) {
Append(base::span(buffer8).first(length));
return;
}
Append(string_);
string_ = String();
}
bool StringBuilder::DoesAppendCauseOverflow(unsigned length) const {
unsigned new_length = length_ + length;
if (new_length < Capacity()) {
return false;
}
// Expanding the underlying vector usually doubles its capacity—unless there
// is no current buffer, in which case `length` will become the capacity.
if (is_8bit_) {
return (HasBuffer() ? buffer8_.capacity() * 2 : length) >=
Buffer8::MaxCapacity();
}
return (HasBuffer() ? buffer16_.capacity() * 2 : length) >=
Buffer16::MaxCapacity();
}
void StringBuilder::Append(base::span<const UChar> chars) {
if (chars.empty()) {
return;
}
DCHECK(chars.data());
// If there's only one char we use append(UChar) instead since it will
// check for latin1 and avoid converting to 16bit if possible.
if (chars.size() == 1) {
Append(chars[0]);
return;
}
unsigned length = base::checked_cast<unsigned>(chars.size());
EnsureBuffer16(length);
buffer16_.AppendSpan(chars);
length_ += length;
}
void StringBuilder::Append(base::span<const LChar> chars) {
if (chars.empty()) {
return;
}
DCHECK(chars.data());
unsigned length = base::checked_cast<unsigned>(chars.size());
if (is_8bit_) {
EnsureBuffer8(length);
buffer8_.AppendSpan(chars);
length_ += length;
return;
}
EnsureBuffer16(length);
buffer16_.AppendSpan(chars);
length_ += length;
}
void StringBuilder::AppendNumber(bool number) {
AppendNumber(static_cast<uint8_t>(number));
}
void StringBuilder::AppendNumber(float number) {
AppendNumber(static_cast<double>(number));
}
void StringBuilder::AppendNumber(double number, unsigned precision) {
NumberToStringBuffer buffer;
Append(NumberToFixedPrecisionString(number, precision, buffer));
}
void StringBuilder::AppendFormat(const char* format, ...) {
va_list args;
static constexpr unsigned kDefaultSize = 256;
Vector<char, kDefaultSize> buffer(kDefaultSize);
va_start(args, format);
int length = base::VSpanPrintf(buffer, format, args);
va_end(args);
DCHECK_GE(length, 0);
if (length >= static_cast<int>(kDefaultSize)) {
buffer.Grow(length + 1);
va_start(args, format);
length = base::VSpanPrintf(buffer, format, args);
va_end(args);
}
Append(base::as_byte_span(buffer).first(static_cast<wtf_size_t>(length)));
}
void StringBuilder::erase(unsigned index) {
if (index >= length_)
return;
if (is_8bit_) {
EnsureBuffer8(0);
buffer8_.EraseAt(index);
} else {
EnsureBuffer16(0);
buffer16_.EraseAt(index);
}
--length_;
}
} // namespace WTF
|