15 #ifndef UTIL_COMMON_STRING_OPRS_H
16 #define UTIL_COMMON_STRING_OPRS_H
29 #if defined(_MSC_VER) && _MSC_VER >= 1600
30 # define UTIL_STRFUNC_STRCASE_CMP(l, r) _stricmp(l, r)
31 # define UTIL_STRFUNC_STRNCASE_CMP(l, r, s) _strnicmp(l, r, s)
32 # define UTIL_STRFUNC_STRCMP(l, r) strcmp(l, r)
33 # define UTIL_STRFUNC_STRNCMP(l, r, s) strncmp(l, r, s)
35 # define UTIL_STRFUNC_STRCASE_CMP(l, r) strcasecmp(l, r)
36 # define UTIL_STRFUNC_STRNCASE_CMP(l, r, s) strncasecmp(l, r, s)
37 # define UTIL_STRFUNC_STRCMP(l, r) strcmp(l, r)
38 # define UTIL_STRFUNC_STRNCMP(l, r, s) strncmp(l, r, s)
41 #if (defined(_MSC_VER) && _MSC_VER >= 1600) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
42 defined(__STDC_LIB_EXT1__)
43 # define UTIL_STRFUNC_SSCANF(...) sscanf_s(__VA_ARGS__)
46 # define UTIL_STRFUNC_VSNPRINTF(buffer, bufsz, fmt, arg) \
47 vsnprintf_s(buffer, static_cast<size_t>(bufsz), _TRUNCATE, fmt, arg)
48 # define UTIL_STRFUNC_SNPRINTF(...) sprintf_s(__VA_ARGS__)
50 # define UTIL_STRFUNC_VSNPRINTF(buffer, bufsz, fmt, arg) vsnprintf_s(buffer, static_cast<size_t>(bufsz), fmt, arg)
51 # define UTIL_STRFUNC_SNPRINTF(...) snprintf_s(__VA_ARGS__)
54 # define UTIL_STRFUNC_C11_SUPPORT 1
56 # define UTIL_STRFUNC_SSCANF(...) sscanf(__VA_ARGS__)
57 # define UTIL_STRFUNC_SNPRINTF(...) snprintf(__VA_ARGS__)
58 # define UTIL_STRFUNC_VSNPRINTF(buffer, bufsz, fmt, arg) vsnprintf(buffer, static_cast<int>(bufsz), fmt, arg)
69 template <
typename TCH =
char>
71 if (c >=
'A' && c <=
'Z') {
72 return static_cast<TCH
>(c -
'A' +
'a');
84 template <
typename TCH =
char>
86 if (c >=
'a' && c <=
'z') {
87 return static_cast<TCH
>(c -
'a' +
'A');
101 out =
static_cast<T
>(0);
102 if (
nullptr == str || !(*str)) {
107 bool is_negative =
false;
108 while (*str && *str ==
'-') {
109 is_negative = !is_negative;
117 if (
'0' == str[0] &&
'x' ==
tolower(str[1])) {
118 for (
size_t i = 2; str[i]; ++i) {
120 if (c >=
'0' && c <=
'9') {
123 }
else if (c >=
'a' && c <=
'f') {
130 }
else if (
'\\' == str[0]) {
131 for (
size_t i = 1; str[i] >=
'0' && str[i] <
'8'; ++i) {
136 for (
size_t i = 0; str[i] >=
'0' && str[i] <=
'9'; ++i) {
152 template <
typename T>
165 template <
typename TStr,
typename TCh>
166 void hex(TStr *out, TCh c,
bool upper_case =
false) {
167 out[0] =
static_cast<TStr
>((c >> 4) & 0x0F);
168 out[1] =
static_cast<TStr
>(c & 0x0F);
170 for (
int i = 0; i < 2; ++i) {
172 out[i] += (upper_case ?
'A' :
'a') - 10;
185 template <
typename TStr,
typename TCh>
186 void oct(TStr *out, TCh c) {
187 out[0] =
static_cast<TStr
>(((c >> 6) & 0x07) +
'0');
188 out[1] =
static_cast<TStr
>(((c >> 3) & 0x07) +
'0');
189 out[2] =
static_cast<TStr
>((c & 0x07) +
'0');
199 template <
typename TCh>
201 const TCh *cs =
reinterpret_cast<const TCh *
>(src);
203 for (i = 0, j = 0; i < ss && j < os; ++i) {
204 if (cs[i] >= 32 && cs[i] < 127) {
207 }
else if (j + 4 <= os) {
225 template <
typename Elem,
typename Traits>
226 void serialization(
const void *src,
size_t ss, std::basic_ostream<Elem, Traits> &out) {
227 const Elem *cs =
reinterpret_cast<const Elem *
>(src);
229 for (i = 0; i < ss; ++i) {
230 if (cs[i] >= 32 && cs[i] < 127) {
233 Elem tmp[4] = {
'\\', 0, 0, 0};
247 template <
typename TCh>
248 void dumphex(
const void *src,
size_t ss, TCh *out,
bool upper_case =
false) {
249 const unsigned char *cs =
reinterpret_cast<const unsigned char *
>(src);
251 for (i = 0; i < ss; ++i) {
252 hex<TCh, unsigned char>(&out[i << 1], cs[i], upper_case);
263 template <
typename Elem,
typename Traits>
264 void dumphex(
const void *src,
size_t ss, std::basic_ostream<Elem, Traits> &out,
bool upper_case =
false) {
265 const unsigned char *cs =
reinterpret_cast<const unsigned char *
>(src);
268 for (i = 0; i < ss; ++i) {
269 hex<Elem, unsigned char>(tmp, cs[i], upper_case);
void str2int(T &out, const char *str)
字符串转整数
T to_int(const char *str)
字符串转整数
static TCH tolower(TCH c)
字符转小写
void hex(TStr *out, TCh c, bool upper_case=false)
字符转十六进制表示
void dumphex(const void *src, size_t ss, TCh *out, bool upper_case=false)
字符转16进制表示
void oct(TStr *out, TCh c)
字符转8进制表示
static TCH toupper(TCH c)
字符转大写
void serialization(const void *src, size_t ss, TCh *out, size_t &os)
字符转8进制表示