Colobot
stringutils.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 #include <cstddef>
28 #include <string>
29 #include <sstream>
30 
31 namespace StrUtils
32 {
33 
35 
37 template<class T>
38 std::string ToString(T value, bool *ok = nullptr)
39 {
40  std::ostringstream s;
41  s << value;
42  if (ok != nullptr)
43  *ok = !s.fail();
44  return s.str();
45 }
46 
48 
50 template<class T>
51 T FromString(const std::string &str, bool *ok = nullptr)
52 {
53  std::istringstream s;
54  s.str(str);
55  T value;
56  s >> value;
57  if (ok != nullptr)
58  *ok = !s.fail();
59  return value;
60 }
61 
63 unsigned int HexStringToInt(const std::string& str);
64 
66 std::string Format(const char *fmt, ...);
67 
69 std::string Replace(const std::string &str, const std::string &oldStr, const std::string &newStr);
70 
71 
73 std::string UnicodeCharToUtf8(unsigned int ch);
74 
76 std::string UnicodeStringToUtf8(const std::wstring &str);
77 
79 unsigned int Utf8CharToUnicode(const std::string &ch);
80 
82 std::wstring Utf8StringToUnicode(const std::string &str);
83 
85 int Utf8CharSizeAt(const std::string &str, unsigned int pos);
86 
88 std::size_t Utf8StringLength(const std::string &str);
89 
90 } // namespace StrUtil
91 
Definition: stringutils.h:31
T FromString(const std::string &str, bool *ok=nullptr)
Converts a value to string.
Definition: stringutils.h:51
std::wstring Utf8StringToUnicode(const std::string &str)
Converts a UTF-8 encoded string to wide Unicode string.
Definition: stringutils.cpp:154
std::string UnicodeCharToUtf8(unsigned int ch)
Converts a wide Unicode char to a single UTF-8 encoded char.
Definition: stringutils.cpp:85
std::string Format(const char *fmt,...)
Replacement for sprintf()
Definition: stringutils.cpp:64
std::string UnicodeStringToUtf8(const std::wstring &str)
Converts a wide Unicode string to a UTF-8 encoded string.
Definition: stringutils.cpp:111
std::string ToString(T value, bool *ok=nullptr)
Converts a value to string.
Definition: stringutils.h:38
unsigned int HexStringToInt(const std::string &str)
Converts string of hex characters to int.
Definition: stringutils.cpp:28
std::string Replace(const std::string &str, const std::string &oldStr, const std::string &newStr)
Returns a string with every occurence of oldStr in str replaced to newStr.
Definition: stringutils.cpp:73
unsigned int Utf8CharToUnicode(const std::string &ch)
Converts a UTF-8 encoded single character to wide Unicode char.
Definition: stringutils.cpp:120
int Utf8CharSizeAt(const std::string &str, unsigned int pos)
Returns the size in bytes of UTF-8 character at given pos in a UTF-8 str.
Definition: stringutils.cpp:171
std::size_t Utf8StringLength(const std::string &str)
Returns the length in characters of UTF-8 string str.
Definition: stringutils.cpp:186