2018-08-15 23:01:54 +08:00
|
|
|
#include "str_util.h"
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2019-12-07 18:01:55 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2018-10-05 02:47:53 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-02-10 19:53:03 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
# include <tchar.h>
|
|
|
|
#endif
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
size_t
|
|
|
|
xstrncpy(char *dest, const char *src, size_t n) {
|
2017-12-12 22:12:07 +08:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < n - 1 && src[i] != '\0'; ++i)
|
|
|
|
dest[i] = src[i];
|
|
|
|
if (n)
|
|
|
|
dest[i] = '\0';
|
|
|
|
return src[i] == '\0' ? i : n;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
size_t
|
|
|
|
xstrjoin(char *dst, const char *const tokens[], char sep, size_t n) {
|
2017-12-12 22:12:07 +08:00
|
|
|
const char *const *remaining = tokens;
|
|
|
|
const char *token = *remaining++;
|
|
|
|
size_t i = 0;
|
|
|
|
while (token) {
|
|
|
|
if (i) {
|
|
|
|
dst[i++] = sep;
|
|
|
|
if (i == n)
|
|
|
|
goto truncated;
|
|
|
|
}
|
|
|
|
size_t w = xstrncpy(dst + i, token, n - i);
|
|
|
|
if (w >= n - i)
|
|
|
|
goto truncated;
|
|
|
|
i += w;
|
|
|
|
token = *remaining++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
|
|
|
|
truncated:
|
|
|
|
dst[n - 1] = '\0';
|
|
|
|
return n;
|
|
|
|
}
|
2018-10-05 02:47:53 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
char *
|
|
|
|
strquote(const char *src) {
|
2018-10-05 02:47:53 +08:00
|
|
|
size_t len = strlen(src);
|
2021-01-24 22:14:53 +08:00
|
|
|
char *quoted = malloc(len + 3);
|
2018-10-05 02:47:53 +08:00
|
|
|
if (!quoted) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy("ed[1], src, len);
|
|
|
|
quoted[0] = '"';
|
|
|
|
quoted[len + 1] = '"';
|
|
|
|
quoted[len + 2] = '\0';
|
|
|
|
return quoted;
|
|
|
|
}
|
2019-02-10 19:53:03 +08:00
|
|
|
|
2019-12-07 18:01:55 +08:00
|
|
|
bool
|
|
|
|
parse_integer(const char *s, long *out) {
|
|
|
|
char *endptr;
|
|
|
|
if (*s == '\0') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
long value = strtol(s, &endptr, 0);
|
|
|
|
if (errno == ERANGE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (*endptr != '\0') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-09 21:32:59 +08:00
|
|
|
size_t
|
|
|
|
parse_integers(const char *s, const char sep, size_t max_items, long *out) {
|
|
|
|
size_t count = 0;
|
|
|
|
char *endptr;
|
|
|
|
do {
|
|
|
|
errno = 0;
|
|
|
|
long value = strtol(s, &endptr, 0);
|
|
|
|
if (errno == ERANGE) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endptr == s || (*endptr != sep && *endptr != '\0')) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
out[count++] = value;
|
|
|
|
if (*endptr == sep) {
|
|
|
|
if (count >= max_items) {
|
|
|
|
// max items already reached, could not accept a new item
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// parse the next token during the next iteration
|
|
|
|
s = endptr + 1;
|
|
|
|
}
|
|
|
|
} while (*endptr != '\0');
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-12-07 18:01:55 +08:00
|
|
|
bool
|
|
|
|
parse_integer_with_suffix(const char *s, long *out) {
|
|
|
|
char *endptr;
|
|
|
|
if (*s == '\0') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
long value = strtol(s, &endptr, 0);
|
|
|
|
if (errno == ERANGE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int mul = 1;
|
|
|
|
if (*endptr != '\0') {
|
|
|
|
if (s == endptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((*endptr == 'M' || *endptr == 'm') && endptr[1] == '\0') {
|
|
|
|
mul = 1000000;
|
|
|
|
} else if ((*endptr == 'K' || *endptr == 'k') && endptr[1] == '\0') {
|
|
|
|
mul = 1000;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((value < 0 && LONG_MIN / mul > value) ||
|
|
|
|
(value > 0 && LONG_MAX / mul < value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = value * mul;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:01:08 +08:00
|
|
|
size_t
|
|
|
|
utf8_truncation_index(const char *utf8, size_t max_len) {
|
|
|
|
size_t len = strlen(utf8);
|
|
|
|
if (len <= max_len) {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
len = max_len;
|
|
|
|
// see UTF-8 encoding <https://en.wikipedia.org/wiki/UTF-8#Description>
|
|
|
|
while ((utf8[len] & 0x80) != 0 && (utf8[len] & 0xc0) != 0xc0) {
|
|
|
|
// the next byte is not the start of a new UTF-8 codepoint
|
|
|
|
// so if we would cut there, the character would be truncated
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:53:03 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
wchar_t *
|
|
|
|
utf8_to_wide_char(const char *utf8) {
|
2019-02-10 19:53:03 +08:00
|
|
|
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
|
|
|
|
if (!len) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-01-24 22:14:53 +08:00
|
|
|
wchar_t *wide = malloc(len * sizeof(wchar_t));
|
2019-02-10 19:53:03 +08:00
|
|
|
if (!wide) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wide, len);
|
|
|
|
return wide;
|
|
|
|
}
|
|
|
|
|
2019-06-10 21:44:45 +08:00
|
|
|
char *
|
|
|
|
utf8_from_wide_char(const wchar_t *ws) {
|
|
|
|
int len = WideCharToMultiByte(CP_UTF8, 0, ws, -1, NULL, 0, NULL, NULL);
|
|
|
|
if (!len) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-01-24 22:14:53 +08:00
|
|
|
char *utf8 = malloc(len);
|
2019-06-10 21:44:45 +08:00
|
|
|
if (!utf8) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, ws, -1, utf8, len, NULL, NULL);
|
|
|
|
return utf8;
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:53:03 +08:00
|
|
|
#endif
|