OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/audio/point.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_split.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "base/strings/stringprintf.h" | |
14 | |
15 namespace media { | |
16 | |
17 std::string PointsToString(const std::vector<Point>& points) { | |
18 std::string points_string; | |
19 if (!points.empty()) { | |
20 for (size_t i = 0; i < points.size() - 1; ++i) { | |
21 points_string.append(points[i].ToString()); | |
22 points_string.append(", "); | |
23 } | |
24 points_string.append(points.back().ToString()); | |
25 } | |
26 return points_string; | |
27 } | |
28 | |
29 std::vector<Point> ParsePointsFromString(const std::string& points_string) { | |
30 std::vector<Point> points; | |
31 if (points_string.empty()) | |
32 return points; | |
33 | |
34 const auto& tokens = | |
35 base::SplitString(points_string, base::kWhitespaceASCII, | |
36 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
37 if (tokens.size() < 3 || tokens.size() % 3 != 0) { | |
38 LOG(ERROR) << "Malformed points string: " << points_string; | |
39 return points; | |
40 } | |
41 | |
42 std::vector<float> float_tokens; | |
43 float_tokens.reserve(tokens.size()); | |
44 for (const auto& token : tokens) { | |
45 double float_token; | |
46 if (!base::StringToDouble(token, &float_token)) { | |
47 LOG(ERROR) << "Unable to convert token=" << token | |
48 << " to double from points string: " << points_string; | |
49 return points; | |
50 } | |
51 float_tokens.push_back(float_token); | |
52 } | |
53 | |
54 points.reserve(float_tokens.size() / 3); | |
55 for (size_t i = 0; i < float_tokens.size(); i += 3) { | |
56 points.push_back( | |
57 Point(float_tokens[i + 0], float_tokens[i + 1], float_tokens[i + 2])); | |
58 } | |
59 | |
60 return points; | |
61 } | |
62 | |
63 } // namespace media | |
OLD | NEW |