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"
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
);
23 , _singleBytePtr(nullptr)
24 , _doubleBytePtr(nullptr)
25 , _littleEndian(false)
29 void setContents(const LChar
* str
, size_t numberOfBytes
)
31 _totalBytes
= numberOfBytes
;
32 _remainingBytes
= numberOfBytes
;
34 _doubleBytePtr
= nullptr;
35 _littleEndian
= false;
38 void setContents(const UChar
* str
, size_t numberOfBytes
)
40 _totalBytes
= numberOfBytes
;
41 _remainingBytes
= numberOfBytes
;
42 _singleBytePtr
= nullptr;
44 _littleEndian
= false;
51 _singleBytePtr
= nullptr;
52 _doubleBytePtr
= nullptr;
53 _littleEndian
= false;
56 bool startsWith(const LChar
* str
,
58 bool caseInsensitive
= false) const
60 if (!str
|| byteCount
> _remainingBytes
)
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
);
82 inline UChar
currentCharacter() const
84 return characterAtIndex(0);
87 inline UChar
nextCharacter()
90 return characterAtIndex(0);
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;
119 void advanceBytePointer()
122 if (!_remainingBytes
)
128 DCHECK(_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
;
146 character
= _singleBytePtr
[index
];
148 character
= _doubleBytePtr
[index
];
151 character
= ByteSwap(character
);
158 size_t _remainingBytes
;
159 const LChar
* _singleBytePtr
;
160 const UChar
* _doubleBytePtr
;
166 #endif // IOS_THIRD_PARTY_BLINK_SRC_HTML_CHARACTER_PROVIDER_H_