deps/libgit2/src/util/str.h

filter: Statementstmt 0.0% 0 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 6 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/util/net.c ↑ Summary deps/libgit2/src/util/thread.h »
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_str_h__
8 #define INCLUDE_str_h__
9  
10 #include "git2_util.h"
11  
12 struct git_str {
13 char *ptr;
14 size_t asize;
15 size_t size;
16 };
17  
18 typedef enum {
19 GIT_STR_BOM_NONE = 0,
20 GIT_STR_BOM_UTF8 = 1,
21 GIT_STR_BOM_UTF16_LE = 2,
22 GIT_STR_BOM_UTF16_BE = 3,
23 GIT_STR_BOM_UTF32_LE = 4,
24 GIT_STR_BOM_UTF32_BE = 5
25 } git_str_bom_t;
26  
27 typedef struct {
28 git_str_bom_t bom; /* BOM found at head of text */
29 unsigned int nul, cr, lf, crlf; /* NUL, CR, LF and CRLF counts */
30 unsigned int printable, nonprintable; /* These are just approximations! */
31 } git_str_text_stats;
32  
33 extern char git_str__initstr[];
34 extern char git_str__oom[];
35  
36 /* Use to initialize string buffer structure when git_str is on stack */
37 #define GIT_STR_INIT { git_str__initstr, 0, 0 }
38  
39 /**
40 * Static initializer for git_str from static string buffer
41 */
42 #define GIT_STR_INIT_CONST(str, len) { (char *)(str), 0, (size_t)(len) }
43  
44 GIT_INLINE(bool) git_str_is_allocated(const git_str *str)
45 {
46 return (str->ptr != NULL && str->asize > 0);
47 }
48  
49 /**
50 * Initialize a git_str structure.
51 *
52 * For the cases where GIT_STR_INIT cannot be used to do static
53 * initialization.
54 */
55 extern int git_str_init(git_str *str, size_t initial_size);
56  
57 extern void git_str_dispose(git_str *str);
58  
59 /**
60 * Resize the string buffer allocation to make more space.
61 *
62 * This will attempt to grow the string buffer to accommodate the target
63 * size. The bstring buffer's `ptr` will be replaced with a newly
64 * allocated block of data. Be careful so that memory allocated by the
65 * caller is not lost. As a special variant, if you pass `target_size` as
66 * 0 and the memory is not allocated by libgit2, this will allocate a new
67 * buffer of size `size` and copy the external data into it.
68 *
69 * Currently, this will never shrink a buffer, only expand it.
70 *
71 * If the allocation fails, this will return an error and the buffer will be
72 * marked as invalid for future operations, invaliding the contents.
73 *
74 * @param str The buffer to be resized; may or may not be allocated yet
75 * @param target_size The desired available size
76 * @return 0 on success, -1 on allocation failure
77 */
78 int git_str_grow(git_str *str, size_t target_size);
79  
80 /**
81 * Resize the buffer allocation to make more space.
82 *
83 * This will attempt to grow the string buffer to accommodate the
84 * additional size. It is similar to `git_str_grow`, but performs the
85 * new size calculation, checking for overflow.
86 *
87 * Like `git_str_grow`, if this is a user-supplied string buffer,
88 * this will allocate a new string uffer.
89 */
90 extern int git_str_grow_by(git_str *str, size_t additional_size);
91  
92 /**
93 * Attempt to grow the buffer to hold at least `target_size` bytes.
94 *
95 * If the allocation fails, this will return an error. If `mark_oom` is
96 * true, this will mark the string buffer as invalid for future
97 * operations; if false, existing string buffer content will be preserved,
98 * but calling code must handle that string buffer was not expanded. If
99 * `preserve_external` is true, then any existing data pointed to be
100 * `ptr` even if `asize` is zero will be copied into the newly allocated
101 * string buffer.
102 */
103 extern int git_str_try_grow(
104 git_str *str, size_t target_size, bool mark_oom);
105  
106 extern void git_str_swap(git_str *str_a, git_str *str_b);
107 extern char *git_str_detach(git_str *str);
108 extern int git_str_attach(git_str *str, char *ptr, size_t asize);
109  
110 /* Populates a `git_str` where the contents are not "owned" by the string
111 * buffer, and calls to `git_str_dispose` will not free the given str.
112 */
113 extern void git_str_attach_notowned(
114 git_str *str, const char *ptr, size_t size);
115  
116 /**
117 * Test if there have been any reallocation failures with this git_str.
118 *
119 * Any function that writes to a git_str can fail due to memory allocation
120 * issues. If one fails, the git_str will be marked with an OOM error and
121 * further calls to modify the string buffer will fail. Check
122 * git_str_oom() at the end of your sequence and it will be true if you
123 * ran out of memory at any point with that string buffer.
124 *
125 * @return false if no error, true if allocation error
126 */
127 0 GIT_INLINE(bool) git_str_oom(const git_str *str)
128 {
129 0 return (str->ptr == git_str__oom);
130 }
131  
132 /*
133 * Functions below that return int value error codes will return 0 on
134 * success or -1 on failure (which generally means an allocation failed).
135 * Using a git_str where the allocation has failed with result in -1 from
136 * all further calls using that string buffer. As a result, you can
137 * ignore the return code of these functions and call them in a series
138 * then just call git_str_oom at the end.
139 */
140  
141 int git_str_set(git_str *str, const void *data, size_t datalen);
142  
143 int git_str_sets(git_str *str, const char *string);
144 int git_str_putc(git_str *str, char c);
145 int git_str_putcn(git_str *str, char c, size_t len);
146 int git_str_put(git_str *str, const char *data, size_t len);
147 int git_str_puts(git_str *str, const char *string);
148 int git_str_printf(git_str *str, const char *format, ...) GIT_FORMAT_PRINTF(2, 3);
149 int git_str_vprintf(git_str *str, const char *format, va_list ap);
150 void git_str_clear(git_str *str);
151 void git_str_consume_bytes(git_str *str, size_t len);
152 void git_str_consume(git_str *str, const char *end);
153 void git_str_truncate(git_str *str, size_t len);
154 void git_str_shorten(git_str *str, size_t amount);
155 void git_str_truncate_at_char(git_str *path, char separator);
156 void git_str_rtruncate_at_char(git_str *path, char separator);
157  
158 /** General join with separator */
159 int git_str_join_n(git_str *str, char separator, int len, ...);
160 /** Fast join of two strings - first may legally point into `str` data */
161 int git_str_join(git_str *str, char separator, const char *str_a, const char *str_b);
162 /** Fast join of three strings - cannot reference `str` data */
163 int git_str_join3(git_str *str, char separator, const char *str_a, const char *str_b, const char *str_c);
164  
165 /**
166 * Join two strings as paths, inserting a slash between as needed.
167 * @return 0 on success, -1 on failure
168 */
169 GIT_INLINE(int) git_str_joinpath(git_str *str, const char *a, const char *b)
170 {
171 return git_str_join(str, '/', a, b);
172 }
173  
174 0 GIT_INLINE(const char *) git_str_cstr(const git_str *str)
175 {
176 0 return str->ptr;
177 }
178  
179 0 GIT_INLINE(size_t) git_str_len(const git_str *str)
180 {
181 0 return str->size;
182 }
183  
184 int git_str_copy_cstr(char *data, size_t datasize, const git_str *str);
185  
186 #define git_str_PUTS(str, cstr) git_str_put(str, cstr, sizeof(cstr) - 1)
187  
188 GIT_INLINE(ssize_t) git_str_rfind_next(const git_str *str, char ch)
189 {
190 ssize_t idx = (ssize_t)str->size - 1;
191 while (idx >= 0 && str->ptr[idx] == ch) idx--;
192 while (idx >= 0 && str->ptr[idx] != ch) idx--;
193 return idx;
194 }
195  
196 GIT_INLINE(ssize_t) git_str_rfind(const git_str *str, char ch)
197 {
198 ssize_t idx = (ssize_t)str->size - 1;
199 while (idx >= 0 && str->ptr[idx] != ch) idx--;
200 return idx;
201 }
202  
203 GIT_INLINE(ssize_t) git_str_find(const git_str *str, char ch)
204 {
205 void *found = memchr(str->ptr, ch, str->size);
206 return found ? (ssize_t)((const char *)found - str->ptr) : -1;
207 }
208  
209 /* Remove whitespace from the end of the string buffer */
210 void git_str_rtrim(git_str *str);
211  
212 int git_str_cmp(const git_str *a, const git_str *b);
213  
214 /* Quote and unquote a string buffer as specified in
215 * http://marc.info/?l=git&m=112927316408690&w=2
216 */
217 int git_str_quote(git_str *str);
218 int git_str_unquote(git_str *str);
219  
220 /* Write data as a hex string */
221 int git_str_encode_hexstr(git_str *str, const char *data, size_t len);
222  
223 /* Write data as base64 encoded in string buffer */
224 int git_str_encode_base64(git_str *str, const char *data, size_t len);
225 /* Decode the given bas64 and write the result to the string buffer */
226 int git_str_decode_base64(git_str *str, const char *base64, size_t len);
227  
228 /* Write data as "base85" encoded in string buffer */
229 int git_str_encode_base85(git_str *str, const char *data, size_t len);
230 /* Decode the given "base85" and write the result to the string buffer */
231 int git_str_decode_base85(git_str *str, const char *base64, size_t len, size_t output_len);
232  
233 /*
234 * Decode the given percent-encoded string and write the result to the
235 * string buffer.
236 */
237 int git_str_decode_percent(git_str *str, const char *encoded, size_t len);
238  
239 /*
240 * Insert, remove or replace a portion of the string buffer.
241 *
242 * @param str The string buffer to work with
243 *
244 * @param where The location in the string buffer where the transformation
245 * should be applied.
246 *
247 * @param nb_to_remove The number of chars to be removed. 0 to not
248 * remove any character in the string buffer.
249 *
250 * @param data A pointer to the data which should be inserted.
251 *
252 * @param nb_to_insert The number of chars to be inserted. 0 to not
253 * insert any character from the string buffer.
254 *
255 * @return 0 or an error code.
256 */
257 int git_str_splice(
258 git_str *str,
259 size_t where,
260 size_t nb_to_remove,
261 const char *data,
262 size_t nb_to_insert);
263  
264 /**
265 * Append string to string buffer, prefixing each character from
266 * `esc_chars` with `esc_with` string.
267 *
268 * @param str String buffer to append data to
269 * @param string String to escape and append
270 * @param esc_chars Characters to be escaped
271 * @param esc_with String to insert in from of each found character
272 * @return 0 on success, <0 on failure (probably allocation problem)
273 */
274 extern int git_str_puts_escaped(
275 git_str *str,
276 const char *string,
277 const char *esc_chars,
278 const char *esc_with);
279  
280 /**
281 * Append string escaping characters that are regex special
282 */
283 GIT_INLINE(int) git_str_puts_escape_regex(git_str *str, const char *string)
284 {
285 return git_str_puts_escaped(str, string, "^.[]$()|*+?{}\\", "\\");
286 }
287  
288 /**
289 * Unescape all characters in a string buffer in place
290 *
291 * I.e. remove backslashes
292 */
293 extern void git_str_unescape(git_str *str);
294  
295 /**
296 * Replace all \r\n with \n.
297 *
298 * @return 0 on success, -1 on memory error
299 */
300 extern int git_str_crlf_to_lf(git_str *tgt, const git_str *src);
301  
302 /**
303 * Replace all \n with \r\n. Does not modify existing \r\n.
304 *
305 * @return 0 on success, -1 on memory error
306 */
307 extern int git_str_lf_to_crlf(git_str *tgt, const git_str *src);
308  
309 /**
310 * Fill string buffer with the common prefix of a array of strings
311 *
312 * String buffer will be set to empty if there is no common prefix
313 */
314 extern int git_str_common_prefix(git_str *buf, char *const *const strings, size_t count);
315  
316 /**
317 * Check if a string buffer begins with a UTF BOM
318 *
319 * @param bom Set to the type of BOM detected or GIT_BOM_NONE
320 * @param str String buffer in which to check the first bytes for a BOM
321 * @return Number of bytes of BOM data (or 0 if no BOM found)
322 */
323 extern int git_str_detect_bom(git_str_bom_t *bom, const git_str *str);
324  
325 /**
326 * Gather stats for a piece of text
327 *
328 * Fill the `stats` structure with counts of unreadable characters, carriage
329 * returns, etc, so it can be used in heuristics. This automatically skips
330 * a trailing EOF (\032 character). Also it will look for a BOM at the
331 * start of the text and can be told to skip that as well.
332 *
333 * @param stats Structure to be filled in
334 * @param str Text to process
335 * @param skip_bom Exclude leading BOM from stats if true
336 * @return Does the string buffer heuristically look like binary data
337 */
338 extern bool git_str_gather_text_stats(
339 git_str_text_stats *stats, const git_str *str, bool skip_bom);
340  
341 /**
342 * Check quickly if string buffer looks like it contains binary data
343 *
344 * @param str string buffer to check
345 * @return 1 if string buffer looks like non-text data
346 */
347 int git_str_is_binary(const git_str *str);
348  
349 /**
350 * Check quickly if buffer contains a NUL byte
351 *
352 * @param str string buffer to check
353 * @return 1 if string buffer contains a NUL byte
354 */
355 int git_str_contains_nul(const git_str *str);
356  
357 #endif
« deps/libgit2/src/util/net.c ↑ Summary deps/libgit2/src/util/thread.h »