aboutsummaryrefslogtreecommitdiff
path: root/cpp/src/util/string_util.cc
blob: 18d0a873ef19c7a617b65c55cd9fd2aef67c7831 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// The original source code is from:
// http://src.chromium.org/viewvc/chrome/trunk/src/base/strings/string_util.cc?revision=268754
//
// Modified to contain only the DoReplaceStringPlaceholders() that works with
// std::string. Replaced DCHECK() with assert() and removed offsets.

#include "string_util.h"

#include <libaddressinput/util/basictypes.h>

#include <algorithm>
#include <cassert>
#include <string>
#include <vector>

namespace i18n {
namespace addressinput {

std::string DoReplaceStringPlaceholders(const std::string& format_string,
                                        const std::vector<std::string>& subst) {
  size_t substitutions = subst.size();

  size_t sub_length = 0;
  for (std::vector<std::string>::const_iterator iter = subst.begin();
       iter != subst.end(); ++iter) {
    sub_length += iter->length();
  }

  std::string formatted;
  formatted.reserve(format_string.length() + sub_length);

  for (std::string::const_iterator i = format_string.begin();
       i != format_string.end(); ++i) {
    if ('$' == *i) {
      if (i + 1 != format_string.end()) {
        ++i;
        assert('$' == *i || '1' <= *i);
        if ('$' == *i) {
          while (i != format_string.end() && '$' == *i) {
            formatted.push_back('$');
            ++i;
          }
          --i;
        } else {
          uintptr_t index = 0;
          while (i != format_string.end() && '0' <= *i && *i <= '9') {
            index *= 10;
            index += *i - '0';
            ++i;
          }
          --i;
          index -= 1;
          if (index < substitutions)
            formatted.append(subst.at(index));
        }
      }
    } else {
      formatted.push_back(*i);
    }
  }
  return formatted;
}

}  // addressinput
}  // i18n