Added string conversion operator to tensorflow::StringPiece.
authorA. Unique TensorFlower <gardener@tensorflow.org>
Fri, 27 Apr 2018 16:25:52 +0000 (09:25 -0700)
committerTensorFlower Gardener <gardener@tensorflow.org>
Fri, 27 Apr 2018 16:28:22 +0000 (09:28 -0700)
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
tensorflow/core/lib/core/stringpiece_test.cc

index 0cf6c24..d7ecc44 100644 (file)
@@ -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 <typename A>
+  explicit operator std::basic_string<char, std::char_traits<char>, A>() const {
+    if (!data()) return {};
+    return std::basic_string<char, std::char_traits<char>, A>(data(), size());
+  }
+
  private:
   const char* data_;
   size_t size_;
index de35d6e..952b9ea 100644 (file)
@@ -55,4 +55,9 @@ TEST(StringPiece, Ctor) {
   }
 }
 
+TEST(StringPiece, ConversionToString) {
+  EXPECT_EQ("", std::string(StringPiece("")));
+  EXPECT_EQ("foo", std::string(StringPiece("foo")));
+}
+
 }  // namespace tensorflow