Revert of Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320) (patchset...
[chromium-blink-merge.git] / ios / third_party / blink / src / html_character_provider.h
blobf6dd4271b779512ca7f821010b82f7adedf8cafc
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.
5 #ifndef IOS_THIRD_PARTY_BLINK_SRC_HTML_CHARACTER_PROVIDER_H_
6 #define IOS_THIRD_PARTY_BLINK_SRC_HTML_CHARACTER_PROVIDER_H_
8 #include "ios/third_party/blink/src/html_tokenizer_adapter.h"
10 namespace WebCore {
12 const LChar kEndOfFileMarker = 0;
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);
19 public:
20 CharacterProvider()
21 : _totalBytes(0)
22 , _remainingBytes(0)
23 , _singleBytePtr(nullptr)
24 , _doubleBytePtr(nullptr)
25 , _littleEndian(false)
29 void setContents(const LChar* str, size_t numberOfBytes)
31 _totalBytes = numberOfBytes;
32 _remainingBytes = numberOfBytes;
33 _singleBytePtr = str;
34 _doubleBytePtr = nullptr;
35 _littleEndian = false;
38 void setContents(const UChar* str, size_t numberOfBytes)
40 _totalBytes = numberOfBytes;
41 _remainingBytes = numberOfBytes;
42 _singleBytePtr = nullptr;
43 _doubleBytePtr = str;
44 _littleEndian = false;
47 void clear()
49 _totalBytes = 0;
50 _remainingBytes = 0;
51 _singleBytePtr = nullptr;
52 _doubleBytePtr = nullptr;
53 _littleEndian = false;
56 bool startsWith(const LChar* str,
57 size_t byteCount,
58 bool caseInsensitive = false) const
60 if (!str || byteCount > _remainingBytes)
61 return false;
63 for (size_t index = 0; index < byteCount; ++index) {
64 UChar lhs = characterAtIndex(index);
65 UChar rhs = str[index];
67 if (caseInsensitive) {
68 if (isASCIIUpper(lhs))
69 lhs = toLowerCase(lhs);
71 if (isASCIIUpper(rhs))
72 rhs = toLowerCase(rhs);
75 if (lhs != rhs)
76 return false;
79 return true;
82 inline UChar currentCharacter() const
84 return characterAtIndex(0);
87 inline UChar nextCharacter()
89 advanceBytePointer();
90 return characterAtIndex(0);
93 inline void next()
95 advanceBytePointer();
98 inline bool isEmpty() const
100 return !_remainingBytes;
103 inline size_t remainingBytes() const
105 return _remainingBytes;
108 inline size_t bytesProvided() const
110 return _totalBytes - _remainingBytes;
113 inline void setLittleEndian()
115 _littleEndian = true;
118 private:
119 void advanceBytePointer()
121 --_remainingBytes;
122 if (!_remainingBytes)
123 return;
125 if (_singleBytePtr)
126 ++_singleBytePtr;
127 else {
128 DCHECK(_doubleBytePtr);
129 ++_doubleBytePtr;
133 UChar characterAtIndex(size_t index) const
135 if (!_remainingBytes) {
136 // There is a quirk in the blink implementation wherein the empty state
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;
142 ASSERT(_singleBytePtr || _doubleBytePtr);
144 UChar character = kEndOfFileMarker;
145 if (_singleBytePtr)
146 character = _singleBytePtr[index];
147 else
148 character = _doubleBytePtr[index];
150 if (_littleEndian)
151 character = ByteSwap(character);
153 return character;
156 private:
157 size_t _totalBytes;
158 size_t _remainingBytes;
159 const LChar* _singleBytePtr;
160 const UChar* _doubleBytePtr;
161 bool _littleEndian;
166 #endif // IOS_THIRD_PARTY_BLINK_SRC_HTML_CHARACTER_PROVIDER_H_