| 404 | } |
| 405 | |
| 406 | bool RE2::Replace(std::string* str, |
| 407 | const RE2& re, |
| 408 | const StringPiece& rewrite) { |
| 409 | StringPiece vec[kVecSize]; |
| 410 | int nvec = 1 + MaxSubmatch(rewrite); |
| 411 | if (nvec > 1 + re.NumberOfCapturingGroups()) |
| 412 | return false; |
| 413 | if (nvec > static_cast<int>(arraysize(vec))) |
| 414 | return false; |
| 415 | if (!re.Match(*str, 0, str->size(), UNANCHORED, vec, nvec)) |
| 416 | return false; |
| 417 | |
| 418 | std::string s; |
| 419 | if (!re.Rewrite(&s, rewrite, vec, nvec)) |
| 420 | return false; |
| 421 | |
| 422 | assert(vec[0].data() >= str->data()); |
| 423 | assert(vec[0].data() + vec[0].size() <= str->data() + str->size()); |
| 424 | str->replace(vec[0].data() - str->data(), vec[0].size(), s); |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | int RE2::GlobalReplace(std::string* str, |
| 429 | const RE2& re, |