From 3731ce8f21bc737773805e53fc3ff2d330e1b5f1 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 9 May 2023 17:34:08 +0000 Subject: [PATCH] tint: Use new string utilities in various places Change-Id: I8c97a8a92f5e5e3d9768933589aa304ff52ab103 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/131748 Kokoro: Kokoro Commit-Queue: Ben Clayton Reviewed-by: Antonio Maiorano --- src/tint/cmd/helper.cc | 9 +++++---- src/tint/cmd/main.cc | 23 +++++------------------ src/tint/writer/glsl/generator_impl.cc | 2 +- src/tint/writer/hlsl/generator_impl.cc | 2 +- src/tint/writer/msl/generator_impl.cc | 2 +- src/tint/writer/text_generator.cc | 9 --------- src/tint/writer/text_generator.h | 6 ------ src/tint/writer/wgsl/generator_impl.cc | 8 ++++---- 8 files changed, 17 insertions(+), 44 deletions(-) diff --git a/src/tint/cmd/helper.cc b/src/tint/cmd/helper.cc index a00e24a18c..2e2398c874 100644 --- a/src/tint/cmd/helper.cc +++ b/src/tint/cmd/helper.cc @@ -22,6 +22,8 @@ #include "spirv-tools/libspirv.hpp" #endif +#include "src/tint/utils/string.h" + namespace tint::cmd { namespace { @@ -34,12 +36,11 @@ enum class InputFormat { InputFormat InputFormatFromFilename(const std::string& filename) { auto input_format = InputFormat::kUnknown; - - if (filename.size() > 5 && filename.substr(filename.size() - 5) == ".wgsl") { + if (utils::HasSuffix(filename, ".wgsl")) { input_format = InputFormat::kWgsl; - } else if (filename.size() > 4 && filename.substr(filename.size() - 4) == ".spv") { + } else if (utils::HasSuffix(filename, ".spv")) { input_format = InputFormat::kSpirvBin; - } else if (filename.size() > 7 && filename.substr(filename.size() - 7) == ".spvasm") { + } else if (utils::HasSuffix(filename, ".spvasm")) { input_format = InputFormat::kSpirvAsm; } return input_format; diff --git a/src/tint/cmd/main.cc b/src/tint/cmd/main.cc index 76a92565af..e7f596a059 100644 --- a/src/tint/cmd/main.cc +++ b/src/tint/cmd/main.cc @@ -205,47 +205,34 @@ Format parse_format(const std::string& fmt) { return Format::kUnknown; } -#if TINT_BUILD_SPV_WRITER || TINT_BUILD_WGSL_WRITER || TINT_BUILD_MSL_WRITER || \ - TINT_BUILD_HLSL_WRITER -/// @param input input string -/// @param suffix potential suffix string -/// @returns true if input ends with the given suffix. -bool ends_with(const std::string& input, const std::string& suffix) { - const auto input_len = input.size(); - const auto suffix_len = suffix.size(); - // Avoid integer overflow. - return (input_len >= suffix_len) && (input_len - suffix_len == input.rfind(suffix)); -} -#endif - /// @param filename the filename to inspect /// @returns the inferred format for the filename suffix Format infer_format(const std::string& filename) { (void)filename; #if TINT_BUILD_SPV_WRITER - if (ends_with(filename, ".spv")) { + if (tint::utils::HasSuffix(filename, ".spv")) { return Format::kSpirv; } - if (ends_with(filename, ".spvasm")) { + if (tint::utils::HasSuffix(filename, ".spvasm")) { return Format::kSpvAsm; } #endif // TINT_BUILD_SPV_WRITER #if TINT_BUILD_WGSL_WRITER - if (ends_with(filename, ".wgsl")) { + if (tint::utils::HasSuffix(filename, ".wgsl")) { return Format::kWgsl; } #endif // TINT_BUILD_WGSL_WRITER #if TINT_BUILD_MSL_WRITER - if (ends_with(filename, ".metal")) { + if (tint::utils::HasSuffix(filename, ".metal")) { return Format::kMsl; } #endif // TINT_BUILD_MSL_WRITER #if TINT_BUILD_HLSL_WRITER - if (ends_with(filename, ".hlsl")) { + if (tint::utils::HasSuffix(filename, ".hlsl")) { return Format::kHlsl; } #endif // TINT_BUILD_HLSL_WRITER diff --git a/src/tint/writer/glsl/generator_impl.cc b/src/tint/writer/glsl/generator_impl.cc index 85644e210c..9e1fbd8221 100644 --- a/src/tint/writer/glsl/generator_impl.cc +++ b/src/tint/writer/glsl/generator_impl.cc @@ -2338,7 +2338,7 @@ void GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) { out << cond_buf.str() << "; "; if (!cont_buf.lines.empty()) { - out << TrimSuffix(cont_buf.lines[0].content, ";"); + out << utils::TrimSuffix(cont_buf.lines[0].content, ";"); } } out << " {"; diff --git a/src/tint/writer/hlsl/generator_impl.cc b/src/tint/writer/hlsl/generator_impl.cc index e8b697b7a8..81a07d23a8 100644 --- a/src/tint/writer/hlsl/generator_impl.cc +++ b/src/tint/writer/hlsl/generator_impl.cc @@ -3719,7 +3719,7 @@ bool GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) { out << cond_buf.str() << "; "; if (!cont_buf.lines.empty()) { - out << TrimSuffix(cont_buf.lines[0].content, ";"); + out << utils::TrimSuffix(cont_buf.lines[0].content, ";"); } } out << " {"; diff --git a/src/tint/writer/msl/generator_impl.cc b/src/tint/writer/msl/generator_impl.cc index d955d7847b..f3f660a5d8 100644 --- a/src/tint/writer/msl/generator_impl.cc +++ b/src/tint/writer/msl/generator_impl.cc @@ -2253,7 +2253,7 @@ bool GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) { out << cond_buf.str() << "; "; if (!cont_buf.lines.empty()) { - out << TrimSuffix(cont_buf.lines[0].content, ";"); + out << utils::TrimSuffix(cont_buf.lines[0].content, ";"); } } out << " {"; diff --git a/src/tint/writer/text_generator.cc b/src/tint/writer/text_generator.cc index 13a75d593a..4b0ab6a40a 100644 --- a/src/tint/writer/text_generator.cc +++ b/src/tint/writer/text_generator.cc @@ -39,15 +39,6 @@ std::string TextGenerator::StructName(const type::Struct* s) { return name; } -std::string TextGenerator::TrimSuffix(std::string str, const std::string& suffix) { - if (str.size() >= suffix.size()) { - if (str.substr(str.size() - suffix.size(), suffix.size()) == suffix) { - return str.substr(0, str.size() - suffix.size()); - } - } - return str; -} - TextGenerator::LineWriter::LineWriter(TextBuffer* buf) : buffer(buf) {} TextGenerator::LineWriter::LineWriter(LineWriter&& other) { diff --git a/src/tint/writer/text_generator.h b/src/tint/writer/text_generator.h index 04fe7851bb..ad3eb801f9 100644 --- a/src/tint/writer/text_generator.h +++ b/src/tint/writer/text_generator.h @@ -114,12 +114,6 @@ class TextGenerator { /// underscores. std::string StructName(const type::Struct* s); - /// @param str the string - /// @param suffix the suffix to remove - /// @return returns str without the provided trailing suffix string. If str - /// doesn't end with suffix, str is returned unchanged. - std::string TrimSuffix(std::string str, const std::string& suffix); - protected: /// LineWriter is a helper that acts as a string buffer, who's content is /// emitted to the TextBuffer as a single line on destruction. diff --git a/src/tint/writer/wgsl/generator_impl.cc b/src/tint/writer/wgsl/generator_impl.cc index 2703e8d0ac..0d626f2d1b 100644 --- a/src/tint/writer/wgsl/generator_impl.cc +++ b/src/tint/writer/wgsl/generator_impl.cc @@ -818,14 +818,14 @@ void GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) { case 0: // No initializer break; case 1: // Single line initializer statement - out << TrimSuffix(init_buf.lines[0].content, ";"); + out << utils::TrimSuffix(init_buf.lines[0].content, ";"); break; default: // Block initializer statement for (size_t i = 1; i < init_buf.lines.size(); i++) { // Indent all by the first line init_buf.lines[i].indent += current_buffer_->current_indent; } - out << TrimSuffix(init_buf.String(), "\n"); + out << utils::TrimSuffix(init_buf.String(), "\n"); break; } @@ -841,14 +841,14 @@ void GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) { case 0: // No continuing break; case 1: // Single line continuing statement - out << TrimSuffix(cont_buf.lines[0].content, ";"); + out << utils::TrimSuffix(cont_buf.lines[0].content, ";"); break; default: // Block continuing statement for (size_t i = 1; i < cont_buf.lines.size(); i++) { // Indent all by the first line cont_buf.lines[i].indent += current_buffer_->current_indent; } - out << TrimSuffix(cont_buf.String(), "\n"); + out << utils::TrimSuffix(cont_buf.String(), "\n"); break; } }