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

string_utils.cpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 39d281a50d297a4b6a4e6881c25ec221c097ec6a (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "base/string_utils.hpp"
#include "base/assert.hpp"

#include "std/target_os.hpp"
#include "std/iterator.hpp"
#include "std/cmath.hpp"
#include "std/iomanip.hpp"

#include <boost/algorithm/string.hpp> // boost::trim


namespace strings
{

bool UniString::IsEqualAscii(char const * s) const
{
  return (size() == strlen(s) && equal(begin(), end(), s));
}

SimpleDelimiter::SimpleDelimiter(char const * delimChars)
{
  string const s(delimChars);
  string::const_iterator it = s.begin();
  while (it != s.end())
    m_delims.push_back(utf8::unchecked::next(it));
}

bool SimpleDelimiter::operator()(UniChar c) const
{
  for (UniString::const_iterator it = m_delims.begin(); it != m_delims.end(); ++it)
    if (*it == c)
      return true;
  return false;
}

UniChar LastUniChar(string const & s)
{
  if (s.empty())
    return 0;
  utf8::unchecked::iterator<string::const_iterator> iter(s.end());
  --iter;
  return *iter;
}

bool to_int(char const * s, int & i, int base /*= 10*/)
{
  char * stop;
  long const x = strtol(s, &stop, base);
  if (stop && *stop == 0)
  {
    i = static_cast<int>(x);
    ASSERT_EQUAL(static_cast<long>(i), x, ());
    return true;
  }
  return false;
}

bool to_uint64(char const * s, uint64_t & i)
{
  char * stop;
#ifdef OMIM_OS_WINDOWS_NATIVE
  i = _strtoui64(s, &stop, 10);
#else
  i = strtoull(s, &stop, 10);
#endif
  return stop && *stop == 0;
}

bool to_int64(char const * s, int64_t & i)
{
  char * stop;
#ifdef OMIM_OS_WINDOWS_NATIVE
  i = _strtoi64(s, &stop, 10);
#else
  i = strtoll(s, &stop, 10);
#endif
  return stop && *stop == 0;
}

bool to_double(char const * s, double & d)
{
  char * stop;
  d = strtod(s, &stop);
  return stop && *stop == 0;
}

string MakeLowerCase(string const & s)
{
  UniString uniStr;
  utf8::unchecked::utf8to32(s.begin(), s.end(), back_inserter(uniStr));

  uniStr = MakeLowerCase(uniStr);

  string r;
  utf8::unchecked::utf32to8(uniStr.begin(), uniStr.end(), back_inserter(r));
  return r;
}

namespace
{
  char ascii_to_lower(char in)
  {
    char const diff = 'z' - 'Z';
    static_assert(diff == 'a' - 'A', "");
    static_assert(diff > 0, "");

    if (in >= 'A' && in <= 'Z')
      return (in + diff);
    return in;
  }
}

void AsciiToLower(string & s)
{
  transform(s.begin(), s.end(), s.begin(), &ascii_to_lower);
}

void Trim(string & s)
{
  boost::trim(s);
}

bool EqualNoCase(string const & s1, string const & s2)
{
  return MakeLowerCase(s1) == MakeLowerCase(s2);
}

UniString MakeUniString(string const & utf8s)
{
  UniString result;
  utf8::unchecked::utf8to32(utf8s.begin(), utf8s.end(), back_inserter(result));
  return result;
}

string ToUtf8(UniString const & s)
{
  string result;
  utf8::unchecked::utf32to8(s.begin(), s.end(), back_inserter(result));
  return result;
}

bool IsASCIIString(string const & str)
{
  for (size_t i = 0; i < str.size(); ++i)
    if (str[i] & 0x80)
      return false;
  return true;
}

bool StartsWith(string const & s1, char const * s2)
{
  return (s1.compare(0, strlen(s2), s2) == 0);
}

string to_string_dac(double d, int dac)
{
  dac = min(numeric_limits<double>::digits10, dac);

  ostringstream ss;

  if (d < 1. && d > -1.)
  {
    string res;
    if (d >= 0.)
    {
      ss << setprecision(dac + 1) << d + 1;
      res = ss.str();
      res[0] = '0';
    }
    else
    {
      ss << setprecision(dac + 1) << d - 1;
      res = ss.str();
      res[1] = '0';
    }
    return res;
  }

  // Calc digits before comma (log10).
  double fD = fabs(d);
  while (fD >= 1.0 && dac < numeric_limits<double>::digits10)
  {
    fD /= 10.0;
    ++dac;
  }

  ss << setprecision(dac) << d;
  return ss.str();
}

bool IsHTML(string const & utf8)
{
  string::const_iterator it = utf8.begin();
  size_t ltCount = 0;
  size_t gtCount = 0;
  while (it != utf8.end())
  {
    UniChar const c = utf8::unchecked::next(it);
    if (c == '<')
      ++ltCount;
    else if (c == '>')
      ++gtCount;
  }
  return (ltCount > 0 && gtCount > 0);
}

bool AlmostEqual(string const & str1, string const & str2, size_t mismatchedCount)
{
  pair<string::const_iterator, string::const_iterator> mis(str1.begin(), str2.begin());
  auto const str1End = str1.end();
  auto const str2End = str2.end();

  for (size_t i = 0; i <= mismatchedCount; ++i)
  {
    mis = mismatch(mis.first, str1End, mis.second);
    if (mis.first == str1End && mis.second == str2End)
      return true;
    if (mis.first != str1End)
      ++mis.first;
    if (mis.second != str2End)
      ++mis.second;
  }
  return false;
}

} // namespace strings