aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/lib/strings/str_util.h
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2016-09-12 14:30:43 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2016-09-12 15:47:57 -0700
commit8875324afcf1db86b97afa1f1be0b15c3a2856a9 (patch)
treea47a6719faeab878595b77548b4560d3e3ff3414 /tensorflow/core/lib/strings/str_util.h
parenta5ca1d2be9075833ec1a6975de686267e1102421 (diff)
Augment TensorFlow str_util::Join with three-argument variant that allows
per element customization via a Formatter function. Change: 132926708
Diffstat (limited to 'tensorflow/core/lib/strings/str_util.h')
-rw-r--r--tensorflow/core/lib/strings/str_util.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/tensorflow/core/lib/strings/str_util.h b/tensorflow/core/lib/strings/str_util.h
index 8198b7c541..deaf6f2e8e 100644
--- a/tensorflow/core/lib/strings/str_util.h
+++ b/tensorflow/core/lib/strings/str_util.h
@@ -93,6 +93,12 @@ string HumanReadableElapsedTime(double seconds);
template <typename T>
string Join(const T& s, const char* sep);
+// A variant of Join where for each element of "s", f(&dest_string, elem)
+// is invoked (f is often constructed with a lambda of the form:
+// [](string* result, ElemType elem)
+template <typename T, typename Formatter>
+string Join(const T& s, const char* sep, Formatter f);
+
struct AllowEmpty {
bool operator()(StringPiece sp) const { return true; }
};
@@ -129,6 +135,30 @@ string Join(const T& s, const char* sep) {
return result;
}
+template <typename T>
+class Formatter {
+ public:
+ Formatter(std::function<void(string*, T)> f) : f_(f) {}
+ void operator()(string* out, const T& t) { f_(out, t); }
+
+ private:
+ std::function<void(string*, T)> f_;
+};
+
+template <typename T, typename Formatter>
+string Join(const T& s, const char* sep, Formatter f) {
+ string result;
+ bool first = true;
+ for (const auto& x : s) {
+ if (!first) {
+ result.append(sep);
+ }
+ f(&result, x);
+ first = false;
+ }
+ return result;
+}
+
inline std::vector<string> Split(StringPiece text, char delim) {
return Split(text, delim, AllowEmpty());
}