2 * Copyright 2013 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef FOLLY_STRINGGEN_H_
18 #define FOLLY_STRINGGEN_H_
20 #include "folly/Range.h"
21 #include "folly/experimental/Gen.h"
27 class StringResplitter;
28 class SplitStringSource;
30 template<class Delimiter, class Output>
33 template<class Delimiter, class OutputBuffer>
36 template<class TargetContainer,
44 * Split the output from a generator into StringPiece "lines" delimited by
45 * the given delimiter. Delimters are NOT included in the output.
47 * resplit() behaves as if the input strings were concatenated into one long
48 * string and then split.
50 // make this a template so we don't require StringResplitter to be complete
52 template <class S=detail::StringResplitter>
53 S resplit(char delimiter) {
57 template <class S=detail::SplitStringSource>
58 S split(const StringPiece& source, char delimiter) {
59 return S(source, delimiter);
63 * Joins a sequence of tokens into a string, with the chosen delimiter.
66 * fbstring result = split("a,b,c", ",") | unsplit(",");
67 * assert(result == "a,b,c");
69 * std::string result = split("a,b,c", ",") | unsplit<std::string>(" ");
70 * assert(result == "a b c");
74 // NOTE: The template arguments are reversed to allow the user to cleanly
75 // specify the output type while still inferring the type of the delimiter.
76 template<class Output = folly::fbstring,
78 class Unsplit = detail::Unsplit<Delimiter, Output>>
79 Unsplit unsplit(const Delimiter& delimiter) {
80 return Unsplit(delimiter);
83 template<class Output = folly::fbstring,
84 class Unsplit = detail::Unsplit<fbstring, Output>>
85 Unsplit unsplit(const char* delimiter) {
86 return Unsplit(delimiter);
90 * Joins a sequence of tokens into a string, appending them to the output
91 * buffer. If the output buffer is empty, an initial delimiter will not be
92 * inserted at the start.
96 * split("a,b,c", ",") | unsplit(",", &buffer);
97 * assert(buffer == "a,b,c");
99 * std::string anotherBuffer("initial");
100 * split("a,b,c", ",") | unsplit(",", &anotherbuffer);
101 * assert(anotherBuffer == "initial,a,b,c");
103 template<class Delimiter,
105 class UnsplitBuffer = detail::UnsplitBuffer<Delimiter, OutputBuffer>>
106 UnsplitBuffer unsplit(Delimiter delimiter, OutputBuffer* outputBuffer) {
107 return UnsplitBuffer(delimiter, outputBuffer);
110 template<class OutputBuffer,
111 class UnsplitBuffer = detail::UnsplitBuffer<fbstring, OutputBuffer>>
112 UnsplitBuffer unsplit(const char* delimiter, OutputBuffer* outputBuffer) {
113 return UnsplitBuffer(delimiter, outputBuffer);
117 template<class... Targets>
118 detail::Map<detail::SplitTo<std::tuple<Targets...>, char, Targets...>>
119 eachToTuple(char delim) {
121 detail::SplitTo<std::tuple<Targets...>, char, Targets...>>(
122 detail::SplitTo<std::tuple<Targets...>, char, Targets...>(delim));
125 template<class... Targets>
126 detail::Map<detail::SplitTo<std::tuple<Targets...>, fbstring, Targets...>>
127 eachToTuple(StringPiece delim) {
129 detail::SplitTo<std::tuple<Targets...>, fbstring, Targets...>>(
130 detail::SplitTo<std::tuple<Targets...>, fbstring, Targets...>(delim));
133 template<class First, class Second>
134 detail::Map<detail::SplitTo<std::pair<First, Second>, char, First, Second>>
135 eachToPair(char delim) {
137 detail::SplitTo<std::pair<First, Second>, char, First, Second>>(
138 detail::SplitTo<std::pair<First, Second>, char, First, Second>(delim));
141 template<class First, class Second>
142 detail::Map<detail::SplitTo<std::pair<First, Second>, fbstring, First, Second>>
143 eachToPair(StringPiece delim) {
145 detail::SplitTo<std::pair<First, Second>, fbstring, First, Second>>(
146 detail::SplitTo<std::pair<First, Second>, fbstring, First, Second>(
147 to<fbstring>(delim)));
153 #include "folly/experimental/StringGen-inl.h"
155 #endif /* FOLLY_STRINGGEN_H_ */