Changeset 486caad for libcfa/src/stdlib.cfa
- Timestamp:
- Mar 25, 2024, 7:15:30 PM (19 months ago)
- Branches:
- master
- Children:
- d734fa1
- Parents:
- df78cce (diff), bf050c5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.cfa
rdf78cce r486caad 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Aug 14 18:22:36 202313 // Update Count : 6 4212 // Last Modified On : Sun Mar 17 08:25:32 2024 13 // Update Count : 699 14 14 // 15 15 … … 21 21 22 22 #include <string.h> // memcpy, memset 23 //#include <math.h> // fabsf, fabs, fabsl24 23 #include <complex.h> // _Complex_I 25 24 #include <assert.h> 25 #include <ctype.h> // isblank 26 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 } // for 75 } // checkif 76 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 } // for 83 } // checkif 84 85 //--------------------------------------- 86 87 float _Complex strto( const char sptr[], char * eptr[] ) { 88 float re, im; 89 char * eeptr; 90 errno = 0; // reset 91 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 } // if 99 } // if 100 } // if 101 if ( eptr != 0p ) *eptr = eeptr; // error case 102 return 0.0f + 0.0f * _Complex_I; 103 } // strto 104 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 } // if 116 } // if 117 } // if 118 if ( eptr != 0p ) *eptr = eeptr; // error case 119 return 0.0 + 0.0 * _Complex_I; 120 } // strto 121 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 } // if 133 } // if 134 } // if 135 if ( eptr != 0p ) *eptr = eeptr; // error case 136 return 0.0L + 0.0L * _Complex_I; 137 } // strto 138 67 139 forall( T | { T strto( const char sptr[], char * eptr[], int ); } ) 68 T convert( const char sptr[] ) { 140 T convert( const char sptr[] ) { // integral 69 141 char * eptr; 70 142 errno = 0; // reset … … 72 144 if ( errno == ERANGE ) throw ExceptionInst( out_of_range ); 73 145 if ( eptr == sptr || // conversion failed, no characters generated 74 *eptr != '\0' ) throw ExceptionInst( invalid_argument ); // not at end ofstr ?146 eptr[0] != '\0' && ! checkif( eptr, isblank ) ) throw ExceptionInst( invalid_argument ); // not at end of blank str ? 75 147 return val; 76 148 } // convert 77 149 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 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 110 160 111 161 //---------------------------------------
Note:
See TracChangeset
for help on using the changeset viewer.