2 * Copyright (C) 2013, Opera Software ASA. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/html/track/vtt/BufferedLineReader.h"
34 #include "wtf/unicode/CharacterNames.h"
38 bool BufferedLineReader::getLine(String& line)
41 // We ran out of data after a CR (U+000D), which means that we may be
42 // in the middle of a CRLF pair. If the next character is a LF (U+000A)
43 // then skip it, and then (unconditionally) return the buffered line.
44 if (!m_buffer.isEmpty()) {
45 scanCharacter(newlineCharacter);
46 m_maybeSkipLF = false;
48 // If there was no (new) data available, then keep m_maybeSkipLF set,
49 // and fall through all the way down to the EOS check at the end of
53 bool shouldReturnLine = false;
54 bool checkForLF = false;
55 while (!m_buffer.isEmpty()) {
56 UChar c = m_buffer.currentChar();
59 if (c == newlineCharacter || c == carriageReturn) {
60 // We found a line ending. Return the accumulated line.
61 shouldReturnLine = true;
62 checkForLF = (c == carriageReturn);
66 // NULs are transformed into U+FFFD (REPLACEMENT CHAR.) in step 1 of
67 // the WebVTT parser algorithm.
69 c = replacementCharacter;
71 m_lineBuffer.append(c);
75 // May be in the middle of a CRLF pair.
76 if (!m_buffer.isEmpty()) {
77 // Scan a potential newline character.
78 scanCharacter(newlineCharacter);
80 // Check for the LF on the next call (unless we reached EOS, in
81 // which case we'll return the contents of the line buffer, and
82 // reset state for the next line.)
87 if (isAtEndOfStream()) {
88 // We've reached the end of the stream proper. Emit a line if the
89 // current line buffer is non-empty. (Note that if shouldReturnLine is
90 // set already, we want to return a line nonetheless.)
91 shouldReturnLine |= !m_lineBuffer.isEmpty();
94 if (shouldReturnLine) {
95 line = m_lineBuffer.toString();
100 ASSERT(m_buffer.isEmpty());