Chromium Code Reviews| Index: media/webm/webm_webvtt_parser.cc |
| diff --git a/media/webm/webm_webvtt_parser.cc b/media/webm/webm_webvtt_parser.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..563fd5e3f51016045e990c5a7c2a23d17d70ef8f |
| --- /dev/null |
| +++ b/media/webm/webm_webvtt_parser.cc |
| @@ -0,0 +1,79 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
acolwell GONE FROM CHROMIUM
2013/05/10 18:41:39
nit: s/2012/2013/
Matthew Heaney (Chromium)
2013/05/11 07:29:13
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/webm/webm_webvtt_parser.h" |
| + |
| +namespace media { |
| + |
| +void WebMWebVTTParser::Parse(const uint8* payload, int payload_size, |
| + std::string* id, |
| + std::string* settings, |
| + std::string* content) { |
| + WebMWebVTTParser parser(payload, payload_size); |
| + parser(id, settings, content); |
| +} |
| + |
| +WebMWebVTTParser::WebMWebVTTParser(const uint8* payload, int payload_size) |
| + : ptr_(payload), |
| + ptr_end_(payload + payload_size) { |
| +} |
| + |
| +void WebMWebVTTParser::operator()(std::string* id, |
|
acolwell GONE FROM CHROMIUM
2013/05/10 18:41:39
nit: It would be better for readability if this fu
Matthew Heaney (Chromium)
2013/05/11 07:29:13
Done.
|
| + std::string* settings, |
| + std::string* content) { |
| + ParseLine(id); |
| + ParseLine(settings); |
| + content->assign(ptr_, ptr_end_); |
| +} |
| + |
| +bool WebMWebVTTParser::GetByte(uint8* byte) { |
| + if (ptr_ >= ptr_end_) |
| + return false; // indicates end-of-stream |
| + |
| + *byte = *ptr_++; |
| + return true; |
| +} |
| + |
| +void WebMWebVTTParser::UngetByte() { |
| + --ptr_; |
| +} |
| + |
| +void WebMWebVTTParser::ParseLine(std::string* line_ptr) { |
| + std::string& line = *line_ptr; |
|
acolwell GONE FROM CHROMIUM
2013/05/10 18:41:39
nit: This isn't really necessary. Just use -> belo
Matthew Heaney (Chromium)
2013/05/11 07:29:13
Done.
|
| + line.clear(); |
| + |
| + // Consume characters from the stream, until we reach end-of-line. |
| + |
| + // The WebVTT spec states that lines may be terminated in any of the following |
| + // three ways: |
| + // LF |
| + // CR |
| + // CR LF |
| + |
| + // The spec is here: |
| + // http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm |
| + |
| + enum { |
| + kLF = '\x0A', |
| + kCR = '\x0D' |
| + }; |
| + |
| + for (;;) { |
| + uint8 byte; |
| + |
| + if (!GetByte(&byte) || byte == kLF) |
| + return; |
| + |
| + if (byte == kCR) { |
| + if (GetByte(&byte) && byte != kLF) |
| + UngetByte(); |
| + |
| + return; |
| + } |
| + |
| + line.push_back(byte); |
| + } |
| +} |
| + |
| +} // namespace media |