llvm.org GIT mirror llvm / 5caba3b
Add StringRef::count({char,StringRef}) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79354 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 11 years ago
2 changed file(s) with 29 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
171171 return npos;
172172 }
173173
174 /// count - Return the number of occurrences of \arg C in the string.
175 size_t count(char C) const {
176 size_t Count = 0;
177 for (size_t i = 0, e = Length; i != e; ++i)
178 if (Data[i] == C)
179 return i;
180 return Count;
181 }
182
183 /// count - Return the number of non-overlapped occurrences of \arg Str in
184 /// the string.
185 size_t count(const StringRef &Str) const {
186 size_t Count = 0;
187 size_t N = Str.size();
188 if (N > Length)
189 return 0;
190 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
191 if (substr(i, N).equals(Str))
192 ++Count;
193 return Count;
194 }
195
174196 /// @}
175197 /// @name Substring Operations
176198 /// @{
9393 EXPECT_TRUE(Str.find("hello") == 0);
9494 EXPECT_TRUE(Str.find("ello") == 1);
9595 EXPECT_TRUE(Str.find("zz") == StringRef::npos);
96
97 EXPECT_TRUE(Str.count('l') == 2);
98 EXPECT_TRUE(Str.count('z') == 0);
99 EXPECT_TRUE(Str.count("helloworld") == 0);
100 EXPECT_TRUE(Str.count("hello") == 1);
101 EXPECT_TRUE(Str.count("ello") == 1);
102 EXPECT_TRUE(Str.count("zz") == 0);
96103 }
97104
98105 } // end anonymous namespace