Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorvng <viktor.govako@gmail.com>2012-06-30 05:16:59 +0400
committerAlex Zolotarev <alex@maps.me>2015-09-23 01:40:25 +0300
commite98923322e35ab144587e39e0b6d83f01b01610e (patch)
tree6ce5df4956998c9e217a6ce91a14acbfddc1690e /base
parent117222c2068aa783263722a65b76f8c95d432143 (diff)
Avoid string copy and std::list penalty in strings::Format.
Diffstat (limited to 'base')
-rw-r--r--base/string_format.cpp30
-rw-r--r--base/string_format.hpp50
2 files changed, 31 insertions, 49 deletions
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<string> const & l)
+ string const FormatImpl(string const & s, string arr[], size_t count)
{
size_t offs = 0;
list<size_t> 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<size_t>::const_iterator offsIt;
- list<string>::const_iterator strIt;
-
- for (offsIt = fieldOffs.begin(), strIt = l.begin();
- (offsIt != fieldOffs.end()) && (strIt != l.end());
- ++offsIt, ++strIt)
+ for (list<size_t>::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 <typename T>
- 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<string> const & l);
+ string const FormatImpl(string const & s, string arr[], size_t count);
template <typename T1>
string const Format(string const & s, T1 const & t1)
{
- list<string> l;
- l.push_back(ToString(t1));
-
- return FormatImpl(s, l);
+ string arr[] = { ToString(t1) };
+ return FormatImpl(s, arr, ARRAY_SIZE(arr));
}
template <typename T1, typename T2>
string const Format(string const & s, T1 const & t1, T2 const & t2)
{
- list<string> 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 <typename T1, typename T2, typename T3>
string const Format(string const & s, T1 const & t1, T2 const & t2, T3 const & t3)
{
- list<string> 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 <typename T1, typename T2, typename T3, typename T4>
string const Format(string const & s, T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4)
{
- list<string> 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 <typename T1, typename T2, typename T3, typename T4, typename T5>
string const Format(string const & s, T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4, T5 const & t5)
{
- list<string> 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));
}
}