utils: Add ReplaceAll() string helper

Helpful for template string substitutions in tests, to reduce string concat noise.

Change-Id: I612084e2c267d93d79c92cf3ac0231b6b904f0e3
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/66440
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Ben Clayton 2021-10-14 20:24:50 +00:00 committed by Tint LUCI CQ
parent 89fb370b30
commit 262210f12b
5 changed files with 79 additions and 0 deletions

View File

@ -481,6 +481,7 @@ libtint_source_set("libtint_core_all_src") {
"utils/hash.h", "utils/hash.h",
"utils/math.h", "utils/math.h",
"utils/scoped_assignment.h", "utils/scoped_assignment.h",
"utils/string.h",
"utils/unique_vector.h", "utils/unique_vector.h",
"writer/append_vector.cc", "writer/append_vector.cc",
"writer/append_vector.h", "writer/append_vector.h",

View File

@ -391,6 +391,7 @@ set(TINT_LIB_SRCS
utils/hash.h utils/hash.h
utils/math.h utils/math.h
utils/scoped_assignment.h utils/scoped_assignment.h
utils/string.h
utils/unique_vector.h utils/unique_vector.h
writer/append_vector.cc writer/append_vector.cc
writer/append_vector.h writer/append_vector.h
@ -722,6 +723,7 @@ if(${TINT_BUILD_TESTS})
utils/math_test.cc utils/math_test.cc
utils/reverse_test.cc utils/reverse_test.cc
utils/scoped_assignment_test.cc utils/scoped_assignment_test.cc
utils/string_test.cc
utils/unique_vector_test.cc utils/unique_vector_test.cc
writer/append_vector_test.cc writer/append_vector_test.cc
writer/float_to_string_test.cc writer/float_to_string_test.cc

41
src/utils/string.h Normal file
View File

@ -0,0 +1,41 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_UTILS_STRING_H_
#define SRC_UTILS_STRING_H_
#include <string>
namespace tint {
namespace utils {
/// @param str the string to apply replacements to
/// @param substr the string to search for
/// @param replacement the replacement string to use instead of `substr`
/// @returns `str` with all occurrances of `substr` replaced with `replacement`
inline std::string ReplaceAll(std::string str,
const std::string& substr,
const std::string& replacement) {
size_t pos = 0;
while ((pos = str.find(substr, pos)) != std::string::npos) {
str.replace(pos, substr.length(), replacement);
pos += replacement.length();
}
return str;
}
} // namespace utils
} // namespace tint
#endif // SRC_UTILS_STRING_H_

34
src/utils/string_test.cc Normal file
View File

@ -0,0 +1,34 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/utils/string.h"
#include "gtest/gtest.h"
namespace tint {
namespace utils {
namespace {
TEST(StringTest, ReplaceAll) {
ASSERT_EQ("xybbcc", ReplaceAll("aabbcc", "aa", "xy"));
ASSERT_EQ("aaxycc", ReplaceAll("aabbcc", "bb", "xy"));
ASSERT_EQ("aabbxy", ReplaceAll("aabbcc", "cc", "xy"));
ASSERT_EQ("xyxybbcc", ReplaceAll("aabbcc", "a", "xy"));
ASSERT_EQ("aaxyxycc", ReplaceAll("aabbcc", "b", "xy"));
ASSERT_EQ("aabbxyxy", ReplaceAll("aabbcc", "c", "xy"));
}
} // namespace
} // namespace utils
} // namespace tint

View File

@ -323,6 +323,7 @@ tint_unittests_source_set("tint_unittests_core_src") {
"../src/utils/math_test.cc", "../src/utils/math_test.cc",
"../src/utils/reverse_test.cc", "../src/utils/reverse_test.cc",
"../src/utils/scoped_assignment_test.cc", "../src/utils/scoped_assignment_test.cc",
"../src/utils/string_test.cc",
"../src/utils/unique_vector_test.cc", "../src/utils/unique_vector_test.cc",
"../src/writer/append_vector_test.cc", "../src/writer/append_vector_test.cc",
"../src/writer/float_to_string_test.cc", "../src/writer/float_to_string_test.cc",