summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/text/segmented_string.h
blob: d0e893ec59f2e02fc8329b5c1c28703bc1f11db8 (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
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
/*
    Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_TEXT_SEGMENTED_STRING_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_TEXT_SEGMENTED_STRING_H_

#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/deque.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/text_position.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

class SegmentedString;

class PLATFORM_EXPORT SegmentedSubstring {
  DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();

 public:
  SegmentedSubstring() { data_.string8_ptr = nullptr; }

  SegmentedSubstring(const String& str)
      : length_(str.length()),
        string_(str) {
    if (length_) {
      if (string_.Is8Bit()) {
        is8_bit_ = true;
        data_.string8_ptr = string_.Characters8();
        current_char_ = *data_.string8_ptr;
      } else {
        is8_bit_ = false;
        data_.string16_ptr = string_.Characters16();
        current_char_ = *data_.string16_ptr;
      }
    } else {
      is8_bit_ = true;
      data_.string8_ptr = nullptr;
    }
  }

  void Clear() {
    length_ = 0;
    is8_bit_ = true;
    data_.string8_ptr = nullptr;
    current_char_ = 0;
  }

  bool ExcludeLineNumbers() const { return !do_not_exclude_line_numbers_; }
  bool DoNotExcludeLineNumbers() const { return do_not_exclude_line_numbers_; }

  void SetExcludeLineNumbers() { do_not_exclude_line_numbers_ = false; }

  int NumberOfCharactersConsumed() const { return string_.length() - length_; }

  void AppendTo(StringBuilder& builder) const {
    int offset = string_.length() - length_;

    if (!offset) {
      if (length_)
        builder.Append(string_);
    } else {
      builder.Append(string_.Substring(offset, length_));
    }
  }

  bool PushIfPossible(UChar c) {
    if (!length_)
      return false;

    // This checks if either 8 or 16 bit strings are in the first character
    // (where we can't rewind). Since length_ is greater than zero, we can check
    // the Impl() directly and avoid a branch here.
    if (data_.void_ptr == string_.Impl()->Bytes())
      return false;

    if (is8_bit_) {
      if (*(data_.string8_ptr - 1) != c)
        return false;

      --data_.string8_ptr;
    } else {
      if (*(data_.string16_ptr - 1) != c)
        return false;

      --data_.string16_ptr;
    }

    current_char_ = c;
    ++length_;
    return true;
  }

  ALWAYS_INLINE UChar GetCurrentChar() const { return current_char_; }

  ALWAYS_INLINE void IncrementAndDecrementLength() {
    current_char_ = is8_bit_ ? *++data_.string8_ptr : *++data_.string16_ptr;
    --length_;
  }

  String CurrentSubString(unsigned length) {
    int offset = string_.length() - length_;
    return string_.Substring(offset, length);
  }

  ALWAYS_INLINE int length() const { return length_; }

 private:
  union {
    const LChar* string8_ptr;
    const UChar* string16_ptr;
    const void* void_ptr;
  } data_;
  int length_ = 0;
  UChar current_char_ = 0;
  bool do_not_exclude_line_numbers_ = true;
  bool is8_bit_ = true;
  String string_;
};

class PLATFORM_EXPORT SegmentedString {
  DISALLOW_NEW();

 public:
  SegmentedString()
      : number_of_characters_consumed_prior_to_current_string_(0),
        number_of_characters_consumed_prior_to_current_line_(0),
        current_line_(0),
        closed_(false),
        empty_(true) {}

  SegmentedString(const String& str)
      : current_string_(str),
        number_of_characters_consumed_prior_to_current_string_(0),
        number_of_characters_consumed_prior_to_current_line_(0),
        current_line_(0),
        closed_(false),
        empty_(!str.length()) {}

  void Clear();
  void Close();

  void Append(const SegmentedString&);
  enum class PrependType {
    kNewInput = 0,
    kUnconsume = 1,
  };
  void Prepend(const SegmentedString&, PrependType);

  bool ExcludeLineNumbers() const {
    return current_string_.ExcludeLineNumbers();
  }
  void SetExcludeLineNumbers();

  void Push(UChar);

  bool IsEmpty() const { return empty_; }
  unsigned length() const;

  bool IsClosed() const { return closed_; }

  enum LookAheadResult {
    kDidNotMatch,
    kDidMatch,
    kNotEnoughCharacters,
  };

  LookAheadResult LookAhead(const String& string) {
    return LookAheadInline(string, kTextCaseSensitive);
  }
  LookAheadResult LookAheadIgnoringCase(const String& string) {
    return LookAheadInline(string, kTextCaseASCIIInsensitive);
  }

  ALWAYS_INLINE void Advance() {
    if (LIKELY(current_string_.length() > 1)) {
      current_string_.IncrementAndDecrementLength();
    } else {
      AdvanceSubstring();
    }
  }

  ALWAYS_INLINE void UpdateLineNumber() {
    if (LIKELY(current_string_.DoNotExcludeLineNumbers())) {
      ++current_line_;
      // Plus 1 because numberOfCharactersConsumed value hasn't incremented yet;
      // it does with length() decrement below.
      number_of_characters_consumed_prior_to_current_line_ =
          NumberOfCharactersConsumed() + 1;
    }
  }

  ALWAYS_INLINE void AdvanceAndUpdateLineNumber() {
    DCHECK_GE(current_string_.length(), 1);

    if (current_string_.GetCurrentChar() == '\n')
      UpdateLineNumber();

    if (LIKELY(current_string_.length() > 1)) {
      current_string_.IncrementAndDecrementLength();
    } else {
      AdvanceSubstring();
    }
  }

  ALWAYS_INLINE void AdvanceAndASSERT(UChar expected_character) {
    DCHECK_EQ(expected_character, CurrentChar());
    Advance();
  }

  ALWAYS_INLINE void AdvanceAndASSERTIgnoringCase(UChar expected_character) {
    DCHECK_EQ(WTF::Unicode::FoldCase(CurrentChar()),
              WTF::Unicode::FoldCase(expected_character));
    Advance();
  }

  ALWAYS_INLINE void AdvancePastNonNewline() {
    DCHECK_NE(CurrentChar(), '\n');
    Advance();
  }

  ALWAYS_INLINE void AdvancePastNewlineAndUpdateLineNumber() {
    DCHECK_EQ(CurrentChar(), '\n');
    DCHECK_GE(current_string_.length(), 1);

    UpdateLineNumber();

    if (LIKELY(current_string_.length() > 1)) {
      current_string_.IncrementAndDecrementLength();
    } else {
      AdvanceSubstring();
    }
  }

  // Writes the consumed characters into consumedCharacters, which must
  // have space for at least |count| characters.
  void Advance(unsigned count, UChar* consumed_characters);

  int NumberOfCharactersConsumed() const {
    int number_of_pushed_characters = 0;
    return number_of_characters_consumed_prior_to_current_string_ +
           current_string_.NumberOfCharactersConsumed() -
           number_of_pushed_characters;
  }

  String ToString() const;

  ALWAYS_INLINE UChar CurrentChar() const {
    return current_string_.GetCurrentChar();
  }

  // The method is moderately slow, comparing to currentLine method.
  OrdinalNumber CurrentColumn() const;
  OrdinalNumber CurrentLine() const;
  // Sets value of line/column variables. Column is specified indirectly by a
  // parameter columnAftreProlog which is a value of column that we should get
  // after a prolog (first prologLength characters) has been consumed.
  void SetCurrentPosition(OrdinalNumber line,
                          OrdinalNumber column_aftre_prolog,
                          int prolog_length);

 private:
  void Append(const SegmentedSubstring&);
  void Prepend(const SegmentedSubstring&, PrependType);

  void AdvanceSubstring();

  inline LookAheadResult LookAheadInline(const String& string,
                                         TextCaseSensitivity case_sensitivity) {
    if (string.length() <= static_cast<unsigned>(current_string_.length())) {
      String current_substring =
          current_string_.CurrentSubString(string.length());
      if (current_substring.StartsWith(string, case_sensitivity))
        return kDidMatch;
      return kDidNotMatch;
    }
    return LookAheadSlowCase(string, case_sensitivity);
  }

  LookAheadResult LookAheadSlowCase(const String& string,
                                    TextCaseSensitivity case_sensitivity) {
    unsigned count = string.length();
    if (count > length())
      return kNotEnoughCharacters;
    UChar* consumed_characters;
    String consumed_string =
        String::CreateUninitialized(count, consumed_characters);
    Advance(count, consumed_characters);
    LookAheadResult result = kDidNotMatch;
    if (consumed_string.StartsWith(string, case_sensitivity))
      result = kDidMatch;
    Prepend(SegmentedString(consumed_string), PrependType::kUnconsume);
    return result;
  }

  bool IsComposite() const { return !substrings_.IsEmpty(); }

  SegmentedSubstring current_string_;
  int number_of_characters_consumed_prior_to_current_string_;
  int number_of_characters_consumed_prior_to_current_line_;
  int current_line_;
  Deque<SegmentedSubstring> substrings_;
  bool closed_;
  bool empty_;
};

}  // namespace blink

#endif