2017-12-29 06:13:28 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <platform.h>
|
2018-12-02 20:32:04 +01:00
|
|
|
#undef HAVE__ITOA
|
2017-12-29 06:13:28 +01:00
|
|
|
#endif
|
2016-08-29 15:49:31 +02:00
|
|
|
#include "strings.h"
|
2016-08-29 10:20:08 +02:00
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
/* libc includes */
|
2018-10-20 19:56:38 +02:00
|
|
|
#include <assert.h>
|
2018-05-21 13:27:02 +02:00
|
|
|
#include <ctype.h>
|
2017-12-11 19:23:56 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
2015-05-18 08:59:38 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2017-12-31 21:33:31 +01:00
|
|
|
#ifdef HAVE_LIBBSD
|
|
|
|
#include <bsd/string.h>
|
|
|
|
#else
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2018-12-02 20:32:04 +01:00
|
|
|
const char* str_itoa_r(int val, char *buf)
|
2018-11-23 14:03:02 +01:00
|
|
|
{
|
2018-12-02 20:32:04 +01:00
|
|
|
#ifdef HAVE__ITOA
|
|
|
|
return _itoa(val, buf, 10);
|
2018-11-23 14:03:02 +01:00
|
|
|
#else
|
2018-12-02 20:32:04 +01:00
|
|
|
snprintf(buf, 12, "%d", val);
|
|
|
|
return buf;
|
2018-11-23 14:03:02 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *str_itoa(int n)
|
|
|
|
{
|
2018-12-02 20:32:04 +01:00
|
|
|
static char buf[12];
|
2018-11-23 14:03:02 +01:00
|
|
|
#ifdef HAVE__ITOA
|
|
|
|
return _itoa(n, buf, 10);
|
|
|
|
#else
|
2018-12-02 20:32:04 +01:00
|
|
|
return str_itoa_r(n, buf);
|
2018-11-23 14:03:02 +01:00
|
|
|
#endif
|
2018-11-23 13:05:38 +01:00
|
|
|
}
|
|
|
|
|
2017-12-30 13:04:00 +01:00
|
|
|
size_t str_strlcpy(char *dst, const char *src, size_t len)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_BSDSTRING
|
|
|
|
return strlcpy(dst, src, len);
|
|
|
|
#else
|
|
|
|
register char *d = dst;
|
|
|
|
register const char *s = src;
|
|
|
|
register size_t n = len;
|
|
|
|
|
2019-01-24 14:24:18 +01:00
|
|
|
assert(src);
|
|
|
|
assert(dst);
|
2017-12-30 13:04:00 +01:00
|
|
|
/* Copy as many bytes as will fit */
|
|
|
|
if (n != 0 && --n != 0) {
|
|
|
|
do {
|
|
|
|
if ((*d++ = *s++) == 0)
|
|
|
|
break;
|
|
|
|
} while (--n != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not enough room in dst, add NUL and traverse rest of src */
|
|
|
|
if (n == 0) {
|
2018-11-27 21:30:08 +01:00
|
|
|
if (len != 0) {
|
|
|
|
*d = '\0'; /* NUL-terminate dst */
|
|
|
|
}
|
|
|
|
return (s - src) + strlen(s); /* count does not include NUL */
|
2017-12-30 13:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-01-24 14:24:18 +01:00
|
|
|
return (s - src - 1); /* count does not include NUL */
|
2017-12-30 13:04:00 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t str_strlcat(char *dst, const char *src, size_t len)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_BSDSTRING
|
|
|
|
return strlcat(dst, src, len);
|
|
|
|
#else
|
|
|
|
register char *d = dst;
|
|
|
|
register const char *s = src;
|
|
|
|
register size_t n = len;
|
|
|
|
size_t dlen;
|
|
|
|
|
|
|
|
/* Find the end of dst and adjust bytes left but don't go past end */
|
|
|
|
while (*d != '\0' && n-- != 0)
|
|
|
|
d++;
|
2019-01-24 14:24:18 +01:00
|
|
|
dlen = d - dst;
|
2017-12-30 13:04:00 +01:00
|
|
|
n = len - dlen;
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return (dlen + strlen(s));
|
|
|
|
while (*s != '\0') {
|
|
|
|
if (n != 1) {
|
|
|
|
*d++ = *s;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*d = '\0';
|
|
|
|
|
|
|
|
return (dlen + (s - src)); /* count does not include NUL */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-12-11 19:23:56 +01:00
|
|
|
size_t str_slprintf(char * dst, size_t size, const char * format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
result = vsnprintf(dst, size, format, args);
|
|
|
|
va_end(args);
|
|
|
|
if (result < 0 || result >= (int)size) {
|
|
|
|
dst[size - 1] = '\0';
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (size_t)result;
|
|
|
|
}
|
|
|
|
|
2018-05-19 13:34:00 +02:00
|
|
|
void str_replace(char *buffer, size_t size, const char *tmpl, const char *var,
|
|
|
|
const char *value)
|
2017-12-11 18:20:21 +01:00
|
|
|
{
|
|
|
|
size_t val_len = strlen(value);
|
|
|
|
size_t var_len = strlen(var);
|
|
|
|
char *s = buffer;
|
|
|
|
while (buffer + size > s) {
|
|
|
|
char *p = strstr(tmpl, var);
|
|
|
|
size_t len;
|
|
|
|
if (p) {
|
2019-01-24 14:24:18 +01:00
|
|
|
len = p - tmpl;
|
2017-12-11 18:20:21 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = strlen(tmpl);
|
|
|
|
}
|
|
|
|
if (len < size) {
|
|
|
|
memmove(s, tmpl, len);
|
|
|
|
tmpl += len;
|
|
|
|
s += len;
|
|
|
|
size -= len;
|
|
|
|
if (p && val_len < size) {
|
|
|
|
tmpl += var_len;
|
|
|
|
memmove(s, value, val_len);
|
|
|
|
s += val_len;
|
|
|
|
size -= val_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!p) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-18 02:55:33 +02:00
|
|
|
int str_hash(const char *s)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2018-05-18 02:55:33 +02:00
|
|
|
int key = 0;
|
2019-01-24 14:24:18 +01:00
|
|
|
assert(s);
|
2014-08-24 22:54:40 +02:00
|
|
|
while (*s) {
|
|
|
|
key = key * 37 + *s++;
|
|
|
|
}
|
|
|
|
return key & 0x7FFFFFFF;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 22:51:57 +01:00
|
|
|
unsigned int jenkins_hash(unsigned int a)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-08-24 22:54:40 +02:00
|
|
|
a = (a + 0x7ed55d16) + (a << 12);
|
|
|
|
a = (a ^ 0xc761c23c) ^ (a >> 19);
|
|
|
|
a = (a + 0x165667b1) + (a << 5);
|
|
|
|
a = (a + 0xd3a2646c) ^ (a << 9);
|
|
|
|
a = (a + 0xfd7046c5) + (a << 3);
|
|
|
|
a = (a ^ 0xb55a4f09) ^ (a >> 16);
|
|
|
|
return a;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 22:51:57 +01:00
|
|
|
unsigned int wang_hash(unsigned int a)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-08-24 22:54:40 +02:00
|
|
|
a = ~a + (a << 15); /* a = (a << 15) - a - 1; */
|
|
|
|
a = a ^ (a >> 12);
|
|
|
|
a = a + (a << 2);
|
|
|
|
a = a ^ (a >> 4);
|
|
|
|
a = a * 2057; /* a = (a + (a << 3)) + (a << 11); */
|
|
|
|
a = a ^ (a >> 16);
|
|
|
|
return a;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2017-12-28 18:29:40 +01:00
|
|
|
|
|
|
|
char *str_strdup(const char *s) {
|
2019-01-24 14:24:18 +01:00
|
|
|
if (s == NULL) return NULL;
|
2017-12-31 21:33:31 +01:00
|
|
|
#ifdef HAVE_STRDUP
|
2019-01-24 14:24:18 +01:00
|
|
|
return strdup(s);
|
2017-12-31 21:33:31 +01:00
|
|
|
#elif defined(_MSC_VER)
|
2019-01-24 14:24:18 +01:00
|
|
|
return _strdup(s);
|
2017-12-28 18:29:40 +01:00
|
|
|
#else
|
2019-01-24 14:24:18 +01:00
|
|
|
size_t len = strlen(s);
|
|
|
|
char *dup = malloc(len+1);
|
|
|
|
memcpy(dup, s, len+1);
|
|
|
|
return dup;
|
2017-12-28 18:29:40 +01:00
|
|
|
#endif
|
|
|
|
}
|
2017-12-31 18:07:29 +01:00
|
|
|
|
2018-12-01 19:54:07 +01:00
|
|
|
void sbs_printf(struct sbstring *sbs, const char *format, ...)
|
|
|
|
{
|
|
|
|
size_t size = sbs->size - (sbs->end - sbs->begin);
|
|
|
|
|
|
|
|
if (size > 0) {
|
|
|
|
va_list argp;
|
|
|
|
va_start(argp, format);
|
2019-01-24 14:24:18 +01:00
|
|
|
int bytes = vsnprintf(sbs->end, size, format, argp);
|
2018-12-01 19:54:07 +01:00
|
|
|
if (bytes > 0) {
|
2019-01-24 14:24:18 +01:00
|
|
|
if ((size_t)bytes >= size) {
|
|
|
|
bytes = size - 1;
|
2018-12-01 19:54:07 +01:00
|
|
|
/* terminate truncated output */
|
2019-01-24 14:24:18 +01:00
|
|
|
sbs->end[bytes] = '\0';
|
2018-12-01 19:54:07 +01:00
|
|
|
}
|
2019-01-24 14:24:18 +01:00
|
|
|
sbs->end += bytes;
|
2018-12-01 19:54:07 +01:00
|
|
|
}
|
|
|
|
va_end(argp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 18:07:29 +01:00
|
|
|
void sbs_init(struct sbstring *sbs, char *buffer, size_t size)
|
|
|
|
{
|
2019-01-24 14:24:18 +01:00
|
|
|
assert(sbs);
|
2018-11-28 17:56:49 +01:00
|
|
|
assert(size > 0);
|
2017-12-31 18:07:29 +01:00
|
|
|
sbs->begin = buffer;
|
|
|
|
sbs->size = size;
|
|
|
|
sbs->end = buffer;
|
|
|
|
buffer[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:56:49 +01:00
|
|
|
void sbs_adopt(struct sbstring *sbs, char *buffer, size_t size)
|
|
|
|
{
|
|
|
|
size_t len = strlen(buffer);
|
2019-01-24 14:24:18 +01:00
|
|
|
assert(sbs);
|
2018-11-28 17:56:49 +01:00
|
|
|
assert(size > len);
|
|
|
|
sbs->begin = buffer;
|
|
|
|
sbs->size = size;
|
|
|
|
sbs->end = buffer + len;
|
|
|
|
}
|
|
|
|
|
2017-12-31 20:07:40 +01:00
|
|
|
void sbs_strncat(struct sbstring *sbs, const char *str, size_t size)
|
|
|
|
{
|
|
|
|
size_t len;
|
2019-01-24 14:24:18 +01:00
|
|
|
assert(sbs);
|
2017-12-31 20:07:40 +01:00
|
|
|
len = sbs->size - (sbs->end - sbs->begin) - 1;
|
|
|
|
if (len < size) {
|
|
|
|
size = len;
|
|
|
|
}
|
|
|
|
memcpy(sbs->end, str, size);
|
|
|
|
sbs->end[size] = '\0';
|
|
|
|
sbs->end += size;
|
|
|
|
}
|
|
|
|
|
2017-12-31 18:07:29 +01:00
|
|
|
void sbs_strcat(struct sbstring *sbs, const char *str)
|
|
|
|
{
|
|
|
|
size_t len;
|
2019-01-24 14:24:18 +01:00
|
|
|
assert(sbs);
|
2017-12-31 18:07:29 +01:00
|
|
|
len = sbs->size - (sbs->end - sbs->begin);
|
2019-01-24 14:24:18 +01:00
|
|
|
str_strlcpy(sbs->end, str, len);
|
2018-11-27 21:30:08 +01:00
|
|
|
sbs->end += strlen(sbs->end);
|
2018-11-27 20:01:47 +01:00
|
|
|
assert(sbs->begin + sbs->size >= sbs->end);
|
2017-12-31 18:07:29 +01:00
|
|
|
}
|
|
|
|
|
2018-11-27 20:50:58 +01:00
|
|
|
void sbs_substr(sbstring *sbs, ptrdiff_t pos, size_t len)
|
2018-11-26 22:01:18 +01:00
|
|
|
{
|
2019-01-24 14:24:18 +01:00
|
|
|
if (pos > sbs->end - sbs->begin) {
|
2018-11-27 20:50:58 +01:00
|
|
|
/* starting past end of string, do nothing */
|
|
|
|
sbs->end = sbs->begin;
|
2018-11-26 22:01:18 +01:00
|
|
|
}
|
2018-11-27 20:50:58 +01:00
|
|
|
if (pos >= 0) {
|
2019-01-24 14:24:18 +01:00
|
|
|
size_t sz = sbs->end - (sbs->begin + pos);
|
2018-11-27 20:50:58 +01:00
|
|
|
if (len > sz) len = sz;
|
|
|
|
if (len - pos > 0) {
|
|
|
|
memmove(sbs->begin, sbs->begin + pos, len);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
memcpy(sbs->begin, sbs->begin + pos, len);
|
|
|
|
}
|
2018-11-27 20:01:47 +01:00
|
|
|
sbs->end = sbs->begin + len;
|
2018-11-27 20:50:58 +01:00
|
|
|
sbs->end[0] = '\0';
|
2018-11-26 22:01:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 20:31:36 +02:00
|
|
|
size_t sbs_length(const struct sbstring *sbs)
|
|
|
|
{
|
2018-11-27 20:01:47 +01:00
|
|
|
assert(sbs->begin + sbs->size >= sbs->end);
|
2019-01-24 14:24:18 +01:00
|
|
|
return sbs->end - sbs->begin;
|
2018-05-27 20:31:36 +02:00
|
|
|
}
|
|
|
|
|
2018-05-17 16:53:56 +02:00
|
|
|
char *str_unescape(char *str) {
|
|
|
|
char *read = str, *write = str;
|
|
|
|
while (*read) {
|
|
|
|
char * pos = strchr(read, '\\');
|
2019-01-24 14:24:18 +01:00
|
|
|
if (pos >= read) {
|
2019-01-22 20:37:23 +01:00
|
|
|
size_t len = (size_t)(pos - read);
|
2018-05-17 16:53:56 +02:00
|
|
|
memmove(write, read, len);
|
|
|
|
write += len;
|
|
|
|
read += (len + 1);
|
|
|
|
switch (read[0]) {
|
|
|
|
case 'r':
|
|
|
|
*write++ = '\r';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
*write++ = '\n';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
*write++ = '\t';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*write++ = read[0];
|
|
|
|
}
|
|
|
|
*write = 0;
|
|
|
|
++read;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size_t len = strlen(read);
|
|
|
|
memmove(write, read, len);
|
|
|
|
write[len] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
2018-05-21 13:27:02 +02:00
|
|
|
|
|
|
|
const char *str_escape_ex(const char *str, char *buffer, size_t size, const char *chars)
|
|
|
|
{
|
2018-05-27 15:15:31 +02:00
|
|
|
size_t slen = strlen(str);
|
2018-05-21 13:27:02 +02:00
|
|
|
const char *read = str;
|
2019-01-22 20:37:23 +01:00
|
|
|
unsigned char *write = (unsigned char *)buffer;
|
2018-05-27 15:15:31 +02:00
|
|
|
if (size < 1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while (slen > 0 && size > 1 && *read) {
|
2018-05-21 13:27:02 +02:00
|
|
|
const char *pos = strpbrk(read, chars);
|
|
|
|
size_t len = size;
|
2019-01-24 14:24:18 +01:00
|
|
|
if (pos >= read) {
|
|
|
|
len = pos - read;
|
2018-05-21 13:27:02 +02:00
|
|
|
}
|
|
|
|
if (len < size) {
|
|
|
|
unsigned char ch = *(const unsigned char *)pos;
|
|
|
|
if (len > 0) {
|
2018-05-27 15:15:31 +02:00
|
|
|
if (len > slen) {
|
|
|
|
len = slen;
|
|
|
|
}
|
2018-05-21 13:27:02 +02:00
|
|
|
memmove(write, read, len);
|
2018-05-27 15:15:31 +02:00
|
|
|
slen -= len;
|
2018-05-21 13:27:02 +02:00
|
|
|
write += len;
|
|
|
|
read += len;
|
|
|
|
size -= len;
|
|
|
|
}
|
|
|
|
switch (ch) {
|
|
|
|
case '\t':
|
|
|
|
if (size > 2) {
|
|
|
|
*write++ = '\\';
|
|
|
|
*write++ = 't';
|
|
|
|
size -= 2;
|
|
|
|
}
|
|
|
|
else size = 1;
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
if (size > 2) {
|
|
|
|
*write++ = '\\';
|
|
|
|
*write++ = 'n';
|
|
|
|
size -= 2;
|
|
|
|
}
|
|
|
|
else size = 1;
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
if (size > 2) {
|
|
|
|
*write++ = '\\';
|
|
|
|
*write++ = 'r';
|
|
|
|
size -= 2;
|
|
|
|
}
|
|
|
|
else size = 1;
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
case '\'':
|
|
|
|
case '\\':
|
|
|
|
if (size > 2) {
|
|
|
|
*write++ = '\\';
|
|
|
|
*write++ = ch;
|
|
|
|
size -= 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (size > 5) {
|
2019-01-22 20:37:23 +01:00
|
|
|
int n = snprintf((char *)write, size, "\\%03o", ch);
|
2018-05-21 13:27:02 +02:00
|
|
|
if (n > 0) {
|
|
|
|
assert(n == 5);
|
|
|
|
write += n;
|
|
|
|
size -= n;
|
|
|
|
}
|
|
|
|
else size = 1;
|
|
|
|
}
|
|
|
|
else size = 1;
|
|
|
|
}
|
|
|
|
++read;
|
2018-05-27 15:15:31 +02:00
|
|
|
--slen;
|
2018-05-21 13:27:02 +02:00
|
|
|
} else {
|
|
|
|
/* end of buffer space */
|
|
|
|
len = size - 1;
|
|
|
|
if (len > 0) {
|
2018-05-27 15:15:31 +02:00
|
|
|
if (len > slen) {
|
|
|
|
len = slen;
|
|
|
|
}
|
2018-05-21 13:27:02 +02:00
|
|
|
memmove(write, read, len);
|
|
|
|
write += len;
|
|
|
|
size -= len;
|
2018-05-27 15:15:31 +02:00
|
|
|
slen -= len;
|
2018-05-21 13:27:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*write = '\0';
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *str_escape(const char *str, char *buffer, size_t size) {
|
|
|
|
return str_escape_ex(str, buffer, size, "\n\t\r\'\"\\");
|
|
|
|
}
|