summaryrefslogtreecommitdiff
path: root/linker/linker_utils.cpp
diff options
context:
space:
mode:
authorDimitry Ivanov <dimitry@google.com>2017-03-11 14:35:38 -0800
committerDimitry Ivanov <dimitry@google.com>2017-03-13 16:25:51 -0700
commit2a6d9b25437c42fd3e0284a6e7a607c842f59fe0 (patch)
treeb1154afd67ab484cd468e2c3dcb9d7960878d383 /linker/linker_utils.cpp
parentb3473f20db82fdc16b1c2dc8b51249277de399ca (diff)
Extract format_string function.
Extract format_string function and add a test. Test: run linker-unit-tests Change-Id: I794a29aaf62e184438ce1a9224b88aa0586c17b5
Diffstat (limited to 'linker/linker_utils.cpp')
-rw-r--r--linker/linker_utils.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/linker/linker_utils.cpp b/linker/linker_utils.cpp
index 6df5f6d96..5bf88e764 100644
--- a/linker/linker_utils.cpp
+++ b/linker/linker_utils.cpp
@@ -36,6 +36,30 @@
#include <sys/stat.h>
#include <unistd.h>
+void format_string(std::string* str, const std::vector<std::pair<std::string, std::string>>& params) {
+ size_t pos = 0;
+ while (pos < str->size()) {
+ pos = str->find("$", pos);
+ if (pos == std::string::npos) break;
+ for (const auto& param : params) {
+ const std::string& token = param.first;
+ const std::string& replacement = param.second;
+ if (str->substr(pos + 1, token.size()) == token) {
+ str->replace(pos, token.size() + 1, replacement);
+ // -1 to compensate for the ++pos below.
+ pos += replacement.size() - 1;
+ break;
+ } else if (str->substr(pos + 1, token.size() + 2) == "{" + token + "}") {
+ str->replace(pos, token.size() + 3, replacement);
+ pos += replacement.size() - 1;
+ break;
+ }
+ }
+ // Skip $ in case it did not match any of the known substitutions.
+ ++pos;
+ }
+}
+
std::string dirname(const char* path) {
const char* last_slash = strrchr(path, '/');