Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fd_cstr.h"
3 :
4 : /* FIXME: WEAN THIS OFF STDLIB FOR NON-HOSTED TARGETS */
5 : #include <stdio.h>
6 : #include <stdlib.h>
7 : #include <stdarg.h>
8 : #include <strings.h>
9 : #include <ctype.h>
10 :
11 30002940 : char const * fd_cstr_to_cstr ( char const * cstr ) { return cstr; }
12 30000015 : char fd_cstr_to_char ( char const * cstr ) { return cstr[0]; }
13 30000015 : schar fd_cstr_to_schar ( char const * cstr ) { return (schar) strtol ( cstr, NULL, 0 ); }
14 30000015 : short fd_cstr_to_short ( char const * cstr ) { return (short) strtol ( cstr, NULL, 0 ); }
15 30001662 : int fd_cstr_to_int ( char const * cstr ) { return (int) strtol ( cstr, NULL, 0 ); }
16 30000024 : long fd_cstr_to_long ( char const * cstr ) { return (long) strtol ( cstr, NULL, 0 ); }
17 30010020 : uchar fd_cstr_to_uchar ( char const * cstr ) { return (uchar) strtoul( cstr, NULL, 0 ); }
18 30000015 : ushort fd_cstr_to_ushort( char const * cstr ) { return (ushort)strtoul( cstr, NULL, 0 ); }
19 30000063 : uint fd_cstr_to_uint ( char const * cstr ) { return (uint) strtoul( cstr, NULL, 0 ); }
20 30002472 : ulong fd_cstr_to_ulong ( char const * cstr ) { return (ulong) strtoul( cstr, NULL, 0 ); }
21 30000018 : float fd_cstr_to_float ( char const * cstr ) { return strtof ( cstr, NULL ); }
22 : #if FD_HAS_DOUBLE
23 30000006 : double fd_cstr_to_double( char const * cstr ) { return strtod ( cstr, NULL ); }
24 : #endif
25 :
26 246 : ulong fd_cstr_to_ulong_octal( char const * cstr ) { return (ulong)strtoul( cstr, NULL, 8 ); }
27 :
28 : ulong
29 : fd_cstr_to_ulong_seq( char const * cstr,
30 : ulong * seq,
31 273 : ulong seq_max ) {
32 273 : ulong seq_cnt = 0UL;
33 :
34 273 : if( !cstr ) return seq_cnt;
35 :
36 264 : char const * p = cstr;
37 498 : for(;;) {
38 :
39 498 : char c;
40 498 : char * q;
41 :
42 633 : c = *p; while( fd_isspace( (int)c ) ) c = *(++p); /* Move and peek at next non-white-space character */
43 498 : if( c=='\0' ) break; /* end of sequence */
44 :
45 366 : ulong seq_ele_0 = strtoul( p, &q, 0 );
46 366 : if( FD_UNLIKELY( p==(char const *)q ) ) return 0UL; /* Malformed sequence, seq_ele_0 is not a ulong */
47 345 : p = (char const *)q;
48 :
49 345 : ulong seq_ele_1 = seq_ele_0;
50 345 : ulong seq_stride = 1UL;
51 :
52 489 : c = *p; while( fd_isspace( (int)c ) ) c = *(++p); /* Move and peek at next non-white-space character */
53 345 : if( c=='-' ) {
54 180 : p++;
55 :
56 180 : seq_ele_1 = strtoul( p, &q, 0 );
57 180 : if( FD_UNLIKELY( p==(char const *)q ) ) return 0UL; /* Malformed sequence, seq_ele_1 is not a ulong */
58 153 : p = (char const *)q;
59 :
60 216 : c = *p; while( fd_isspace( (int)c ) ) c = *(++p); /* Move and peek at next non-white-space character */
61 153 : if( c=='/' || c==':' ) {
62 72 : p++;
63 :
64 72 : seq_stride = strtoul( p, &q, 0 );
65 72 : if( FD_UNLIKELY( p==(char const *)q ) ) return 0UL; /* Malformed sequence, seq_stride is not a ulong */
66 51 : p = (char const *)q;
67 51 : }
68 153 : }
69 :
70 315 : c = *p; while( fd_isspace( (int)c ) ) c = *(++p); /* Move and peek at next non-white-space character */
71 297 : if( !(c==',' || c=='\0' ) ) return 0UL; /* Malformed sequence, delimiter */
72 264 : if( c==',' ) p++;
73 :
74 : /* Append the range to sequence. Written this slightly funny way to
75 : be robust against overflow with seq_ele_1 and/or seq_stride being
76 : near or equal to ULONG_MAX */
77 :
78 264 : if( FD_UNLIKELY( (seq_ele_1<seq_ele_0) | (!seq_stride) )) return 0UL; /* Malformed sequence, bad range */
79 :
80 234 : ulong seq_ele = seq_ele_0;
81 6855 : while( ((seq_ele_0<=seq_ele) & (seq_ele<seq_ele_1)) ) {
82 6621 : if( FD_LIKELY( seq_cnt<seq_max ) ) seq[ seq_cnt ] = seq_ele;
83 6621 : seq_cnt++;
84 6621 : seq_ele += seq_stride;
85 6621 : }
86 234 : if( seq_ele==seq_ele_1 ) {
87 198 : if( FD_LIKELY( seq_cnt<seq_max ) ) seq[ seq_cnt ] = seq_ele;
88 198 : seq_cnt++;
89 198 : }
90 234 : }
91 :
92 132 : return seq_cnt;
93 264 : }
94 :
95 : int
96 : fd_cstr_casecmp( char const * a,
97 15465 : char const * b ) {
98 15465 : return strcasecmp( a, b );
99 15465 : }
100 :
101 : ulong
102 : fd_cstr_nlen( char const * s,
103 1242 : ulong m ) {
104 1242 : return strnlen( s, m );
105 1242 : }
106 :
107 : char *
108 : fd_cstr_printf( char * buf,
109 : ulong sz,
110 : ulong * opt_len,
111 300159216 : char const * fmt, ... ) {
112 300159216 : if( FD_UNLIKELY( (!buf) | (!sz) ) ) {
113 0 : if( opt_len ) *opt_len = 0UL;
114 0 : return buf;
115 0 : }
116 300159216 : va_list ap;
117 300159216 : va_start( ap, fmt );
118 300159216 : int ret = vsnprintf( buf, sz, fmt, ap );
119 300159216 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, sz-1UL ) );
120 300159216 : buf[ len ] = '\0';
121 300159216 : va_end( ap );
122 300159216 : if( opt_len ) *opt_len = len;
123 300159216 : return buf;
124 300159216 : }
125 :
126 : int
127 : fd_cstr_printf_check( char * buf,
128 : ulong sz,
129 : ulong * opt_len,
130 243 : char const * fmt, ... ) {
131 243 : if( FD_UNLIKELY( (!buf) | (!sz) ) ) {
132 0 : if( opt_len ) *opt_len = 0UL;
133 0 : return 0;
134 0 : }
135 243 : va_list ap;
136 243 : va_start( ap, fmt );
137 243 : int ret = vsnprintf( buf, sz, fmt, ap );
138 243 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, sz-1UL ) );
139 243 : buf[ len ] = '\0';
140 243 : va_end( ap );
141 243 : if( opt_len ) *opt_len = len;
142 243 : return len==(ulong)ret;
143 243 : }
144 :
145 : char *
146 : fd_cstr_append_printf( char * buf,
147 516 : char const * fmt, ... ) {
148 516 : if( FD_UNLIKELY( !buf ) ) return NULL;
149 516 : va_list ap;
150 516 : va_start( ap, fmt );
151 516 : int ret = vsprintf( buf, fmt, ap );
152 516 : va_end( ap );
153 516 : return buf + fd_ulong_if( ret<0, 0UL, (ulong)ret );
154 516 : }
155 :
156 : ulong
157 : fd_cstr_tokenize( char ** tok,
158 : ulong tok_max,
159 : char * p,
160 240 : char delim ) {
161 240 : if( FD_UNLIKELY( !p ) ) return 0UL;
162 :
163 237 : ulong tok_cnt = 0UL;
164 612 : for(;;) {
165 :
166 : /* Find token start and record it (if possible) */
167 726 : while( fd_isspace( (int)p[0] ) ) p++;
168 612 : if( p[0]=='\0' ) break;
169 573 : if( tok_cnt<tok_max ) tok[ tok_cnt ] = p;
170 573 : tok_cnt++;
171 :
172 : /* Find the token end and terminate it */
173 3048 : while( ((p[0]!=delim) & (p[0]!='\0')) ) p++;
174 573 : if( p[0]=='\0' ) break;
175 375 : p[0] = '\0';
176 375 : p++;
177 375 : }
178 :
179 237 : return tok_cnt;
180 240 : }
|