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