summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/wtf/text/string_builder.cc
blob: 5f6594a71c502d1adead434b12b6bef0e48655be (plain)
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
/*
 * 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 "base/optional.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::ToString() {
  if (!length_)
    return g_empty_string;
  if (string_.IsNull()) {
    if (is8_bit_)
      string_ = String(Characters8(), length_);
    else
      string_ = String(Characters16(), length_);
    ClearBuffer();
  }
  return string_;
}

AtomicString StringBuilder::ToAtomicString() {
  if (!length_)
    return g_empty_atom;
  if (string_.IsNull()) {
    if (is8_bit_)
      string_ = AtomicString(Characters8(), length_);
    else
      string_ = AtomicString(Characters16(), length_);
    ClearBuffer();
  }
  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 (is8_bit_)
    return String(Characters8() + start, length);
  return String(Characters16() + start, length);
}

void StringBuilder::Swap(StringBuilder& builder) {
  base::Optional<Buffer8> buffer8;
  base::Optional<Buffer16> buffer16;
  if (has_buffer_) {
    if (is8_bit_) {
      buffer8 = std::move(buffer8_);
      buffer8_.~Buffer8();
    } else {
      buffer16 = std::move(buffer16_);
      buffer16_.~Buffer16();
    }
  }

  if (builder.has_buffer_) {
    if (builder.is8_bit_) {
      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(is8_bit_, builder.is8_bit_);
  std::swap(has_buffer_, builder.has_buffer_);
}

void StringBuilder::ClearBuffer() {
  if (!has_buffer_)
    return;
  if (is8_bit_)
    buffer8_.~Buffer8();
  else
    buffer16_.~Buffer16();
  has_buffer_ = false;
}

void StringBuilder::Clear() {
  ClearBuffer();
  string_ = String();
  length_ = 0;
  is8_bit_ = true;
}

unsigned StringBuilder::Capacity() const {
  if (!HasBuffer())
    return 0;
  if (is8_bit_)
    return buffer8_.capacity();
  return buffer16_.capacity();
}

void StringBuilder::ReserveCapacity(unsigned new_capacity) {
  if (is8_bit_)
    EnsureBuffer8(new_capacity);
  else
    EnsureBuffer16(new_capacity);
}

void StringBuilder::Resize(unsigned new_size) {
  DCHECK_LE(new_size, length_);
  string_ = string_.Left(new_size);
  length_ = new_size;
  if (HasBuffer()) {
    if (is8_bit_)
      buffer8_.resize(new_size);
    else
      buffer16_.resize(new_size);
  }
}

void StringBuilder::CreateBuffer8(unsigned added_size) {
  DCHECK(!HasBuffer());
  DCHECK(is8_bit_);
  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(is8_bit_ || !HasBuffer());
  Buffer8 buffer8;
  unsigned length = length_;
  if (has_buffer_) {
    buffer8 = std::move(buffer8_);
    buffer8_.~Buffer8();
  }
  new (&buffer16_) Buffer16;
  has_buffer_ = true;
  // See createBuffer8's call to reserveInitialCapacity for why we do this.
  buffer16_.ReserveInitialCapacity(
      length_ +
      std::max<unsigned>(added_size, InitialBufferSize() / sizeof(UChar)));
  is8_bit_ = false;
  length_ = 0;
  if (!buffer8.IsEmpty()) {
    Append(buffer8.data(), length);
    return;
  }
  Append(string_);
  string_ = String();
}

void StringBuilder::Append(const UChar* characters, unsigned length) {
  if (!length)
    return;
  DCHECK(characters);

  // 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 (length == 1) {
    Append(*characters);
    return;
  }

  EnsureBuffer16(length);
  buffer16_.Append(characters, length);
  length_ += length;
}

void StringBuilder::Append(const LChar* characters, unsigned length) {
  if (!length)
    return;
  DCHECK(characters);

  if (is8_bit_) {
    EnsureBuffer8(length);
    buffer8_.Append(characters, length);
    length_ += length;
    return;
  }

  EnsureBuffer16(length);
  buffer16_.Append(characters, length);
  length_ += length;
}

template <typename IntegerType>
static void AppendIntegerInternal(StringBuilder& builder, IntegerType input) {
  IntegerToStringConverter<IntegerType> converter(input);
  builder.Append(converter.Characters8(), converter.length());
}

void StringBuilder::AppendNumber(int number) {
  AppendIntegerInternal(*this, number);
}

void StringBuilder::AppendNumber(unsigned number) {
  AppendIntegerInternal(*this, number);
}

void StringBuilder::AppendNumber(long number) {
  AppendIntegerInternal(*this, number);
}

void StringBuilder::AppendNumber(unsigned long number) {
  AppendIntegerInternal(*this, number);
}

void StringBuilder::AppendNumber(long long number) {
  AppendIntegerInternal(*this, number);
}

void StringBuilder::AppendNumber(unsigned long long number) {
  AppendIntegerInternal(*this, number);
}

void StringBuilder::AppendNumber(double number, unsigned precision) {
  NumberToStringBuffer buffer;
  Append(NumberToFixedPrecisionString(number, precision, buffer));
}

void StringBuilder::erase(unsigned index) {
  if (index >= length_)
    return;

  if (is8_bit_) {
    EnsureBuffer8(0);
    buffer8_.EraseAt(index);
  } else {
    EnsureBuffer16(0);
    buffer16_.EraseAt(index);
  }
  --length_;
}

}  // namespace WTF