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>
This commit is contained in:
Ben Clayton 2023-05-09 17:34:08 +00:00 committed by Dawn LUCI CQ
parent ce1025fde3
commit 3731ce8f21
8 changed files with 17 additions and 44 deletions

View File

@ -22,6 +22,8 @@
#include "spirv-tools/libspirv.hpp" #include "spirv-tools/libspirv.hpp"
#endif #endif
#include "src/tint/utils/string.h"
namespace tint::cmd { namespace tint::cmd {
namespace { namespace {
@ -34,12 +36,11 @@ enum class InputFormat {
InputFormat InputFormatFromFilename(const std::string& filename) { InputFormat InputFormatFromFilename(const std::string& filename) {
auto input_format = InputFormat::kUnknown; auto input_format = InputFormat::kUnknown;
if (utils::HasSuffix(filename, ".wgsl")) {
if (filename.size() > 5 && filename.substr(filename.size() - 5) == ".wgsl") {
input_format = InputFormat::kWgsl; 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; 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; input_format = InputFormat::kSpirvAsm;
} }
return input_format; return input_format;

View File

@ -205,47 +205,34 @@ Format parse_format(const std::string& fmt) {
return Format::kUnknown; 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 /// @param filename the filename to inspect
/// @returns the inferred format for the filename suffix /// @returns the inferred format for the filename suffix
Format infer_format(const std::string& filename) { Format infer_format(const std::string& filename) {
(void)filename; (void)filename;
#if TINT_BUILD_SPV_WRITER #if TINT_BUILD_SPV_WRITER
if (ends_with(filename, ".spv")) { if (tint::utils::HasSuffix(filename, ".spv")) {
return Format::kSpirv; return Format::kSpirv;
} }
if (ends_with(filename, ".spvasm")) { if (tint::utils::HasSuffix(filename, ".spvasm")) {
return Format::kSpvAsm; return Format::kSpvAsm;
} }
#endif // TINT_BUILD_SPV_WRITER #endif // TINT_BUILD_SPV_WRITER
#if TINT_BUILD_WGSL_WRITER #if TINT_BUILD_WGSL_WRITER
if (ends_with(filename, ".wgsl")) { if (tint::utils::HasSuffix(filename, ".wgsl")) {
return Format::kWgsl; return Format::kWgsl;
} }
#endif // TINT_BUILD_WGSL_WRITER #endif // TINT_BUILD_WGSL_WRITER
#if TINT_BUILD_MSL_WRITER #if TINT_BUILD_MSL_WRITER
if (ends_with(filename, ".metal")) { if (tint::utils::HasSuffix(filename, ".metal")) {
return Format::kMsl; return Format::kMsl;
} }
#endif // TINT_BUILD_MSL_WRITER #endif // TINT_BUILD_MSL_WRITER
#if TINT_BUILD_HLSL_WRITER #if TINT_BUILD_HLSL_WRITER
if (ends_with(filename, ".hlsl")) { if (tint::utils::HasSuffix(filename, ".hlsl")) {
return Format::kHlsl; return Format::kHlsl;
} }
#endif // TINT_BUILD_HLSL_WRITER #endif // TINT_BUILD_HLSL_WRITER

View File

@ -2338,7 +2338,7 @@ void GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) {
out << cond_buf.str() << "; "; out << cond_buf.str() << "; ";
if (!cont_buf.lines.empty()) { if (!cont_buf.lines.empty()) {
out << TrimSuffix(cont_buf.lines[0].content, ";"); out << utils::TrimSuffix(cont_buf.lines[0].content, ";");
} }
} }
out << " {"; out << " {";

View File

@ -3719,7 +3719,7 @@ bool GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) {
out << cond_buf.str() << "; "; out << cond_buf.str() << "; ";
if (!cont_buf.lines.empty()) { if (!cont_buf.lines.empty()) {
out << TrimSuffix(cont_buf.lines[0].content, ";"); out << utils::TrimSuffix(cont_buf.lines[0].content, ";");
} }
} }
out << " {"; out << " {";

View File

@ -2253,7 +2253,7 @@ bool GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) {
out << cond_buf.str() << "; "; out << cond_buf.str() << "; ";
if (!cont_buf.lines.empty()) { if (!cont_buf.lines.empty()) {
out << TrimSuffix(cont_buf.lines[0].content, ";"); out << utils::TrimSuffix(cont_buf.lines[0].content, ";");
} }
} }
out << " {"; out << " {";

View File

@ -39,15 +39,6 @@ std::string TextGenerator::StructName(const type::Struct* s) {
return name; 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(TextBuffer* buf) : buffer(buf) {}
TextGenerator::LineWriter::LineWriter(LineWriter&& other) { TextGenerator::LineWriter::LineWriter(LineWriter&& other) {

View File

@ -114,12 +114,6 @@ class TextGenerator {
/// underscores. /// underscores.
std::string StructName(const type::Struct* s); 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: protected:
/// LineWriter is a helper that acts as a string buffer, who's content is /// LineWriter is a helper that acts as a string buffer, who's content is
/// emitted to the TextBuffer as a single line on destruction. /// emitted to the TextBuffer as a single line on destruction.

View File

@ -818,14 +818,14 @@ void GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) {
case 0: // No initializer case 0: // No initializer
break; break;
case 1: // Single line initializer statement case 1: // Single line initializer statement
out << TrimSuffix(init_buf.lines[0].content, ";"); out << utils::TrimSuffix(init_buf.lines[0].content, ";");
break; break;
default: // Block initializer statement default: // Block initializer statement
for (size_t i = 1; i < init_buf.lines.size(); i++) { for (size_t i = 1; i < init_buf.lines.size(); i++) {
// Indent all by the first line // Indent all by the first line
init_buf.lines[i].indent += current_buffer_->current_indent; init_buf.lines[i].indent += current_buffer_->current_indent;
} }
out << TrimSuffix(init_buf.String(), "\n"); out << utils::TrimSuffix(init_buf.String(), "\n");
break; break;
} }
@ -841,14 +841,14 @@ void GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) {
case 0: // No continuing case 0: // No continuing
break; break;
case 1: // Single line continuing statement case 1: // Single line continuing statement
out << TrimSuffix(cont_buf.lines[0].content, ";"); out << utils::TrimSuffix(cont_buf.lines[0].content, ";");
break; break;
default: // Block continuing statement default: // Block continuing statement
for (size_t i = 1; i < cont_buf.lines.size(); i++) { for (size_t i = 1; i < cont_buf.lines.size(); i++) {
// Indent all by the first line // Indent all by the first line
cont_buf.lines[i].indent += current_buffer_->current_indent; cont_buf.lines[i].indent += current_buffer_->current_indent;
} }
out << TrimSuffix(cont_buf.String(), "\n"); out << utils::TrimSuffix(cont_buf.String(), "\n");
break; break;
} }
} }