| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013, Opera Software ASA. All rights reserved. | 2 * Copyright (C) 2013, Opera Software ASA. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 namespace blink { | 36 namespace blink { |
| 37 | 37 |
| 38 bool BufferedLineReader::getLine(String& line) | 38 bool BufferedLineReader::getLine(String& line) |
| 39 { | 39 { |
| 40 if (m_maybeSkipLF) { | 40 if (m_maybeSkipLF) { |
| 41 // We ran out of data after a CR (U+000D), which means that we may be | 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) | 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. | 43 // then skip it, and then (unconditionally) return the buffered line. |
| 44 if (!m_buffer.isEmpty()) { | 44 if (!m_buffer.isEmpty()) { |
| 45 scanCharacter(newlineCharacter); | 45 scanCharacter(characterNewline); |
| 46 m_maybeSkipLF = false; | 46 m_maybeSkipLF = false; |
| 47 } | 47 } |
| 48 // If there was no (new) data available, then keep m_maybeSkipLF set, | 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 | 49 // and fall through all the way down to the EOS check at the end of |
| 50 // the method. | 50 // the method. |
| 51 } | 51 } |
| 52 | 52 |
| 53 bool shouldReturnLine = false; | 53 bool shouldReturnLine = false; |
| 54 bool checkForLF = false; | 54 bool checkForLF = false; |
| 55 while (!m_buffer.isEmpty()) { | 55 while (!m_buffer.isEmpty()) { |
| 56 UChar c = m_buffer.currentChar(); | 56 UChar c = m_buffer.currentChar(); |
| 57 m_buffer.advance(); | 57 m_buffer.advance(); |
| 58 | 58 |
| 59 if (c == newlineCharacter || c == carriageReturn) { | 59 if (c == characterNewline || c == characterCarriageReturn) { |
| 60 // We found a line ending. Return the accumulated line. | 60 // We found a line ending. Return the accumulated line. |
| 61 shouldReturnLine = true; | 61 shouldReturnLine = true; |
| 62 checkForLF = (c == carriageReturn); | 62 checkForLF = (c == characterCarriageReturn); |
| 63 break; | 63 break; |
| 64 } | 64 } |
| 65 | 65 |
| 66 // NULs are transformed into U+FFFD (REPLACEMENT CHAR.) in step 1 of | 66 // NULs are transformed into U+FFFD (REPLACEMENT CHAR.) in step 1 of |
| 67 // the WebVTT parser algorithm. | 67 // the WebVTT parser algorithm. |
| 68 if (c == '\0') | 68 if (c == '\0') |
| 69 c = replacementCharacter; | 69 c = characterReplacement; |
| 70 | 70 |
| 71 m_lineBuffer.append(c); | 71 m_lineBuffer.append(c); |
| 72 } | 72 } |
| 73 | 73 |
| 74 if (checkForLF) { | 74 if (checkForLF) { |
| 75 // May be in the middle of a CRLF pair. | 75 // May be in the middle of a CRLF pair. |
| 76 if (!m_buffer.isEmpty()) { | 76 if (!m_buffer.isEmpty()) { |
| 77 // Scan a potential newline character. | 77 // Scan a potential newline character. |
| 78 scanCharacter(newlineCharacter); | 78 scanCharacter(characterNewline); |
| 79 } else { | 79 } else { |
| 80 // Check for the LF on the next call (unless we reached EOS, in | 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 | 81 // which case we'll return the contents of the line buffer, and |
| 82 // reset state for the next line.) | 82 // reset state for the next line.) |
| 83 m_maybeSkipLF = true; | 83 m_maybeSkipLF = true; |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 if (isAtEndOfStream()) { | 87 if (isAtEndOfStream()) { |
| 88 // We've reached the end of the stream proper. Emit a line if the | 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 | 89 // current line buffer is non-empty. (Note that if shouldReturnLine is |
| 90 // set already, we want to return a line nonetheless.) | 90 // set already, we want to return a line nonetheless.) |
| 91 shouldReturnLine |= !m_lineBuffer.isEmpty(); | 91 shouldReturnLine |= !m_lineBuffer.isEmpty(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 if (shouldReturnLine) { | 94 if (shouldReturnLine) { |
| 95 line = m_lineBuffer.toString(); | 95 line = m_lineBuffer.toString(); |
| 96 m_lineBuffer.clear(); | 96 m_lineBuffer.clear(); |
| 97 return true; | 97 return true; |
| 98 } | 98 } |
| 99 | 99 |
| 100 ASSERT(m_buffer.isEmpty()); | 100 ASSERT(m_buffer.isEmpty()); |
| 101 return false; | 101 return false; |
| 102 } | 102 } |
| 103 | 103 |
| 104 } // namespace blink | 104 } // namespace blink |
| OLD | NEW |