LkEngine 0.1.2
 
Loading...
Searching...
No Matches
StringUtils.h
Go to the documentation of this file.
1
5#pragma once
6
13
14#include <algorithm>
15#include <bitset>
16#include <cctype>
17#include <codecvt>
18#include <locale>
19#include <stdint.h>
20#include <string>
21#include <string_view>
22
31
32 [[nodiscard]] static constexpr std::string_view RemovePrefix(std::string_view Str, char Prefix = 'L')
33 {
34 return (Str.starts_with(Prefix)) ? Str.substr(1) : Str;
35 }
36
46 template<typename TChar = char, typename StringType>
47 [[nodiscard]] static std::basic_string<TChar> ToLower(const StringType& Input)
48 {
49 std::basic_string_view<TChar> StringView(Input);
50 std::basic_string<TChar> Result(StringView);
51
52 std::transform(Result.begin(), Result.end(), Result.begin(), [](TChar Character)
53 {
54 return static_cast<TChar>(std::tolower(Character, std::locale{}));
55 });
56
57 return Result;
58 }
59
60 template<typename TChar = char, typename StringType>
61 [[nodiscard]] static std::basic_string<TChar> ToUpper(const StringType& Input)
62 {
63 std::basic_string_view<TChar> StringView(Input);
64 std::basic_string<TChar> Result(StringView);
65
66 std::transform(Result.begin(), Result.end(), Result.begin(), [](TChar Character)
67 {
68 return static_cast<TChar>(std::toupper(Character, std::locale{}));
69 });
70
71 return Result;
72 }
73
74 [[nodiscard]] static std::string ToLower(const char* String)
75 {
76 return ToLower<char>(std::string_view(String));
77 }
78
79 [[nodiscard]] static std::string ToUpper(const char* String)
80 {
81 return ToUpper<char>(std::string_view(String));
82 }
83
84 [[nodiscard]] static std::wstring ToLower(const wchar_t* String)
85 {
86 return ToLower<wchar_t>(std::wstring_view(String));
87 }
88
89 [[nodiscard]] static std::wstring ToUpper(const wchar_t* String)
90 {
91 return ToUpper<wchar_t>(std::wstring_view(String));
92 }
93
94 [[nodiscard]] static std::wstring ToLower(const std::wstring& String)
95 {
96 return ToLower<wchar_t>(String);
97 }
98
99 [[nodiscard]] static std::wstring ToUpper(const std::wstring& String)
100 {
101 return ToUpper<wchar_t>(String);
102 }
103
104 [[nodiscard]] static std::string BytesToString(const uint64_t Bytes)
105 {
106 static constexpr uint64_t GB = 1024 * 1024 * 1024;
107 static constexpr uint64_t MB = 1024 * 1024;
108 static constexpr uint64_t KB = 1024;
109
110 static constexpr uint16_t BufSize = 32;
111 char Buffer[BufSize + 1] {};
112
113 if (Bytes > GB)
114 {
115 snprintf(Buffer, BufSize, "%.2f GB", (float)Bytes / (float)GB);
116 }
117 else if (Bytes > MB)
118 {
119 snprintf(Buffer, BufSize, "%.2f MB", (float)Bytes / (float)MB);
120 }
121 else if (Bytes > KB)
122 {
123 snprintf(Buffer, BufSize, "%.2f KB", (float)Bytes / (float)KB);
124 }
125 else
126 {
127 snprintf(Buffer, BufSize, "%.2f Bytes", (float)Bytes);
128 }
129
130 return std::string(Buffer);
131 }
132
136 template<typename TargetString, typename SourceString>
137 [[nodiscard]] static TargetString Convert(const SourceString& Input)
138 {
139 /* Wide String to narrow. */
140 if constexpr (std::is_same_v<SourceString, std::wstring>
141 && std::is_same_v<TargetString, std::string>)
142 {
143 return std::wstring_convert<std::codecvt_utf8<wchar_t>>{}.to_bytes(Input);
144 }
145 else if constexpr (std::is_same_v<SourceString, const wchar_t*>
146 && std::is_same_v<TargetString, std::string>)
147 {
148 return std::wstring_convert<std::codecvt_utf8<wchar_t>>{}.to_bytes(Input);
149 }
150 /* Narrow String to wide. */
151 else if constexpr (std::is_same_v<SourceString, std::string>
152 && std::is_same_v<TargetString, std::wstring>)
153 {
154 return std::wstring_convert<std::codecvt_utf8<wchar_t>>{}.from_bytes(Input);
155 }
156 else if constexpr (std::is_same_v<SourceString, std::string>
157 && std::is_same_v<TargetString, const wchar_t*>)
158 {
159 return std::wstring_convert<std::codecvt_utf8<wchar_t>>{}.from_bytes(Input);
160 }
161 else
162 {
163 static_assert(sizeof(SourceString) <= 0, "Unsupported conversion");
164 }
165 }
166
167 static void ReplaceToken(std::string& String, const char* Token, const std::string& Value)
168 {
169 std::size_t Pos = 0;
170 while ((Pos = String.find(Token, Pos)) != std::string::npos)
171 {
172 String.replace(Pos, strlen(Token), Value);
173 Pos += strlen(Token);
174 }
175 }
176
177 template<typename T>
178 static std::string ConvertFlags(const T Flags)
179 {
180 static_assert(std::is_integral_v<T> || std::is_enum_v<T>, "Cannot convert non-integral types, only flags are allowed");
181 constexpr uint32_t Bits = static_cast<uint32_t>(sizeof(decltype(Flags)) * 8);
182 std::bitset<Bits> FlagBitset(Flags);
183 return FlagBitset.to_string();
184 }
185
186}
187
Core macros used by the entire engine.
String utility. Contains various utility functions for strings.
Definition StringUtils.h:30