Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    r8f650f0 rb5e725a  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Mar 17 08:25:32 2024
    13 // Update Count     : 699
     12// Last Modified On : Mon Aug 14 18:22:36 2023
     13// Update Count     : 642
    1414//
    1515
     
    2121
    2222#include <string.h>                                                                             // memcpy, memset
     23//#include <math.h>                                                                             // fabsf, fabs, fabsl
    2324#include <complex.h>                                                                    // _Complex_I
    2425#include <assert.h>
    25 #include <ctype.h>                                                                              // isblank
    2626
    2727#pragma GCC visibility push(default)
     
    6565//---------------------------------------
    6666
    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 
    13967forall( T | { T strto( const char sptr[], char * eptr[], int ); } )
    140 T convert( const char sptr[] ) {                                                // integral
     68T convert( const char sptr[] ) {
    14169        char * eptr;
    14270        errno = 0;                                                                                      // reset
     
    14472        if ( errno == ERANGE ) throw ExceptionInst( out_of_range );
    14573        if ( eptr == sptr ||                                                            // conversion failed, no characters generated
    146                  eptr[0] != '\0' && ! checkif( eptr, isblank ) ) throw ExceptionInst( invalid_argument ); // not at end of blank str ?
     74                 *eptr != '\0' ) throw ExceptionInst( invalid_argument ); // not at end of str ?
    14775        return val;
    14876} // convert
    14977
    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
     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
    160110
    161111//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.