Gary Morrison | ced8c6f | 2020-02-27 19:35:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "string_ops.hpp" |
| 9 | |
| 10 | using namespace std; |
| 11 | |
| 12 | // Replace first occurrence of find_str within orig of replace_str: |
| 13 | size_t find_replace_1st (const string &find_str, const string &replace_str, |
| 14 | string &orig) { |
| 15 | size_t where = 0; |
| 16 | where = orig.find(find_str, where); |
| 17 | if (where != string::npos) { |
| 18 | orig.replace(where, find_str.length(), replace_str); |
| 19 | } |
| 20 | return where; |
| 21 | } |
| 22 | |
| 23 | // Replace all occurrences of find_str in "this" string, with replace_str: |
| 24 | size_t find_replace_all (const string &find_str, const string &replace_str, |
| 25 | string &orig) { |
| 26 | size_t where = 0; |
| 27 | do { |
| 28 | where = orig.find(find_str, where); |
| 29 | if (where != string::npos) { |
| 30 | orig.replace(where, find_str.length(), replace_str); |
| 31 | } |
| 32 | } while (where != string::npos); |
| 33 | return where; |
| 34 | } |