Changes in / [3ad5c50:2583407]
- Files:
-
- 4 edited
-
libcfa/src/stdlib.cfa (modified) (4 diffs)
-
libcfa/src/stdlib.hfa (modified) (3 diffs)
-
tests/.expect/ato.x64.txt (modified) (1 diff)
-
tests/ato.cfa (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.cfa
r3ad5c50 r2583407 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Mar 15 18:47:28 202413 // Update Count : 6 8512 // Last Modified On : Mon Aug 14 18:22:36 2023 13 // Update Count : 642 14 14 // 15 15 … … 24 24 #include <complex.h> // _Complex_I 25 25 #include <assert.h> 26 #include <ctype.h>27 26 28 27 #pragma GCC visibility push(default) … … 66 65 //--------------------------------------- 67 66 68 // Cannot overload with singular (isspace) counterparts because they are macros.69 70 bool isalnums( const char s[] ) {71 for () {72 if ( *s == '\0' ) return true;73 if ( ! isalnum( *s ) ) return false;74 s += 1;75 } // for76 } // isalnums77 78 bool isalphas( const char s[] ) {79 for () {80 if ( *s == '\0' ) return true;81 if ( ! isalpha( *s ) ) return false;82 s += 1;83 } // for84 } // isblanks85 86 bool iscntrls( const char s[] ) {87 for () {88 if ( *s == '\0' ) return true;89 if ( ! iscntrl( *s ) ) return false;90 s += 1;91 } // for92 } // iscntrls93 94 bool isdigits( const char s[] ) {95 for () {96 if ( *s == '\0' ) return true;97 if ( ! isdigit( *s ) ) return false;98 s += 1;99 } // for100 } // isdigits101 102 bool isgraphs( const char s[] ) {103 for () {104 if ( *s == '\0' ) return true;105 if ( ! isgraph( *s ) ) return false;106 s += 1;107 } // for108 } // isgraphs109 110 bool islowers( const char s[] ) {111 for () {112 if ( *s == '\0' ) return true;113 if ( ! islower( *s ) ) return false;114 s += 1;115 } // for116 } // islowers117 118 bool isprints( const char s[] ) {119 for () {120 if ( *s == '\0' ) return true;121 if ( ! isprint( *s ) ) return false;122 s += 1;123 } // for124 } // isprints125 126 bool ispuncts( const char s[] ) {127 for () {128 if ( *s == '\0' ) return true;129 if ( ! ispunct( *s ) ) return false;130 s += 1;131 } // for132 } // ispuncts133 134 bool isspaces( const char s[] ) {135 for () {136 if ( *s == '\0' ) return true;137 if ( ! isspace( *s ) ) return false;138 s += 1;139 } // for140 } // isspaces141 142 bool isblanks( const char s[] ) {143 for () {144 if ( *s == '\0' ) return true;145 if ( ! isblank( *s ) ) return false;146 s += 1;147 } // for148 } // isblanks149 150 bool isuppers( const char s[] ) {151 for () {152 if ( *s == '\0' ) return true;153 if ( ! isupper( *s ) ) return false;154 s += 1;155 } // for156 } // isuppers157 158 bool isxdigits( const char s[] ) {159 for () {160 if ( *s == '\0' ) return true;161 if ( ! isxdigit( *s ) ) return false;162 s += 1;163 } // for164 } // isxdigits165 166 //---------------------------------------167 168 float _Complex strto( const char sptr[], char * eptr[] ) {169 float re, im;170 char * eeptr;171 errno = 0; // reset172 re = strtof( sptr, &eeptr );173 if ( sptr != eeptr ) {174 im = strtof( eeptr, &eeptr );175 if ( sptr != eeptr ) {176 if ( *eeptr == 'i' ) {177 if ( eptr != 0p ) *eptr = eeptr + 1;178 return re + im * _Complex_I;179 } // if180 } // if181 } // if182 if ( eptr != 0p ) *eptr = eeptr; // error case183 return 0.0f + 0.0f * _Complex_I;184 } // strto185 186 double _Complex strto( const char sptr[], char * eptr[] ) {187 double re, im;188 char * eeptr;189 re = strtod( sptr, &eeptr );190 if ( sptr != eeptr ) {191 im = strtod( eeptr, &eeptr );192 if ( sptr != eeptr ) {193 if ( *eeptr == 'i' ) {194 if ( eptr != 0p ) *eptr = eeptr + 1;195 return re + im * _Complex_I;196 } // if197 } // if198 } // if199 if ( eptr != 0p ) *eptr = eeptr; // error case200 return 0.0 + 0.0 * _Complex_I;201 } // strto202 203 long double _Complex strto( const char sptr[], char * eptr[] ) {204 long double re, im;205 char * eeptr;206 re = strtold( sptr, &eeptr );207 if ( sptr != eeptr ) {208 im = strtold( eeptr, &eeptr );209 if ( sptr != eeptr ) {210 if ( *eeptr == 'i' ) {211 if ( eptr != 0p ) *eptr = eeptr + 1;212 return re + im * _Complex_I;213 } // if214 } // if215 } // if216 if ( eptr != 0p ) *eptr = eeptr; // error case217 return 0.0L + 0.0L * _Complex_I;218 } // strto219 220 67 forall( T | { T strto( const char sptr[], char * eptr[], int ); } ) 221 T convert( const char sptr[] ) { // integral68 T convert( const char sptr[] ) { 222 69 char * eptr; 223 70 errno = 0; // reset … … 225 72 if ( errno == ERANGE ) throw ExceptionInst( out_of_range ); 226 73 if ( eptr == sptr || // conversion failed, no characters generated 227 eptr[0] != '\0' && ! isspaces( eptr ) ) throw ExceptionInst( invalid_argument ); // not at end of blankstr ?74 *eptr != '\0' ) throw ExceptionInst( invalid_argument ); // not at end of str ? 228 75 return val; 229 76 } // convert 230 77 231 forall( T | { T strto( const char sptr[], char * eptr[] ); } ) 232 T convert( const char sptr[] ) { // floating-point 233 char * eptr; 234 errno = 0; // reset 235 T val = strto( sptr, &eptr ); // attempt conversion 236 if ( errno == ERANGE ) throw ExceptionInst( out_of_range ); 237 if ( eptr == sptr || // conversion failed, no characters generated 238 eptr[0] != '\0' && ! isspaces( eptr ) ) throw ExceptionInst( invalid_argument ); // not at end of blank str ? 239 return val; 240 } // convert 78 float _Complex strto( const char sptr[], char * eptr[] ) { 79 float re, im; 80 char * eeptr; 81 re = strtof( sptr, &eeptr ); 82 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } 83 im = strtof( eeptr, &eeptr ); 84 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } 85 if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } 86 return re + im * _Complex_I; 87 } // strto 88 89 double _Complex strto( const char sptr[], char * eptr[] ) { 90 double re, im; 91 char * eeptr; 92 re = strtod( sptr, &eeptr ); 93 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } 94 im = strtod( eeptr, &eeptr ); 95 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } 96 if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } 97 return re + im * _Complex_I; 98 } // strto 99 100 long double _Complex strto( const char sptr[], char * eptr[] ) { 101 long double re, im; 102 char * eeptr; 103 re = strtold( sptr, &eeptr ); 104 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } 105 im = strtold( eeptr, &eeptr ); 106 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } 107 if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } 108 return re + im * _Complex_I; 109 } // strto 241 110 242 111 //--------------------------------------- -
libcfa/src/stdlib.hfa
r3ad5c50 r2583407 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Mar 15 18:47:26 202413 // Update Count : 7 9212 // Last Modified On : Sun Oct 8 09:18:28 2023 13 // Update Count : 789 14 14 // 15 15 … … 291 291 forall( T & | sized(T) | { void ^?{}( T & ); } ) void adelete( T arr[] ); 292 292 forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } ) void adelete( T arr[], TT rest ); 293 //---------------------------------------294 295 // Cannot overload with singular (isspace) counterparts because they are macros.296 297 bool isalnums( const char s[] );298 bool isalphas( const char s[] );299 bool iscntrls( const char s[] );300 bool isdigits( const char s[] );301 bool isgraphs( const char s[] );302 bool islowers( const char s[] );303 bool isprints( const char s[] );304 bool ispuncts( const char s[] );305 bool isspaces( const char s[] );306 bool isblanks( const char s[] );307 bool isuppers( const char s[] );308 bool isxdigits( const char s[] );309 293 310 294 //--------------------------------------- … … 331 315 332 316 forall( T | { T strto( const char sptr[], char * eptr[], int ); } ) 333 T convert( const char sptr[] ); // integrals 334 forall( T | { T strto( const char sptr[], char * eptr[] ); } ) 335 T convert( const char sptr[] ); // floating-point (no base) 317 T convert( const char sptr[] ); 336 318 337 319 static inline { -
tests/.expect/ato.x64.txt
r3ad5c50 r2583407 3 3 -123 -123 4 4 123 123 5 -123 -123 6 123 123 5 -123 -123 6 123 123 7 7 -123.456 -123.456 8 8 -123.456789012346 -123.4567890123456 9 -123.456789012345679 -123.45678901234567890123456789 9 -123.456789012345679 -123.45678901234567890123456789 10 10 -123.456-123.456i -123.456-123.456i 11 11 -123.456789012346+123.456789012346i -123.4567890123456+123.4567890123456i 12 12 123.456789012345679-123.456789012345679i 123.45678901234567890123456789-123.45678901234567890123456789i 13 123.45678901234-123.456789i 123.45678901234 -123.4567890i13 123.45678901234-123.456789i 123.45678901234-123.4567890i 14 14 -123 -123 15 15 123 123 16 16 -123 -123 17 17 123 123 18 -123 -123 19 123 123 18 -123 -123 19 123 123 20 20 -123.456 -123.456 21 21 -123.456789012346 -123.4567890123456 22 -123.456789012345679 -123.45678901234567890123456789 22 -123.456789012345679 -123.45678901234567890123456789 23 23 -123.456-123.456i -123.456-123.456i 24 -123.456789012346+123.456789012346i -123.4567890123456+123.4567890123456i 25 123.456789012345679-123.456789012345679i 123.45678901234567890123456789-123.45678901234567890123456789i 26 123.45678901234-123.456789i 123.45678901234 -123.4567890i 27 invalid argument 2.0fred 28 invalid argument 2 3x 29 -123 -123 30 123 123 31 -123 -123 32 123 123 33 -123 -123 34 123 123 35 -123.456 -123.456 36 -123.456789012346 -123.4567890123456 37 -123.456789012345679 -123.45678901234567890123456789 38 -123.456-123.456i -123.456-123.456i 24 0.+0.i 2 3 39 25 -123.456789012346+123.456789012346i -123.4567890123456+123.4567890123456i 40 26 123.456789012345679-123.456789012345679i 123.45678901234567890123456789-123.45678901234567890123456789i 41 27 123.45678901234-123.456789i 123.45678901234-123.4567890i 42 invalid argument 2.0fred43 invalid argument 2 3x -
tests/ato.cfa
r3ad5c50 r2583407 10 10 // Created On : Thu Feb 4 08:10:57 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Mar 15 17:58:35 202413 // Update Count : 14512 // Last Modified On : Tue Dec 4 21:33:53 2018 13 // Update Count : 92 14 14 // 15 15 … … 18 18 19 19 int main( void ) { 20 // ato21 22 20 const char * sptr = "-123"; 23 21 int i = ato( sptr ); … … 34 32 sout | uli | sptr; 35 33 36 sptr = " -123 "; // spaces allowed34 sptr = "-123"; 37 35 long long int lli = ato( sptr ); 38 36 sout | lli | sptr; 39 sptr = " 123 "; // spaces allowed37 sptr = "123"; 40 38 unsigned long long int ulli = ato( sptr ); 41 39 sout | ulli | sptr; … … 47 45 double d = ato( sptr ); 48 46 sout | d | sptr; 49 sptr = " -123.45678901234567890123456789 "; // spaces allowed47 sptr = "-123.45678901234567890123456789"; 50 48 long double ld = ato( sptr ); 51 49 sout | ld | sptr; … … 60 58 long double _Complex ldc = ato( sptr ); 61 59 sout | ldc | sptr; 62 sptr = " 123.45678901234 -123.4567890i "; // spaces allowed60 sptr = "123.45678901234-123.4567890i"; 63 61 long double _Complex ldc2 = ato( sptr ); 64 62 sout | ldc2 | sptr; 65 63 66 // strto67 64 68 65 sptr = "-123"; 69 i = strto( sptr, 0 p, 10 );66 i = strto( sptr, 0, 10 ); 70 67 sout | i | sptr; 71 68 sptr = "123"; 72 ui = strto( sptr, 0 p, 10 );69 ui = strto( sptr, 0, 10 ); 73 70 sout | ui | sptr; 74 71 75 72 sptr = "-123"; 76 li = strto( sptr, 0 p, 10 );73 li = strto( sptr, 0, 10 ); 77 74 sout | li | sptr; 78 75 sptr = "123"; 79 uli = strto( sptr, 0 p, 10 );76 uli = strto( sptr, 0, 10 ); 80 77 sout | uli | sptr; 81 78 82 sptr = " -123 "; // spaces allowed83 lli = strto( sptr, 0 p, 10 );79 sptr = "-123"; 80 lli = strto( sptr, 0, 10 ); 84 81 sout | lli | sptr; 85 sptr = " 123 "; // spaces allowed86 ulli = strto( sptr, 0 p, 10 );82 sptr = "123"; 83 ulli = strto( sptr, 0, 10 ); 87 84 sout | ulli | sptr; 88 85 89 86 sptr = "-123.456"; 90 f = strto( sptr, 0 p);87 f = strto( sptr, 0 ); 91 88 sout | f | sptr; 92 89 sptr = "-123.4567890123456"; 93 d = strto( sptr, 0 p);90 d = strto( sptr, 0 ); 94 91 sout | d | sptr; 95 sptr = " -123.45678901234567890123456789 "; // spaces allowed96 ld = strto( sptr, 0 p);92 sptr = "-123.45678901234567890123456789"; 93 ld = strto( sptr, 0 ); 97 94 sout | ld | sptr; 98 95 99 96 sptr = "-123.456-123.456i"; 100 fc = strto( sptr, 0 p);97 fc = strto( sptr, 0 ); 101 98 sout | fc | sptr; 99 100 char * eptr = 0; 101 // sptr = "2fred"; 102 // fc = strto( sptr, &eptr ); 103 // sout | fc | sptr | eptr; 104 105 sptr = "2 3"; 106 fc = strto( sptr, &eptr ); 107 sout | fc | sptr | eptr; 108 102 109 sptr = "-123.4567890123456+123.4567890123456i"; 103 dc = strto( sptr, 0 p);110 dc = strto( sptr, 0 ); 104 111 sout | dc | sptr; 105 112 sptr = "123.45678901234567890123456789-123.45678901234567890123456789i"; 106 ldc = strto( sptr, 0p ); 107 sout | ldc | sptr; 108 sptr = " 123.45678901234 -123.4567890i "; // spaces allowed 109 ldc2 = strto( sptr, 0p ); 110 sout | ldc2 | sptr; 111 112 sptr = "2.0fred"; 113 char * eptr = 0p; 114 errno = 0; // reset 115 f = strto( sptr, &eptr ); 116 if ( errno == ERANGE ) sout | "out of range"; 117 if ( eptr == sptr || // conversion failed, no characters generated 118 *eptr != '\0' ) sout | "invalid argument" | sptr; // not at end of str ? 119 else assert( false ); 120 121 sptr = "2 3x"; 122 eptr = 0p; 123 errno = 0; // reset 124 fc = strto( sptr, &eptr ); 125 if ( errno == ERANGE ) sout | "out of range"; 126 if ( eptr == sptr || // conversion failed, no characters generated 127 *eptr != '\0' ) sout | "invalid argument" | sptr; // not at end of str ? 128 else assert( false ); 129 130 // convert 131 132 sptr = "-123"; 133 i = convert( sptr ); 134 sout | i | sptr; 135 sptr = "123"; 136 ui = convert( sptr ); 137 sout | ui | sptr; 138 139 sptr = "-123"; 140 li = convert( sptr ); 141 sout | li | sptr; 142 sptr = "123"; 143 uli = convert( sptr ); 144 sout | uli | sptr; 145 146 sptr = " -123 "; // spaces allowed 147 lli = convert( sptr ); 148 sout | lli | sptr; 149 sptr = " 123 "; // spaces allowed 150 ulli = convert( sptr ); 151 sout | ulli | sptr; 152 153 sptr = "-123.456"; 154 f = convert( sptr ); 155 sout | f | sptr; 156 sptr = "-123.4567890123456"; 157 d = convert( sptr ); 158 sout | d | sptr; 159 sptr = " -123.45678901234567890123456789 "; // spaces allowed 160 ld = convert( sptr ); 161 sout | ld | sptr; 162 163 sptr = "-123.456-123.456i"; 164 fc = convert( sptr ); 165 sout | fc | sptr; 166 sptr = "-123.4567890123456+123.4567890123456i"; 167 dc = convert( sptr ); 168 sout | dc | sptr; 169 sptr = "123.45678901234567890123456789-123.45678901234567890123456789i"; 170 ldc = convert( sptr ); 113 ldc = strto( sptr, 0 ); 171 114 sout | ldc | sptr; 172 115 sptr = "123.45678901234-123.4567890i"; 173 ldc2 = convert( sptr);116 ldc2 = strto( sptr, 0 ); 174 117 sout | ldc2 | sptr; 175 176 sptr = "2.0fred";177 try {178 f = convert( sptr );179 assert( false );180 } catch( invalid_argument * ) {181 sout | "invalid argument" | sptr;182 } // try183 184 sptr = "2 3x";185 try {186 fc = convert( sptr );187 assert( false );188 } catch( invalid_argument * ) {189 sout | "invalid argument" | sptr;190 } // try191 118 } // main 192 119
Note:
See TracChangeset
for help on using the changeset viewer.