Home | History | Annotate | Download | only in tf2xla
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/compiler/tf2xla/str_util.h"
     17 
     18 #include <string>
     19 #include <utility>
     20 #include <vector>
     21 
     22 namespace tensorflow {
     23 namespace str_util {
     24 
     25 static void ReplaceAll(string* text, StringPiece from, StringPiece to) {
     26   size_t pos = 0;
     27   while ((pos = text->find(from.data(), pos, from.size())) != string::npos) {
     28     text->replace(pos, from.size(), to.data(), to.size());
     29     pos += to.size();
     30     if (from.empty()) {
     31       pos++;  // Match at the beginning of the text and after every byte
     32     }
     33   }
     34 }
     35 
     36 void ReplaceAllPairs(string* text,
     37                      const std::vector<std::pair<string, string>>& replace) {
     38   for (const std::pair<string, string>& from_to : replace) {
     39     ReplaceAll(text, from_to.first, from_to.second);
     40   }
     41 }
     42 
     43 }  // namespace str_util
     44 }  // namespace tensorflow
     45