2018-02-08 20:47:31 +08:00
|
|
|
#ifndef STRUTIL_H
|
|
|
|
#define STRUTIL_H
|
|
|
|
|
2021-01-09 02:24:51 +08:00
|
|
|
#include "common.h"
|
|
|
|
|
2019-12-07 18:01:55 +08:00
|
|
|
#include <stdbool.h>
|
2017-12-12 22:12:07 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
// like strncpy, except:
|
|
|
|
// - it copies at most n-1 chars
|
|
|
|
// - the dest string is nul-terminated
|
|
|
|
// - it does not write useless bytes if strlen(src) < n
|
|
|
|
// - it returns the number of chars actually written (max n-1) if src has
|
|
|
|
// been copied completely, or n if src has been truncated
|
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
|
|
|
|
|
|
|
// join tokens by sep into dst
|
2021-04-18 19:33:15 +08:00
|
|
|
// returns the number of chars actually written (max n-1) if no truncation
|
2017-12-12 22:12:07 +08:00
|
|
|
// occurred, or n if truncated
|
2019-03-03 03:09:56 +08:00
|
|
|
size_t
|
|
|
|
xstrjoin(char *dst, const char *const tokens[], char sep, size_t n);
|
2018-02-08 20:47:31 +08:00
|
|
|
|
2018-10-05 02:47:53 +08:00
|
|
|
// quote a string
|
|
|
|
// returns the new allocated string, to be freed by the caller
|
2019-03-03 03:09:56 +08:00
|
|
|
char *
|
|
|
|
strquote(const char *src);
|
2018-10-05 02:47:53 +08:00
|
|
|
|
2019-12-07 18:01:55 +08:00
|
|
|
// parse s as an integer into value
|
|
|
|
// returns true if the conversion succeeded, false otherwise
|
|
|
|
bool
|
|
|
|
parse_integer(const char *s, long *out);
|
|
|
|
|
2019-12-09 21:32:59 +08:00
|
|
|
// parse s as integers separated by sep (for example '1234:2000')
|
|
|
|
// returns the number of integers on success, 0 on failure
|
|
|
|
size_t
|
|
|
|
parse_integers(const char *s, const char sep, size_t max_items, long *out);
|
|
|
|
|
2019-12-07 18:01:55 +08:00
|
|
|
// parse s as an integer into value
|
|
|
|
// like parse_integer(), but accept 'k'/'K' (x1000) and 'm'/'M' (x1000000) as
|
|
|
|
// suffix
|
|
|
|
// returns true if the conversion succeeded, false otherwise
|
|
|
|
bool
|
|
|
|
parse_integer_with_suffix(const char *s, long *out);
|
|
|
|
|
2021-04-19 15:22:53 +08:00
|
|
|
// search s in the list separated by sep
|
|
|
|
// for example, strlist_contains("a,bc,def", ',', "bc") returns true
|
|
|
|
bool
|
|
|
|
strlist_contains(const char *list, char sep, const char *s);
|
|
|
|
|
2019-05-31 01:01:08 +08:00
|
|
|
// return the index to truncate a UTF-8 string at a valid position
|
|
|
|
size_t
|
|
|
|
utf8_truncation_index(const char *utf8, size_t max_len);
|
|
|
|
|
2019-02-10 19:53:03 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
// convert a UTF-8 string to a wchar_t string
|
|
|
|
// returns the new allocated string, to be freed by the caller
|
2019-03-03 03:09:56 +08:00
|
|
|
wchar_t *
|
|
|
|
utf8_to_wide_char(const char *utf8);
|
2019-06-10 21:44:45 +08:00
|
|
|
|
|
|
|
char *
|
|
|
|
utf8_from_wide_char(const wchar_t *s);
|
2019-02-10 19:53:03 +08:00
|
|
|
#endif
|
|
|
|
|
2018-02-08 20:47:31 +08:00
|
|
|
#endif
|