From 60a649d4c71dfd02db589f95ed4b04b3b40641b1 Mon Sep 17 00:00:00 2001 From: Daniel De Graaf Date: Fri, 27 Aug 2010 22:12:25 -0400 Subject: Add FormatSubstitute utility class --- src/inspstring.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/inspstring.cpp') diff --git a/src/inspstring.cpp b/src/inspstring.cpp index bcdcbb3c1..3a345bd55 100644 --- a/src/inspstring.cpp +++ b/src/inspstring.cpp @@ -224,3 +224,42 @@ std::string Base64ToBin(const std::string& data_str, const char* table) } return rv; } + +std::string FormatSubstitute::format(const std::string& src) +{ + std::string result; + result.reserve(MAXBUF); + for (unsigned int i = 0; i < src.length(); i++) + { + char c = src[i]; + if (c == '$') + { + std::string word; + i++; + if (src[i] == '{') + { + // processing something like "${foo}bar" + while (src[i] && src[i] != '}') + word.push_back(src[i++]); + } + else + { + while (isalnum(src[i])) + word.push_back(src[i++]); + i--; + } + result.append(lookup(word)); + } + else + result.push_back(c); + } + return result; +} + +std::string MapFormatSubstitute::lookup(const std::string& word) +{ + SubstMap::const_iterator value = map.find(word); + if (value == map.end()) + return ""; + return value->second; +} -- cgit v1.3.1-10-gc9f91