forked from artemsen/swayimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.h
76 lines (66 loc) · 2.14 KB
/
str.h
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
// SPDX-License-Identifier: MIT
// String operations.
// Copyright (C) 2024 Artem Senichev <[email protected]>
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <sys/types.h>
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
#endif
/** String slice. */
struct str_slice {
const char* value;
size_t len;
};
/**
* Duplicate string.
* @param src source string to duplicate
* @param dst pointer to destination buffer
* @return pointer to new string, caller must free it
*/
char* str_dup(const char* src, char** dst);
/**
* Append string.
* @param src source string to append
* @param dst pointer to destination buffer
* @return pointer to merged string, caller must free it
*/
char* str_append(const char* src, size_t len, char** dst);
/**
* Convert text string to number.
* @param text text to convert
* @param len length of the source string (0=auto)
* @param value output variable
* @param base numeric base
* @return false if text has invalid format
*/
bool str_to_num(const char* text, size_t len, ssize_t* value, int base);
/**
* Convert ansi string to wide char format.
* @param src source string to encode
* @param dst pointer to destination buffer
* @return pointer to wide string, caller must free it
*/
wchar_t* str_to_wide(const char* src, wchar_t** dst);
/**
* Split string ("abc,def" -> "abc", "def").
* @param text source string to split
* @param delimiter delimiter character
* @param slices output array of slices
* @param max_slices max number of slices (size of array)
* @return real number of slices in source string
*/
size_t str_split(const char* text, char delimeter, struct str_slice* slices,
size_t max_slices);
/**
* Search for value in string array.
* @param array source array of strings
* @param array_sz number of strings in array
* @param value text to search
* @param value_len length of the value (0=auto)
* @return index of value in array or -1 if not found
*/
ssize_t str_search_index(const char** array, size_t array_sz, const char* value,
size_t value_len);
#define str_index(a, v, s) str_search_index((a), ARRAY_SIZE(a), v, s)