llvm.org GIT mirror llvm / 0ad7f9b
StringRef: Add find(char) and find(StringRef). Also, regroup functions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78712 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 11 years ago
2 changed file(s) with 54 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
122122 }
123123
124124 /// @}
125 /// @name Utility Functions
125 /// @name String Predicates
126 /// @{
127
128 /// startswith - Check if this string starts with the given \arg Prefix.
129 bool startswith(const StringRef &Prefix) const {
130 return substr(0, Prefix.Length).equals(Prefix);
131 }
132
133 /// endswith - Check if this string ends with the given \arg Suffix.
134 bool endswith(const StringRef &Suffix) const {
135 return slice(size() - Suffix.Length, size()).equals(Suffix);
136 }
137
138 /// @}
139 /// @name String Searching
140 /// @{
141
142 /// find - Search for the character \arg C in the string.
143 ///
144 /// \return - The index of the first occurence of \arg C, or npos if not
145 /// found.
146 size_t find(char C) const {
147 for (size_t i = 0, e = Length; i != e; ++i)
148 if (Data[i] == C)
149 return i;
150 return npos;
151 }
152
153 /// find - Search for the string \arg Str in the string.
154 ///
155 /// \return - The index of the first occurence of \arg Str, or npos if not
156 /// found.
157 size_t find(const StringRef &Str) const {
158 size_t N = Str.size();
159 if (N > Length)
160 return npos;
161 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
162 if (substr(i, N).equals(Str))
163 return i;
164 return npos;
165 }
166
167 /// @}
168 /// @name Substring Operations
126169 /// @{
127170
128171 /// substr - Return a reference to the substring from [Start, Start + N).
166209 /// \param Separator - The character to split on.
167210 /// \return - The split substrings.
168211 std::pair split(char Separator) const {
169 iterator it = std::find(begin(), end(), Separator);
170 if (it == end())
212 size_t Idx = find(Separator);
213 if (Idx == npos)
171214 return std::make_pair(*this, StringRef());
172 return std::make_pair(StringRef(begin(), it - begin()),
173 StringRef(it + 1, end() - (it + 1)));
174 }
175
176 /// startswith - Check if this string starts with the given \arg Prefix.
177 bool startswith(const StringRef &Prefix) const {
178 return substr(0, Prefix.Length).equals(Prefix);
179 }
180
181 /// endswith - Check if this string ends with the given \arg Suffix.
182 bool endswith(const StringRef &Suffix) const {
183 return slice(size() - Suffix.Length, size()).equals(Suffix);
215 return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
184216 }
185217
186218 /// @}
8686 raw_string_ostream OS(Storage);
8787 OS << StringRef("hello");
8888 EXPECT_EQ("hello", OS.str());
89
90 EXPECT_TRUE(Str.find('l') == 2);
91 EXPECT_TRUE(Str.find('z') == StringRef::npos);
92 EXPECT_TRUE(Str.find("helloworld") == StringRef::npos);
93 EXPECT_TRUE(Str.find("hello") == 0);
94 EXPECT_TRUE(Str.find("ello") == 1);
95 EXPECT_TRUE(Str.find("zz") == StringRef::npos);
8996 }
9097
9198 } // end anonymous namespace