libeditwl
Lightweight C++ library for Nintendo DS(i) formats
Loading...
Searching...
No Matches
util_String.hpp
1
2#pragma once
3#include <twl/twl_Include.hpp>
4#include <locale>
5#include <codecvt>
6#include <algorithm>
7#include <cstring>
8
9namespace twl::util {
10
11 inline std::string ConvertFromUnicode(const std::u16string &u_str) {
12 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
13 return convert.to_bytes(u_str);
14 }
15
16 inline std::u16string ConvertToUnicode(const std::string &str) {
17 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
18 return convert.from_bytes(str);
19 }
20
21 inline bool ConvertStringToNumber(const std::string &str, u32 &out_num, const u32 base = 10) {
22 errno = 0;
23 char *end_ptr;
24 const auto res = std::strtoul(str.c_str(), &end_ptr, base);
25 if((errno != 0) || (end_ptr == str.c_str())) {
26 return false;
27 }
28 else {
29 out_num = res;
30 return true;
31 }
32 }
33
34 template<size_t N, typename C>
35 inline std::basic_string<C> GetNonNullTerminatedCString(const C (&c_str)[N]) {
36 const auto actual_len = std::min(N, std::char_traits<C>::length(c_str));
37 return std::basic_string<C>(c_str, actual_len);
38 }
39
40 template<size_t N, typename C>
41 inline void SetNonNullTerminatedCString(C (&out_c_str)[N], const std::basic_string<C> &str) {
42 const auto actual_len = std::min(N, str.length());
43 std::memset(out_c_str, 0, N * sizeof(C));
44 std::memcpy(out_c_str, str.c_str(), actual_len * sizeof(C));
45 }
46
47 inline std::string ToLowerString(const std::string &str) {
48 auto lower_str = str;
49 std::transform(str.begin(), str.end(), lower_str.begin(), ::tolower);
50 return lower_str;
51 }
52
53 std::vector<std::string> SplitString(const std::string &str, const char separator);
54
55}