File: Word.cpp

package info (click to toggle)
ycmd 0%2B20230103%2Bgitf53e7ac%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,904 kB
  • sloc: python: 41,740; cpp: 6,055; java: 295; sh: 271; cs: 194; javascript: 150; ansic: 82; makefile: 46; xml: 18; objc: 10
file content (295 lines) | stat: -rw-r--r-- 10,167 bytes parent folder | download
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
// Copyright (C) 2018 ycmd contributors
//
// This file is part of ycmd.
//
// ycmd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ycmd 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ycmd.  If not, see <http://www.gnu.org/licenses/>.

#include "Repository.h"
#include "CodePoint.h"
#include "Word.h"

#include <string>

namespace YouCompleteMe {

namespace {

// Break a sequence of code points into characters (grapheme clusters) according
// to the rules in
// https://www.unicode.org/reports/tr29/tr29-37.html#Grapheme_Cluster_Boundary_Rules
std::vector< std::string > BreakCodePointsIntoCharacters(
  const CodePointSequence &code_points ) {

  std::vector< std::string > characters;

  // Rules GB1 and GB2 (break at the start and at the end of the text) are
  // automatically satisfied.

  auto code_point_pos = code_points.begin();
  if ( code_point_pos == code_points.end() ) {
    return characters;
  }

  std::string character;
  character.append( ( *code_point_pos )->Normal() );

  auto previous_code_point_pos = code_point_pos;
  ++code_point_pos;
  if ( code_point_pos == code_points.end() ) {
    characters.push_back( character );
    return characters;
  }

  bool is_regional_indicator_nb_odd = false;
  bool within_emoji_modifier = false;

  for ( ; code_point_pos != code_points.end() ; ++previous_code_point_pos,
                                                ++code_point_pos ) {
    auto previous_property = ( *previous_code_point_pos )->GetBreakProperty();
    const auto &code_point = ( *code_point_pos )->Normal();
    auto property = ( *code_point_pos )->GetBreakProperty();

    switch( previous_property ) {
      case BreakProperty::CR:
        switch( property ) {
          // Rule GB3: do not break between a CR and LF.
          case BreakProperty::LF:
            character.append( code_point );
            break;
          // Rule GB4: otherwise, break after CR.
          default:
            characters.push_back( character );
            character = code_point;
        }
        break;
      // Rule GB4: break after controls and LF.
      case BreakProperty::CONTROL:
      case BreakProperty::LF:
        characters.push_back( character );
        character = code_point;
        break;
      case BreakProperty::L:
        switch( property ) {
          // Rule GB6: do not break Hangul syllable sequences.
          case BreakProperty::L:
          case BreakProperty::V:
          case BreakProperty::LV:
          case BreakProperty::LVT:
          // Rule GB9: do not break before extending characters or when using a
          // zero-width joiner (ZWJ).
          case BreakProperty::EXTEND:
          case BreakProperty::ZWJ:
          // Rule GB9a: do not break before spacing marks.
          case BreakProperty::SPACINGMARK:
            character.append( code_point );
            break;
          default:
            characters.push_back( character );
            character = code_point;
        }
        break;
      case BreakProperty::LV:
      case BreakProperty::V:
        switch( property ) {
          // Rule GB7: do not break Hangul syllable sequences.
          case BreakProperty::V:
          case BreakProperty::T:
          // Rule GB9: do not break before extending characters or when using a
          // zero-width joiner (ZWJ).
          case BreakProperty::EXTEND:
          case BreakProperty::ZWJ:
          // Rule GB9a: do not break before spacing marks.
          case BreakProperty::SPACINGMARK:
            character.append( code_point );
            break;
          default:
            characters.push_back( character );
            character = code_point;
        }
        break;
      case BreakProperty::LVT:
      case BreakProperty::T:
        switch( property ) {
          // Rule GB8: do not break Hangul syllable sequences.
          case BreakProperty::T:
          // Rule GB9: do not break before extending characters or when using a
          // zero-width joiner (ZWJ).
          case BreakProperty::EXTEND:
          case BreakProperty::ZWJ:
          // Rule GB9a: do not break before spacing marks.
          case BreakProperty::SPACINGMARK:
            character.append( code_point );
            break;
          default:
            characters.push_back( character );
            character = code_point;
        }
        break;
      case BreakProperty::PREPEND:
        switch( property ) {
          // Rules GB5: break before controls.
          case BreakProperty::CONTROL:
          case BreakProperty::CR:
          case BreakProperty::LF:
            characters.push_back( character );
            character = code_point;
            break;
          // Rule GB9b: do not break after prepend characters.
          default:
            character.append( code_point );
        }
        break;
      case BreakProperty::EXTEND:
        switch( property ) {
          // Rule GB9: do not break before extending characters or when using a
          // zero-width joiner (ZWJ).
          case BreakProperty::EXTEND:
          case BreakProperty::ZWJ:
            character.append( code_point );
            break;
          // Rule GB9a: do not break before spacing marks.
          case BreakProperty::SPACINGMARK:
            character.append( code_point );
            within_emoji_modifier = false;
            break;
          default:
            characters.push_back( character );
            character = code_point;
            within_emoji_modifier = false;
        }
        break;
      case BreakProperty::ZWJ:
        switch( property ) {
          // Rule GB9: do not break before extending characters or when using a
          // zero-width joiner (ZWJ).
          case BreakProperty::EXTEND:
          case BreakProperty::ZWJ:
          // Rule GB9a: do not break before spacing marks.
          case BreakProperty::SPACINGMARK:
            character.append( code_point );
            within_emoji_modifier = false;
            break;
          // Rule GB11: do not break within emoji modifier sequences of emoji
          // zwj sequences.
          case BreakProperty::EXTPICT:
            if ( within_emoji_modifier ) {
              character.append( code_point );
              within_emoji_modifier = false;
            } else {
              characters.push_back( character );
              character = code_point;
            }
            break;
          default:
            characters.push_back( character );
            character = code_point;
            within_emoji_modifier = false;
        }
        break;
      case BreakProperty::EXTPICT:
        switch( property ) {
          // Rule GB9a: do not break before spacing marks.
          case BreakProperty::SPACINGMARK:
            character.append( code_point );
            break;
          // Rule GB11: do not break within emoji modifier sequences of emoji
          // zwj sequences.
          case BreakProperty::EXTEND:
          case BreakProperty::ZWJ:
            character.append( code_point );
            within_emoji_modifier = true;
            break;
          default:
            characters.push_back( character );
            character = code_point;
        }
        break;
      case BreakProperty::REGIONAL_INDICATOR:
        switch( property ) {
          // Rule GB9: do not break before extending characters or when using a
          // zero-width joiner (ZWJ).
          case BreakProperty::EXTEND:
          case BreakProperty::ZWJ:
          // Rule GB9a: do not break before spacing marks.
          case BreakProperty::SPACINGMARK:
            character.append( code_point );
            is_regional_indicator_nb_odd = false;
            break;
          // Rules GB12 and GB13: do not break within emoji flag sequences. That
          // is, do not break between regional indicator (RI) symbols if there
          // is an odd number of RI characters before the break point.
          case BreakProperty::REGIONAL_INDICATOR:
            is_regional_indicator_nb_odd = !is_regional_indicator_nb_odd;
            if ( is_regional_indicator_nb_odd ) {
              character.append( code_point );
            } else {
              characters.push_back( character );
              character = code_point;
            }
            break;
          default:
            characters.push_back( character );
            character = code_point;
            is_regional_indicator_nb_odd = false;
        }
        break;
      default:
        switch( property ) {
          // Rule GB9: do not break before extending characters or when using a
          // zero-width joiner (ZWJ).
          case BreakProperty::EXTEND:
          case BreakProperty::ZWJ:
          // Rule GB9a: do not break before spacing marks.
          case BreakProperty::SPACINGMARK:
            character.append( code_point );
            break;
          // Rules GB5: break before controls.
          // Rules GB999.
          default:
            characters.push_back( character );
            character = code_point;
        }
    }
  }

  characters.push_back( character );
  return characters;
}

} // unnamed namespace

void Word::BreakIntoCharacters() {
  const CodePointSequence &code_points = BreakIntoCodePoints( text_ );

  characters_ = Repository< Character >::Instance().GetElements(
    BreakCodePointsIntoCharacters( code_points ) );
}


void Word::ComputeBytesPresent() {
  for ( const auto &character : characters_ ) {
    for ( auto byte : character->Base() ) {
      bytes_present_.set( static_cast< uint8_t >( byte ) );
    }
  }
}


Word::Word( std::string&& text )
  : text_( std::move( text ) ) {
  BreakIntoCharacters();
  ComputeBytesPresent();
}

} // namespace YouCompleteMe