-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_string.h
95 lines (78 loc) · 2.72 KB
/
dynamic_string.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// dynamic_string.h
// PL/0
//
// Created by Kevin Colley on 5/2/18.
// Copyright © 2018 Kevin Colley. All rights reserved.
//
#ifndef PL0_DYNAMIC_STRING_H
#define PL0_DYNAMIC_STRING_H
#include <stddef.h>
#include <stdbool.h>
#include "dynamic_array.h"
/*! Dynamically resizing string */
typedef dynamic_array(char) dynamic_string;
/*! Compute the string length of a dynamic string (excludes null terminator) */
static inline size_t string_length(const dynamic_string* pstr) {
size_t length = pstr->count;
if(length >= 1 && pstr->elems[length - 1] == '\0') {
--length;
}
return length;
}
/*! Determine whether the dynamic string is empty */
static inline bool string_empty(const dynamic_string* pstr) {
return pstr->count == 0 || (pstr->count == 1 && pstr->elems[0] == '\0');
}
/*! Append a character to a dynamic string */
static inline void string_appendChar(dynamic_string* pstr, char c) {
/* If the string is currently null-terminated, replace null-terminator with new char */
if(pstr->count >= 1 && pstr->elems[pstr->count - 1] == '\0') {
pstr->elems[pstr->count - 1] = c;
}
else {
array_append(pstr, c);
}
}
/*! Inserts the character at the specified index */
static inline void string_insertChar(dynamic_string* pstr, size_t i, char c) {
array_insert(pstr, i, &c);
}
/*! Removes the character at the specified index */
static inline void string_removeIndex(dynamic_string* pstr, size_t i) {
array_removeIndex(pstr, i);
}
/*! Removes the indexed range from the string */
static inline void string_removeRange(dynamic_string* pstr, size_t start, size_t end) {
array_removeRange(pstr, start, end);
}
/*! Get a reference to the string's contents as a null-terminated C string */
static inline char* string_cstr(dynamic_string* pstr) {
/* This will only add a null terminator if the string doesn't already end with one */
string_appendChar(pstr, '\0');
array_shrink(pstr);
return pstr->elems;
}
/*! Append a C string to a dynamic string */
static inline void string_append(dynamic_string* pstr, const char* cstr) {
if(!cstr) {
return;
}
/* Chop off null terminator if it exists and extend the character array */
pstr->count = string_length(pstr);
array_extend(pstr, cstr, strlen(cstr));
}
/*! Append a C string to a dymaic string, copying a maximum of length bytes */
static inline void string_appendLength(dynamic_string* pstr, const char* cstr, size_t length) {
if(!cstr || length == 0) {
return;
}
/* Chop off null terminator if it exists and extend the character array */
pstr->count = string_length(pstr);
array_extend(pstr, cstr, strnlen(cstr, length));
}
/*! Clear the contents of a dynamic string */
static inline void string_clear(dynamic_string* pstr) {
array_clear(pstr);
}
#endif /* PL0_DYNAMIC_STRING_H */