-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathal_strfmt.c
361 lines (319 loc) · 7.4 KB
/
al_strfmt.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
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
/*
Written by John MacCallum, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2009-ll, The Regents of
the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#include "al_strfmt.h"
#if __DBL_DIG__ == 15 && __DBL_MANT_DIG__ == 53 && FLT_RADIX == 2
//#define AL_STRFMT_PRINTALLBITS
#else
#error nope
#endif
int al_strfmt_precision(double d);
int al_strfmt_int8(char *buf, size_t n, int8_t i)
{
return snprintf(buf, n, "'%c'", i);
}
int al_strfmt_int16(char *buf, size_t n, int16_t i)
{
return snprintf(buf, n, "%"PRId16, i);
}
int al_strfmt_int32(char *buf, size_t n, int32_t i)
{
return snprintf(buf, n, "%"PRId32, i);
}
int al_strfmt_int64(char *buf, size_t n, int64_t i)
{
return snprintf(buf, n, "%"PRId64, i);
}
int al_strfmt_uint8(char *buf, size_t n, uint8_t i)
{
return snprintf(buf, n, "'%c'", i);
}
int al_strfmt_uint16(char *buf, size_t n, uint16_t i)
{
return snprintf(buf, n, "%"PRIu16, i);
}
int al_strfmt_uint32(char *buf, size_t n, uint32_t i)
{
return snprintf(buf, n, "%"PRIu32, i);
}
int al_strfmt_uint64(char *buf, size_t n, uint64_t i)
{
return snprintf(buf, n, "%"PRIu64, i);
}
int al_strfmt_float32(char *buf, size_t n, float f)
{
return al_strfmt_float64(buf, n, f);
}
int al_strfmt_float(char *buf, size_t n, float f)
{
return al_strfmt_float32(buf, n, f);
}
int al_strfmt_double(char *buf, size_t n, float f)
{
return al_strfmt_float64(buf, n, f);
}
#ifdef AL_STRFMT_PRINTALLBITS
int al_strfmt_float64(char *buf, size_t n, double f)
{
snprintf(buf, n, "%.*f", al_strfmt_precision(f), f);
}
#else
int al_strfmt_float64(char *buf, size_t n, double f)
{
int need_point = f - floor(f) == 0 ? 1 : 0;
if(need_point){
return snprintf(buf, n, "%g.", f);
}else{
return snprintf(buf, n, "%g", f);
}
}
#endif
int al_strfmt_bool(char *buf, size_t n, char b)
{
switch(b){
case 'T':
return snprintf(buf, n, "true");
case 'F':
return snprintf(buf, n, "false");
default:
return 0;
}
}
int al_strfmt_null(char *buf, size_t n)
{
return snprintf(buf, n, "nil");
}
int al_strfmt_quotedString(char *buf, size_t n, char *str)
{
if(!str){
return 0;
}
return snprintf(buf, n, "\"%s\"", str);
}
int al_strfmt_stringWithQuotedMeta(char *buf, size_t n, char *str)
{
if(!str){
return 0;
}
long len = strlen(str);
if(!buf){
return al_strfmt_countMeta(len, str) + len;
}
{
int i = 0;
for(int j = 0; j < len; j++){
if(al_strfmt_isMeta(str[j])){
if(i < n){
buf[i] = '\\';
}
i++;
}
if(i < n){
buf[i] = str[j];
}
i++;
}
if(i < n){
buf[i] = '\0';
}else{
buf[i - 1] = '\0';
}
return i;
}
}
/*
int al_strfmt_quotedStringWithQuotedMeta(char *buf, size_t n, char *str)
{
if(!str){
return 0;
}
if(!buf){
return al_strfmt_stringWithQuotedMeta(buf, n, str) + 2;
}else{
char tmp[n];
int nn = al_strfmt_stringWithQuotedMeta(tmp, n, str);
al_strfmt_quotedString(buf, n, tmp);
return nn + 2;
}
}
int al_strfmt_addQuotes(int len, char *buf, char **out)
{
if(!buf){
return 0;
}
if(!(*out)){
*out = osc_mem_alloc(len + 3);
}
int i = 0;
if(buf[0] != '\"'){
(*out)[i++] = '\"';
}
strncpy((*out) + i, buf, len);
i += len;
if(buf[len - 1] != '\"'){
(*out)[i++] = '\"';
}
(*out)[i] = '\0';
return i;
}
*/
int al_strfmt_countMeta(int len, char *buf)
{
int i;
int n = 0;
for(i = 0; i < len; i++){
if(al_strfmt_isMeta(buf[i])){
n++;
}
}
return n;
}
int al_strfmt_isMeta(char c)
{
int i;
for(i = 0; i < sizeof(AL_STRFMT_META_CHARS); i++){
if(c == AL_STRFMT_META_CHARS[i]){
return 1;
}
}
return 0;
}
/*
int al_strfmt_addQuotesAndQuoteMeta(int len, char *buf, char **out)
{
if(!buf){
return 0;
}
int n = len + 2 + al_strfmt_countMeta(len, buf);
if(!(*out)){
*out = osc_mem_alloc(n);
}
int i = 0;
if(1){//buf[0] != '\"'){
(*out)[i++] = '\"';
}
//strncpy((*out) + i, buf, len);
//i += len;
int j;
for(j = 0; j < len; j++){
if(al_strfmt_isMeta(buf[j])){
(*out)[i++] = '\\';
}
(*out)[i++] = buf[j];
}
if(1){//buf[len - 1] != '\"'){
(*out)[i++] = '\"';
}
(*out)[i] = '\0';
return n;
}
*/
int al_strfmt_strlenPadded(char *str)
{
int len = strlen(str);
int rem = len % 4;
if(rem){
return len + (4 - rem);
}else{
return len + 4;
}
}
/*
int al_strfmt_precision(double d)
{
char buf[43];
//1 + // sign, '-' or '+'
//(sizeof(d) * CHAR_BIT + 3) / 4 + // mantissa hex digits max
//1 + // decimal point, '.'
//1 + // mantissa-exponent separator, 'p'
//1 + // mantissa sign, '-' or '+'
//(sizeof(d) * CHAR_BIT + 2) / 3 + // exponent decimal digits max
//1 // string terminator, '\0'
//];
int n;
char *pp, *p;
int e, lsbFound, lsbPos;
// convert d into "+/- 0x h.hhhh p +/- ddd" representation and check for errors
if ((n = snprintf(buf, sizeof(buf), "%+a", d)) < 0 ||
(unsigned)n >= sizeof(buf))
return -1;
//printf("{%s}", buf);
// make sure the conversion didn't produce something like "nan" or "inf"
// instead of "+/- 0x h.hhhh p +/- ddd"
if (strstr(buf, "0x") != buf + 1 ||
(pp = strchr(buf, 'p')) == NULL)
return 0;
// extract the base-2 exponent manually, checking for overflows
e = 0;
p = pp + 1 + (pp[1] == '-' || pp[1] == '+'); // skip the exponent sign at first
for (; *p != '\0'; p++)
{
if (e > INT_MAX / 10)
return -2;
e *= 10;
if (e > INT_MAX - (*p - '0'))
return -2;
e += *p - '0';
}
if (pp[1] == '-') // apply the sign to the exponent
e = -e;
//printf("[%s|%d]", buf, e);
// find the position of the least significant non-zero bit
lsbFound = lsbPos = 0;
for (p = pp - 1; *p != 'x'; p--)
{
if (*p == '.')
continue;
if (!lsbFound)
{
int hdigit = (*p >= 'a') ? (*p - 'a' + 10) : (*p - '0'); // assuming ASCII chars
if (hdigit)
{
static const int lsbPosInNibble[16] = { 0,4,3,4, 2,4,3,4, 1,4,3,4, 2,4,3,4 };
lsbFound = 1;
lsbPos = -lsbPosInNibble[hdigit];
}
}
else
{
lsbPos -= 4;
}
}
lsbPos += 4;
if (!lsbFound)
return 0; // d is 0 (integer)
// adjust the least significant non-zero bit position
// by the base-2 exponent (just add them), checking
// for overflows
if (lsbPos >= 0 && e >= 0)
return 0; // lsbPos + e >= 0, d is integer
if (lsbPos < 0 && e < 0)
if (lsbPos < INT_MIN - e)
return -2; // d isn't integer and needs too many fractional digits
if ((lsbPos += e) >= 0)
return 0; // d is integer
if (lsbPos == INT_MIN && -INT_MAX != INT_MIN)
return -2; // d isn't integer and needs too many fractional digits
return -lsbPos;
}
*/