------------------------------------------------------------------------ ReverseFindNth() return index of nth-to-last occurrence of c in the string, or string::npos if n > number of occurrences of c. (returns string::npos if n <= 0) ------------------------------------------------------------------------
| 1043 | // (returns string::npos if n <= 0) |
| 1044 | //------------------------------------------------------------------------ |
| 1045 | int ReverseFindNth(StringPiece s, char c, int n) { |
| 1046 | if ( n <= 0 ) { |
| 1047 | return static_cast<int>(StringPiece::npos); |
| 1048 | } |
| 1049 | |
| 1050 | size_t pos = s.size(); |
| 1051 | |
| 1052 | for ( int i = 0; i < n; ++i ) { |
| 1053 | // If pos == 0, we return StringPiece::npos right away. Otherwise, |
| 1054 | // the following find_last_of call would take (pos - 1) as string::npos, |
| 1055 | // which means it would again search the entire input string. |
| 1056 | if (pos == 0) { |
| 1057 | return static_cast<int>(StringPiece::npos); |
| 1058 | } |
| 1059 | pos = s.find_last_of(c, pos - 1); |
| 1060 | if ( pos == string::npos ) { |
| 1061 | break; |
| 1062 | } |
| 1063 | } |
| 1064 | return pos; |
| 1065 | } |
| 1066 | |
| 1067 | namespace strings { |
| 1068 |
nothing calls this directly
no test coverage detected