-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStringOps.h
246 lines (219 loc) · 8 KB
/
StringOps.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//----------------------------------------------------------------------------------------------------------------------
// String related routines
// Copyright (c) 2024 Samuel Gomes
//----------------------------------------------------------------------------------------------------------------------
#pragma once
#include "Common.h"
#include "Types.h"
#include <cstdint>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <regex>
/// @brief Used for formatting strings.
/// @tparam T The type of the value to format.
/// @param value The value to format.
/// @param fmt The format specifier.
/// @return A formatted string.
template <typename T>
inline const char *__String_Format(const T &value, const char *fmt)
{
g_TmpBuf[0] = '\0';
snprintf(reinterpret_cast<char *>(g_TmpBuf), sizeof(g_TmpBuf), fmt, value);
return reinterpret_cast<char *>(g_TmpBuf);
}
/// @brief Gets a string form of the boolean value passed
/// @param n A boolean value
/// @param fmt The format type needed
/// @return A string form of the boolean value
inline const char *String_FormatBoolean(int32_t n, uint32_t fmt)
{
static const char *const BOOLEAN_STRINGS[][2] = {
{"False", "True"},
{"0", "1"},
{"0", "-1"},
{"Off", "On"},
{"Disabled", "Enabled"},
{"Disable", "Enable"},
{"No", "Yes"},
{"Low", "High"},
{"Closed", "Opened"},
{"Close", "Open"},
{"Inactive", "Active"},
{"Absent", "Present"},
{"Disengaged", "Engaged"},
{"Disengage", "Engage"},
{"Disconnected", "Connected"},
{"Disconnect", "Connect"},
{"Invalid", "Valid"},
{"Down", "Up"},
{"Stopped", "Started"},
{"Stop", "Start"},
{"Unavailable", "Available"},
{"Out", "In"}};
return BOOLEAN_STRINGS[fmt < _countof(BOOLEAN_STRINGS) ? fmt : 0][n != 0];
}
/// @brief Check if the character is an alphanumeric character
/// @param ch The character to check
/// @return True if the character is an alphanumeric character
inline qb_bool String_IsAlphaNumeric(uint32_t ch)
{
return TO_QB_BOOL(std::isalnum(ch));
}
/// @brief Check if the character is an alphabetic character
/// @param ch The character to check
/// @return True if the character is an alphabetic character
inline qb_bool String_IsAlphabetic(uint32_t ch)
{
return TO_QB_BOOL(std::isalpha(ch));
}
/// @brief Check if the character is a lowercase character
/// @param ch The character to check
/// @return True if the character is a lowercase character
inline qb_bool String_IsLowerCase(uint32_t ch)
{
return TO_QB_BOOL(std::islower(ch));
}
/// @brief Check if the character is an uppercase character
/// @param ch The character to check
/// @return True if the character is an uppercase character
inline qb_bool String_IsUpperCase(uint32_t ch)
{
return TO_QB_BOOL(std::isupper(ch));
}
/// @brief Check if the character is a numeric character
/// @param ch The character to check
/// @return True if the character is a numeric character
inline qb_bool String_IsDigit(uint32_t ch)
{
return TO_QB_BOOL(std::isdigit(ch));
}
/// @brief Check if the character is a hexadecimal numeric character
/// @param ch The character to check
/// @return True if the character is a hexadecimal numeric character
inline qb_bool String_IsHexadecimalDigit(uint32_t ch)
{
return TO_QB_BOOL(std::isxdigit(ch));
}
/// @brief Check if the character is a control character
/// @param ch The character to check
/// @return True if the character is a control character
inline qb_bool String_IsControlCharacter(uint32_t ch)
{
return TO_QB_BOOL(std::iscntrl(ch));
}
/// @brief Check if the character has a graphical representation
/// @param ch The character to check
/// @return True if the character has a graphical representation
inline qb_bool String_IsGraphicalCharacter(uint32_t ch)
{
return TO_QB_BOOL(std::isgraph(ch));
}
/// @brief Check if the character is a white-space character
/// @param ch The character to check
/// @return True if the character is white-space character
inline qb_bool String_IsWhiteSpace(uint32_t ch)
{
return TO_QB_BOOL(std::isspace(ch));
}
/// @brief Check if the character is a blank character
/// @param ch The character to check
/// @return True if the character is a blank character
inline qb_bool String_IsBlank(uint32_t ch)
{
return TO_QB_BOOL(std::isblank(ch));
}
/// @brief Check if the character can be printed
/// @param ch The character to check
/// @return True if the character can be printed
inline qb_bool String_IsPrintable(uint32_t ch)
{
return TO_QB_BOOL(std::isprint(ch));
}
/// @brief Check if the character is a punctuation character
/// @param ch The character to check
/// @return True if the character is a punctuation character
inline qb_bool String_IsPunctuation(uint32_t ch)
{
return TO_QB_BOOL(std::ispunct(ch));
}
/// @brief Compiles a RegEx and returns a context
/// @param pattern A RegEx pattern
/// @return A RegEx context
inline uintptr_t __String_RegExCompile(const char *pattern)
{
try
{
auto regexPtr = new std::regex(pattern);
return reinterpret_cast<uintptr_t>(regexPtr);
}
catch (const std::regex_error &e)
{
return 0; // return null context on error
}
}
/// @brief Frees resources for a compiled pattern
/// @param pattern A RegEx context
inline void String_RegExFree(uintptr_t pattern)
{
if (pattern)
{
delete reinterpret_cast<std::regex *>(pattern);
}
}
/// @brief Searches of a occurrence of a compiled pattern in `text`
/// @param pattern A RegEx context
/// @param text The string to search
/// @param index The position in the string to start searching
/// @param matchLength The length of the match [output]
/// @return Starting position of the match (>= 0) or a negative value indicating the error
inline int32_t __String_RegExSearchCompiled(uintptr_t pattern, const char *text, int32_t index, int32_t *matchLength)
{
if (!pattern)
return -2; // invalid context
auto regexPtr = reinterpret_cast<std::regex *>(pattern);
std::cmatch cm;
// Convert index to size_t for regex_search
auto startIndex = static_cast<size_t>(index) - 1; // QB string index starts at 1
if (std::regex_search(text + startIndex, cm, *regexPtr))
{
*matchLength = static_cast<int32_t>(cm.length());
return static_cast<int32_t>(cm.position()); // return the starting position of the match (must be adjusted on the QB64 side!!!)
}
return -1; // no match found
}
/// @brief Searches for an occurrence of a pattern in `text`
/// @param pattern A RegEx pattern
/// @param text The string to search
/// @param index The position in the string to start searching
/// @param matchLength The length of the match [output]
/// @return Starting position of the match (>= 0) or a negative value indicating the error
inline int32_t __String_RegExSearch(const char *pattern, const char *text, int32_t index, int32_t *matchLength)
{
auto compiledPattern = __String_RegExCompile(pattern);
auto result = __String_RegExSearchCompiled(compiledPattern, text, index, matchLength);
String_RegExFree(compiledPattern); // clean up compiled context
return result;
}
/// @brief Checks if `text` is a RegEx match
/// @param pattern A RegEx context
/// @param text The string to match
/// @return True if it is a match. False otherwise
inline qb_bool __String_RegExMatchCompiled(uintptr_t pattern, const char *text)
{
if (!pattern)
return QB_FALSE; // invalid context
auto regexPtr = reinterpret_cast<std::regex *>(pattern);
return TO_QB_BOOL(std::regex_match(text, *regexPtr));
}
/// @brief Checks if `text` is a RegEx match
/// @param pattern A RegEx pattern
/// @param text The string to match
/// @return True if it is a match. False otherwise
inline qb_bool __String_RegExMatch(const char *pattern, const char *text)
{
uintptr_t compiledPattern = __String_RegExCompile(pattern);
auto result = __String_RegExMatchCompiled(compiledPattern, text);
String_RegExFree(compiledPattern); // clean up compiled context
return result;
}