Changeset 8f650f0 for libcfa/src


Ignore:
Timestamp:
Mar 17, 2024, 8:36:08 AM (6 weeks ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
057608a
Parents:
42422fb
Message:

remove isspace-like routines for C arrays, and replace with general checkif routine

Location:
libcfa/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    r42422fb r8f650f0  
    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 : 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>
    26 #include <ctype.h>
     25#include <ctype.h>                                                                              // isblank
    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[] ) {
     67// Check if all string characters are a specific kind, e.g., checkif( s, isblank )
     68
     69bool checkif( const char s[], int (* kind)( int ) ) {
    7170        for () {
    7271                if ( *s == '\0' ) return true;
    73                 if ( ! isalnum( *s ) ) return false;
     72                if ( ! kind( *s ) ) return false;
    7473                s += 1;
    7574        } // for
    76 } // isalnums
    77 
    78 bool isalphas( const char s[] ) {
     75} // checkif
     76
     77bool checkif( const char s[], int (* kind)( int, locale_t ), locale_t locale ) {
    7978        for () {
    8079                if ( *s == '\0' ) return true;
    81                 if ( ! isalpha( *s ) ) return false;
     80                if ( ! kind( *s, locale ) ) return false;
    8281                s += 1;
    8382        } // 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
     83} // checkif
    16584
    16685//---------------------------------------
     
    225144        if ( errno == ERANGE ) throw ExceptionInst( out_of_range );
    226145        if ( eptr == sptr ||                                                            // conversion failed, no characters generated
    227                  eptr[0] != '\0' && ! isspaces( eptr ) ) throw ExceptionInst( invalid_argument ); // not at end of blank str ?
     146                 eptr[0] != '\0' && ! checkif( eptr, isblank ) ) throw ExceptionInst( invalid_argument ); // not at end of blank str ?
    228147        return val;
    229148} // convert
     
    236155        if ( errno == ERANGE ) throw ExceptionInst( out_of_range );
    237156        if ( eptr == sptr ||                                                            // conversion failed, no characters generated
    238                  eptr[0] != '\0' && ! isspaces( eptr ) ) throw ExceptionInst( invalid_argument ); // not at end of blank str ?
     157                 eptr[0] != '\0' && ! checkif( eptr, isblank ) ) throw ExceptionInst( invalid_argument ); // not at end of blank str ?
    239158        return val;
    240159} // convert
  • libcfa/src/stdlib.hfa

    r42422fb r8f650f0  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Mar 15 18:47:26 2024
    13 // Update Count     : 792
     12// Last Modified On : Sun Mar 17 08:25:31 2024
     13// Update Count     : 796
    1414//
    1515
     
    293293//---------------------------------------
    294294
    295 // Cannot overload with singular (isspace) counterparts because they are macros.
    296 
    297 bool isalnums( const char s[] );
    298 bool isalphas( const char s[] );
    299 bool iscntrls( const char s[] );
    300 bool isdigits( const char s[] );
    301 bool isgraphs( const char s[] );
    302 bool islowers( const char s[] );
    303 bool isprints( const char s[] );
    304 bool ispuncts( const char s[] );
    305 bool isspaces( const char s[] );
    306 bool isblanks( const char s[] );
    307 bool isuppers( const char s[] );
    308 bool isxdigits( const char s[] );
     295// Check if all string characters are a specific kind, e.g., checkif( s, isblank )
     296bool checkif( const char s[], int (* kind)( int ) );
     297bool checkif( const char s[], int (* kind)( int, locale_t ), locale_t locale );
    309298
    310299//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.