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

string_format.cpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2de89527a15b27c93ef7e6143293e3c4e3463eb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "string_format.hpp"

#include "../std/list.hpp"


namespace strings
{
  string const FormatImpl(string const & s, string arr[], size_t count)
  {
    size_t offs = 0;
    list<size_t> fieldOffs;

    string res = s;

    while (true)
    {
      offs = res.find("^", offs);
      if (offs == string::npos)
        break;
      else
      {
        if ((offs != 0) && (res[offs - 1] == '\\'))
        {
          res = res.erase(offs - 1, 1);
          --offs;
        }
        else
          fieldOffs.push_back(offs);

        ++offs;
      }
    }

    offs = 0;
    size_t i = 0;

    for (list<size_t>::const_iterator offsIt = fieldOffs.begin();
        (offsIt != fieldOffs.end()) && (i < count);
        ++offsIt, ++i)
    {
      res.replace(*offsIt + offs, 1, arr[i]);
      offs += (arr[i].size() - 1);
    }

    return res;
  }
}