Otclient  14/8/2020
string.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef STDEXT_STRING_H
24 #define STDEXT_STRING_H
25 
26 #include <string>
27 #include <cstring>
28 #include <vector>
29 
30 #include "types.h"
31 #include "cast.h"
32 
33 namespace stdext {
34 
35 template<typename T> std::string to_string(const T& t) { return unsafe_cast<std::string, T>(t); }
36 template<typename T> T from_string(const std::string& str, T def = T()) { return unsafe_cast<T, std::string>(str, def); }
37 
39 std::string resolve_path(const std::string& filePath, std::string sourcePath);
41 std::string date_time_string();
42 
43 std::string dec_to_hex(uint64_t num);
44 uint64_t hex_to_dec(const std::string& str);
45 void tolower(std::string& str);
46 void toupper(std::string& str);
47 void trim(std::string& str);
48 void ucwords(std::string& str);
49 char upchar(char c);
50 char lochar(char c);
51 bool ends_with(const std::string& str, const std::string& test);
52 bool starts_with(const std::string& str, const std::string& test);
53 void replace_all(std::string& str, const std::string& search, const std::string& replacement);
54 
55 bool is_valid_utf8(const std::string& src);
56 std::string utf8_to_latin1(const std::string& src);
57 std::string latin1_to_utf8(const std::string& src);
58 
59 #ifdef WIN32
60 std::wstring utf8_to_utf16(const std::string& src);
61 std::string utf16_to_utf8(const std::wstring& src);
62 std::string utf16_to_latin1(const std::wstring& src);
63 std::wstring latin1_to_utf16(const std::string& src);
64 #endif
65 
66 std::vector<std::string> split(const std::string& str, const std::string& separators = " ");
67 template<typename T> std::vector<T> split(const std::string& str, const std::string& separators = " ") {
68  std::vector<std::string> splitted = split(str, separators);
69  std::vector<T> results(splitted.size());
70  for(uint i=0;i<splitted.size();++i)
71  results[i] = safe_cast<T>(splitted[i]);
72  return results;
73 }
74 
75 }
76 
77 #endif
stdext::latin1_to_utf8
std::string latin1_to_utf8(const std::string &src)
Definition: string.cpp:170
cast.h
types.h
stdext::ucwords
void ucwords(std::string &str)
Definition: string.cpp:245
stdext::starts_with
bool starts_with(const std::string &str, const std::string &test)
Definition: string.cpp:263
stdext::from_string
T from_string(const std::string &str, T def=T())
Definition: string.h:36
stdext::resolve_path
std::string resolve_path(const std::string &filePath, std::string sourcePath)
Resolve a file path by combining sourcePath with filePath.
Definition: string.cpp:35
uint
unsigned int uint
Definition: types.h:31
stdext::is_valid_utf8
bool is_valid_utf8(const std::string &src)
Definition: string.cpp:75
stdext::trim
void trim(std::string &str)
Definition: string.cpp:226
stdext::hex_to_dec
uint64_t hex_to_dec(const std::string &str)
Definition: string.cpp:67
stdext::utf8_to_latin1
std::string utf8_to_latin1(const std::string &src)
Definition: string.cpp:146
stdext::ends_with
bool ends_with(const std::string &str, const std::string &test)
Definition: string.cpp:258
stdext::toupper
void toupper(std::string &str)
Definition: string.cpp:221
stdext::to_string
std::string to_string(const T &t)
Definition: string.h:35
stdext::lochar
char lochar(char c)
Definition: string.cpp:238
stdext::split
std::vector< std::string > split(const std::string &str, const std::string &separators)
Definition: string.cpp:273
stdext::replace_all
void replace_all(std::string &str, const std::string &search, const std::string &replacement)
Definition: string.cpp:268
stdext::date_time_string
std::string date_time_string()
Get current date and time in a std::string.
Definition: string.cpp:48
stdext
Definition: any.h:30
stdext::tolower
void tolower(std::string &str)
Definition: string.cpp:216
stdext::upchar
char upchar(char c)
Definition: string.cpp:231
stdext::dec_to_hex
std::string dec_to_hex(uint64_t num)
Definition: string.cpp:58