Add a utils/string_stream class.

This CL adds `utils::StringStream`. This is a wrapper over
std::stringstream which forces the locale to always be `classic`. The
logic to format floats and doubles as expected is moved from
`float_to_string` and handled in the StreamStream. This will make all of
our float emission the same.

Bug: tint:1686
Change-Id: If51868f577580d3ea6ab94d3910393e239fd55e4
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/121800
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
dan sinclair 2023-02-28 13:05:21 +00:00 committed by Dawn LUCI CQ
parent a2bbe2372e
commit 7ca41fffb7
6 changed files with 259 additions and 29 deletions

View File

@ -221,6 +221,8 @@ libtint_source_set("libtint_base_src") {
"utils/slice.h",
"utils/string.cc",
"utils/string.h",
"utils/string_stream.cc",
"utils/string_stream.h",
"utils/unique_allocator.h",
"utils/unique_vector.h",
"utils/vector.h",
@ -1559,6 +1561,7 @@ if (tint_build_unittests) {
"utils/reverse_test.cc",
"utils/scoped_assignment_test.cc",
"utils/slice_test.cc",
"utils/string_stream_test.cc",
"utils/string_test.cc",
"utils/transform_test.cc",
"utils/unique_allocator_test.cc",

View File

@ -530,6 +530,8 @@ list(APPEND TINT_LIB_SRCS
utils/slice.h
utils/string.cc
utils/string.h
utils/string_stream.cc
utils/string_stream.h
utils/unique_allocator.h
utils/unique_vector.h
utils/vector.h
@ -985,6 +987,7 @@ if(TINT_BUILD_TESTS)
utils/reverse_test.cc
utils/scoped_assignment_test.cc
utils/slice_test.cc
utils/string_stream_test.cc
utils/string_test.cc
utils/transform_test.cc
utils/unique_allocator_test.cc

View File

@ -0,0 +1,27 @@
// Copyright 2023 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/string_stream.h"
namespace tint::utils {
StringStream::StringStream() {
sstream_.flags(sstream_.flags() | std::ios_base::showpoint | std::ios_base::fixed);
sstream_.imbue(std::locale::classic());
sstream_.precision(9);
}
StringStream::~StringStream() = default;
} // namespace tint::utils

View File

@ -0,0 +1,105 @@
// Copyright 2023 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_TINT_UTILS_STRING_STREAM_H_
#define SRC_TINT_UTILS_STRING_STREAM_H_
#include <functional>
#include <limits>
#include <sstream>
#include <string>
namespace tint::utils {
/// Stringstream wrapper which automatically resets the locale and sets floating point emission
/// settings needed for Tint.
class StringStream {
public:
/// Constructor
StringStream();
/// Destructor
~StringStream();
/// Emit `value` to the stream
/// @param value the value to emit
/// @returns a reference to this
template <typename T,
typename std::enable_if<!std::is_floating_point<T>::value>::type* = nullptr>
StringStream& operator<<(const T& value) {
sstream_ << value;
return *this;
}
/// Emit `value` to the stream
/// @param value the value to emit
/// @returns a reference to this
template <typename T,
typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
StringStream& operator<<(const T& value) {
// Try printing the float in fixed point, with a smallish limit on the precision
std::stringstream fixed;
fixed.flags(fixed.flags() | std::ios_base::showpoint | std::ios_base::fixed);
fixed.imbue(std::locale::classic());
fixed.precision(9);
fixed << value;
std::string str = fixed.str();
// If this string can be parsed without loss of information, use it.
// (Use double here to dodge a bug in older libc++ versions which would incorrectly read
// back FLT_MAX as INF.)
double roundtripped;
fixed >> roundtripped;
auto float_equal_no_warning = std::equal_to<T>();
if (float_equal_no_warning(value, static_cast<T>(roundtripped))) {
while (str.length() >= 2 && str[str.size() - 1] == '0' && str[str.size() - 2] != '.') {
str.pop_back();
}
sstream_ << str;
return *this;
}
// Resort to scientific, with the minimum precision needed to preserve the whole float
std::stringstream sci;
sci.imbue(std::locale::classic());
sci.precision(std::numeric_limits<T>::max_digits10);
sci << value;
sstream_ << sci.str();
return *this;
}
/// The callback to emit a `endl` to the stream
using StdEndl = std::ostream& (*)(std::ostream&);
/// @param manipulator the callback to emit too
/// @returns a reference to this
StringStream& operator<<(StdEndl manipulator) {
// call the function, and return it's value
manipulator(sstream_);
return *this;
}
/// @returns the string contents of the stream
std::string str() const { return sstream_.str(); }
private:
std::stringstream sstream_;
};
} // namespace tint::utils
#endif // SRC_TINT_UTILS_STRING_STREAM_H_

View File

@ -0,0 +1,117 @@
// Copyright 2023 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/string_stream.h"
#include <math.h>
#include <cstring>
#include <limits>
#include "gtest/gtest.h"
namespace tint::utils {
namespace {
using StringStreamTest = testing::Test;
TEST_F(StringStreamTest, Endl) {
StringStream s;
s << std::endl;
EXPECT_EQ(s.str(), "\n");
}
TEST_F(StringStreamTest, Zero) {
StringStream s;
s << 0.0f;
EXPECT_EQ(s.str(), "0.0");
}
TEST_F(StringStreamTest, One) {
StringStream s;
s << 1.0f;
EXPECT_EQ(s.str(), "1.0");
}
TEST_F(StringStreamTest, MinusOne) {
StringStream s;
s << -1.0f;
EXPECT_EQ(s.str(), "-1.0");
}
TEST_F(StringStreamTest, Billion) {
StringStream s;
s << 1e9f;
EXPECT_EQ(s.str(), "1000000000.0");
}
TEST_F(StringStreamTest, Small) {
StringStream s;
s << std::numeric_limits<float>::epsilon();
EXPECT_NE(s.str(), "0.0");
}
TEST_F(StringStreamTest, Highest) {
const auto highest = std::numeric_limits<float>::max();
const auto expected_highest = 340282346638528859811704183484516925440.0f;
if (highest < expected_highest || highest > expected_highest) {
GTEST_SKIP() << "std::numeric_limits<float>::max() is not as expected for "
"this target";
}
StringStream s;
s << std::numeric_limits<float>::max();
EXPECT_EQ(s.str(), "340282346638528859811704183484516925440.0");
}
TEST_F(StringStreamTest, Lowest) {
// Some compilers complain if you test floating point numbers for equality.
// So say it via two inequalities.
const auto lowest = std::numeric_limits<float>::lowest();
const auto expected_lowest = -340282346638528859811704183484516925440.0f;
if (lowest < expected_lowest || lowest > expected_lowest) {
GTEST_SKIP() << "std::numeric_limits<float>::lowest() is not as expected for "
"this target";
}
StringStream s;
s << std::numeric_limits<float>::lowest();
EXPECT_EQ(s.str(), "-340282346638528859811704183484516925440.0");
}
TEST_F(StringStreamTest, Precision) {
{
StringStream s;
s << 1e-8f;
EXPECT_EQ(s.str(), "0.00000001");
}
{
StringStream s;
s << 1e-9f;
EXPECT_EQ(s.str(), "0.000000001");
}
{
StringStream s;
s << 1e-10f;
EXPECT_EQ(s.str(), "1.00000001e-10");
}
{
StringStream s;
s << 1e-20f;
EXPECT_EQ(s.str(), "9.99999968e-21");
}
}
} // namespace
} // namespace tint::utils

View File

@ -22,6 +22,7 @@
#include <sstream>
#include "src/tint/debug.h"
#include "src/tint/utils/string_stream.h"
namespace tint::writer {
@ -52,35 +53,9 @@ struct Traits<double> {
template <typename F>
std::string ToString(F f) {
// Try printing the float in fixed point, with a smallish limit on the precision
std::stringstream fixed;
fixed.flags(fixed.flags() | std::ios_base::showpoint | std::ios_base::fixed);
fixed.imbue(std::locale::classic());
fixed.precision(9);
fixed << f;
std::string str = fixed.str();
// If this string can be parsed without loss of information, use it.
// (Use double here to dodge a bug in older libc++ versions which would incorrectly read back
// FLT_MAX as INF.)
double roundtripped;
fixed >> roundtripped;
auto float_equal_no_warning = std::equal_to<F>();
if (float_equal_no_warning(f, static_cast<F>(roundtripped))) {
while (str.length() >= 2 && str[str.size() - 1] == '0' && str[str.size() - 2] != '.') {
str.pop_back();
}
return str;
}
// Resort to scientific, with the minimum precision needed to preserve the whole float
std::stringstream sci;
sci.imbue(std::locale::classic());
sci.precision(std::numeric_limits<F>::max_digits10);
sci << f;
return sci.str();
utils::StringStream s;
s << f;
return s.str();
}
template <typename F>