summaryrefslogtreecommitdiffstats
path: root/src/common/stringutils.cpp
blob: 9edc82d6f9b1dcffc3ba08c4c00cfbd91f3cd67a (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
/*
 * This file is part of the Colobot: Gold Edition source code
 * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
 * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://gnu.org/licenses
 */


#include "common/stringutils.h"

#include <cstdarg>
#include <cstdio>
#include <vector>


static std::string VFormat(const char *fmt, va_list ap)
{
    size_t size = 1024;
    char stackbuf[1024];
    std::vector<char> dynamicbuf;
    char *buf = &stackbuf[0];

    while (1)
    {
        int needed = vsnprintf (buf, size, fmt, ap);

        if (needed <= static_cast<int>(size) && needed >= 0)
        {
            return std::string(buf, static_cast<size_t>(needed));
        }

        size = (needed > 0) ? (needed+1) : (size*2);
        dynamicbuf.resize(size);
        buf = &dynamicbuf[0];
    }
}

std::string StrUtils::Format(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    std::string buf = VFormat(fmt, ap);
    va_end(ap);
    return buf;
}

std::string StrUtils::Replace(const std::string &str, const std::string &oldStr, const std::string &newStr)
{
  std::string result = str;
  size_t pos = 0;
  while ((pos = str.find(oldStr, pos)) != std::string::npos)
  {
    result.replace(pos, oldStr.length(), newStr);
    pos += newStr.length();
  }
  return result;
}

std::string StrUtils::UnicodeCharToUtf8(unsigned int ch)
{
  std::string result;
  if (ch < 0x0080)
  {
    result += static_cast<char>(ch);
  }
  else if (ch < 0x0800)
  {
    char ch1 = 0xC0 | ((ch & 0x07C0) >> 6);
    char ch2 = 0x80 | (ch & 0x3F);
    result += ch1;
    result += ch2;
  }
  else
  {
    char ch1 = 0xE0 | ((ch & 0xF000) >> 12);
    char ch2 = 0x80 | ((ch & 0x07C0) >> 6);
    char ch3 = 0x80 | (ch & 0x3F);
    result += ch1;
    result += ch2;
    result += ch3;
  }
  return result;
}

std::string StrUtils::UnicodeStringToUtf8(const std::wstring &str)
{
  std::string result;
  for (unsigned int i = 0; i < str.size(); ++i)
    result += StrUtils::UnicodeCharToUtf8(static_cast<unsigned int>(str[i]));

  return result;
}

unsigned int StrUtils::Utf8CharToUnicode(const std::string &ch)
{
  if (ch.empty())
    return 0;

  unsigned int result = 0;
  if ((ch[0] & 0x80) == 0)
  {
    if (ch.size() == 1)
      result = static_cast<unsigned int>(ch[0]);
  }
  else if ((ch[0] & 0xC0) == 0xC0)
  {
    if (ch.size() == 2)
    {
      unsigned int ch1 = (ch[0] & 0x1F) << 6;
      unsigned int ch2 = (ch[1] & 0x3F);
      result = ch1 | ch2;
    }
  }
  else
  {
    if (ch.size() == 3)
    {
      unsigned int ch1 = (ch[0] & 0xF0) << 12;
      unsigned int ch2 = (ch[1] & 0xC0) << 6;
      unsigned int ch3 = (ch[2] & 0xC0);
      result = ch1 | ch2 | ch3;
    }
  }

  return result;
}

std::wstring StrUtils::Utf8StringToUnicode(const std::string &str)
{
  std::wstring result;
  unsigned int pos = 0;
  while (pos < str.size())
  {
    int len = StrUtils::Utf8CharSizeAt(str, pos);
    if (len == 0)
      break;

    std::string ch = str.substr(pos, len);
    result += static_cast<wchar_t>(StrUtils::Utf8CharToUnicode(ch));
    pos += len;
  }
  return result;
}

int StrUtils::Utf8CharSizeAt(const std::string &str, unsigned int pos)
{
  if (pos >= str.size())
    return 0;

  if ((str[pos] & 0x80) == 0)
    return 1;
  else if ((str[pos] & 0xC0) == 0xC0)
    return 2;
  else
    return 3;

  return 0;
}

size_t StrUtils::Utf8StringLength(const std::string &str)
{
  size_t result = 0;
  unsigned int i = 0;
  while (i < str.size())
  {
      i += Utf8CharSizeAt(str, i);
      ++result;
  }
  return result;
}