forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.h
129 lines (112 loc) · 4.86 KB
/
split.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef VERIBLE_COMMON_STRINGS_SPLIT_H_
#define VERIBLE_COMMON_STRINGS_SPLIT_H_
#include <functional>
#include <vector>
#include "absl/strings/string_view.h"
namespace verible {
namespace internal {
inline size_t DelimiterSize(absl::string_view str) { return str.length(); }
inline size_t DelimiterSize(char c) { return 1; }
} // namespace internal
// Generator function that returns substrings between delimiters.
// Behaves like a std::function<absl::string_view(DelimiterType)>.
// The delimiter could be different with each call.
// This can serve as a quick lexer or tokenizer for some applications.
// Compared to absl::StrSplit, the space needed to store results one at a time
// is determined by the caller, and could even be constant.
//
// Example usage:
// StringSpliterator gen("some, text, ...");
// do {
// absl::string_view token = gen(',');
// // do something with token
// } while (gen);
//
// See also MakeStringSpliterator().
class StringSpliterator {
public:
explicit StringSpliterator(absl::string_view original)
: remainder_(original) {}
// Copy-able, movable, assignable.
StringSpliterator(const StringSpliterator&) = default;
StringSpliterator(StringSpliterator&&) = default;
StringSpliterator& operator=(const StringSpliterator&) = default;
StringSpliterator& operator=(StringSpliterator&&) = default;
// Returns true if there is at least one result to come.
operator bool() const { // NOLINT(google-explicit-constructor)
return !end_;
}
// Returns the substring up to the next occurrence of the delimiter,
// and advances internal state to point to text after the delimiter.
// If the delimiter is not found, return the remaining string.
// Delimiter type D can be a string_view or char (overloads of
// string_view::find()).
// The heterogenous operator() lets objects of this type work as a
// std::function<absl::string_view(absl::string_view)> and
// std::function<absl::string_view(char)>.
template <class D>
absl::string_view operator()(const D delimiter) {
const size_t pos = remainder_.find(delimiter);
if (pos == absl::string_view::npos) {
// This is the last partition.
// If the remainder_ was already empty, this will continue
// to return empty strings.
const absl::string_view result(remainder_);
remainder_.remove_prefix(remainder_.length()); // empty
end_ = true;
return result;
} else {
// More text follows after the next occurrence of the delimiter.
// If the text ends with the delimiter, then the last string
// returned before the end() will be empty.
const absl::string_view result(remainder_.substr(0, pos));
// Skip over the delimiter.
remainder_.remove_prefix(pos + internal::DelimiterSize(delimiter));
return result;
}
}
// Returns the un-scanned portion of text.
absl::string_view Remainder() const { return remainder_; }
private:
// The remaining substring that has not been consumed.
// With each call to operator(), this shrinks from the front.
absl::string_view remainder_;
// A split that fails to find a delimiter still returns one element,
// the original string, thus end_ should always be initialized to false.
bool end_ = false;
};
// Convenience function that returns a string_view generator using
// StringSpliterator with the same delimiter on every split.
template <class D>
std::function<absl::string_view()> MakeStringSpliterator(
absl::string_view original, D delimiter) {
// note: in-lambda initializers require c++14
auto splitter = StringSpliterator(original);
return [=]() mutable /* splitter */ { return splitter(delimiter); };
}
// Returns line-based view of original text.
// Each line in the returned vector excludes the trailing \n.
// If original text did not terminate with a \n, interpret the final partial
// line as a whole line.
std::vector<absl::string_view> SplitLines(absl::string_view text);
// Returns line-based view of original text. Keeps the trailing \n.
// Lines in the returned vector include the trailing \n.
// If original text did not terminate with a \n, interpret the final partial
// line as a whole line.
std::vector<absl::string_view> SplitLinesKeepLineTerminator(
absl::string_view text);
} // namespace verible
#endif // VERIBLE_COMMON_STRINGS_SPLIT_H_