From 4be623c52a6518eb86756a0369706c1d6670f6bb Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 7 Feb 2023 18:36:35 -0800 Subject: Arithmetic simplifications and more IR clean up logic. (#2632) --- source/slang/slang-emit-source-writer.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'source/slang/slang-emit-source-writer.cpp') diff --git a/source/slang/slang-emit-source-writer.cpp b/source/slang/slang-emit-source-writer.cpp index bed9a2dbc..7692bd0ec 100644 --- a/source/slang/slang-emit-source-writer.cpp +++ b/source/slang/slang-emit-source-writer.cpp @@ -247,8 +247,21 @@ void SourceWriter::emit(double value) stream.setf(std::ios::fixed, std::ios::floatfield); stream.precision(20); stream << value; - - emit(stream.str().c_str()); + auto str = stream.str(); + auto slice = UnownedStringSlice(str.c_str()); + // Remove redundant trailing 0s. + if (slice.end() > slice.begin()) + { + auto lastChar = slice.end() - 1; + while (lastChar > slice.begin() && *lastChar == '0') + lastChar--; + if (*lastChar == '.') + lastChar++; + if (lastChar > slice.end() - 1) + lastChar = slice.end() - 1; + slice = slice.subString(0, lastChar - slice.begin() + 1); + } + emit(slice); } void SourceWriter::advanceToSourceLocationIfValid(const SourceLoc& sourceLocation) -- cgit v1.2.3