1/*
2 * Copyright (c) 2010-2017 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
33namespace stdext {
34
35template<typename T> std::string to_string(const T& t) { return unsafe_cast<std::string, T>(t); }
36template<typename T> T from_string(const std::string& str, T def = T()) { return unsafe_cast<T, std::string>(str, def); }
37
38/// Resolve a file path by combining sourcePath with filePath
39std::string resolve_path(const std::string& filePath, std::string sourcePath);
40/// Get current date and time in a std::string
41std::string date_time_string();
42std::string timestamp_to_date(time_t tnow);
43
44std::string dec_to_hex(uint32_t num);
45std::string dec_to_hex(uint64_t num);
46uint64_t hex_to_dec(const std::string& str);
47void tolower(std::string& str);
48void toupper(std::string& str);
49void trim(std::string& str);
50void ucwords(std::string& str);
51char upchar(char c);
52char lochar(char c);
53bool ends_with(const std::string& str, const std::string& test);
54bool starts_with(const std::string& str, const std::string& test);
55void replace_all(std::string& str, const std::string& search, const std::string& replacement);
56
57bool is_valid_utf8(const std::string& src);
58std::string utf8_to_latin1(const std::string& src);
59std::string latin1_to_utf8(const std::string& src);
60
61#ifdef WIN32
62std::wstring utf8_to_utf16(const std::string& src);
63std::string utf16_to_utf8(const std::wstring& src);
64std::string utf16_to_latin1(const std::wstring& src);
65std::wstring latin1_to_utf16(const std::string& src);
66#endif
67
68std::vector<std::string> split(const std::string& str, const std::string& separators = " ");
69template<typename T> std::vector<T> split(const std::string& str, const std::string& separators = " ") {
70 std::vector<std::string> splitted = split(str, separators);
71 std::vector<T> results(splitted.size());
72 for(uint i=0;i<splitted.size();++i)
73 results[i] = safe_cast<T>(splitted[i]);
74 return results;
75}
76
77}
78
79#endif
80