Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(635)

Side by Side Diff: Source/core/html/track/vtt/VTTScanner.h

Issue 119143002: Introduce VTTScanner - a parser helper for various VTT parsing needs (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Unstuffed VTTScanner class decl. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/html/track/vtt/VTTParser.cpp ('k') | Source/core/html/track/vtt/VTTScanner.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2013, Opera Software ASA. 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of Opera Software ASA nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef VTTScanner_h
31 #define VTTScanner_h
32
33 #include "platform/ParsingUtilities.h"
34 #include "wtf/text/WTFString.h"
35
36 namespace WebCore {
37
38 // Helper class for "scanning" an input string and performing parsing of
39 // "micro-syntax"-like constructs.
40 //
41 // There's two primary operations: match and scan.
42 //
43 // The 'match' operation matches an explicitly or implicitly specified sequence
44 // against the characters ahead of the current input pointer, and returns true
45 // if the sequence can be matched.
46 //
47 // The 'scan' operation performs a 'match', and if the match is successful it
48 // advance the input pointer past the matched sequence.
49 class VTTScanner {
jochen (gone - plz use gerrit) 2014/01/03 11:02:24 can you make this class non-copiable as well?
50 public:
51 VTTScanner(const String& line);
jochen (gone - plz use gerrit) 2014/01/03 11:02:24 explicit
52
53 typedef const LChar* Position;
54
55 struct Run {
jochen (gone - plz use gerrit) 2014/01/03 11:02:24 this is really a class with all the fancy methods
56 Run(Position start, Position end, bool is8Bit)
57 : start(start), end(end), is8Bit(is8Bit) { }
58
59 bool isEmpty() const { return start == end; }
60 size_t length() const;
61
62 Position start;
63 Position end;
64 bool is8Bit;
65 };
66
67 // Check if the input pointer points at the specified position.
68 bool isAt(Position checkPosition) const { return position() == checkPosition ; }
69 // Check if the input pointer points at the end of the input.
70 bool isAtEnd() const { return position() == end(); }
71 // Match the character |c| against the character at the input pointer (~look ahead).
72 bool match(char c) const { return !isAtEnd() && currentChar() == c; }
73 // Scan the character |c|.
74 bool scan(char);
75 // Scan the first |charactersCount| characters of the string |characters|.
76 bool scan(const LChar* characters, size_t charactersCount);
77
78 // Scan the literal |characters|.
79 template<unsigned charactersCount>
80 bool scan(const char (&characters)[charactersCount]);
81
82 // Skip (advance the input pointer) as long as the specified
83 // |characterPredicate| returns true, and the input pointer is not passed
84 // the end of the input.
85 template<bool characterPredicate(UChar)>
86 void skipWhile();
87
88 // Return the run of characters for which the specified
89 // |characterPredicate| returns true. The start of the run will be the
90 // current input pointer.
91 template<bool characterPredicate(UChar)>
92 Run collectWhile();
93
94 // Return a String constructed from the rest of the input (between input
95 // pointer and end of input), and advance the input pointer accordingly.
96 String restOfInputAsString();
97
98 // Scan a set of ASCII digits from the input. Return the number of digits
99 // scanned, and set |number| to the computed value. If the digits make up a
100 // number that does not fit the 'int' type, |number| is set to INT_MAX.
101 // Note: Does not handle sign.
102 unsigned scanDigits(int& number);
103
104 protected:
105 Position position() const { return m_data.characters8; }
106 Position end() const { return m_end.characters8; }
107 void seekTo(Position);
108 UChar currentChar() const;
109 void advance(unsigned amount = 1);
110 // Adapt a UChar-predicate to an LChar-predicate.
111 // (For use with skipWhile/Until from ParsingUtilities.h).
112 template<bool characterPredicate(UChar)>
113 static inline bool LCharPredicateAdapter(LChar c) { return characterPredicat e(c); }
114 union {
115 const LChar* characters8;
116 const UChar* characters16;
117 } m_data;
118 union {
119 const LChar* characters8;
120 const UChar* characters16;
121 } m_end;
122 bool m_is8Bit;
jochen (gone - plz use gerrit) 2014/01/03 11:02:24 it would be nicer if you had accessors and made th
123 };
124
125 inline size_t VTTScanner::Run::length() const
126 {
127 if (is8Bit)
128 return end - start;
129 return reinterpret_cast<const UChar*>(end) - reinterpret_cast<const UChar*>( start);
130 }
131
132 template<unsigned charactersCount>
133 inline bool VTTScanner::scan(const char (&characters)[charactersCount])
134 {
135 return scan(reinterpret_cast<const LChar*>(characters), charactersCount - 1) ;
136 }
137
138 template<bool characterPredicate(UChar)>
139 inline void VTTScanner::skipWhile()
140 {
141 if (m_is8Bit)
142 ::skipWhile<LChar, LCharPredicateAdapter<characterPredicate> >(m_data.ch aracters8, m_end.characters8);
143 else
144 ::skipWhile<UChar, characterPredicate>(m_data.characters16, m_end.charac ters16);
145 }
146
147 template<bool characterPredicate(UChar)>
148 inline VTTScanner::Run VTTScanner::collectWhile()
149 {
150 Run run(position(), position(), m_is8Bit);
151 if (m_is8Bit) {
152 const LChar* current = m_data.characters8;
153 ::skipWhile<LChar, LCharPredicateAdapter<characterPredicate> >(current, m_end.characters8);
154 run.end = current;
155 } else {
156 const UChar* current = m_data.characters16;
157 ::skipWhile<UChar, characterPredicate>(current, m_end.characters16);
158 run.end = reinterpret_cast<Position>(current);
159 }
160 return run;
161 }
162
163 inline void VTTScanner::seekTo(Position position)
164 {
165 ASSERT(position <= end());
166 m_data.characters8 = position;
167 }
168
169 inline UChar VTTScanner::currentChar() const
170 {
171 ASSERT(position() < end());
172 return m_is8Bit ? *m_data.characters8 : *m_data.characters16;
173 }
174
175 inline void VTTScanner::advance(unsigned amount)
176 {
177 ASSERT(position() < end());
178 if (m_is8Bit)
179 m_data.characters8 += amount;
180 else
181 m_data.characters16 += amount;
182 }
183
184 // Wrapper of VTTScanner that allows easy interaction with a parsing model
185 // where a <String, index> tuple is used.
186 class VTTLegacyScanner : public VTTScanner {
187 WTF_MAKE_NONCOPYABLE(VTTLegacyScanner);
188 public:
189 VTTLegacyScanner(const String& line, unsigned* outPosition)
190 : VTTScanner(line)
191 , m_outPosition(outPosition)
192 {
193 ASSERT(outPosition && *outPosition <= line.length());
194 // Adjust state according to |*outPosition|.
195 advance(*outPosition);
196 // Save the start position to allow adjusting |*outPosition|.
197 if (m_is8Bit)
198 m_start.characters8 = m_data.characters8;
199 else
200 m_start.characters16 = m_data.characters16;
201 }
202 ~VTTLegacyScanner()
203 {
204 // "Export" the updated position.
205 unsigned advancedChars = m_is8Bit ? m_data.characters8 - m_start.charact ers8 : m_data.characters16 - m_start.characters16;
206 *m_outPosition += advancedChars;
207 }
208
209 private:
210 unsigned* m_outPosition;
211 union {
212 const LChar* characters8;
213 const UChar* characters16;
214 } m_start;
215 };
216
217 }
218
219 #endif
OLDNEW
« no previous file with comments | « Source/core/html/track/vtt/VTTParser.cpp ('k') | Source/core/html/track/vtt/VTTScanner.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698