• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef V8_OBJECTS_STRING_COMPARATOR_H_
6 #define V8_OBJECTS_STRING_COMPARATOR_H_
7 
8 #include "src/base/logging.h"
9 #include "src/common/globals.h"
10 #include "src/objects/string.h"
11 #include "src/utils/utils.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 // Compares the contents of two strings by reading and comparing
17 // int-sized blocks of characters.
18 template <typename Char>
CompareRawStringContents(const Char * const a,const Char * const b,int length)19 static inline bool CompareRawStringContents(const Char* const a,
20                                             const Char* const b, int length) {
21   return CompareChars(a, b, length) == 0;
22 }
23 
24 template <typename Chars1, typename Chars2>
25 class RawStringComparator : public AllStatic {
26  public:
compare(const Chars1 * a,const Chars2 * b,int len)27   static inline bool compare(const Chars1* a, const Chars2* b, int len) {
28     DCHECK(sizeof(Chars1) != sizeof(Chars2));
29     for (int i = 0; i < len; i++) {
30       if (a[i] != b[i]) {
31         return false;
32       }
33     }
34     return true;
35   }
36 };
37 
38 template <>
39 class RawStringComparator<uint16_t, uint16_t> {
40  public:
compare(const uint16_t * a,const uint16_t * b,int len)41   static inline bool compare(const uint16_t* a, const uint16_t* b, int len) {
42     return CompareRawStringContents(a, b, len);
43   }
44 };
45 
46 template <>
47 class RawStringComparator<uint8_t, uint8_t> {
48  public:
compare(const uint8_t * a,const uint8_t * b,int len)49   static inline bool compare(const uint8_t* a, const uint8_t* b, int len) {
50     return CompareRawStringContents(a, b, len);
51   }
52 };
53 
54 class StringComparator {
55   class State {
56    public:
State()57     State() : is_one_byte_(true), length_(0), buffer8_(nullptr) {}
58     State(const State&) = delete;
59     State& operator=(const State&) = delete;
60 
61     void Init(String string);
62 
VisitOneByteString(const uint8_t * chars,int length)63     inline void VisitOneByteString(const uint8_t* chars, int length) {
64       is_one_byte_ = true;
65       buffer8_ = chars;
66       length_ = length;
67     }
68 
VisitTwoByteString(const uint16_t * chars,int length)69     inline void VisitTwoByteString(const uint16_t* chars, int length) {
70       is_one_byte_ = false;
71       buffer16_ = chars;
72       length_ = length;
73     }
74 
75     void Advance(int consumed);
76 
77     ConsStringIterator iter_;
78     bool is_one_byte_;
79     int length_;
80     union {
81       const uint8_t* buffer8_;
82       const uint16_t* buffer16_;
83     };
84   };
85 
86  public:
87   inline StringComparator() = default;
88   StringComparator(const StringComparator&) = delete;
89   StringComparator& operator=(const StringComparator&) = delete;
90 
91   template <typename Chars1, typename Chars2>
Equals(State * state_1,State * state_2,int to_check)92   static inline bool Equals(State* state_1, State* state_2, int to_check) {
93     const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_);
94     const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_);
95     return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check);
96   }
97 
98   bool Equals(String string_1, String string_2);
99 
100  private:
101   State state_1_;
102   State state_2_;
103 };
104 
105 }  // namespace internal
106 }  // namespace v8
107 
108 #endif  // V8_OBJECTS_STRING_COMPARATOR_H_
109