File: utf16_ragel_iterator.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (169 lines) | stat: -rw-r--r-- 5,505 bytes parent folder | download | duplicates (3)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_UTF16_RAGEL_ITERATOR_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_UTF16_RAGEL_ITERATOR_H_

#include <unicode/uchar.h>

#include "base/check_op.h"
#include "base/containers/span.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/text/emoji_segmentation_category.h"
#include "third_party/blink/renderer/platform/text/emoji_segmentation_category_inline_header.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/text/character_names.h"

namespace blink {

// UTF16RagelIterator is set up on top of a UTF-16 UChar* buffer iterating over
// a Blink internal text string and as such is used as an adapter between Blink
// strings and the Ragel-based emoji scanner. It supports forwarding and
// reversing using arithmetic operators. Dereferencing the iterator means
// retrieving a character class as defined in the Ragel grammar of
// third-party/emoji-segmenter. The dereferenced character category is cached
// since Ragel dereferences multiple times without moving the iterator's cursor.
class PLATFORM_EXPORT UTF16RagelIterator {
  STACK_ALLOCATED();

 public:
  UTF16RagelIterator()
      : cursor_(0),
        cached_category_(EmojiSegmentationCategory::kInvalidCacheEntry) {}

  explicit UTF16RagelIterator(base::span<const UChar> buffer,
                              unsigned cursor = 0)
      : buffer_(buffer),
        cursor_(cursor),
        cached_category_(EmojiSegmentationCategory::kInvalidCacheEntry) {}

  UTF16RagelIterator end() {
    UTF16RagelIterator ret = *this;
    ret.cursor_ = static_cast<unsigned>(buffer_.size());
    return ret;
  }

  size_t size() { return buffer_.size(); }

  UTF16RagelIterator& SetCursor(unsigned new_cursor) {
    DCHECK_GE(new_cursor, 0u);
    DCHECK_LT(new_cursor, buffer_.size());
    cursor_ = new_cursor;
    InvalidateCache();
    return *this;
  }

  unsigned Cursor() { return cursor_; }

  UTF16RagelIterator& operator+=(int v) {
    if (v > 0) {
      U16_FWD_N(buffer_, cursor_, buffer_.size(), v);
    } else if (v < 0) {
      U16_BACK_N(buffer_, 0, cursor_, -v);
    }
    InvalidateCache();
    return *this;
  }

  UTF16RagelIterator& operator-=(int v) { return *this += -v; }

  UTF16RagelIterator operator+(int v) {
    UTF16RagelIterator ret = *this;
    return ret += v;
  }

  UTF16RagelIterator operator-(int v) { return *this + -v; }

  int operator-(const UTF16RagelIterator& other) {
    DCHECK_EQ(buffer_, other.buffer_);
    return cursor_ - other.cursor_;
  }

  UTF16RagelIterator& operator++() {
    DCHECK_LT(cursor_, buffer_.size());
    U16_FWD_1(buffer_, cursor_, buffer_.size());
    InvalidateCache();
    return *this;
  }

  UTF16RagelIterator& operator--() {
    DCHECK_GT(cursor_, 0u);
    U16_BACK_1(buffer_, 0, cursor_);
    InvalidateCache();
    return *this;
  }

  UTF16RagelIterator operator++(int) {
    UTF16RagelIterator ret = *this;
    ++(*this);
    return ret;
  }

  UTF16RagelIterator operator--(int) {
    UTF16RagelIterator ret = *this;
    --(*this);
    return ret;
  }

  UTF16RagelIterator operator=(int v) {
    // We need this integer assignment operator because Ragel has initialization
    // code for assigning 0 to ts, te.
    DCHECK_EQ(v, 0);
    UTF16RagelIterator ret = *this;
    ret.cursor_ = v;
    return ret;
  }

  EmojiSegmentationCategory operator*() {
    DCHECK(!buffer_.empty());
    if (cached_category_ == EmojiSegmentationCategory::kInvalidCacheEntry) {
      UChar32 codepoint;
      U16_GET(buffer_, 0, cursor_, buffer_.size(), codepoint);
      cached_category_ = GetEmojiSegmentationCategory(codepoint);
    }
    return cached_category_;
  }

  inline void InvalidateCache() {
    cached_category_ = EmojiSegmentationCategory::kInvalidCacheEntry;
  }

  bool operator==(const UTF16RagelIterator& other) const {
    return buffer_.data() == other.buffer_.data() &&
           buffer_.size() == other.buffer_.size() && cursor_ == other.cursor_;
  }

  bool operator!=(const UTF16RagelIterator& other) const {
    return !(*this == other);
  }

  // Peeks the next codepoint. Note: Does not peak the
  // `EmojiSegmentationCategory` as does `operator*()`. For performance reasons,
  // this method is simplified to return U+FFFD when the cursor is at the end of
  // the stream, instead of using `std::optional` or similar.
  //
  // TODO(drott): Before moving to ICU UNSAFE functions, check
  // InputMethodControllerTest.DeleteSurroundingTextInCodePointsWithInvalidSurrogatePair
  // and DeleteSurroundingTextInCodePointsWithInvalidSurrogatePair which cause
  // this code to encounter an unmatched lead surrogate as the last character in
  // the buffer. (Potential issue with InputMethodController, or the tests?).
  UChar32 PeekCodepoint() {
    UChar32 output = kReplacementCharacter;
    unsigned temp_cursor = cursor_;
    U16_FWD_1(buffer_, temp_cursor, buffer_.size());
    if (temp_cursor < buffer_.size()) {
      U16_GET(buffer_, 0, temp_cursor, buffer_.size(), output);
    }
    return output;
  }

 private:
  base::span<const UChar> buffer_;
  unsigned cursor_;
  EmojiSegmentationCategory cached_category_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_UTF16_RAGEL_ITERATOR_H_