2021-11-13 06:12:51 +08:00
|
|
|
#ifndef SC_STR_H
|
|
|
|
#define SC_STR_H
|
2018-02-08 20:47:31 +08:00
|
|
|
|
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>
|
|
|
|
|
2021-11-13 06:08:19 +08:00
|
|
|
/**
|
|
|
|
* 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
|
2021-11-13 06:08:19 +08:00
|
|
|
sc_strncpy(char *dest, const char *src, size_t n);
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2021-11-13 06:08:19 +08:00
|
|
|
/**
|
|
|
|
* Join tokens by separator `sep` into `dst`
|
|
|
|
*
|
|
|
|
* Return the number of chars actually written (max n-1) if no truncation
|
|
|
|
* occurred, or n if truncated.
|
|
|
|
*/
|
2019-03-03 03:09:56 +08:00
|
|
|
size_t
|
2021-11-13 06:08:19 +08:00
|
|
|
sc_str_join(char *dst, const char *const tokens[], char sep, size_t n);
|
2018-02-08 20:47:31 +08:00
|
|
|
|
2021-11-13 06:08:19 +08:00
|
|
|
/**
|
|
|
|
* Quote a string
|
|
|
|
*
|
|
|
|
* Return a new allocated string, surrounded with quotes (`"`).
|
|
|
|
*/
|
2019-03-03 03:09:56 +08:00
|
|
|
char *
|
2021-11-13 06:08:19 +08:00
|
|
|
sc_str_quote(const char *src);
|
2018-10-05 02:47:53 +08:00
|
|
|
|
2021-11-13 06:08:19 +08:00
|
|
|
/**
|
|
|
|
* Parse `s` as an integer into `out`
|
|
|
|
*
|
|
|
|
* Return true if the conversion succeeded, false otherwise.
|
|
|
|
*/
|
2019-12-07 18:01:55 +08:00
|
|
|
bool
|
2021-11-13 06:08:19 +08:00
|
|
|
sc_str_parse_integer(const char *s, long *out);
|
2019-12-07 18:01:55 +08:00
|
|
|
|
2021-11-13 06:08:19 +08:00
|
|
|
/**
|
|
|
|
* Parse `s` as integers separated by `sep` (for example `1234:2000`) into `out`
|
|
|
|
*
|
|
|
|
* Returns the number of integers on success, 0 on failure.
|
|
|
|
*/
|
2019-12-09 21:32:59 +08:00
|
|
|
size_t
|
2021-11-13 06:08:19 +08:00
|
|
|
sc_str_parse_integers(const char *s, const char sep, size_t max_items,
|
|
|
|
long *out);
|
2019-12-09 21:32:59 +08:00
|
|
|
|
2021-11-13 06:08:19 +08:00
|
|
|
/**
|
|
|
|
* Parse `s` as an integer into `out`
|
|
|
|
*
|
|
|
|
* Like `sc_str_parse_integer()`, but accept 'k'/'K' (x1000) and 'm'/'M'
|
|
|
|
* (x1000000) as suffixes.
|
|
|
|
*
|
|
|
|
* Return true if the conversion succeeded, false otherwise.
|
|
|
|
*/
|
2019-12-07 18:01:55 +08:00
|
|
|
bool
|
2021-11-13 06:08:19 +08:00
|
|
|
sc_str_parse_integer_with_suffix(const char *s, long *out);
|
2019-12-07 18:01:55 +08:00
|
|
|
|
2021-11-13 06:08:19 +08:00
|
|
|
/**
|
|
|
|
* Search `s` in the list separated by `sep`
|
|
|
|
*
|
|
|
|
* For example, sc_str_list_contains("a,bc,def", ',', "bc") returns true.
|
|
|
|
*/
|
2021-04-19 15:22:53 +08:00
|
|
|
bool
|
2021-11-13 06:08:19 +08:00
|
|
|
sc_str_list_contains(const char *list, char sep, const char *s);
|
2021-04-19 15:22:53 +08:00
|
|
|
|
2021-11-13 06:08:19 +08:00
|
|
|
/**
|
|
|
|
* Return the index to truncate a UTF-8 string at a valid position
|
|
|
|
*/
|
2019-05-31 01:01:08 +08:00
|
|
|
size_t
|
2021-11-13 06:08:19 +08:00
|
|
|
sc_str_utf8_truncation_index(const char *utf8, size_t max_len);
|
2019-05-31 01:01:08 +08:00
|
|
|
|
2019-02-10 19:53:03 +08:00
|
|
|
#ifdef _WIN32
|
2021-11-13 06:08:19 +08:00
|
|
|
/**
|
|
|
|
* Convert a UTF-8 string to a wchar_t string
|
|
|
|
*
|
|
|
|
* Return the new allocated string, to be freed by the caller.
|
|
|
|
*/
|
2019-03-03 03:09:56 +08:00
|
|
|
wchar_t *
|
2021-11-13 06:08:19 +08:00
|
|
|
sc_str_to_wchars(const char *utf8);
|
2019-06-10 21:44:45 +08:00
|
|
|
|
2021-11-13 06:08:19 +08:00
|
|
|
/**
|
|
|
|
* Convert a wchar_t string to a UTF-8 string
|
|
|
|
*
|
|
|
|
* Return the new allocated string, to be freed by the caller.
|
|
|
|
*/
|
2019-06-10 21:44:45 +08:00
|
|
|
char *
|
2021-11-13 06:08:19 +08:00
|
|
|
sc_str_from_wchars(const wchar_t *s);
|
2019-02-10 19:53:03 +08:00
|
|
|
#endif
|
|
|
|
|
2021-11-07 03:26:35 +08:00
|
|
|
/**
|
|
|
|
* Wrap input lines to fit in `columns` columns
|
|
|
|
*
|
|
|
|
* Break input lines at word boundaries (spaces) so that they fit in `columns`
|
|
|
|
* columns, left-indented by `indent` spaces.
|
|
|
|
*/
|
2021-11-13 06:08:19 +08:00
|
|
|
char *
|
|
|
|
sc_str_wrap_lines(const char *input, unsigned columns, unsigned indent);
|
2021-11-07 03:26:35 +08:00
|
|
|
|
2021-11-18 01:25:56 +08:00
|
|
|
/**
|
|
|
|
* Truncate the data after the first line
|
|
|
|
*
|
|
|
|
* An '\0' is always written at the end of the data, even if no newline
|
|
|
|
* character is encountered.
|
|
|
|
*
|
|
|
|
* Return the size of the resulting line.
|
|
|
|
*/
|
|
|
|
size_t
|
|
|
|
sc_str_truncate_first_line(char *data, size_t len);
|
|
|
|
|
2018-02-08 20:47:31 +08:00
|
|
|
#endif
|