---------------------------------------------------------------------- CountSubstring() Return the number times a "substring" appears in the "text" NOTE: this function's complexity is O(|text| * |substring|) It is meant for short "text" (such as to ensure the printf format string has the right number of arguments). DO NOT pass in long "text". -------------------------------------------------------
| 454 | // DO NOT pass in long "text". |
| 455 | // ---------------------------------------------------------------------- |
| 456 | int CountSubstring(StringPiece text, StringPiece substring) { |
| 457 | CHECK_GT(substring.length(), 0); |
| 458 | |
| 459 | int count = 0; |
| 460 | StringPiece::size_type curr = 0; |
| 461 | while (StringPiece::npos != (curr = text.find(substring, curr))) { |
| 462 | ++count; |
| 463 | ++curr; |
| 464 | } |
| 465 | return count; |
| 466 | } |
| 467 | |
| 468 | // ---------------------------------------------------------------------- |
| 469 | // strstr_delimited() |