Chromium Code Reviews| Index: media/audio/point.cc |
| diff --git a/media/audio/point.cc b/media/audio/point.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2580bb37751b0fe719ab4d1dd2c645f90e062f04 |
| --- /dev/null |
| +++ b/media/audio/point.cc |
| @@ -0,0 +1,61 @@ |
| +// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
|
mcasas
2015/09/02 03:05:37
// Copyright 2015,
with no (c)
ajm
2015/09/02 06:41:48
Thanks, copied from an old file.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/audio/point.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/strings/stringprintf.h" |
| + |
| +namespace media { |
| + |
| +Point::Point() : x_(0), y_(0), z_(0){}; |
| +Point::Point(float x, float y, float z) : x_(x), y_(y), z_(z) {} |
| + |
| +bool Point::IsValid() const { |
| + return std::isfinite(x_) && std::isfinite(y_) && std::isfinite(z_); |
| +} |
| + |
| +std::string Point::ToString() const { |
| + return base::StringPrintf("x=%.3f, y=%.3f, z=%.3f", x_, y_, z_); |
| +} |
| + |
| +std::vector<Point> ParsePointsFromString(const std::string& points_string) { |
| + std::vector<Point> points; |
| + if (points_string.empty()) { |
|
mcasas
2015/09/02 03:05:37
No {}
|
| + return points; |
| + } |
| + |
| + const auto& tokens = |
| + base::SplitString(points_string, base::kWhitespaceASCII, |
| + base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| + if (tokens.size() < 3 || tokens.size() % 3 != 0) { |
| + LOG(ERROR) << "Malformed points string: " << points_string; |
| + return points; |
| + } |
| + |
| + std::vector<float> float_tokens; |
| + float_tokens.reserve(tokens.size()); |
|
mcasas
2015/09/02 03:05:37
std::vector<float> float_tokens(tokens.size());
ajm
2015/09/02 06:41:48
That will insert tokens.size() default-initialized
mcasas
2015/09/03 15:29:27
Actually both are the same, reserve() will also in
ajm
2015/09/03 20:32:04
They are most certainly not the same. reserve() al
|
| + for (const auto& token : tokens) { |
| + double float_token; |
| + if (!base::StringToDouble(token, &float_token)) { |
| + LOG(ERROR) << "Unable to convert token=" << token |
| + << " to double from points string: " << points_string; |
| + return points; |
| + } |
| + float_tokens.push_back(float_token); |
| + } |
| + |
| + points.reserve(float_tokens.size() / 3); |
| + for (size_t i = 0; i < float_tokens.size(); i += 3) { |
| + points.push_back( |
| + Point(float_tokens[i + 0], float_tokens[i + 1], float_tokens[i + 2])); |
| + } |
| + |
| + return points; |
| +} |
| + |
| +} // namespace media |