Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    r54af365 rb5e725a  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Mar 15 18:47:28 2024
    13 // Update Count     : 685
     12// Last Modified On : Mon Aug 14 18:22:36 2023
     13// Update Count     : 642
    1414//
    1515
     
    2424#include <complex.h>                                                                    // _Complex_I
    2525#include <assert.h>
    26 #include <ctype.h>
    2726
    2827#pragma GCC visibility push(default)
     
    6665//---------------------------------------
    6766
    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         } // for
    76 } // isalnums
    77 
    78 bool isalphas( const char s[] ) {
    79         for () {
    80                 if ( *s == '\0' ) return true;
    81                 if ( ! isalpha( *s ) ) return false;
    82                 s += 1;
    83         } // for
    84 } // isblanks
    85 
    86 bool iscntrls( const char s[] ) {
    87         for () {
    88                 if ( *s == '\0' ) return true;
    89                 if ( ! iscntrl( *s ) ) return false;
    90                 s += 1;
    91         } // for
    92 } // iscntrls
    93 
    94 bool isdigits( const char s[] ) {
    95         for () {
    96                 if ( *s == '\0' ) return true;
    97                 if ( ! isdigit( *s ) ) return false;
    98                 s += 1;
    99         } // for
    100 } // isdigits
    101 
    102 bool isgraphs( const char s[] ) {
    103         for () {
    104                 if ( *s == '\0' ) return true;
    105                 if ( ! isgraph( *s ) ) return false;
    106                 s += 1;
    107         } // for
    108 } // isgraphs
    109 
    110 bool islowers( const char s[] ) {
    111         for () {
    112                 if ( *s == '\0' ) return true;
    113                 if ( ! islower( *s ) ) return false;
    114                 s += 1;
    115         } // for
    116 } // islowers
    117 
    118 bool isprints( const char s[] ) {
    119         for () {
    120                 if ( *s == '\0' ) return true;
    121                 if ( ! isprint( *s ) ) return false;
    122                 s += 1;
    123         } // for
    124 } // isprints
    125 
    126 bool ispuncts( const char s[] ) {
    127         for () {
    128                 if ( *s == '\0' ) return true;
    129                 if ( ! ispunct( *s ) ) return false;
    130                 s += 1;
    131         } // for
    132 } // ispuncts
    133 
    134 bool isspaces( const char s[] ) {
    135         for () {
    136                 if ( *s == '\0' ) return true;
    137                 if ( ! isspace( *s ) ) return false;
    138                 s += 1;
    139         } // for
    140 } // isspaces
    141 
    142 bool isblanks( const char s[] ) {
    143         for () {
    144                 if ( *s == '\0' ) return true;
    145                 if ( ! isblank( *s ) ) return false;
    146                 s += 1;
    147         } // for
    148 } // isblanks
    149 
    150 bool isuppers( const char s[] ) {
    151         for () {
    152                 if ( *s == '\0' ) return true;
    153                 if ( ! isupper( *s ) ) return false;
    154                 s += 1;
    155         } // for
    156 } // isuppers
    157 
    158 bool isxdigits( const char s[] ) {
    159         for () {
    160                 if ( *s == '\0' ) return true;
    161                 if ( ! isxdigit( *s ) ) return false;
    162                 s += 1;
    163         } // for
    164 } // isxdigits
    165 
    166 //---------------------------------------
    167 
    168 float _Complex strto( const char sptr[], char * eptr[] ) {
    169         float re, im;
    170         char * eeptr;
    171         errno = 0;                                                                                      // reset
    172         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                         } // if
    180                 } // if
    181         } // if
    182         if ( eptr != 0p ) *eptr = eeptr;                                        // error case
    183         return 0.0f + 0.0f * _Complex_I;
    184 } // strto
    185 
    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                         } // if
    197                 } // if
    198         } // if
    199         if ( eptr != 0p ) *eptr = eeptr;                                        // error case
    200         return 0.0 + 0.0 * _Complex_I;
    201 } // strto
    202 
    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                         } // if
    214                 } // if
    215         } // if
    216         if ( eptr != 0p ) *eptr = eeptr;                                        // error case
    217         return 0.0L + 0.0L * _Complex_I;
    218 } // strto
    219 
    22067forall( T | { T strto( const char sptr[], char * eptr[], int ); } )
    221 T convert( const char sptr[] ) {                                                // integral
     68T convert( const char sptr[] ) {
    22269        char * eptr;
    22370        errno = 0;                                                                                      // reset
     
    22572        if ( errno == ERANGE ) throw ExceptionInst( out_of_range );
    22673        if ( eptr == sptr ||                                                            // conversion failed, no characters generated
    227                  eptr[0] != '\0' && ! isspaces( eptr ) ) throw ExceptionInst( invalid_argument ); // not at end of blank str ?
     74                 *eptr != '\0' ) throw ExceptionInst( invalid_argument ); // not at end of str ?
    22875        return val;
    22976} // convert
    23077
    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
     78float _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
     89double _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
     100long 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
    241110
    242111//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.