diff --git a/base/string_format.cpp b/base/string_format.cpp index 122aeadc92..fb8b46f7f2 100644 --- a/base/string_format.cpp +++ b/base/string_format.cpp @@ -1,25 +1,27 @@ #include "string_format.hpp" -#include "logging.hpp" + +#include "../std/list.hpp" + namespace strings { - string const FormatImpl(string const & s, list const & l) + string const FormatImpl(string const & s, string arr[], size_t count) { size_t offs = 0; list fieldOffs; - string temp = s; + string res = s; while (true) { - offs = temp.find("%", offs); + offs = res.find("%", offs); if (offs == string::npos) break; else { - if ((offs != 0) && (temp[offs - 1] == '\\')) + if ((offs != 0) && (res[offs - 1] == '\\')) { - temp = temp.erase(offs - 1, 1); + res = res.erase(offs - 1, 1); --offs; } else @@ -30,18 +32,14 @@ namespace strings } offs = 0; + size_t i = 0; - string res = temp; - - list::const_iterator offsIt; - list::const_iterator strIt; - - for (offsIt = fieldOffs.begin(), strIt = l.begin(); - (offsIt != fieldOffs.end()) && (strIt != l.end()); - ++offsIt, ++strIt) + for (list::const_iterator offsIt = fieldOffs.begin(); + (offsIt != fieldOffs.end()) && (i < count); + ++offsIt, ++i) { - res.replace(*offsIt + offs, 1, *strIt); - offs += strIt->size() - 1; + res.replace(*offsIt + offs, 1, arr[i]); + offs += (arr[i].size() - 1); } return res; diff --git a/base/string_format.hpp b/base/string_format.hpp index d97a8faeb9..5bb29ca5de 100644 --- a/base/string_format.hpp +++ b/base/string_format.hpp @@ -1,73 +1,57 @@ #pragma once +#include "base.hpp" +#include "macros.hpp" #include "../std/string.hpp" #include "../std/sstream.hpp" -#include "../std/list.hpp" + namespace strings { template - string ToString(T t) + string ToString(T const & t) { + // May be we should use DebugPrint here. Not sure. + ostringstream out; out << t; return out.str(); } - string const FormatImpl(string const & s, list const & l); + string const FormatImpl(string const & s, string arr[], size_t count); template string const Format(string const & s, T1 const & t1) { - list l; - l.push_back(ToString(t1)); - - return FormatImpl(s, l); + string arr[] = { ToString(t1) }; + return FormatImpl(s, arr, ARRAY_SIZE(arr)); } template string const Format(string const & s, T1 const & t1, T2 const & t2) { - list l; - l.push_back(ToString(t1)); - l.push_back(ToString(t2)); - - return FormatImpl(s, l); + string arr[] = { ToString(t1), ToString(t2) }; + return FormatImpl(s, arr, ARRAY_SIZE(arr)); } template string const Format(string const & s, T1 const & t1, T2 const & t2, T3 const & t3) { - list l; - l.push_back(ToString(t1)); - l.push_back(ToString(t2)); - l.push_back(ToString(t3)); - - return FormatImpl(s, l); + string arr[] = { ToString(t1), ToString(t2), ToString(t3) }; + return FormatImpl(s, arr, ARRAY_SIZE(arr)); } template string const Format(string const & s, T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4) { - list l; - l.push_back(ToString(t1)); - l.push_back(ToString(t2)); - l.push_back(ToString(t3)); - l.push_back(ToString(t4)); - - return FormatImpl(s, l); + string arr[] = { ToString(t1), ToString(t2), ToString(t3), ToString(t4) }; + return FormatImpl(s, arr, ARRAY_SIZE(arr)); } template string const Format(string const & s, T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4, T5 const & t5) { - list l; - l.push_back(ToString(t1)); - l.push_back(ToString(t2)); - l.push_back(ToString(t3)); - l.push_back(ToString(t4)); - l.push_back(ToString(t5)); - - return FormatImpl(s, l); + string arr[] = { ToString(t1), ToString(t2), ToString(t3), ToString(t4), ToString(t5) }; + return FormatImpl(s, arr, ARRAY_SIZE(arr)); } }