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 "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" |
| 12 |
| 13 namespace media { |
| 14 |
| 15 std::string PointsToString(const std::vector<Point>& points) { |
| 16 std::string points_string; |
| 17 if (!points.empty()) { |
| 18 for (size_t i = 0; i < points.size() - 1; ++i) { |
| 19 points_string.append(points[i].ToString()); |
| 20 points_string.append(", "); |
| 21 } |
| 22 points_string.append(points.back().ToString()); |
| 23 } |
| 24 return points_string; |
| 25 } |
| 26 |
| 27 std::vector<Point> ParsePointsFromString(const std::string& points_string) { |
| 28 std::vector<Point> points; |
| 29 if (points_string.empty()) |
| 30 return points; |
| 31 |
| 32 const auto& tokens = |
| 33 base::SplitString(points_string, base::kWhitespaceASCII, |
| 34 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 35 if (tokens.size() < 3 || tokens.size() % 3 != 0) { |
| 36 LOG(ERROR) << "Malformed points string: " << points_string; |
| 37 return points; |
| 38 } |
| 39 |
| 40 std::vector<float> float_tokens; |
| 41 float_tokens.reserve(tokens.size()); |
| 42 for (const auto& token : tokens) { |
| 43 double float_token; |
| 44 if (!base::StringToDouble(token, &float_token)) { |
| 45 LOG(ERROR) << "Unable to convert token=" << token |
| 46 << " to double from points string: " << points_string; |
| 47 return points; |
| 48 } |
| 49 float_tokens.push_back(float_token); |
| 50 } |
| 51 |
| 52 points.reserve(float_tokens.size() / 3); |
| 53 for (size_t i = 0; i < float_tokens.size(); i += 3) { |
| 54 points.push_back( |
| 55 Point(float_tokens[i + 0], float_tokens[i + 1], float_tokens[i + 2])); |
| 56 } |
| 57 |
| 58 return points; |
| 59 } |
| 60 |
| 61 } // namespace media |
OLD | NEW |