---------------------------------------------------------------------- TrimRunsInString Removes leading and trailing runs, and collapses middle runs of a set of characters into a single character (the first one specified in 'remove'). Useful for collapsing runs of repeated delimiters, whitespace, etc. E.g., TrimRunsInString(&s, " :,()") removes leading and trailing delimiter chars and collapses
| 353 | // "first,last::(area)phone, ::zip" -> "first last area phone zip" |
| 354 | // ---------------------------------------------------------------------- |
| 355 | void TrimRunsInString(string* s, StringPiece remove) { |
| 356 | string::iterator dest = s->begin(); |
| 357 | string::iterator src_end = s->end(); |
| 358 | for (string::iterator src = s->begin(); src != src_end; ) { |
| 359 | if (remove.find(*src) == StringPiece::npos) { |
| 360 | *(dest++) = *(src++); |
| 361 | } else { |
| 362 | // Skip to the end of this run of chars that are in 'remove'. |
| 363 | for (++src; src != src_end; ++src) { |
| 364 | if (remove.find(*src) == StringPiece::npos) { |
| 365 | if (dest != s->begin()) { |
| 366 | // This is an internal run; collapse it. |
| 367 | *(dest++) = remove[0]; |
| 368 | } |
| 369 | *(dest++) = *(src++); |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | s->erase(dest, src_end); |
| 376 | } |
| 377 | |
| 378 | // ---------------------------------------------------------------------- |
| 379 | // RemoveNullsInString |