Changes in libcfa/src/stdlib.cfa [8f650f0:b5e725a]
- File:
-
- 1 edited
-
libcfa/src/stdlib.cfa (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.cfa
r8f650f0 rb5e725a 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Mar 17 08:25:32 202413 // Update Count : 6 9912 // Last Modified On : Mon Aug 14 18:22:36 2023 13 // Update Count : 642 14 14 // 15 15 … … 21 21 22 22 #include <string.h> // memcpy, memset 23 //#include <math.h> // fabsf, fabs, fabsl 23 24 #include <complex.h> // _Complex_I 24 25 #include <assert.h> 25 #include <ctype.h> // isblank26 26 27 27 #pragma GCC visibility push(default) … … 65 65 //--------------------------------------- 66 66 67 // Check if all string characters are a specific kind, e.g., checkif( s, isblank )68 69 bool checkif( const char s[], int (* kind)( int ) ) {70 for () {71 if ( *s == '\0' ) return true;72 if ( ! kind( *s ) ) return false;73 s += 1;74 } // for75 } // checkif76 77 bool checkif( const char s[], int (* kind)( int, locale_t ), locale_t locale ) {78 for () {79 if ( *s == '\0' ) return true;80 if ( ! kind( *s, locale ) ) return false;81 s += 1;82 } // for83 } // checkif84 85 //---------------------------------------86 87 float _Complex strto( const char sptr[], char * eptr[] ) {88 float re, im;89 char * eeptr;90 errno = 0; // reset91 re = strtof( sptr, &eeptr );92 if ( sptr != eeptr ) {93 im = strtof( eeptr, &eeptr );94 if ( sptr != eeptr ) {95 if ( *eeptr == 'i' ) {96 if ( eptr != 0p ) *eptr = eeptr + 1;97 return re + im * _Complex_I;98 } // if99 } // if100 } // if101 if ( eptr != 0p ) *eptr = eeptr; // error case102 return 0.0f + 0.0f * _Complex_I;103 } // strto104 105 double _Complex strto( const char sptr[], char * eptr[] ) {106 double re, im;107 char * eeptr;108 re = strtod( sptr, &eeptr );109 if ( sptr != eeptr ) {110 im = strtod( eeptr, &eeptr );111 if ( sptr != eeptr ) {112 if ( *eeptr == 'i' ) {113 if ( eptr != 0p ) *eptr = eeptr + 1;114 return re + im * _Complex_I;115 } // if116 } // if117 } // if118 if ( eptr != 0p ) *eptr = eeptr; // error case119 return 0.0 + 0.0 * _Complex_I;120 } // strto121 122 long double _Complex strto( const char sptr[], char * eptr[] ) {123 long double re, im;124 char * eeptr;125 re = strtold( sptr, &eeptr );126 if ( sptr != eeptr ) {127 im = strtold( eeptr, &eeptr );128 if ( sptr != eeptr ) {129 if ( *eeptr == 'i' ) {130 if ( eptr != 0p ) *eptr = eeptr + 1;131 return re + im * _Complex_I;132 } // if133 } // if134 } // if135 if ( eptr != 0p ) *eptr = eeptr; // error case136 return 0.0L + 0.0L * _Complex_I;137 } // strto138 139 67 forall( T | { T strto( const char sptr[], char * eptr[], int ); } ) 140 T convert( const char sptr[] ) { // integral68 T convert( const char sptr[] ) { 141 69 char * eptr; 142 70 errno = 0; // reset … … 144 72 if ( errno == ERANGE ) throw ExceptionInst( out_of_range ); 145 73 if ( eptr == sptr || // conversion failed, no characters generated 146 eptr[0] != '\0' && ! checkif( eptr, isblank ) ) throw ExceptionInst( invalid_argument ); // not at end of blankstr ?74 *eptr != '\0' ) throw ExceptionInst( invalid_argument ); // not at end of str ? 147 75 return val; 148 76 } // convert 149 77 150 forall( T | { T strto( const char sptr[], char * eptr[] ); } ) 151 T convert( const char sptr[] ) { // floating-point 152 char * eptr; 153 errno = 0; // reset 154 T val = strto( sptr, &eptr ); // attempt conversion 155 if ( errno == ERANGE ) throw ExceptionInst( out_of_range ); 156 if ( eptr == sptr || // conversion failed, no characters generated 157 eptr[0] != '\0' && ! checkif( eptr, isblank ) ) throw ExceptionInst( invalid_argument ); // not at end of blank str ? 158 return val; 159 } // 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 160 110 161 111 //---------------------------------------
Note:
See TracChangeset
for help on using the changeset viewer.