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:
parent
ce1025fde3
commit
3731ce8f21
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 << " {";
|
||||
|
|
|
@ -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 << " {";
|
||||
|
|
|
@ -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 << " {";
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue