From f7f02482f486a7c430fe030d62f756685cd8d9d0 Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Fri, 27 Apr 2018 09:25:52 -0700 Subject: [PATCH] Added string conversion operator to tensorflow::StringPiece. Marked ToString method as deprecated. This will allow tensorflow::StringPiece to be replaced with absl::string_view (once the deprecated method is removed) as absl::string_view does not contain the ToString method. PiperOrigin-RevId: 194551042 --- tensorflow/core/lib/core/stringpiece.h | 8 ++++++++ tensorflow/core/lib/core/stringpiece_test.cc | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/tensorflow/core/lib/core/stringpiece.h b/tensorflow/core/lib/core/stringpiece.h index 0cf6c24..d7ecc44 100644 --- a/tensorflow/core/lib/core/stringpiece.h +++ b/tensorflow/core/lib/core/stringpiece.h @@ -92,6 +92,7 @@ class StringPiece { StringPiece substr(size_t pos, size_t n = npos) const; // Return a string that contains the copy of the referenced data. + // DEPRECATED: use std::string(sv) instead. std::string ToString() const { return std::string(data_, size_); } // Three-way comparison. Returns value: @@ -100,6 +101,13 @@ class StringPiece { // > 0 iff "*this" > "b" int compare(StringPiece b) const; + // Converts to `std::basic_string`. + template + explicit operator std::basic_string, A>() const { + if (!data()) return {}; + return std::basic_string, A>(data(), size()); + } + private: const char* data_; size_t size_; diff --git a/tensorflow/core/lib/core/stringpiece_test.cc b/tensorflow/core/lib/core/stringpiece_test.cc index de35d6e..952b9ea 100644 --- a/tensorflow/core/lib/core/stringpiece_test.cc +++ b/tensorflow/core/lib/core/stringpiece_test.cc @@ -55,4 +55,9 @@ TEST(StringPiece, Ctor) { } } +TEST(StringPiece, ConversionToString) { + EXPECT_EQ("", std::string(StringPiece(""))); + EXPECT_EQ("foo", std::string(StringPiece("foo"))); +} + } // namespace tensorflow -- 2.7.4