Home | History | Annotate | Download | only in vtt
      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 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 
     31 #include "config.h"
     32 #include "core/html/track/vtt/BufferedLineReader.h"
     33 
     34 #include "wtf/unicode/CharacterNames.h"
     35 
     36 namespace WebCore {
     37 
     38 bool BufferedLineReader::getLine(String& line)
     39 {
     40     if (m_maybeSkipLF) {
     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;
     47         }
     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
     50         // the method.
     51     }
     52 
     53     bool shouldReturnLine = false;
     54     bool checkForLF = false;
     55     while (!m_buffer.isEmpty()) {
     56         UChar c = m_buffer.currentChar();
     57         m_buffer.advance();
     58 
     59         if (c == newlineCharacter || c == carriageReturn) {
     60             // We found a line ending. Return the accumulated line.
     61             shouldReturnLine = true;
     62             checkForLF = (c == carriageReturn);
     63             break;
     64         }
     65 
     66         // NULs are transformed into U+FFFD (REPLACEMENT CHAR.) in step 1 of
     67         // the WebVTT parser algorithm.
     68         if (c == '\0')
     69             c = replacementCharacter;
     70 
     71         m_lineBuffer.append(c);
     72     }
     73 
     74     if (checkForLF) {
     75         // May be in the middle of a CRLF pair.
     76         if (!m_buffer.isEmpty()) {
     77             // Scan a potential newline character.
     78             scanCharacter(newlineCharacter);
     79         } else {
     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.)
     83             m_maybeSkipLF = true;
     84         }
     85     }
     86 
     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();
     92     }
     93 
     94     if (shouldReturnLine) {
     95         line = m_lineBuffer.toString();
     96         m_lineBuffer.clear();
     97         return true;
     98     }
     99 
    100     ASSERT(m_buffer.isEmpty());
    101     return false;
    102 }
    103 
    104 } // namespace WebCore
    105