Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
stringutils.h
Go to the documentation of this file.
1 // * This file is part of the COLOBOT source code
2 // * Copyright (C) 2012, Polish Portal of Colobot (PPC)
3 // *
4 // * This program is free software: you can redistribute it and/or modify
5 // * it under the terms of the GNU General Public License as published by
6 // * the Free Software Foundation, either version 3 of the License, or
7 // * (at your option) any later version.
8 // *
9 // * This program is distributed in the hope that it will be useful,
10 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // * GNU General Public License for more details.
13 // *
14 // * You should have received a copy of the GNU General Public License
15 // * along with this program. If not, see http://www.gnu.org/licenses/.
16 
22 #pragma once
23 
24 
25 #include <string>
26 #include <sstream>
27 
28 namespace StrUtils {
29 
31 
33 template<class T>
34 std::string ToString(T value, bool *ok = NULL)
35 {
36  std::ostringstream s;
37  s << value;
38  if (ok != NULL)
39  *ok = !s.fail();
40  return s.str();
41 }
42 
44 
46 template<class T>
47 T FromString(const std::string &str, bool *ok = NULL)
48 {
49  std::istringstream s;
50  s.str(str);
51  T value;
52  s >> value;
53  if (ok != NULL)
54  *ok = !s.fail();
55  return value;
56 }
57 
59 std::string Replace(const std::string &str, const std::string &oldStr, const std::string &newStr);
60 
61 
63 std::string UnicodeCharToUtf8(unsigned int ch);
64 
66 std::string UnicodeStringToUtf8(const std::wstring &str);
67 
69 unsigned int Utf8CharToUnicode(const std::string &ch);
70 
72 std::wstring Utf8StringToUnicode(const std::string &str);
73 
75 int Utf8CharSizeAt(const std::string &str, unsigned int pos);
76 
78 size_t Utf8StringLength(const std::string &str);
79 
80 }; // namespace StrUtil
81