MCPcopy Create free account
hub / github.com/apache/impala / ReverseFindNth

Function ReverseFindNth

be/src/gutil/strings/util.cc:1045–1065  ·  view source on GitHub ↗

------------------------------------------------------------------------ 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) ------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

1043// (returns string::npos if n <= 0)
1044//------------------------------------------------------------------------
1045int 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
1067namespace strings {
1068

Callers

nothing calls this directly

Calls 2

sizeMethod · 0.45
find_last_ofMethod · 0.45

Tested by

no test coverage detected