Ignore:
Timestamp:
Mar 25, 2024, 7:15:30 PM (19 months ago)
Author:
JiadaL <j82liang@…>
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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    rdf78cce r486caad  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Aug 14 18:22:36 2023
    13 // Update Count     : 642
     12// Last Modified On : Sun Mar 17 08:25:32 2024
     13// Update Count     : 699
    1414//
    1515
     
    2121
    2222#include <string.h>                                                                             // memcpy, memset
    23 //#include <math.h>                                                                             // fabsf, fabs, fabsl
    2423#include <complex.h>                                                                    // _Complex_I
    2524#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
     69bool 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
     77bool 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
     87float _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
     105double _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
     122long 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
    67139forall( T | { T strto( const char sptr[], char * eptr[], int ); } )
    68 T convert( const char sptr[] ) {
     140T convert( const char sptr[] ) {                                                // integral
    69141        char * eptr;
    70142        errno = 0;                                                                                      // reset
     
    72144        if ( errno == ERANGE ) throw ExceptionInst( out_of_range );
    73145        if ( eptr == sptr ||                                                            // conversion failed, no characters generated
    74                  *eptr != '\0' ) throw ExceptionInst( invalid_argument ); // not at end of str ?
     146                 eptr[0] != '\0' && ! checkif( eptr, isblank ) ) throw ExceptionInst( invalid_argument ); // not at end of blank str ?
    75147        return val;
    76148} // convert
    77149
    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
     150forall( T | { T strto( const char sptr[], char * eptr[] ); } )
     151T 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
    110160
    111161//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.