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 : #include "src/unicode-inl.h"
7 :
8 0 : int Ucs2CharLength(unibrow::uchar c) {
9 2537775 : if (c == unibrow::Utf8::kIncomplete || c == unibrow::Utf8::kBufferEmpty) {
10 : return 0;
11 2465220 : } else if (c < 0xFFFF) {
12 : return 1;
13 : } else {
14 0 : return 2;
15 : }
16 : }
17 :
18 72410 : int Utf8LengthHelper(const char* s) {
19 72410 : unibrow::Utf8::Utf8IncrementalBuffer buffer(unibrow::Utf8::kBufferEmpty);
20 72410 : unibrow::Utf8::State state = unibrow::Utf8::State::kAccept;
21 :
22 : int length = 0;
23 72410 : const uint8_t* c = reinterpret_cast<const uint8_t*>(s);
24 5003140 : while (*c != '\0') {
25 2465365 : unibrow::uchar tmp = unibrow::Utf8::ValueOfIncremental(&c, &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 53278 : }
|