-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.c
157 lines (139 loc) · 3.9 KB
/
slice.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
/*
* This file is part of the INA Meter application for Flipper Zero (https://github.com/cepetr/flipper-tina).
*
* 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, version 3.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "slice.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
Slice slice_trim_left(Slice slice) {
while(slice.start < slice.end && isspace((unsigned char)*slice.start)) {
slice.start++;
}
return slice;
}
Slice slice_trim_right(Slice slice) {
while(slice.start < slice.end && isspace((unsigned char)*(slice.end - 1))) {
slice.end--;
}
return slice;
}
Slice slice_trim(Slice slice) {
return slice_trim_left(slice_trim_right(slice));
}
bool slice_equals_cstr(Slice slice, const char* str) {
const char* p = slice.start;
while(p < slice.end && *str != '\0' && *p == *str) {
p++;
str++;
}
return p == slice.end && *str == '\0';
}
bool slice_to_cstr(Slice slice, char* buffer, size_t size) {
size_t len = slice_len(slice);
if(len < size) {
memcpy(buffer, slice.start, len);
buffer[len] = '\0';
return true;
} else {
return false;
}
}
bool slice_parse_int32(Slice slice, int base, int32_t* result) {
char temp[32];
if(slice_to_cstr(slice, temp, sizeof(temp))) {
char* endptr;
int32_t value = strtol(temp, &endptr, base);
if(*endptr == '\0') {
*result = value;
return true;
}
}
return false;
}
bool slice_parse_uint32(Slice slice, int base, uint32_t* result) {
char temp[32];
if(slice_to_cstr(slice, temp, sizeof(temp))) {
char* endptr;
uint32_t value = strtoul(temp, &endptr, base);
if(*endptr == '\0') {
*result = value;
return true;
}
}
return false;
}
bool slice_parse_float(Slice slice, float* result) {
char temp[32];
if(slice_to_cstr(slice, temp, sizeof(temp))) {
char* endptr;
float value = strtof(temp, &endptr);
if(*endptr == '\0') {
*result = value;
return true;
}
}
return false;
}
bool slice_parse_double(Slice slice, double* result) {
char temp[32];
if(slice_to_cstr(slice, temp, sizeof(temp))) {
char* endptr;
double value = strtod(temp, &endptr);
if(*endptr == '\0') {
*result = value;
return true;
}
}
return false;
}
Slice tok_until_char(Slice* slice, char c) {
const char* p = slice->start;
while(p < slice->end && *p != c) {
p++;
}
Slice result = {slice->start, p};
slice->start = p;
return result;
}
Slice tok_until_eol(Slice* slice) {
Slice result = tok_until_char(slice, '\n');
// Trim the trailing '\r' character
if(result.start < result.end && *(result.end - 1) == '\r') {
result.end--;
slice->start--;
}
return result;
}
bool tok_skip_char(Slice* slice, char c) {
if(slice->start < slice->end && *slice->start == c) {
slice->start++;
return true;
} else {
return false;
}
}
bool tok_skip_eol(Slice* slice) {
size_t len = slice->end - slice->start;
const char* p = slice->start;
if(len > 0 && *p == '\n') {
slice->start += 1;
return true;
} else if(len > 1 && *p == '\r' && *(p + 1) == '\n') {
slice->start += 2;
return true;
} else {
return false;
}
}