Add StripPrefix and StripSuffix to TF.

PiperOrigin-RevId: 278491513
Change-Id: I14d7827c46ae975c6831b2bdab7f90441ab5fcf6
This commit is contained in:
Mihai Maruseac 2019-11-04 16:34:23 -08:00 committed by TensorFlower Gardener
parent d8325fafd4
commit 0552579265
4 changed files with 30 additions and 1 deletions

View File

@ -17,9 +17,9 @@ load(
"tf_additional_proto_hdrs",
"tf_additional_tensor_coding_deps",
"tf_additional_test_srcs",
"tf_fingerprint_deps",
"tf_protobuf_compiler_deps",
"tf_protobuf_deps",
"tf_fingerprint_deps",
)
load(
"//tensorflow/core/platform:default/build_refactor.bzl",
@ -476,6 +476,7 @@ cc_library(
hdrs = ["str_util.h"],
deps = [
":logging",
":macros",
":stringpiece",
":types",
"@com_google_absl//absl/strings",

View File

@ -112,6 +112,14 @@ bool ConsumeSuffix(StringPiece* s, StringPiece expected) {
return absl::ConsumeSuffix(s, expected);
}
StringPiece StripPrefix(StringPiece s, StringPiece expected) {
return absl::StripPrefix(s, expected);
}
StringPiece StripSuffix(StringPiece s, StringPiece expected) {
return absl::StripSuffix(s, expected);
}
// Return lower-cased version of s.
string Lowercase(StringPiece s) { return absl::AsciiStrToLower(s); }

View File

@ -21,6 +21,7 @@ limitations under the License.
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "tensorflow/core/platform/macros.h"
#include "tensorflow/core/platform/stringpiece.h"
#include "tensorflow/core/platform/types.h"
@ -75,6 +76,16 @@ bool ConsumePrefix(StringPiece* s, StringPiece expected);
// Otherwise, return false.
bool ConsumeSuffix(StringPiece* s, StringPiece expected);
// If "s" starts with "expected", return a view into "s" after "expected" but
// keep "s" unchanged.
// Otherwise, return the original "s".
TF_MUST_USE_RESULT StringPiece StripPrefix(StringPiece s, StringPiece expected);
// If "s" ends with "expected", return a view into "s" until "expected" but
// keep "s" unchanged.
// Otherwise, return the original "s".
TF_MUST_USE_RESULT StringPiece StripSuffix(StringPiece s, StringPiece expected);
// Return lower-cased version of s.
string Lowercase(StringPiece s);

View File

@ -222,6 +222,15 @@ TEST(ConsumePrefix, Basic) {
EXPECT_EQ(input, "f");
}
TEST(StripPrefix, Basic) {
EXPECT_EQ(str_util::StripPrefix("abcdef", "abcdefg"), "abcdef");
EXPECT_EQ(str_util::StripPrefix("abcdef", "abce"), "abcdef");
EXPECT_EQ(str_util::StripPrefix("abcdef", ""), "abcdef");
EXPECT_EQ(str_util::StripPrefix("abcdef", "abcdeg"), "abcdef");
EXPECT_EQ(str_util::StripPrefix("abcdef", "abcdef"), "");
EXPECT_EQ(str_util::StripPrefix("abcdef", "abcde"), "f");
}
TEST(JoinStrings, Basic) {
std::vector<string> s;
s = {"hi"};