Debian Codegraph @

src/lib/util/string.h

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2018 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef _STRING_H_
22 #define _STRING_H_
23
24 #include "config.h"
25
26 #include <stddef.h>
27 #include <stdbool.h>
28
29 #define STRING_EXPLODE_TRIM_LEFT 0x0001
30 #define STRING_EXPLODE_TRIM_RIGHT 0x0002
31 #define STRING_EXPLODE_SKIP_EMPTY 0x0004
32 #define STRING_EXPLODE_SKIP_COMMENT 0x0008
33 #define STRING_EXPLODE_ALL 0xFFFF
34
35 /**
36 * Check if the string is empty.
37 *
38 * @param str String to test.
39 *
40 * @return True if @str is NULL or empty, false otherwise.
41 */
42 bool
43 string_is_empty(const char *str);
44
45 /**
46 * Remove whitespaces from left side of the string and return new location
47 * within the same string.
48 *
49 * @param str String to trim from the left side.
50 *
51 * @return Pointer to the first non-whitespace character of @str.
52 */
53 const char *
54 string_trim_left_const(const char *str);
55
56 /**
57 * Remove whitespaces from left side of the string and return new copy
58 * of the string.
59 *
60 * @param str String to trim from the left side.
61 *
62 * @return New copy of the string with skipped whitespaces from the left side.
63 */
64 char *
65 string_trim_left(const char *str);
66
67 /**
68 * Remove whitespaces from right side of the string and return new copy
69 * of the string.
70 *
71 * @param str String to trim from the right side.
72 *
73 * @return New copy of the string without whitespaces on the right side.
74 */
75 char *
76 string_trim_right(const char *str);
77
78 /**
79 * Remove whitespaces from both left and right side of the string.
80 *
81 * @param str String to trim from both sides.
82 *
83 * @return New copy of the string trimmed from both sides.
84 */
85 char *
86 string_trim(const char *str);
87
88 /**
89 * Remove whitespaces from both left and right side of the string.
90 * If the trimmed string is empty, return NULL rather than an empty string.
91 *
92 * @param str String to trim from both sides.
93 *
94 * @return New copy of the string trimmed from both sides.
95 */
96 char *
97 string_trim_noempty(const char *str);
98
99 /**
100 * Split the string on each delimiter and convert it into NULL-terminated
101 * string array.
102 *
103 * @param str String to split.
104 * @param delimiter Delimiter.
105 * @param flags Bit mask of flags. See STRING_EXPLODE_* macros.
106 *
107 * @return String array or NULL if allocation fails.
108 */
109 char **
110 string_explode(const char *str, char delimiter, unsigned int flags);
111
112 /**
113 * Concatenates items of NULL-terminated string array with delimiter.
114 *
115 * @param array Array to concatenate.
116 * @param delimiter Delimiter.
117 *
118 * @return String or NULL if allocation fails.
119 */
120 char *
121 string_implode(const char **array, char delimiter);
122
123 /**
124 * Replace given position within a string with another string in place.
125 * The position that is being replaced must be large enough to contain the
126 * new string.
127 *
128 * When all replacements are done, call @string_replace_shake() to create
129 * the final string.
130 *
131 * @param str Destination string.
132 * @param start Start index position.
133 * @param end End index position.
134 * @param with Replacement string.
135 */
136 void
137 string_replace_position(char *str, size_t start, size_t end, const char *with);
138
139 /**
140 * Remove line from a string.
141 *
142 * When all replacements are done, call @string_replace_shake() to create
143 * the final string.
144 *
145 * @param beginning Pointer to the left most character of the string.
146 * @param str Destination string.
147 * @param inner_position Position inside the line the will be removed.
148 */
149 void
150 string_remove_line(char *beginning, char *str, size_t inner_position);
151
152 /**
153 * Remove string from @from (including) to @to (excluding).
154 *
155 * When all replacements are done, call @string_replace_shake() to create
156 * the final string.
157 *
158 * @param str Destination string.
159 * @param from Starting position.
160 * @param to Terminating position.
161 */
162 void
163 string_remove_range(char *str, size_t from, size_t to);
164
165 /**
166 * Remove rest of the string content starting at @from.
167 *
168 * When all replacements are done, call @string_replace_shake() to create
169 * the final string.
170 *
171 * @param str Destination string.
172 * @param from Starting position.
173 */
174 void
175 string_remove_remainder(char *str, size_t from);
176
177 /**
178 * Shake all empty positions after replaced strings.
179 */
180 void
181 string_replace_shake(char *str, size_t original_length);
182
183 /**
184 * Compute Levenshtein distance of two strings.
185 */
186 int
187 string_levenshtein(const char *a, const char *b);
188
189 #endif /* _STRING_H_ */