-
Notifications
You must be signed in to change notification settings - Fork 6
/
Strings.hpp
199 lines (148 loc) · 5.69 KB
/
Strings.hpp
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
/*
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CAT_STRINGS_HPP
#define CAT_STRINGS_HPP
/*
These are ANSI C String function that do not work with UNICODE, UTF-8, etc.
*/
#include <cat/Platform.hpp>
#if defined(CAT_COMPILER_MSVC)
#include <string.h> // _stricmp
#elif defined(CAT_COMPILER_GCC)
#include <strings.h> // strcasecmp
#endif
namespace cat {
// Portable, safe, faster itoa: Converts x to string, returning number of characters produced
// Returns false if output is clipped (13 character buffer is good enough for 32-bit decimal)
bool CAT_EXPORT IntegerToArray(s32 x, char *outs, int outs_buf_size, int radix = 10);
// Returns true if character is alphabetic
CAT_INLINE bool IsAlpha(char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
// Returns true if character is alphabetic or numeric
CAT_INLINE bool IsAlphanumeric(char ch)
{
return IsAlpha(ch) || (ch >= '0' && ch <= '9');
}
// iStrEqual(): Returns true if strings match. Case-insensitive
#if defined(CAT_COMPILER_MSVC)
CAT_INLINE bool iStrEqual(const char *A, const char *B)
{
return _stricmp(A, B) == 0;
}
#elif defined(CAT_COMPILER_GCC)
CAT_INLINE bool iStrEqual(const char *A, const char *B)
{
return strcasecmp(A, B) == 0;
}
#else
# define CAT_UNKNOWN_BUILTIN_ISTRCMP
bool iStrEqual(const char *A, const char *B);
#endif
// Get length of string that has a maximum length (potentially no trailing nul)
u32 CAT_EXPORT GetFixedStrLen(const char *str, u32 max_len);
// Set a fixed string buffer (zero-padded) from a variable-length string,
// both either zero or length-terminated. Returns length of copied string
u32 CAT_EXPORT SetFixedStr(char *dest, u32 dest_len, const char *src, u32 src_max_len);
// Returns true if buffer contains any non-zero bytes
bool CAT_EXPORT IsZeroFixedBuffer(const void *buffer, u32 bytes);
// Replaces all similar-looking glyphs with a common character
char CAT_EXPORT DesimilarizeCharacter(char ch);
// Replaces all similar-looking glyphs with common characters while copying a string
void CAT_EXPORT CopyDesimilarizeString(const char *from, char *to);
// Replaces all similar-looking glyphs with common characters in a fixed string
u32 CAT_EXPORT DesimilarizeFixedString(char *str, u32 max_len);
// Copies the input string to an output string replacing lowercase letters with their uppercase equivalents
void CAT_EXPORT CopyToUppercaseString(const char *from, char *to);
// Copies the input string to an output string replacing uppercase letters with their lowercase equivalents
void CAT_EXPORT CopyToLowercaseString(const char *from, char *to);
// Copies the contents of a line from a text file into a nul-terminated output buffer
int CAT_EXPORT ReadLineFromTextFileBuffer(u8 *data, u32 remaining, char *outs, int len);
//// Nul-Terminated Fixed-Length String
template<int MAX_LEN>
class NulTermFixedStr
{
char _str[MAX_LEN+1];
public:
CAT_INLINE void Clear()
{
_str[0] = '\0';
}
CAT_INLINE void SetFromRangeString(const char *str, int len)
{
if (len > MAX_LEN)
len = MAX_LEN;
memcpy(_str, str, len);
_str[len] = '\0';
}
CAT_INLINE void SetFromNulTerminatedString(const char *str)
{
CAT_STRNCPY(_str, str, sizeof(_str));
}
CAT_INLINE void SetFromInteger(int x, int radix = 10)
{
IntegerToArray(x, _str, sizeof(_str), radix);
}
CAT_INLINE operator char*()
{
return _str;
}
// Case-insensitive check if first 'len' characters of two strings match
bool CaseCompare(const char *str, int len)
{
char a, b, *fixed = _str;
// NOTE: str may not be nul-terminated
// For each character,
while (len--)
{
a = *fixed;
b = *str;
// If a character differs,
if (a != b)
{
// If a is upper case,
if (a >= 'A' && a <= 'Z')
{
// If switching it to lower case doesn't fix it,
if (a + 'a' - 'A' != b)
return false;
}
else // a is lower case
{
// If switching it to upper case doesn't fix it,
if (a + 'A' - 'a' != b)
return false;
}
}
// Next character for each string
++fixed;
++str;
}
return true;
}
};
} // namespace cat
#endif // CAT_STRINGS_HPP