1 | // Copyright 2014 The Chromium Authors. All rights reserved.
|
---|
2 | // Copyright (C) 2016 Apple Inc. All rights reserved.
|
---|
3 | //
|
---|
4 | // Redistribution and use in source and binary forms, with or without
|
---|
5 | // modification, are permitted provided that the following conditions are
|
---|
6 | // met:
|
---|
7 | //
|
---|
8 | // * Redistributions of source code must retain the above copyright
|
---|
9 | // notice, this list of conditions and the following disclaimer.
|
---|
10 | // * Redistributions in binary form must reproduce the above
|
---|
11 | // copyright notice, this list of conditions and the following disclaimer
|
---|
12 | // in the documentation and/or other materials provided with the
|
---|
13 | // distribution.
|
---|
14 | // * Neither the name of Google Inc. nor the names of its
|
---|
15 | // contributors may be used to endorse or promote products derived from
|
---|
16 | // this software without specific prior written permission.
|
---|
17 | //
|
---|
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 |
|
---|
30 | #pragma once
|
---|
31 |
|
---|
32 | #include <wtf/text/StringView.h>
|
---|
33 |
|
---|
34 | namespace WebCore {
|
---|
35 |
|
---|
36 | constexpr LChar kEndOfFileMarker = 0;
|
---|
37 |
|
---|
38 | class CSSTokenizerInputStream {
|
---|
39 | WTF_MAKE_NONCOPYABLE(CSSTokenizerInputStream);
|
---|
40 | WTF_MAKE_FAST_ALLOCATED;
|
---|
41 | public:
|
---|
42 | explicit CSSTokenizerInputStream(const String& input);
|
---|
43 |
|
---|
44 | // Gets the char in the stream. Will return (NUL) kEndOfFileMarker when at the
|
---|
45 | // end of the stream.
|
---|
46 | UChar nextInputChar() const
|
---|
47 | {
|
---|
48 | if (m_offset >= m_stringLength)
|
---|
49 | return kEndOfFileMarker;
|
---|
50 | return (*m_string)[m_offset];
|
---|
51 | }
|
---|
52 |
|
---|
53 | // Gets the char at lookaheadOffset from the current stream position. Will
|
---|
54 | // return NUL (kEndOfFileMarker) if the stream position is at the end.
|
---|
55 | UChar peek(unsigned lookaheadOffset) const
|
---|
56 | {
|
---|
57 | if ((m_offset + lookaheadOffset) >= m_stringLength)
|
---|
58 | return kEndOfFileMarker;
|
---|
59 | return (*m_string)[m_offset + lookaheadOffset];
|
---|
60 | }
|
---|
61 |
|
---|
62 | void advance(unsigned offset = 1) { m_offset += offset; }
|
---|
63 | void pushBack(UChar cc)
|
---|
64 | {
|
---|
65 | --m_offset;
|
---|
66 | ASSERT_UNUSED(cc, nextInputChar() == cc);
|
---|
67 | }
|
---|
68 |
|
---|
69 | double getDouble(unsigned start, unsigned end) const;
|
---|
70 |
|
---|
71 | template<bool characterPredicate(UChar)>
|
---|
72 | unsigned skipWhilePredicate(unsigned offset)
|
---|
73 | {
|
---|
74 | if (m_string->is8Bit()) {
|
---|
75 | const LChar* characters8 = m_string->characters8();
|
---|
76 | while ((m_offset + offset) < m_stringLength && characterPredicate(characters8[m_offset + offset]))
|
---|
77 | ++offset;
|
---|
78 | } else {
|
---|
79 | const UChar* characters16 = m_string->characters16();
|
---|
80 | while ((m_offset + offset) < m_stringLength && characterPredicate(characters16[m_offset + offset]))
|
---|
81 | ++offset;
|
---|
82 | }
|
---|
83 | return offset;
|
---|
84 | }
|
---|
85 |
|
---|
86 | void advanceUntilNonWhitespace();
|
---|
87 |
|
---|
88 | unsigned length() const { return m_stringLength; }
|
---|
89 | unsigned offset() const { return std::min(m_offset, m_stringLength); }
|
---|
90 |
|
---|
91 | StringView rangeAt(unsigned start, unsigned length) const
|
---|
92 | {
|
---|
93 | ASSERT(start + length <= m_stringLength);
|
---|
94 | return StringView(m_string.get()).substring(start, length); // FIXME: Should make a constructor on StringView for this.
|
---|
95 | }
|
---|
96 |
|
---|
97 | private:
|
---|
98 | size_t m_offset;
|
---|
99 | const size_t m_stringLength;
|
---|
100 | RefPtr<StringImpl> m_string;
|
---|
101 | };
|
---|
102 |
|
---|
103 | } // namespace WebCore
|
---|