Index: CMakeLists.txt =================================================================== --- CMakeLists.txt +++ CMakeLists.txt @@ -514,6 +514,7 @@ add_subdirectory(utils/PerfectShuffle) add_subdirectory(utils/count) add_subdirectory(utils/not) + add_subdirectory(utils/strreplace) add_subdirectory(utils/llvm-lit) add_subdirectory(utils/yaml-bench) else() Index: test/CMakeLists.txt =================================================================== --- test/CMakeLists.txt +++ test/CMakeLists.txt @@ -50,6 +50,7 @@ FileCheck count not + strreplace yaml2obj obj2yaml verify-uselistorder Index: test/lit.cfg =================================================================== --- test/lit.cfg +++ test/lit.cfg @@ -219,6 +219,7 @@ r"\bobj2yaml\b", r"\byaml2obj\b", r"\bverify-uselistorder\b", + r"\bstrreplace\b", # Handle these specially as they are strings searched # for during testing. r"\| \bcount\b", Index: utils/Makefile =================================================================== --- utils/Makefile +++ utils/Makefile @@ -8,7 +8,7 @@ ##===----------------------------------------------------------------------===## LEVEL = .. -PARALLEL_DIRS := FileCheck TableGen PerfectShuffle count fpcmp llvm-lit not \ +PARALLEL_DIRS := FileCheck TableGen PerfectShuffle count fpcmp llvm-lit not strreplace \ unittest EXTRA_DIST := check-each-file codegen-diff countloc.sh \ Index: utils/strreplace/CMakeLists.txt =================================================================== --- /dev/null +++ utils/strreplace/CMakeLists.txt @@ -0,0 +1,5 @@ +add_llvm_utility(strreplace + strreplace.cpp + ) + +target_link_libraries(strreplace) Index: utils/strreplace/Makefile =================================================================== --- /dev/null +++ utils/strreplace/Makefile @@ -0,0 +1,21 @@ +##===- utils/strreplace/Makefile ----------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TOOLNAME = strreplace +USEDLIBS = LLVMSupport.a + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +# FIXME: Don't install this utility +#NO_INSTALL = 1 + +include $(LEVEL)/Makefile.common + Index: utils/strreplace/strreplace.cpp =================================================================== --- /dev/null +++ utils/strreplace/strreplace.cpp @@ -0,0 +1,43 @@ +//===- strreplace.cpp: string replacement tool -===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include +#include + +using namespace std; + +std::string replace_all(string s, const string &from, const string &to) { + const size_t fromLen = from.length(); + const size_t toLen = to.length(); + size_t i = 0; + while (true) { + i = s.find(from, i); + if (i == string::npos) + break; + s.erase(i, fromLen); + s.insert(i, to); + i += toLen; + } + return s; +} + +int main(int argc, const char **argv) { + if (argc < 3) { + cout << "Usage: " << argv[0] << " \n" + << "Reads stdin, replaces all occurrences of with and " + "prints the result into stdout.\n"; + return 1; + } + const string from = argv[1]; + const string to = argv[2]; + string line; + while (getline(cin, line)) + cout << replace_all(line, from, to) << '\n'; + return 0; +}