rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rstr.h
Go to the documentation of this file.
1/* RLIB - Convenience library for useful things
2 * Copyright (C) 2015 Haakon Sporsheim <haakon.sporsheim@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3.0 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library.
16 * See the COPYING file at the root of the source repository.
17 */
18#ifndef __R_STR_H__
19#define __R_STR_H__
20
21#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
22#error "#include <rlib.h> only please."
23#endif
24
25#include <rlib/rtypes.h>
26#include <rlib/data/rlist.h>
27#include <stdarg.h>
28#include <stdio.h>
29
55
63#define R_STR_SIZEOF(str) (sizeof (str) - 1)
64
75#define R_STR_WITH_SIZE_ARGS(str) (str), R_STR_SIZEOF (str)
76
88
89
114R_API const rchar * r_strerror (int errnum, rchar * buf, rsize size);
115
124R_API rsize r_strlen (const rchar * str);
125
141#define r_str_equals(a,b) (r_strcmp (a, b) == 0)
143R_API int r_strcasecmp (const rchar * a, const rchar * b);
145R_API int r_strcasecmp_size (const rchar * a, rssize asize, const rchar * b, rssize bsize);
147R_API int r_strcmp (const rchar * a, const rchar * b);
149R_API int r_strcmp_size (const rchar * a, rssize asize, const rchar * b, rssize bsize);
151R_API int r_strncasecmp (const rchar * a, const rchar * b, rsize len);
153R_API int r_strncmp (const rchar * a, const rchar * b, rsize len);
154
170R_API rboolean r_str_has_prefix (const rchar * str, const rchar * prefix);
177#define r_str_has_substring(str, sub) (r_str_idx_of_str (str, -1, sub, -1) >= 0)
184R_API rboolean r_str_has_suffix (const rchar * str, const rchar * suffix);
185
204R_API rssize r_str_idx_of_c (const rchar * str, rssize strsize, rchar c);
210 const rchar * c, rssize chars);
215 const rchar * sub, rssize subsize);
218 const rchar * sub, rssize subsize);
227R_API rchar * r_str_ptr_of_c (const rchar * str, rssize strsize, rchar c);
237R_API rchar * r_str_ptr_of_c_any (const rchar * str, rssize strsize,
238 const rchar * c, rssize chars);
247R_API rchar * r_str_ptr_of_str (const rchar * str, rssize strsize,
248 const rchar * sub, rssize subsize);
256 const rchar * sub, rssize subsize);
262R_API rchar * r_strchr (const rchar * str, int c);
267R_API rsize r_strcspn (const rchar * str, const rchar * cset);
272R_API rchar * r_strnchr (const rchar * str, int c, rsize size);
274R_API rchar * r_strnrchr (const rchar * str, int c, rsize size);
276R_API rchar * r_strnstr (const rchar * str, const rchar * sub, rsize size);
281R_API rchar * r_strpbrk (const rchar * str, const rchar * set);
286R_API rchar * r_strrchr (const rchar * str, int c);
291R_API rsize r_strspn (const rchar * str, const rchar * set);
296R_API rchar * r_strstr (const rchar * str, const rchar * sub);
297
311R_API rchar * r_stpcpy (rchar * dst, const rchar * src);
313R_API rchar * r_stpncpy (rchar * dst, const rchar * src, rsize len);
315R_API rchar * r_strcat (rchar * dst, const rchar * src);
317R_API rchar * r_strcpy (rchar * dst, const rchar * src);
319R_API rchar * r_strncat (rchar * dst, const rchar * src, rsize len);
321R_API rchar * r_strncpy (rchar * dst, const rchar * src, rsize len);
322
335R_API rchar * r_strdup (const rchar * str);
341R_API rchar * r_strdup_size (const rchar * str, rssize size);
343R_API rchar * r_strdup_strip (const rchar * str, const rchar * chars);
347R_API rchar * r_strndup (const rchar * str, rsize n);
348
363R_API const rchar * r_str_lstrip (const rchar * str, const rchar * chars);
365R_API const rchar * r_str_lwstrip (const rchar * str);
367R_API rchar * r_str_strip (rchar * str, const rchar * chars);
369R_API rchar * r_str_tstrip (rchar * str, const rchar * chars);
374
394R_API rchar * r_strjoinv (const rchar * delim, va_list args);
400R_API rchar * r_strnjoin (rchar * str, rsize size, const rchar * delim, ...) R_ATTR_NULL_TERMINATED;
402R_API rchar * r_strnjoinv (rchar * str, rsize size, const rchar * delim, va_list args);
412R_API rchar ** r_strsplit (const rchar * str, const rchar * delim, rsize max);
413
435R_API int r_asprintf (rchar ** str, const rchar * fmt, ...) R_ATTR_PRINTF (2, 3);
437#define r_snprintf snprintf
438#ifdef _MSC_VER
439/* MSVC deprecates plain sprintf / vsprintf (C4996). Route through
440 * the secure *_s variants using SIZE_MAX as the buffer size, which
441 * skips the runtime bounds check and preserves the unbounded
442 * semantics callers expect (they've already sized the buffer). */
447static inline int r_sprintf (rchar * buf, const rchar * fmt, ...)
448{
449 va_list ap;
450 int ret;
451 va_start (ap, fmt);
452 ret = vsprintf_s (buf, (size_t)-1, fmt, ap);
453 va_end (ap);
454 return ret;
455}
457#define r_vsprintf(buf, fmt, ap) vsprintf_s ((buf), (size_t)-1, (fmt), (ap))
458#else
460#define r_sprintf sprintf
462#define r_vsprintf vsprintf
463#endif
468R_API rchar * r_strprintf (const rchar * fmt, ...) R_ATTR_PRINTF (1, 2);
470R_API rchar * r_strvprintf (const rchar * fmt, va_list args) R_ATTR_PRINTF (1, 0);
472R_API int r_vasprintf (rchar ** str, const rchar * fmt, va_list args) R_ATTR_PRINTF (2, 0);
474#define r_vsnprintf vsnprintf
475
484R_API int r_strscanf (const rchar * str, const rchar * fmt, ...) R_ATTR_SCANF (2, 3);
486R_API int r_strvscanf (const rchar * str, const rchar * fmt, va_list args) R_ATTR_SCANF (2, 0);
487
516R_API rdouble r_str_to_double (const rchar * str, const rchar ** endptr, RStrParse * res);
518R_API rfloat r_str_to_float (const rchar * str, const rchar ** endptr, RStrParse * res);
520R_API rint8 r_str_to_int8 (const rchar * str, const rchar ** endptr,
521 ruint base, RStrParse * res);
523R_API rint16 r_str_to_int16 (const rchar * str, const rchar ** endptr,
524 ruint base, RStrParse * res);
526R_API rint32 r_str_to_int32 (const rchar * str, const rchar ** endptr,
527 ruint base, RStrParse * res);
529R_API rint64 r_str_to_int64 (const rchar * str, const rchar ** endptr,
530 ruint base, RStrParse * res);
532R_API ruint8 r_str_to_uint8 (const rchar * str, const rchar ** endptr,
533 ruint base, RStrParse * res);
535R_API ruint16 r_str_to_uint16 (const rchar * str, const rchar ** endptr,
536 ruint base, RStrParse * res);
538R_API ruint32 r_str_to_uint32 (const rchar * str, const rchar ** endptr,
539 ruint base, RStrParse * res);
541R_API ruint64 r_str_to_uint64 (const rchar * str, const rchar ** endptr,
542 ruint base, RStrParse * res);
543
553R_API rdouble r_str_to_double_size (const rchar * str, rssize size, const rchar ** endptr, RStrParse * res);
554R_API rfloat r_str_to_float_size (const rchar * str, rssize size, const rchar ** endptr, RStrParse * res);
555R_API rint8 r_str_to_int8_size (const rchar * str, rssize size, const rchar ** endptr, ruint base, RStrParse * res);
556R_API rint16 r_str_to_int16_size (const rchar * str, rssize size, const rchar ** endptr, ruint base, RStrParse * res);
557R_API rint32 r_str_to_int32_size (const rchar * str, rssize size, const rchar ** endptr, ruint base, RStrParse * res);
558R_API rint64 r_str_to_int64_size (const rchar * str, rssize size, const rchar ** endptr, ruint base, RStrParse * res);
559R_API ruint8 r_str_to_uint8_size (const rchar * str, rssize size, const rchar ** endptr, ruint base, RStrParse * res);
560R_API ruint16 r_str_to_uint16_size (const rchar * str, rssize size, const rchar ** endptr, ruint base, RStrParse * res);
561R_API ruint32 r_str_to_uint32_size (const rchar * str, rssize size, const rchar ** endptr, ruint base, RStrParse * res);
562R_API ruint64 r_str_to_uint64_size (const rchar * str, rssize size, const rchar ** endptr, ruint base, RStrParse * res);
565#if RLIB_SIZEOF_INT == 8
566#define r_str_to_int r_str_to_int64
567#define r_str_to_uint r_str_to_uint64
568#define r_str_to_int_size r_str_to_int64_size
569#define r_str_to_uint_size r_str_to_uint64_size
570#elif RLIB_SIZEOF_INT == 4
571#define r_str_to_int r_str_to_int32
572#define r_str_to_uint r_str_to_uint32
573#define r_str_to_int_size r_str_to_int32_size
574#define r_str_to_uint_size r_str_to_uint32_size
575#elif RLIB_SIZEOF_INT == 2
576#define r_str_to_int r_str_to_int16
577#define r_str_to_uint r_str_to_uint16
578#define r_str_to_int_size r_str_to_int16_size
579#define r_str_to_uint_size r_str_to_uint16_size
580#elif RLIB_SIZEOF_INT == 1
581#define r_str_to_int r_str_to_int8
582#define r_str_to_uint r_str_to_uint8
583#define r_str_to_int_size r_str_to_int8_size
584#define r_str_to_uint_size r_str_to_uint8_size
585#endif
586
588#define r_strtod(str, endptr) r_str_to_double (str, endptr, NULL)
590#define r_strtof(str, endptr) r_str_to_float (str, endptr, NULL)
591#if RLIB_SIZEOF_LONG == 8
592#define r_strtol(str, endptr, base) r_str_to_int64 (str, endptr, base, NULL)
593#define r_strtoul(str, endptr, base) r_str_to_uint64 (str, endptr, base, NULL)
594#elif RLIB_SIZEOF_LONG == 4
595#define r_strtol(str, endptr, base) r_str_to_int32 (str, endptr, base, NULL)
596#define r_strtoul(str, endptr, base) r_str_to_uint32 (str, endptr, base, NULL)
597#elif RLIB_SIZEOF_LONG == 2
598#define r_strtol(str, endptr, base) r_str_to_int16 (str, endptr, base, NULL)
599#define r_strtoul(str, endptr, base) r_str_to_uint16 (str, endptr, base, NULL)
600#elif RLIB_SIZEOF_LONG == 1
601#define r_strtol(str, endptr, base) r_str_to_int8 (str, endptr, base, NULL)
602#define r_strtoul(str, endptr, base) r_str_to_uint8 (str, endptr, base, NULL)
603#endif
605#define r_strtoll(str, endptr, base) r_str_to_int64 (str, endptr, base, NULL)
607#define r_strtoull(str, endptr, base) r_str_to_uint64 (str, endptr, base, NULL)
608
628typedef struct {
631} RStrChunk;
633#define R_STR_CHUNK_INIT { NULL, 0 }
635#define r_str_chunk_dup(chunk) r_strndup ((chunk)->str, (chunk)->size)
637#define r_str_chunk_end(chunk) ((chunk)->str + (chunk)->size)
638
640R_API int r_str_chunk_casecmp (const RStrChunk * buf, const rchar * str, rssize size);
648R_API int r_str_chunk_cmp (const RStrChunk * buf, const rchar * str, rssize size);
657#define r_str_chunk_idx_of_c(buf, c) r_str_idx_of_c ((buf)->str, (buf)->size, c)
659#define r_str_chunk_idx_of_c_any(buf, c, chars) r_str_idx_of_c_any ((buf)->str, (buf)->size, c, chars)
661#define r_str_chunk_idx_of_c_case(buf, c) r_str_idx_of_c_case ((buf)->str, (buf)->size, c)
663#define r_str_chunk_idx_of_str(buf, sub, subsize) r_str_idx_of_str ((buf)->str, (buf)->size, sub, subsize)
665#define r_str_chunk_idx_of_str_case(buf, sub, subsize) r_str_idx_of_str_case ((buf)->str, (buf)->size, sub, subsize)
680#define r_str_chunk_ptr_of_c(buf, c) r_str_ptr_of_c ((buf)->str, (buf)->size, c)
682#define r_str_chunk_ptr_of_c_any(buf, c, chars) r_str_ptr_of_c_any ((buf)->str, (buf)->size, c, chars)
684#define r_str_chunk_ptr_of_c_case(buf, c) r_str_ptr_of_c_case ((buf)->str, (buf)->size, c)
686#define r_str_chunk_ptr_of_str(buf, sub, subsize) r_str_ptr_of_str ((buf)->str, (buf)->size, sub, subsize)
688#define r_str_chunk_ptr_of_str_case(buf, sub, subsize) r_str_ptr_of_str_case ((buf)->str, (buf)->size, sub, subsize)
700R_API ruint r_str_chunk_splitv (RStrChunk * buf, const rchar * delim, va_list args);
703
722typedef struct {
725} RStrKV;
727#define R_STR_KV_INIT { R_STR_CHUNK_INIT, R_STR_CHUNK_INIT }
729#define r_str_kv_dup_key(kv) r_str_chunk_dup (&(kv)->key)
731#define r_str_kv_dup_value(kv) r_str_chunk_dup (&(kv)->val)
732
749 const rchar * delim, const rchar ** endptr);
761 const rchar * kvdelim, rssize kvdsize, const rchar * delim, rssize dsize,
762 const rchar ** endptr);
763
768R_API rboolean r_str_kv_is_key (const RStrKV * kv, const rchar * key, rssize size);
770R_API rboolean r_str_kv_is_value (const RStrKV * kv, const rchar * val, rssize size);
771
799
814
823
836
850 const rchar * pattern);
863 const rchar * pattern, RStrMatchResult ** result);
864
881#define R_STR_MEM_DUMP_SIZE(align) \
882 ((align * 4) + (align / 4) + (RLIB_SIZEOF_VOID_P * 2) + 8)
892R_API void r_str_dump (rchar * dst, const rchar * src, rsize size);
898R_API ruint8 * r_str_hex_mem (const rchar * hex, rsize * outsize) R_ATTR_MALLOC;
904R_API rsize r_str_hex_to_binary (const rchar * hex, ruint8 * bin, rsize size);
916 rsize size, rsize align);
922 rsize size, rsize align);
928R_API rchar * r_str_mem_hex (const ruint8 * ptr, rsize size);
934 const rchar * divider, rsize interval);
935
953R_API RSList * r_str_list_new (const rchar * str0, ...);
955R_API RSList * r_str_list_newv (const rchar * str0, va_list args);
962R_API rchar ** r_strv_new (const rchar * str0, ...);
964R_API rchar ** r_strv_newv (const rchar * str0, va_list args);
966R_API rchar ** r_strv_copy (rchar * const * strv);
967
969R_API rboolean r_strv_contains (rchar * const * strv, const rchar * str);
971R_API void r_strv_foreach (rchar ** strv, RFunc func, rpointer user);
976R_API rchar * r_strv_join (rchar * const * strv, const rchar * delim);
978R_API rsize r_strv_len (rchar * const * strv);
979
981R_API void r_strv_free (rchar ** strv);
982
985R_END_DECLS
986
/* r_str group */
988
989#endif /* __R_STR_H__ */
990
#define R_ATTR_NULL_TERMINATED
Mark a variadic function as requiring a NULL sentinel.
Definition rmacros.h:336
#define R_API
Public-API decoration: resolves to R_API_EXPORT while building rlib and R_API_IMPORT for consumers.
Definition rmacros.h:115
#define R_BEGIN_DECLS
Open an extern "C" block under C++ (no-op in C).
Definition rmacros.h:196
rchar * r_str_twstrip(rchar *str)
Strip trailing whitespace from str in place.
rchar * r_strjoinv(const rchar *delim, va_list args)
va_list flavour of r_strjoin.
rchar * r_strnchr(const rchar *str, int c, rsize size)
Bounded variant of r_strchr; scans at most size bytes.
rchar * r_strrchr(const rchar *str, int c)
Locate the last occurrence of c in str. Behaves like strrchr.
rboolean r_str_has_prefix(const rchar *str, const rchar *prefix)
Test whether str begins with prefix.
rchar * r_stpncpy(rchar *dst, const rchar *src, rsize len)
Like r_strncpy but returns the address of the NUL it wrote.
rchar * r_strdup_wstrip(const rchar *str)
Duplicate str with leading and trailing whitespace removed.
rboolean r_str_has_suffix(const rchar *str, const rchar *suffix)
Test whether str ends with suffix.
rchar * r_strncat(rchar *dst, const rchar *src, rsize len)
Append at most len bytes of src to dst. Behaves like strncat.
int r_strscanf(const rchar *str, const rchar *fmt,...) R_ATTR_SCANF(2
sscanf passthrough.
ruint32 r_str_to_uint32(const rchar *str, const rchar **endptr, ruint base, RStrParse *res)
Parse a 32-bit unsigned integer from str.
rint8 r_str_to_int8(const rchar *str, const rchar **endptr, ruint base, RStrParse *res)
Parse an 8-bit signed integer from str.
rchar * r_strnjoinv(rchar *str, rsize size, const rchar *delim, va_list args)
va_list flavour of r_strnjoin.
RStrParse
Status code returned by the string-parsing helpers.
Definition rstr.h:83
int r_strcmp_size(const rchar *a, rssize asize, const rchar *b, rssize bsize)
Byte-comparison with explicit sizes; -1 means NUL-terminated.
rchar * r_str_ptr_of_c(const rchar *str, rssize strsize, rchar c)
Locate the first occurrence of byte c in str.
rint32 r_str_to_int32(const rchar *str, const rchar **endptr, ruint base, RStrParse *res)
Parse a 32-bit signed integer from str.
const rchar * r_str_lstrip(const rchar *str, const rchar *chars)
Skip leading chars; returns a pointer past them.
rchar * r_strchr(const rchar *str, int c)
Locate the first occurrence of c in str. Behaves like strchr.
rssize r_str_idx_of_str(const rchar *str, rssize strsize, const rchar *sub, rssize subsize)
Index of the first occurrence of sub inside str.
rssize r_str_idx_of_str_case(const rchar *str, rssize strsize, const rchar *sub, rssize subsize)
Case-insensitive variant of r_str_idx_of_str.
rfloat r_str_to_float(const rchar *str, const rchar **endptr, RStrParse *res)
Single-precision counterpart of r_str_to_double.
int r_strcasecmp(const rchar *a, const rchar *b)
Case-insensitive byte-comparison of two NUL-terminated strings.
rchar * r_str_ptr_of_c_any(const rchar *str, rssize strsize, const rchar *c, rssize chars)
Locate the first occurrence in str of any byte from the set c (the first chars bytes of which form th...
rssize r_str_idx_of_c_any(const rchar *str, rssize strsize, const rchar *c, rssize chars)
Index of the first byte in str that occurs in the set c (the first chars bytes of which form the set)...
rssize r_str_idx_of_c(const rchar *str, rssize strsize, rchar c)
Locate the first occurrence of byte c in str, returning its index.
rsize r_strcspn(const rchar *str, const rchar *cset)
Length of the initial substring of str that contains no bytes from cset. Behaves like strcspn.
rchar * r_str_ptr_of_str_case(const rchar *str, rssize strsize, const rchar *sub, rssize subsize)
Case-insensitive variant of r_str_ptr_of_str.
rchar * r_strndup(const rchar *str, rsize n)
Duplicate at most n bytes of str (always NUL-terminated).
rchar * r_strvprintf(const rchar *fmt, va_list args)
va_list flavour of r_strprintf.
int r_strcasecmp_size(const rchar *a, rssize asize, const rchar *b, rssize bsize)
Case-insensitive byte-comparison with explicit sizes; -1 means NUL-terminated.
rchar * r_strcat(rchar *dst, const rchar *src)
Append src to dst. Behaves like strcat.
rchar * r_strdup_strip(const rchar *str, const rchar *chars)
Duplicate str with leading and trailing chars removed.
rint64 r_str_to_int64(const rchar *str, const rchar **endptr, ruint base, RStrParse *res)
Parse a 64-bit signed integer from str.
rchar * r_strdup(const rchar *str)
Duplicate a NUL-terminated string.
rchar * r_str_strip(rchar *str, const rchar *chars)
Strip leading and trailing chars from str in place.
rchar * r_strjoin(const rchar *delim,...)
Concatenate the variadic NUL-terminated strings, separating them with delim, into a freshly allocated...
rchar * r_strncpy(rchar *dst, const rchar *src, rsize len)
Copy at most len bytes of src into dst. Behaves like strncpy.
rchar * r_str_tstrip(rchar *str, const rchar *chars)
Strip trailing chars from str in place.
rssize r_str_idx_of_c_case(const rchar *str, rssize strsize, rchar c)
Case-insensitive variant of r_str_idx_of_c.
rchar * r_strnrchr(const rchar *str, int c, rsize size)
Bounded variant of r_strrchr; scans at most size bytes.
rint16 r_str_to_int16(const rchar *str, const rchar **endptr, ruint base, RStrParse *res)
Parse a 16-bit signed integer from str.
rsize r_strlen(const rchar *str)
Return the byte length of the NUL-terminated string str.
ruint64 r_str_to_uint64(const rchar *str, const rchar **endptr, ruint base, RStrParse *res)
Parse a 64-bit unsigned integer from str.
rchar * r_strdup_size(const rchar *str, rssize size)
Duplicate the first size bytes of str.
rchar * r_strstr(const rchar *str, const rchar *sub)
Locate the first occurrence of sub inside str. Behaves like strstr.
rchar * r_str_ptr_of_str(const rchar *str, rssize strsize, const rchar *sub, rssize subsize)
Locate the first occurrence of sub inside str.
int int r_strvscanf(const rchar *str, const rchar *fmt, va_list args) R_ATTR_SCANF(2
va_list flavour of r_strscanf.
rchar * r_strcpy(rchar *dst, const rchar *src)
Copy src into dst including the terminating NUL. Behaves like strcpy.
rchar * r_strpbrk(const rchar *str, const rchar *set)
Locate the first byte in str that occurs in set. Behaves like strpbrk.
ruint8 r_str_to_uint8(const rchar *str, const rchar **endptr, ruint base, RStrParse *res)
Parse an 8-bit unsigned integer from str.
rchar * r_strprintf(const rchar *fmt,...)
Format into a freshly r_malloc'd buffer and return it.
rchar ** r_strsplit(const rchar *str, const rchar *delim, rsize max)
Split str on delim into a freshly allocated rchar** string vector (NULL-terminated).
rchar * r_strnstr(const rchar *str, const rchar *sub, rsize size)
Bounded variant of r_strstr; scans at most size bytes.
ruint16 r_str_to_uint16(const rchar *str, const rchar **endptr, ruint base, RStrParse *res)
Parse a 16-bit unsigned integer from str.
rchar * r_stpcpy(rchar *dst, const rchar *src)
Like r_strcpy but returns the address of the NUL it wrote.
int r_vasprintf(rchar **str, const rchar *fmt, va_list args)
va_list flavour of r_asprintf.
int r_asprintf(rchar **str, const rchar *fmt,...)
Format into a freshly r_malloc'd buffer and store its address at *str.
rchar * r_str_wstrip(rchar *str)
Strip leading and trailing whitespace from str in place.
int r_strcmp(const rchar *a, const rchar *b)
Byte-comparison of two NUL-terminated strings. Behaves like strcmp.
const rchar * r_strerror(int errnum, rchar *buf, rsize size)
Thread-safe wrapper around the C runtime's strerror.
rdouble r_str_to_double(const rchar *str, const rchar **endptr, RStrParse *res)
Parse a double-precision float from str.
rsize r_strspn(const rchar *str, const rchar *set)
Length of the initial substring of str that consists entirely of bytes from set. Behaves like strspn.
int r_strncasecmp(const rchar *a, const rchar *b, rsize len)
Case-insensitive byte-comparison of at most len bytes.
rchar * r_strnjoin(rchar *str, rsize size, const rchar *delim,...)
Join into a caller-supplied buffer of size bytes.
#define r_sprintf
sprintf passthrough on non-MSVC platforms.
Definition rstr.h:460
const rchar * r_str_lwstrip(const rchar *str)
Skip leading whitespace; returns a pointer past it.
int r_strncmp(const rchar *a, const rchar *b, rsize len)
Byte-comparison of at most len bytes. Behaves like strncmp.
@ R_STR_PARSE_RANGE
Definition rstr.h:85
@ R_STR_PARSE_INVAL
Definition rstr.h:86
@ R_STR_PARSE_OK
Definition rstr.h:84
char rchar
Default character type (char).
Definition rtypes.h:137
float rfloat
Single-precision floating point.
Definition rtypes.h:67
signed short rint16
Signed 16-bit integer.
Definition rtypes.h:186
signed long rint64
Signed 64-bit integer.
Definition rtypes.h:188
double rdouble
Double-precision floating point.
Definition rtypes.h:69
int rboolean
Boolean type (typedef'd to int).
Definition rtypes.h:59
signed char rint8
Signed 8-bit integer.
Definition rtypes.h:185
void(* RFunc)(rpointer data, rpointer user)
Generic iteration callback over (data, user) pairs.
Definition rtypes.h:403
unsigned int ruint
Unsigned int.
Definition rtypes.h:157
unsigned int ruint32
Unsigned 32-bit integer.
Definition rtypes.h:192
void * rpointer
Generic pointer (alias for void *).
Definition rtypes.h:327
unsigned char ruint8
Unsigned 8-bit integer.
Definition rtypes.h:190
signed int rint32
Signed 32-bit integer.
Definition rtypes.h:187
unsigned long rsize
Unsigned pointer-sized size type (like size_t).
Definition rtypes.h:290
unsigned long ruint64
Unsigned 64-bit integer.
Definition rtypes.h:193
unsigned short ruint16
Unsigned 16-bit integer.
Definition rtypes.h:191
signed long rssize
Signed pointer-sized size type (like ssize_t).
Definition rtypes.h:291
Linked-list types: doubly / singly linked plus the callback-flavoured specialisations.
int r_str_chunk_casecmp(const RStrChunk *buf, const rchar *str, rssize size)
Case-insensitive variant of r_str_chunk_cmp.
rboolean r_str_mem_dump(rchar *str, const ruint8 *ptr, rsize size, rsize align)
Render size bytes from ptr into str as a multi-line hex+ASCII dump.
rchar * r_str_mem_hex(const ruint8 *ptr, rsize size)
Encode size bytes from ptr as a contiguous lowercase hex string.
RStrTokenType
Token type produced by the pattern matcher.
Definition rstr.h:792
@ R_STR_TOKEN_WILDCARD_SIZED
Definition rstr.h:796
@ R_STR_TOKEN_CHARS
Definition rstr.h:794
@ R_STR_TOKEN_NONE
Definition rstr.h:793
@ R_STR_TOKEN_COUNT
Definition rstr.h:797
@ R_STR_TOKEN_WILDCARD
Definition rstr.h:795
RStrMatchResultType
Status code from r_str_match_pattern.
Definition rstr.h:806
@ R_STR_MATCH_RESULT_OOM
Definition rstr.h:808
@ R_STR_MATCH_RESULT_INVAL
Definition rstr.h:807
@ R_STR_MATCH_RESULT_PATTERN_NOT_IMPL
Definition rstr.h:810
@ R_STR_MATCH_RESULT_OK
Definition rstr.h:811
@ R_STR_MATCH_RESULT_INVALID_PATTERN
Definition rstr.h:809
@ R_STR_MATCH_RESULT_NO_MATCH
Definition rstr.h:812
rchar * r_str_mem_dump_dup(const ruint8 *ptr, rsize size, rsize align)
Like r_str_mem_dump but allocates the destination buffer.
RStrParse r_str_kv_parse(RStrKV *kv, const rchar *str, rssize size, const rchar *delim, const rchar **endptr)
Parse one key<delim>value pair starting at str.
rsize r_strv_len(rchar *const *strv)
Number of strings in strv (the trailing NULL excluded).
ruint8 * r_str_hex_mem(const rchar *hex, rsize *outsize)
Decode hex digits from hex into a freshly allocated byte buffer; stores the resulting size in *outsiz...
rchar * r_strv_join(rchar *const *strv, const rchar *delim)
Join the strings in strv with delim into a freshly allocated buffer; caller r_free's.
rboolean r_str_match_simple_pattern(const rchar *str, rssize size, const rchar *pattern)
Test whether str matches a simple shell-style pattern.
rboolean r_str_kv_is_value(const RStrKV *kv, const rchar *val, rssize size)
Test whether the KV's value matches val.
RStrParse r_str_chunk_next_line(const RStrChunk *buf, RStrChunk *line)
Consume one line from buf into line.
RStrParse r_str_kv_parse_multiple(RStrKV *kv, const rchar *str, rssize size, const rchar *kvdelim, rssize kvdsize, const rchar *delim, rssize dsize, const rchar **endptr)
Parse a sequence of key / value pairs.
int r_str_chunk_cmp(const RStrChunk *buf, const rchar *str, rssize size)
Compare a chunk against a (str, size) pair byte-for-byte.
rchar ** r_strv_new(const rchar *str0,...)
Build a NULL-terminated rchar** vector from a variadic argument list of NUL-terminated strings.
void r_strv_foreach(rchar **strv, RFunc func, rpointer user)
Invoke func(s, user) for each string s in strv.
rsize r_str_hex_to_binary(const rchar *hex, ruint8 *bin, rsize size)
Decode hex digits from hex into bin (capacity size bytes).
ruint r_str_chunk_splitv(RStrChunk *buf, const rchar *delim, va_list args)
va_list flavour of r_str_chunk_split.
RSList * r_str_list_new(const rchar *str0,...)
Build an RSList from a NULL-terminated variadic argument list of NUL-terminated strings.
void r_str_dump(rchar *dst, const rchar *src, rsize size)
Render the pointer of src followed by the printable-ASCII rendering of its size bytes (non-printable ...
rchar ** r_strv_copy(rchar *const *strv)
Duplicate a string vector (each element and the vector itself).
void r_str_chunk_wstrip(RStrChunk *buf)
Strip leading and trailing whitespace from the chunk in place.
ruint r_str_chunk_split(RStrChunk *buf, const rchar *delim,...)
Split buf into chunks using the NUL-terminated string delim as the separator.
rchar * r_str_mem_hex_full(const ruint8 *ptr, rsize size, const rchar *divider, rsize interval)
Encode ptr as hex with an arbitrary divider inserted every interval bytes (e.g. ":" for "DE:AD:BE:EF"...
void r_str_chunk_lwstrip(RStrChunk *buf)
Strip leading whitespace from the chunk in place.
rboolean r_str_kv_is_key(const RStrKV *kv, const rchar *key, rssize size)
Test whether the KV's key matches key (first size bytes; -1 = NUL-terminated).
rboolean r_strv_contains(rchar *const *strv, const rchar *str)
TRUE iff strv contains a string equal to str.
rboolean r_str_chunk_has_prefix(const RStrChunk *buf, const rchar *str, rssize size)
Test whether the chunk starts with the given prefix.
RSList * r_str_list_newv(const rchar *str0, va_list args)
va_list flavour of r_str_list_new.
void r_strv_free(rchar **strv)
Free a string vector built by r_strv_new and friends.
RStrMatchResultType r_str_match_pattern(const rchar *str, rssize size, const rchar *pattern, RStrMatchResult **result)
Match str against pattern and, on success, allocate a detailed match-result record into *result.
rchar ** r_strv_newv(const rchar *str0, va_list args)
va_list flavour of r_strv_new.
Foundational type aliases used by every rlib header.
Pointer-and-size view of a string region.
Definition rstr.h:628
rsize size
Definition rstr.h:630
rchar * str
Definition rstr.h:629
A pair of borrowed chunks representing one key / value binding inside a larger string.
Definition rstr.h:722
RStrChunk val
Definition rstr.h:724
RStrChunk key
Definition rstr.h:723
Result of r_str_match_pattern.
Definition rstr.h:830
rchar * ptr
Definition rstr.h:831
rsize tokens
Definition rstr.h:833
rchar * end
Definition rstr.h:832
RStrMatchToken token[0]
Definition rstr.h:834
One token of a parsed pattern, paired with the matched span.
Definition rstr.h:818
RStrTokenType type
Definition rstr.h:819
const rchar * pattern
Definition rstr.h:820
RStrChunk chunk
Definition rstr.h:821