-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_util.h
214 lines (176 loc) · 5.63 KB
/
string_util.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
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* string_util.h
*
* This file defines general-purpose string functions.
*/
#ifndef THOR_STRING_UTIL_H
#define THOR_STRING_UTIL_H
#pragma once
#ifndef THOR_BASETYPES_H
#include "basetypes.h"
#endif
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <locale>
namespace thor
{
// Returns c-style string length in characters (not bytes)
thor_size_type string_length(const char* s);
thor_size_type string_length(const wchar_t* s);
const char* string_find(const char* s, char find);
const wchar_t* string_find(const wchar_t* s, wchar_t find);
const char* string_find_right(const char* s, char find);
const wchar_t* string_find_right(const wchar_t* s, wchar_t find);
// String formatting
thor_size_type string_format_count(const char* s, ...);
thor_size_type string_format_count(const wchar_t* s, ...);
thor_size_type string_format_count_v(const char* s, va_list va);
thor_size_type string_format_count_v(const wchar_t* s, va_list va);
thor_size_type string_format(char* d, thor_size_type dlen, const char* s, ...);
thor_size_type string_format(wchar_t* d, thor_size_type dlen, const wchar_t* s, ...);
thor_size_type string_format_v(char* d, thor_size_type dlen, const char* s, va_list va);
thor_size_type string_format_v(wchar_t* d, thor_size_type dlen, const wchar_t* s, va_list va);
int string_compare(const char* lhs, const char* rhs);
int string_compare(const wchar_t* lhs, const wchar_t* rhs);
int string_compare_i(const char* lhs, const char* rhs);
int string_compare_i(const wchar_t* lhs, const wchar_t* rhs);
// Memory compare
template<typename T> int memory_compare(const T* lhs, const T* rhs, thor_size_type len);
int memory_compare_i(const char* lhs, const char* rhs, thor_size_type len);
int memory_compare_i(const wchar_t* lhs, const wchar_t* rhs, thor_size_type len);
///////////////////////////////////////////////////////////////////////////////
// Inline implementations
///////////////////////////////////////////////////////////////////////////////
inline thor_size_type string_length(const char* s)
{
return s ? ::strlen(s) : 0;
}
inline thor_size_type string_length(const wchar_t* s)
{
return s ? ::wcslen(s) : 0;
}
///////////////////////////////////////////////////////////////////////////////
inline const char* string_find(const char* s, char val)
{
return s ? ::strchr(s, val) : 0;
}
inline const wchar_t* string_find(const wchar_t* s, wchar_t val)
{
return s ? ::wcschr(s, val) : 0;
}
inline const char* string_find_right(const char* s, char val)
{
return s ? ::strrchr(s, val) : 0;
}
inline const wchar_t* string_find_right(const wchar_t* s, wchar_t val)
{
return s ? ::wcsrchr(s, val) : 0;
}
///////////////////////////////////////////////////////////////////////////////
inline thor_size_type string_format_count(const char* s, ...)
{
va_list va;
va_start(va, s);
thor_size_type count = (thor_size_type)_vscprintf(s, va);
va_end(va);
return count;
}
inline thor_size_type string_format_count(const wchar_t* s, ...)
{
va_list va;
va_start(va, s);
thor_size_type count = (thor_size_type)_vscwprintf(s, va);
va_end(va);
return count;
}
inline thor_size_type string_format_count_v(const char* s, va_list va)
{
return (thor_size_type)_vscprintf(s, va);
}
inline thor_size_type string_format_count_v(const wchar_t* s, va_list va)
{
return (thor_size_type)_vscwprintf(s, va);
}
inline thor_size_type string_format(char* d, thor_size_type dlen, const char* s, ...)
{
va_list va;
va_start(va, s);
thor_size_type count = string_format_v(d, dlen, s, va);
va_end(va);
return count;
}
inline thor_size_type string_format(wchar_t* d, thor_size_type dlen, const wchar_t* s, ...)
{
va_list va;
va_start(va, s);
thor_size_type count = string_format_v(d, dlen, s, va);
va_end(va);
return count;
}
inline thor_size_type string_format_v(char* d, thor_size_type dlen, const char* s, va_list va)
{
return (thor_size_type)vsprintf_s(d, dlen, s, va);
}
inline thor_size_type string_format_v(wchar_t* d, thor_size_type dlen, const wchar_t* s, va_list va)
{
return (thor_size_type)vswprintf_s(d, dlen, s, va);
}
///////////////////////////////////////////////////////////////////////////////
inline int string_compare(const char* lhs, const char* rhs)
{
THOR_DEBUG_ASSERT(lhs && rhs);
return ::strcmp(lhs, rhs);
}
inline int string_compare(const wchar_t* lhs, const wchar_t* rhs)
{
THOR_DEBUG_ASSERT(lhs && rhs);
return ::wcscmp(lhs, rhs);
}
inline int string_compare_i(const char* lhs, const char* rhs)
{
THOR_DEBUG_ASSERT(lhs && rhs);
return ::_stricmp(lhs, rhs);
}
inline int string_compare_i(const wchar_t* lhs, const wchar_t* rhs)
{
THOR_DEBUG_ASSERT(lhs && rhs);
return ::_wcsicmp(lhs, rhs);
}
///////////////////////////////////////////////////////////////////////////////
template<typename T> inline int memory_compare(const T* lhs, const T* rhs, thor_size_type len)
{
return ::memcmp(lhs, rhs, len * sizeof(T));
}
inline int memory_compare_i(const char* lhs, const char* rhs, thor_size_type len)
{
const char* lhs_end = lhs + len;
while (lhs != lhs_end)
{
int lc = towlower(*lhs), rc = towlower(*rhs);
if (lc != rc)
{
return lc - rc;
}
++lhs, ++rhs;
}
return 0;
}
inline int memory_compare_i(const wchar_t* lhs, const wchar_t* rhs, thor_size_type len)
{
const wchar_t* lhs_end = lhs + len;
while (lhs != lhs_end)
{
int lc = towlower(*lhs), rc = towlower(*rhs);
if (lc != rc)
{
return lc - rc;
}
++lhs, ++rhs;
}
return 0;
}
}
#endif