-
Notifications
You must be signed in to change notification settings - Fork 0
/
max-string.c
173 lines (143 loc) · 5.4 KB
/
max-string.c
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
/*
MIT License
Copyright (c) 2022 Darren Anderson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "max-string.h"
#include <string.h>
#include <malloc.h>
#include <assert.h>
/**
* Concatenates two strings into a new buffer and returns a pointer to it.
* Will create a new buffer, and not modify the inputs.
*
* @param a The first string.
* @param b The second string.
* @return A pointer to the concatenated string buffer.
*/
char* mstrcat(const char* a, const char *b) {
size_t strlen_a = strlen(a);
size_t strlen_b = strlen(b);
char* ret = malloc(strlen_a + strlen_b + 1);
memcpy(ret, a, strlen_a);
memcpy((ret + strlen_a), b, strlen_b);
ret[strlen_a + strlen_b] = '\0';
return ret;
}
/**
* Trims any occurrences of characters in the trim_set param at the start and end of a string.
* Will create a new buffer, and not modify the input.
*
* @param input The input string.
* @param trim_set The character set to trim.
* @return A pointer to the buffer containing the trimmed string.
*/
char* mstrtrim(const char* input, const char* trim_set) {
char* ret = malloc(strlen(input) + 1);
strcpy(ret, input);
char* start = ret;
char* end = ret + strlen(ret) - 1;
while (start <= end && strchr(trim_set, *start)) start++;
while (end > start && strchr(trim_set, *end)) end--;
size_t len = (start > end) ? 0 : ((end - start) + 1);
if (ret != start) memmove(ret, start, len);
ret[len] = '\0';
return ret;
}
/**
* Selects a substring from the input at the specified offset for the specified length.
* Will return a new buffer and not modify the input.
*
* @param input Input string to select from.
* @param offset The offset.
* @param length The length of the substring.
* @return A pointer to the new buffer containing the substring.
*/
char* mstrsubstr(const char *input, int offset, size_t length) {
assert(strlen(input) >= (offset + length));
char* ret = malloc(length + 1);
memcpy(ret, (input + offset), length);
ret[length] = '\0';
return ret;
}
/**
* Replaces a specified character with a specified replacement in a string.
* Will return a new buffer and not modify the input.
*
* @param input The input string to use.
* @param find The character to find.
* @param replace The character to replace it with.
* @return A pointer to the new buffer containing the modified string.
*/
char* mstrreplace(const char *input, char find, char replace) {
char* ret = malloc(strlen(input) + 1);
memcpy(ret, input, strlen(input) + 1);
char* current_pos = strchr(ret, find);
while (current_pos) {
*current_pos = replace;
current_pos = strchr(current_pos, find);
}
return ret;
}
/**
* Remove a specified character from a string.
* Will return a new buffer and not modify the input.
*
* @param input The input string to use.
* @param garbage The character to remove.
* @return A pointer to the new buffer containing the modified string.
*/
char* mstrremove(const char *input, char garbage) {
char* ret = malloc(strlen(input) + 1);
memcpy(ret, input, strlen(input) + 1);
char *src, *dst;
for (src = dst = ret; *src != '\0'; src++) {
*dst = *src;
if (*dst != garbage)
dst++;
}
*dst = '\0';
return ret;
}
/**
* Splits a string into multiple substrings based on a delimiter.
*
* @param input The input string to use.
* @param delimiter The delimiter to split the string at.
* @param token_count The integer to update with the token count.
* @return An array of strings.
*/
char** mstrsplit(const char *input, char delimiter, int* token_count) {
const char* start = input;
char* current_pos = strchr(input, delimiter);
char** ret = malloc(sizeof(char*));
int i = 0;
while (current_pos) {
ret = realloc(ret, sizeof(char*) * (i + 1));
ret[i] = mstrsubstr(start, 0, (current_pos - start));
start = current_pos + 1;
current_pos = strchr(start, delimiter);
i++;
}
if (start < (input + strlen(input))) {
// Add the rest of the string
ret = realloc(ret, sizeof(char*) * (i + 1));
ret[i++] = mstrsubstr(start, 0, (input + strlen(input)) - start);
}
*token_count = i;
return ret;
}