-
-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathStringUtils.cpp
More file actions
159 lines (128 loc) · 4.86 KB
/
Copy pathStringUtils.cpp
File metadata and controls
159 lines (128 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*---------------------------------------------------------*\
| StringUtils.cpp |
| |
| String utility functions |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
/*---------------------------------------------------------*\
| codecvt is deprecated, but there's no replacement so we |
| can ignore the warnings |
\*---------------------------------------------------------*/
#if defined(_MSC_VER)
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
#elif defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__clang__)
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <cctype>
#include <codecvt>
#include <locale>
#include <regex>
#include <string>
#include "StringUtils.h"
const char* StringUtils::wchar_to_char(const wchar_t* pwchar)
{
if(pwchar == nullptr)
{
return "";
}
/*-----------------------------------------------------*\
| Get the number of characters in the string. |
\*-----------------------------------------------------*/
int currentCharIndex = 0;
char currentChar = (char)pwchar[currentCharIndex];
while(currentChar != '\0')
{
currentCharIndex++;
currentChar = (char)pwchar[currentCharIndex];
}
const int charCount = currentCharIndex + 1;
/*-----------------------------------------------------*\
| Allocate a new block of memory size char (1 byte) |
| instead of wide char (2 bytes) |
\*-----------------------------------------------------*/
char* filePathC = (char*)malloc(sizeof(char) * charCount);
for(int i = 0; i < charCount; i++)
{
/*-------------------------------------------------*\
| Convert to char (1 byte) |
\*-------------------------------------------------*/
char character = (char)pwchar[i];
*filePathC = character;
filePathC += sizeof(char);
}
filePathC += '\0';
filePathC -= (sizeof(char) * charCount);
return(filePathC);
}
std::string StringUtils::wchar_to_string(const wchar_t* pwchar)
{
if(pwchar == nullptr)
{
return std::string();
}
return wstring_to_string(std::wstring(pwchar));
}
std::string StringUtils::wstring_to_string(const std::wstring wstring)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
return(converter.to_bytes(wstring));
}
std::string StringUtils::u16string_to_string(const std::u16string wstring)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> converter;
return(converter.to_bytes(wstring));
}
const std::string StringUtils::remove_null_terminating_chars(std::string input)
{
while(!input.empty() && input.back() == 0)
{
input.pop_back();
}
return(input);
}
std::string StringUtils::u32int_to_hexString(unsigned int value)
{
char hex_str[20] = {0};
snprintf(hex_str, sizeof(hex_str), "%X", value);
return std::string(hex_str);
}
std::string StringUtils::make_filename(std::string input)
{
/*-----------------------------------------------------*\
| Replace : characters with - characters |
\*-----------------------------------------------------*/
input = std::regex_replace(input, std::regex(":"), "-");
/*-----------------------------------------------------*\
| Remove all other characters |
\*-----------------------------------------------------*/
input = std::regex_replace(input, std::regex("[#%&\\{\\}\\\\<>\\*\\?/!`';@+|=]"), "");
/*-----------------------------------------------------*\
| Remove leading . characters |
\*-----------------------------------------------------*/
input = std::regex_replace(input, std::regex("^\\.+"), "");
/*-----------------------------------------------------*\
| Remove control characters |
\*-----------------------------------------------------*/
input = std::regex_replace(input, std::regex("[\\x00-\\x1F\\x7F]"), "");
/*-----------------------------------------------------*\
| Return complete string |
\*-----------------------------------------------------*/
return(input);
}
std::string StringUtils::normalize_hex_id(const std::string& id)
{
std::string out;
out.reserve(id.size());
for(char c : id)
{
if(c != '-')
{
out += (char)tolower((unsigned char)c);
}
}
return out;
}