Line data Source code
1 : // Copyright 2019 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 "src/objects/string-comparator.h"
6 :
7 : #include "src/objects/string-inl.h"
8 :
9 : namespace v8 {
10 : namespace internal {
11 :
12 123596 : void StringComparator::State::Init(String string) {
13 123596 : ConsString cons_string = String::VisitFlat(this, string);
14 123596 : iter_.Reset(cons_string);
15 123596 : if (!cons_string.is_null()) {
16 : int offset;
17 43 : string = iter_.Next(&offset);
18 43 : String::VisitFlat(this, string, offset);
19 : }
20 123596 : }
21 :
22 100664126 : void StringComparator::State::Advance(int consumed) {
23 : DCHECK(consumed <= length_);
24 : // Still in buffer.
25 100664126 : if (length_ != consumed) {
26 50332063 : if (is_one_byte_) {
27 50332063 : buffer8_ += consumed;
28 : } else {
29 0 : buffer16_ += consumed;
30 : }
31 50332063 : length_ -= consumed;
32 50332063 : return;
33 : }
34 : // Advance state.
35 : int offset;
36 100664126 : String next = iter_.Next(&offset);
37 : DCHECK_EQ(0, offset);
38 : DCHECK(!next.is_null());
39 50332063 : String::VisitFlat(this, next);
40 : }
41 :
42 61798 : bool StringComparator::Equals(String string_1, String string_2) {
43 : int length = string_1->length();
44 61798 : state_1_.Init(string_1);
45 61798 : state_2_.Init(string_2);
46 50332063 : while (true) {
47 50393861 : int to_check = Min(state_1_.length_, state_2_.length_);
48 : DCHECK(to_check > 0 && to_check <= length);
49 : bool is_equal;
50 50393861 : if (state_1_.is_one_byte_) {
51 50332895 : if (state_2_.is_one_byte_) {
52 : is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check);
53 : } else {
54 : is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check);
55 : }
56 : } else {
57 60966 : if (state_2_.is_one_byte_) {
58 : is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check);
59 : } else {
60 : is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check);
61 : }
62 : }
63 : // Looping done.
64 50393861 : if (!is_equal) return false;
65 50393844 : length -= to_check;
66 : // Exit condition. Strings are equal.
67 50393844 : if (length == 0) return true;
68 50332063 : state_1_.Advance(to_check);
69 50332063 : state_2_.Advance(to_check);
70 : }
71 : }
72 :
73 : } // namespace internal
74 120216 : } // namespace v8
|