tint: Use new string utilities in various places
Change-Id: I8c97a8a92f5e5e3d9768933589aa304ff52ab103
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/131748
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/src/tint/cmd/helper.cc b/src/tint/cmd/helper.cc
index a00e24a..2e2398c 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 @@
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 76a9256..e7f596a 100644
--- a/src/tint/cmd/main.cc
+++ b/src/tint/cmd/main.cc
@@ -205,47 +205,34 @@
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 85644e2..9e1fbd8 100644
--- a/src/tint/writer/glsl/generator_impl.cc
+++ b/src/tint/writer/glsl/generator_impl.cc
@@ -2338,7 +2338,7 @@
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 e8b697b..81a07d2 100644
--- a/src/tint/writer/hlsl/generator_impl.cc
+++ b/src/tint/writer/hlsl/generator_impl.cc
@@ -3719,7 +3719,7 @@
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 d955d78..f3f660a 100644
--- a/src/tint/writer/msl/generator_impl.cc
+++ b/src/tint/writer/msl/generator_impl.cc
@@ -2253,7 +2253,7 @@
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 13a75d5..4b0ab6a 100644
--- a/src/tint/writer/text_generator.cc
+++ b/src/tint/writer/text_generator.cc
@@ -39,15 +39,6 @@
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 04fe785..ad3eb80 100644
--- a/src/tint/writer/text_generator.h
+++ b/src/tint/writer/text_generator.h
@@ -114,12 +114,6 @@
/// 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 2703e8d..0d626f2 100644
--- a/src/tint/writer/wgsl/generator_impl.cc
+++ b/src/tint/writer/wgsl/generator_impl.cc
@@ -818,14 +818,14 @@
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 @@
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;
}
}