/usr/include/obs/util/dstr.h is in libobs-dev 21.0.2+dfsg1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | /*
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include <string.h>
#include <stdarg.h>
#include "c99defs.h"
#include "bmem.h"
/*
* Dynamic string
*
* Helper struct/functions for dynamically sizing string buffers.
*/
#ifdef __cplusplus
extern "C" {
#endif
struct strref;
struct dstr {
char *array;
size_t len; /* number of characters, excluding null terminator */
size_t capacity;
};
#ifndef _MSC_VER
#define PRINTFATTR(f, a) __attribute__((__format__(__printf__, f, a)))
#else
#define PRINTFATTR(f, a)
#endif
EXPORT int astrcmpi(const char *str1, const char *str2);
EXPORT int wstrcmpi(const wchar_t *str1, const wchar_t *str2);
EXPORT int astrcmp_n(const char *str1, const char *str2, size_t n);
EXPORT int wstrcmp_n(const wchar_t *str1, const wchar_t *str2, size_t n);
EXPORT int astrcmpi_n(const char *str1, const char *str2, size_t n);
EXPORT int wstrcmpi_n(const wchar_t *str1, const wchar_t *str2, size_t n);
EXPORT char *astrstri(const char *str, const char *find);
EXPORT wchar_t *wstrstri(const wchar_t *str, const wchar_t *find);
EXPORT char *strdepad(char *str);
EXPORT wchar_t *wcsdepad(wchar_t *str);
EXPORT char **strlist_split(const char *str, char split_ch, bool include_empty);
EXPORT void strlist_free(char **strlist);
static inline void dstr_init(struct dstr *dst);
static inline void dstr_init_move(struct dstr *dst, struct dstr *src);
static inline void dstr_init_move_array(struct dstr *dst, char *str);
static inline void dstr_init_copy(struct dstr *dst, const char *src);
static inline void dstr_init_copy_dstr(struct dstr *dst,
const struct dstr *src);
EXPORT void dstr_init_copy_strref(struct dstr *dst, const struct strref *src);
static inline void dstr_free(struct dstr *dst);
static inline void dstr_array_free(struct dstr *array, const size_t count);
static inline void dstr_move(struct dstr *dst, struct dstr *src);
static inline void dstr_move_array(struct dstr *dst, char *str);
EXPORT void dstr_copy(struct dstr *dst, const char *array);
static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src);
EXPORT void dstr_copy_strref(struct dstr *dst, const struct strref *src);
EXPORT void dstr_ncopy(struct dstr *dst, const char *array,
const size_t len);
EXPORT void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *src,
const size_t len);
static inline void dstr_resize(struct dstr *dst, const size_t num);
static inline void dstr_reserve(struct dstr *dst, const size_t num);
static inline bool dstr_is_empty(const struct dstr *str);
static inline void dstr_cat(struct dstr *dst, const char *array);
EXPORT void dstr_cat_dstr(struct dstr *dst, const struct dstr *str);
EXPORT void dstr_cat_strref(struct dstr *dst, const struct strref *str);
static inline void dstr_cat_ch(struct dstr *dst, char ch);
EXPORT void dstr_ncat(struct dstr *dst, const char *array, const size_t len);
EXPORT void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str,
const size_t len);
EXPORT void dstr_insert(struct dstr *dst, const size_t idx,
const char *array);
EXPORT void dstr_insert_dstr(struct dstr *dst, const size_t idx,
const struct dstr *str);
EXPORT void dstr_insert_ch(struct dstr *dst, const size_t idx,
const char ch);
EXPORT void dstr_remove(struct dstr *dst, const size_t idx, const size_t count);
PRINTFATTR(2, 3)
EXPORT void dstr_printf(struct dstr *dst, const char *format, ...);
PRINTFATTR(2, 3)
EXPORT void dstr_catf(struct dstr *dst, const char *format, ...);
EXPORT void dstr_vprintf(struct dstr *dst, const char *format, va_list args);
EXPORT void dstr_vcatf(struct dstr *dst, const char *format, va_list args);
EXPORT void dstr_safe_printf(struct dstr *dst, const char *format,
const char *val1, const char *val2, const char *val3,
const char *val4);
static inline const char *dstr_find_i(const struct dstr *str,
const char *find);
static inline const char *dstr_find(const struct dstr *str,
const char *find);
EXPORT void dstr_replace(struct dstr *str, const char *find,
const char *replace);
static inline int dstr_cmp(const struct dstr *str1, const char *str2);
static inline int dstr_cmpi(const struct dstr *str1, const char *str2);
static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
const size_t n);
static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
const size_t n);
EXPORT void dstr_depad(struct dstr *dst);
EXPORT void dstr_left(struct dstr *dst, const struct dstr *str,
const size_t pos);
EXPORT void dstr_mid(struct dstr *dst, const struct dstr *str,
const size_t start, const size_t count);
EXPORT void dstr_right(struct dstr *dst, const struct dstr *str,
const size_t pos);
static inline char dstr_end(const struct dstr *str);
EXPORT void dstr_from_mbs(struct dstr *dst, const char *mbstr);
EXPORT char *dstr_to_mbs(const struct dstr *str);
EXPORT void dstr_from_wcs(struct dstr *dst, const wchar_t *wstr);
EXPORT wchar_t *dstr_to_wcs(const struct dstr *str);
EXPORT void dstr_to_upper(struct dstr *str);
EXPORT void dstr_to_lower(struct dstr *str);
#undef PRINTFATTR
/* ------------------------------------------------------------------------- */
static inline void dstr_init(struct dstr *dst)
{
dst->array = NULL;
dst->len = 0;
dst->capacity = 0;
}
static inline void dstr_init_move_array(struct dstr *dst, char *str)
{
dst->array = str;
dst->len = (!str) ? 0 : strlen(str);
dst->capacity = dst->len + 1;
}
static inline void dstr_init_move(struct dstr *dst, struct dstr *src)
{
*dst = *src;
dstr_init(src);
}
static inline void dstr_init_copy(struct dstr *dst, const char *str)
{
dstr_init(dst);
dstr_copy(dst, str);
}
static inline void dstr_init_copy_dstr(struct dstr *dst, const struct dstr *src)
{
dstr_init(dst);
dstr_copy_dstr(dst, src);
}
static inline void dstr_free(struct dstr *dst)
{
bfree(dst->array);
dst->array = NULL;
dst->len = 0;
dst->capacity = 0;
}
static inline void dstr_array_free(struct dstr *array, const size_t count)
{
size_t i;
for (i = 0; i < count; i++)
dstr_free(array+i);
}
static inline void dstr_move_array(struct dstr *dst, char *str)
{
dstr_free(dst);
dst->array = str;
dst->len = (!str) ? 0 : strlen(str);
dst->capacity = dst->len + 1;
}
static inline void dstr_move(struct dstr *dst, struct dstr *src)
{
dstr_free(dst);
dstr_init_move(dst, src);
}
static inline void dstr_ensure_capacity(struct dstr *dst, const size_t new_size)
{
size_t new_cap;
if (new_size <= dst->capacity)
return;
new_cap = (!dst->capacity) ? new_size : dst->capacity*2;
if (new_size > new_cap)
new_cap = new_size;
dst->array = (char*)brealloc(dst->array, new_cap);
dst->capacity = new_cap;
}
static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src)
{
if (dst->array)
dstr_free(dst);
if (src->len) {
dstr_ensure_capacity(dst, src->len + 1);
memcpy(dst->array, src->array, src->len + 1);
dst->len = src->len;
}
}
static inline void dstr_reserve(struct dstr *dst, const size_t capacity)
{
if (capacity == 0 || capacity <= dst->len)
return;
dst->array = (char*)brealloc(dst->array, capacity);
dst->capacity = capacity;
}
static inline void dstr_resize(struct dstr *dst, const size_t num)
{
if (!num) {
dstr_free(dst);
return;
}
dstr_ensure_capacity(dst, num + 1);
dst->array[num] = 0;
dst->len = num;
}
static inline bool dstr_is_empty(const struct dstr *str)
{
if (!str->array || !str->len)
return true;
if (!*str->array)
return true;
return false;
}
static inline void dstr_cat(struct dstr *dst, const char *array)
{
size_t len;
if (!array || !*array)
return;
len = strlen(array);
dstr_ncat(dst, array, len);
}
static inline void dstr_cat_ch(struct dstr *dst, char ch)
{
dstr_ensure_capacity(dst, ++dst->len + 1);
dst->array[dst->len-1] = ch;
dst->array[dst->len] = 0;
}
static inline const char *dstr_find_i(const struct dstr *str, const char *find)
{
return astrstri(str->array, find);
}
static inline const char *dstr_find(const struct dstr *str, const char *find)
{
return strstr(str->array, find);
}
static inline int dstr_cmp(const struct dstr *str1, const char *str2)
{
return strcmp(str1->array, str2);
}
static inline int dstr_cmpi(const struct dstr *str1, const char *str2)
{
return astrcmpi(str1->array, str2);
}
static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
const size_t n)
{
return astrcmp_n(str1->array, str2, n);
}
static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
const size_t n)
{
return astrcmpi_n(str1->array, str2, n);
}
static inline char dstr_end(const struct dstr *str)
{
if (dstr_is_empty(str))
return 0;
return str->array[str->len - 1];
}
#ifdef __cplusplus
}
#endif
|