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 13namespace v8 { 14namespace internal { 15 16class StringComparator { 17 class State { 18 public: 19 State() : is_one_byte_(true), length_(0), buffer8_(nullptr) {} 20 State(const State&) = delete; 21 State& operator=(const State&) = delete; 22 23 void Init(String string, 24 const SharedStringAccessGuardIfNeeded& access_guard); 25 26 inline void VisitOneByteString(const uint8_t* chars, int length) { 27 is_one_byte_ = true; 28 buffer8_ = chars; 29 length_ = length; 30 } 31 32 inline void VisitTwoByteString(const uint16_t* chars, int length) { 33 is_one_byte_ = false; 34 buffer16_ = chars; 35 length_ = length; 36 } 37 38 void Advance(int consumed, 39 const SharedStringAccessGuardIfNeeded& access_guard); 40 41 ConsStringIterator iter_; 42 bool is_one_byte_; 43 int length_; 44 union { 45 const uint8_t* buffer8_; 46 const uint16_t* buffer16_; 47 }; 48 }; 49 50 public: 51 inline StringComparator() = default; 52 StringComparator(const StringComparator&) = delete; 53 StringComparator& operator=(const StringComparator&) = delete; 54 55 template <typename Chars1, typename Chars2> 56 static inline bool Equals(State* state_1, State* state_2, int to_check) { 57 const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_); 58 const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_); 59 return CompareCharsEqual(a, b, to_check); 60 } 61 62 bool Equals(String string_1, String string_2, 63 const SharedStringAccessGuardIfNeeded& access_guard); 64 65 private: 66 State state_1_; 67 State state_2_; 68}; 69 70} // namespace internal 71} // namespace v8 72 73#endif // V8_OBJECTS_STRING_COMPARATOR_H_ 74