summaryrefslogtreecommitdiff
path: root/chromium/media/formats/hls/source_string.cc
blob: ad9db86f436992d869e9b192b3168824e70c0266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/formats/hls/source_string.h"

#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/types/pass_key.h"
#include "media/formats/hls/parse_status.h"

namespace media::hls {

// static
template <>
SourceString SourceString::Create(base::PassKey<SourceLineIterator>,
                                  size_t line,
                                  base::StringPiece str) {
  return SourceString(line, 1, str, {});
}

// static
template <>
ResolvedSourceString ResolvedSourceString::Create(
    base::PassKey<VariableDictionary>,
    size_t line,
    size_t column,
    base::StringPiece str,
    ResolvedSourceStringState resolution_state) {
  return ResolvedSourceString(line, column, str, resolution_state);
}

// static
template <typename ResolutionState>
GenericSourceString<ResolutionState>
GenericSourceString<ResolutionState>::CreateForTesting(base::StringPiece str) {
  return GenericSourceString::CreateForTesting(1, 1, str);
}

// static
template <>
SourceString SourceString::CreateForTesting(size_t line,
                                            size_t column,
                                            base::StringPiece str) {
  return SourceString::CreateForTesting(line, column, str, {});
}

// static
template <>
ResolvedSourceString ResolvedSourceString::CreateForTesting(
    size_t line,
    size_t column,
    base::StringPiece str) {
  return ResolvedSourceString::CreateForTesting(
      line, column, str,
      ResolvedSourceStringState{.contains_substitutions = false});
}

// static
template <typename ResolutionState>
GenericSourceString<ResolutionState>
GenericSourceString<ResolutionState>::CreateForTesting(
    size_t line,
    size_t column,
    base::StringPiece str,
    ResolutionState resolution_state) {
  return GenericSourceString(line, column, str, resolution_state);
}

template <typename ResolutionState>
GenericSourceString<ResolutionState>
GenericSourceString<ResolutionState>::Substr(size_t pos, size_t count) const {
  const auto column = column_ + pos;
  return GenericSourceString(line_, column, str_.substr(pos, count),
                             resolution_state_);
}

template <typename ResolutionState>
GenericSourceString<ResolutionState>
GenericSourceString<ResolutionState>::Consume(size_t count) {
  count = std::min(count, str_.size());

  auto consumed = Substr(0, count);
  *this = Substr(count);

  return consumed;
}

template <typename ResolutionState>
GenericSourceString<ResolutionState>
GenericSourceString<ResolutionState>::ConsumeDelimiter(char c) {
  const auto index = Str().find_first_of(c);
  const auto prefix = Consume(index);
  Consume(1);
  return prefix;
}

template <>
ResolvedSourceString SourceString::SkipVariableSubstitution() const {
  return ResolvedSourceString(
      Line(), Column(), Str(),
      ResolvedSourceStringState{.contains_substitutions = false});
}

template <typename ResolutionState>
void GenericSourceString<ResolutionState>::TrimStart() {
  auto start = Str().find_first_not_of(" \t");
  Consume(start);
}

template <>
bool SourceString::ContainsSubstitutions() const {
  return false;
}

template <>
bool ResolvedSourceString::ContainsSubstitutions() const {
  return resolution_state_.contains_substitutions;
}

template <typename ResolutionState>
GenericSourceString<ResolutionState>::GenericSourceString(
    size_t line,
    size_t column,
    base::StringPiece str,
    ResolutionState resolution_state)
    : line_(line),
      column_(column),
      str_(str),
      resolution_state_(resolution_state) {}

SourceLineIterator::SourceLineIterator(base::StringPiece source)
    : current_line_(1), source_(source) {}

ParseStatus::Or<SourceString> SourceLineIterator::Next() {
  if (source_.empty()) {
    return ParseStatusCode::kReachedEOF;
  }

  const auto line_end = source_.find_first_of("\r\n");
  if (line_end == base::StringPiece::npos) {
    return ParseStatusCode::kInvalidEOL;
  }

  const auto line_content = source_.substr(0, line_end);
  const auto following = source_.substr(line_end);

  // Trim (and validate) newline sequence from the following text
  if (base::StartsWith(following, "\n")) {
    source_ = following.substr(1);
  } else if (base::StartsWith(following, "\r\n")) {
    source_ = following.substr(2);
  } else {
    return ParseStatusCode::kInvalidEOL;
  }

  const auto line_number = current_line_;
  current_line_ += 1;

  return SourceString::Create({}, line_number, line_content);
}

// These forward declarations tell the compiler that we will use
// `GenericSourceString` with these arguments, allowing us to keep these
// definitions in our .cc without causing linker errors. This also means if
// anyone tries to instantiate a `GenericSourceString` with anything but these
// two specializations they'll most likely get linker errors.
template class MEDIA_EXPORT GenericSourceString<SourceStringState>;
template class MEDIA_EXPORT GenericSourceString<ResolvedSourceStringState>;

}  // namespace media::hls