Line data Source code
1 : // Copyright 2018 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "test/cctest/unicode-helpers.h"
6 :
7 0 : int Ucs2CharLength(unibrow::uchar c) {
8 2537775 : if (c == unibrow::Utf8::kIncomplete || c == unibrow::Utf8::kBufferEmpty) {
9 : return 0;
10 2465220 : } else if (c < 0xFFFF) {
11 : return 1;
12 : } else {
13 0 : return 2;
14 : }
15 : }
16 :
17 72410 : int Utf8LengthHelper(const char* s) {
18 72410 : unibrow::Utf8::Utf8IncrementalBuffer buffer(unibrow::Utf8::kBufferEmpty);
19 72410 : unibrow::Utf8::State state = unibrow::Utf8::State::kAccept;
20 :
21 : int length = 0;
22 72410 : size_t i = 0;
23 2610185 : while (s[i] != '\0') {
24 : unibrow::uchar tmp =
25 2465365 : unibrow::Utf8::ValueOfIncremental(s[i], &i, &state, &buffer);
26 2465365 : length += Ucs2CharLength(tmp);
27 : }
28 72410 : unibrow::uchar tmp = unibrow::Utf8::ValueOfIncrementalFinish(&state);
29 72410 : length += Ucs2CharLength(tmp);
30 72410 : return length;
31 85011 : }
|