OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium 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 IOS_THIRD_PARTY_BLINK_SRC_HTML_CHARACTER_PROVIDER_H_ |
| 6 #define IOS_THIRD_PARTY_BLINK_SRC_HTML_CHARACTER_PROVIDER_H_ |
| 7 |
| 8 #include "ios/third_party/blink/src/html_tokenizer_adapter.h" |
| 9 |
| 10 namespace WebCore { |
| 11 |
| 12 const LChar kEndOfFileMarker = 0; |
| 13 |
| 14 // CharacterProvider provides input characters to WebCore::HTMLTokenizer. |
| 15 // It replaces WebCore::SegmentedString (which sits ontop of WTF::String). |
| 16 class CharacterProvider { |
| 17 WTF_MAKE_NONCOPYABLE(CharacterProvider); |
| 18 |
| 19 public: |
| 20 CharacterProvider() |
| 21 : _totalBytes(0) |
| 22 , _remainingBytes(0) |
| 23 , _singleBytePtr(nullptr) |
| 24 , _doubleBytePtr(nullptr) |
| 25 , _littleEndian(false) |
| 26 { |
| 27 } |
| 28 |
| 29 void setContents(const LChar* str, size_t numberOfBytes) |
| 30 { |
| 31 _totalBytes = numberOfBytes; |
| 32 _remainingBytes = numberOfBytes; |
| 33 _singleBytePtr = str; |
| 34 _doubleBytePtr = nullptr; |
| 35 _littleEndian = false; |
| 36 } |
| 37 |
| 38 void setContents(const UChar* str, size_t numberOfBytes) |
| 39 { |
| 40 _totalBytes = numberOfBytes; |
| 41 _remainingBytes = numberOfBytes; |
| 42 _singleBytePtr = nullptr; |
| 43 _doubleBytePtr = str; |
| 44 _littleEndian = false; |
| 45 } |
| 46 |
| 47 void clear() |
| 48 { |
| 49 _totalBytes = 0; |
| 50 _remainingBytes = 0; |
| 51 _singleBytePtr = nullptr; |
| 52 _doubleBytePtr = nullptr; |
| 53 _littleEndian = false; |
| 54 } |
| 55 |
| 56 bool startsWith(const LChar* str, |
| 57 size_t byteCount, |
| 58 bool caseInsensitive = false) const |
| 59 { |
| 60 if (!str || byteCount > _remainingBytes) |
| 61 return false; |
| 62 |
| 63 for (size_t index = 0; index < byteCount; ++index) { |
| 64 UChar lhs = characterAtIndex(index); |
| 65 UChar rhs = str[index]; |
| 66 |
| 67 if (caseInsensitive) { |
| 68 if (isASCIIUpper(lhs)) |
| 69 lhs = toLowerCase(lhs); |
| 70 |
| 71 if (isASCIIUpper(rhs)) |
| 72 rhs = toLowerCase(rhs); |
| 73 } |
| 74 |
| 75 if (lhs != rhs) |
| 76 return false; |
| 77 } |
| 78 |
| 79 return true; |
| 80 } |
| 81 |
| 82 inline UChar currentCharacter() const |
| 83 { |
| 84 return characterAtIndex(0); |
| 85 } |
| 86 |
| 87 inline UChar nextCharacter() |
| 88 { |
| 89 advanceBytePointer(); |
| 90 return characterAtIndex(0); |
| 91 } |
| 92 |
| 93 inline void next() |
| 94 { |
| 95 advanceBytePointer(); |
| 96 } |
| 97 |
| 98 inline bool isEmpty() const |
| 99 { |
| 100 return !_remainingBytes; |
| 101 } |
| 102 |
| 103 inline size_t remainingBytes() const |
| 104 { |
| 105 return _remainingBytes; |
| 106 } |
| 107 |
| 108 inline size_t bytesProvided() const |
| 109 { |
| 110 return _totalBytes - _remainingBytes; |
| 111 } |
| 112 |
| 113 inline void setLittleEndian() |
| 114 { |
| 115 _littleEndian = true; |
| 116 } |
| 117 |
| 118 private: |
| 119 void advanceBytePointer() |
| 120 { |
| 121 --_remainingBytes; |
| 122 if (!_remainingBytes) |
| 123 return; |
| 124 |
| 125 if (_singleBytePtr) |
| 126 ++_singleBytePtr; |
| 127 else { |
| 128 DCHECK(_doubleBytePtr); |
| 129 ++_doubleBytePtr; |
| 130 } |
| 131 } |
| 132 |
| 133 UChar characterAtIndex(size_t index) const |
| 134 { |
| 135 if (!_remainingBytes) { |
| 136 // There is a quirk in the blink implementation wherein the empty st
ate |
| 137 // is not set on the source until next() has been called when |
| 138 // _remainingBytes is zero. In this case, return kEndOfFileMarker. |
| 139 return kEndOfFileMarker; |
| 140 } |
| 141 |
| 142 ASSERT(_singleBytePtr || _doubleBytePtr); |
| 143 |
| 144 UChar character = kEndOfFileMarker; |
| 145 if (_singleBytePtr) |
| 146 character = _singleBytePtr[index]; |
| 147 else |
| 148 character = _doubleBytePtr[index]; |
| 149 |
| 150 if (_littleEndian) |
| 151 character = ByteSwap(character); |
| 152 |
| 153 return character; |
| 154 } |
| 155 |
| 156 private: |
| 157 size_t _totalBytes; |
| 158 size_t _remainingBytes; |
| 159 const LChar* _singleBytePtr; |
| 160 const UChar* _doubleBytePtr; |
| 161 bool _littleEndian; |
| 162 }; |
| 163 |
| 164 } |
| 165 |
| 166 #endif // IOS_THIRD_PARTY_BLINK_SRC_HTML_CHARACTER_PROVIDER_H_ |
OLD | NEW |