aboutsummaryrefslogtreecommitdiff
path: root/include/str.h
blob: 0e8ae5905394f05aebbb9f2eee67bdf37ef6b69b (plain)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*

  pkgit - package it!

  Copyright (C) 2026 dacctal
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/

#ifndef PKGIT_STR_H
#define PKGIT_STR_H

#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

/**
 * Heap-allocated, owned character string.
 *
 * All library functions assert that pointers passed in are valid and point to
 * valid data. All references to "C strings" infer null-terminated pointers to
 * characters.
 *
 * When data is NULL, it is deemed invalid.
 */
typedef struct {
	char *data;
	size_t cap;
	size_t len;
} str;

/**
 * A view into any character buffer that may not be
 * resized, but may be heap-allocated.
 *
 * When data is NULL, it is deemed invalid.
 */
typedef struct {
	const char *data;
	size_t len;
} str_slc;

#define str_is_valid(str) ((str)->data != NULL)

// for printf formatting
#define str_fmt(s) (int)((s)->len), (s)->data

/**
 * Creates and initializes an empty, valid str.
 */
str str_new(void);

/**
 * Creates and initializes an empty str with a specified capacity.
 */
str str_new_with_capacity(size_t cap);

/**
 * Make a str from a C string.
 */
str str_from_cstr(const char *s);

/**
 * Adopts an existing heap-allocated C string, reusing its buffer in the
 * resultant str.
 */
str str_adopt(char *s);

/**
 * Equivalent to str_from_cstr
 */
str mstr(const char *s);

/**
 * Make a str from a string slice.
 */
str str_from_str_slc(const str_slc s);

/**
 * Return a str from the result of an sprintf-style call.
 */
str str_format(const char *format, ...);

/**
 * Saves the result of an sprintf-style call into s.
 */
void str_format_into(str *s, const char *format, ...);

/**
 * Reserves 1.5*new_cap bytes of space in the string.
 */
void str_reserve(str *s, size_t new_cap);

/**
 * Reserves EXACTLY new_cap of bytes of space in the string.
 */
void str_reserve_exact(str *s, size_t new_cap);

/**
 * Clears the entire string buffer to zeroes and resets the length.
 */
void str_clear(str *s);

/**
 * Frees the internal heap-allocated buffer.
 */
void str_free(str *s);

/**
 * Copies the entirety of src into dest, resizing it if necessary, while
 * overwriting all existing data.
 * dest will be initialized if necessary.
 */
void str_copy_into(str *dest, const str *src);

/**
 * Copies a source C string at src into an already initialized
 * str, resizing the buffer if necessary, overwriting all existing data.
 * dest will be initialized if necessary.
 */
void str_copy_cstr_into(str *dest, const char *src);

/**
 * Copies a source string slice at src into an already initialized
 * str, resizing the buffer if necessary, overwriting all existing data.
 * dest will be initialized if necessary.
 */
void str_copy_str_slc_into(str *dest, const str_slc src);

/**
 * Copies n bytes of src into an already initialized str dest,
 * resizing it if necessary, while overwriting all existing data.
 * dest will be initialized if necessary.
 */
void str_ncopy_into(str *dest, const str *src, size_t count);

/**
 * Copies n bytes of a C string src into an already initialized str dest,
 * resizing it if necessary, while overwriting all existing data.
 * dest will be initialized if necessary.
 */
void str_ncopy_cstr_into(str *dest, const char *src, size_t count);

/**
 * Copies n bytes of a string slice src into an already initialized str dest,
 * resizing it if necessary, while overwriting all existing data.
 * dest will be initialized if necessary.
 */
void str_ncopy_str_slc_into(str *dest, const str_slc src, size_t count);

/**
 * Duplicates a str exactly, retaining its original capacity.
 */
str str_dupe(const str *src);

/**
 * Appends a str into an existing str, resizing the buffer if necessary.
 */
void str_append(str *src, const str *new_str);

/**
 * Appends a single character new into src, resizing the buffer if necessary.
 */
void str_append_char(str *src, char new_char);

/**
 * Appends a C string into an existing str, resizing the buffer if necessary.
 */
void str_append_cstr(str *src, const char *new_cstr);

/**
 * Appends a string slice into an existing str, resizing the buffer if
 * necessary.
 */
void str_append_str_slc(str *src, const str_slc new_slc);

/**
 * Appends a new formatted section of the string, with the same calling
 * style as str_append into an existing string buffer.
 */
void str_append_format(str *dest, const char *format, ...);

/**
 * Removes the last character from src, returning it.
 */
char str_pop_last(str *src);

/**
 * Concatenates lhs and rhs together into a new string.
 */
str str_concat(const str *lhs, const str *rhs);

/**
 * Concatenates lhs and a C string rhs together into a new string.
 */
str str_concat_cstr(const str *lhs, const char *rhs);

/**
 * Concatenates lhs and a str_slc rhs together into a new string.
 */
str str_concat_str_slc(const str *lhs, const str_slc rhs);

/**
 * Trims all whitespace characters in s away only on the left, and returns the
 * result.
 */
str str_trim_left(const str *s);

/**
 * Trims all whitespace characters in s away only on the left, and returns the
 * result.
 */
str str_trim_right(const str *s);

/**
 * Trims all whitespace characters in s away on the left and right, and returns
 * the result.
 */
str str_trim(const str *s);

/**
 * Converts all characters in s to uppercase and returns the result.
 */
str str_to_upper(const str *s);

/**
 * Converts all characters in s to lowercase and returns the result
 */
str str_to_lower(const str *s);

/**
 * Trims all whitespace characters in s away only on the left, modifying the
 * string s.
 */
void str_trim_left_in_place(str *s);

/**
 * Trims all whitespace characters in s away only on the right, modifying the
 * string s.
 */
void str_trim_right_in_place(str *s);

/**
 * Trims all whitespace characters in s away on the left and right, modifying
 * the string s.
 */
void str_trim_in_place(str *s);

/**
 * Converts all characters in s to uppercase, modifying the string s.
 */
void str_to_upper_in_place(str *s);

/**
 * Converts all characters in s to lowercase, modifying the string s.
 */
void str_to_lower_in_place(str *s);

/**
 * Returns if lhs and rhs are equal.
 */
bool str_equal(const str *lhs, const str *rhs);

/**
 * Returns if lhs and rhs (a C string) are equal.
 */
bool str_equal_cstr(const str *lhs, const char *rhs);

/**
 * Returns if lhs and rhs (a string slice) are equal.
 */
bool str_equal_str_slc(const str *lhs, const str_slc rhs);

/**
 * Compares the strings lhs and rhs lexicographically, similar to strcmp.
 */
int str_compare(const str *lhs, const str *rhs);

/**
 * Compares the strings lhs and rhs (a C string) lexicographically, similar to
 * strcmp.
 */
int str_compare_cstr(const str *lhs, const char *rhs);

/**
 * Compares the strings lhs and rhs (a string slice) lexicographically, similar
 * to strcmp.
 */
int str_compare_str_slc(const str *lhs, const str_slc rhs);

/**
 * Returns if lhs and rhs are equal, ignoring case.
 */
bool str_equal_case_insensitive(const str *lhs, const str *rhs);

/**
 * Returns if lhs and rhs (a C string) are equal, ignoring case.
 */
bool str_equal_cstr_case_insensitive(const str *lhs, const char *rhs);

/**
 * Returns if lhs and rhs (a string slice) are equal, ignoring case.
 */
bool str_equal_str_slc_case_insensitive(const str *lhs, const str_slc rhs);

/**
 * Compares the strings lhs and rhs lexicographically, ignoring case, similar to
 * strcasecmp.
 */
int str_compare_case_insensitive(const str *lhs, const str *rhs);

/**
 * Compares the strings lhs and rhs (a C string) lexicographically, ignoring
 * case, similar to strcasecmp.
 */
int str_compare_cstr_case_insensitive(const str *lhs, const char *rhs);

/**
 * Compares the strings lhs and rhs (a String slice) lexicographically, ignoring
 * case, similar to strcasecmp.
 */
int str_compare_str_slc_case_insensitive(const str *lhs, const str_slc rhs);

/**
 * Slices a string from `begin` to `end`, clamping values to [0, src->len] if
 * needed, and returning the result.
 * ptrdiff_t's are used, as slicing functions may return negative values.
 */
str str_slice_to_str(const str *src, ptrdiff_t begin, ptrdiff_t end);

/**
 * Slices a C string from `begin` to `end`, clamping values to [0, src->len] if
 * needed, returning the result as a str.
 * ptrdiff_t's are used, as slicing functions may return negative values.
 */
str str_slice_from_cstr(const char *src, ptrdiff_t begin, ptrdiff_t end);

/**
 * Slices a string from `begin` to `end`, clamping values to [0, src->len] if
 * needed, returning the result as a str_slc.
 * ptrdiff_t's are used, as slicing functions may return negative values.
 */
str_slc str_slice(const str *src, ptrdiff_t begin, ptrdiff_t end);

/**
 * Slices a string slice from beginning to end, clamping values to [0, src->len]
 * if needed Python, returning the result as a str.
 * ptrdiff_t's are used, as slicing functions may return negative values.
 */
str str_slice_from_str_slc(const str_slc src, ptrdiff_t begin, ptrdiff_t end);

/**
 * Converts a str to a double with strtod(3), returning how many bytes of src
 * were converted to a double.
 */
size_t str_to_double_pro(const str *src, double *res);

/**
 * Converts a str to a int64_t with strtoll(3), returning how many bytes of src
 * were converted to a double.
 */
size_t str_to_int64_pro(const str *src, int64_t *res, int base);

/**
 * Converts a str to a double with strtod(3), returning a success or failure.
 */
bool str_to_double(const str *src, double *res);

/**
 * Converts a str to a int64_t with strtoll(3), returning a success or failure.
 */
bool str_to_int64(const str *src, int64_t *res);

/**
 * Reads a single line from f into a str, and returns it.
 * Panics on failure.
 */
str str_read_line_from_file(FILE *f);

/**
 * Reads count characters from f into a str, and returns it.
 * Panics on failure.
 */
str str_read_chars_from_file(FILE *f, size_t count);

/**
 * Reads the entirety of f into a str, and returns it.
 * Panics on failure.
 */
str str_read_entire_file(FILE *f);

/**
 * Writes the entirety of a str to a file, returning a success or failure.
 * Panics on failure.
 */
bool str_write_to_file(const str *s, FILE *f);

/**
 * Gets the nth character of a str, with bounds checking.
 */
char str_at(const str *s, size_t i);

/**
 * Gets the first character of a str, with bounds checking.
 */
char str_first(const str *s);

/**
 * Gets the last character of a str, with bounds checking.
 */
char str_last(const str *s);

/**
 * Finds a character c in s, from the left to the right.
 * Returns -1 on failure
 */
ptrdiff_t str_find_char(const str *s, char c);

/**
 * Finds a character c in s from the right to the left.
 * Returns s->len on failure.
 */
ptrdiff_t str_find_char_right(const str *s, char c);

void str_print(const str *s);

void str_println(const str *s);

void str_fprint(const str *s, FILE *f);

void str_fprintln(const str *s, FILE *f);

/**
 * Converts a str to a str_slc.
 */
str_slc str_slc_from_str(const str *s);

/**
 * Converts a C string to a str_slc.
 */
str_slc str_slc_from_cstr(const char *s);

/**
 * Equivalent to str_slc_from_cstr.
 */
str_slc mslc(const char *s);

str str_slc_concat(const str_slc lhs, const str_slc rhs);

str str_slc_concat_cstr(const str_slc lhs, const char *rhs);

bool str_slc_equal(const str_slc lhs, const str_slc rhs);

bool str_slc_equal_cstr(const str_slc lhs, const char *rhs);

bool str_slc_equal_case_insensitive(const str_slc lhs, const str_slc rhs);

bool str_slc_equal_cstr_case_insensitive(const str_slc lhs, const char *rhs);

int str_slc_compare(const str_slc lhs, const str_slc rhs);

int str_slc_compare_cstr(const str_slc lhs, const char *rhs);

int str_slc_compare_case_insensitive(const str_slc lhs, const str_slc rhs);

int str_slc_compare_cstr_case_insensitive(const str_slc lhs, const char *rhs);

/**
 * Slices a string slice from `begin` to `end`, clamping values to [0, src->len]
 * if needed, and returning the result.
 * ptrdiff_t's are used, as slicing functions may return negative values.
 */
str_slc str_slc_slice(const str_slc s, ptrdiff_t begin, ptrdiff_t end);

/**
 * Slices a string slice from `begin` to `end`, clamping values to [0, src->len]
 * if needed, and returning the result.
 * ptrdiff_t's are used, as slicing functions may return negative values.
 */
str str_slc_slice_to_str(const str_slc s, ptrdiff_t begin, ptrdiff_t end);

str str_slc_to_lower(const str_slc s);
str str_slc_to_upper(const str_slc s);

str_slc str_slc_trim_left(const str_slc s);
str_slc str_slc_trim_right(const str_slc s);
str_slc str_slc_trim(const str_slc s);

size_t str_slc_to_double_pro(const str_slc src, double *res);
size_t str_slc_to_int64_pro(const str_slc src, int64_t *res, int base);
bool str_slc_to_double(const str_slc src, double *res);
bool str_slc_to_int64(const str_slc src, int64_t *res);

bool str_slc_write_to_file(const str_slc src, FILE *f);

char str_slc_first(const str_slc s);
char str_slc_last(const str_slc s);
char str_slc_at(const str_slc s, size_t i);

void str_slc_print(const str_slc s);
void str_slc_println(const str_slc s);
void str_slc_fprint(const str_slc s, FILE *f);
void str_slc_fprintln(const str_slc s, FILE *f);

/**
 * Finds a character c in s, from the left to the right.
 * Returns -1 on failure
 */
ptrdiff_t str_slc_find_char(const str_slc s, char c);

/**
 * Finds a character c in s from the right to the left.
 * Returns s->len on failure.
 */
ptrdiff_t str_slc_find_char_right(const str_slc s, char c);

str str_from_after_delim(str *arg, char delimiter);
str str_from_after_delim_str_slc(str_slc arg, char delimiter);
str str_from_before_delim(str *arg, char delimiter);
str str_from_before_delim_str_slc(str_slc arg, char delimiter);
str_slc str_slc_from_after_delim(str_slc arg, char delimiter);
str_slc str_slc_from_before_delim(str_slc arg, char delimiter);

#endif