From 7e6ab875c962b04dae5427e8597921ab12f5c56e Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 25 Jul 2023 20:20:45 -0400 Subject: [PATCH] cwText.h/cpp : Added isInteger(), isReal(), isIdentifier(). --- cwText.cpp | 38 ++++++++++++++++++++++++++++++++++++++ cwText.h | 7 ++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/cwText.cpp b/cwText.cpp index eebaf8e..d9009df 100644 --- a/cwText.cpp +++ b/cwText.cpp @@ -82,6 +82,44 @@ const char* cw::nextNonWhiteChar( const char* s ) const char* cw::nextNonWhiteCharEOS( const char* s ) { return _nextNonWhiteChar(s,true); } +bool cw::isInteger( const char* s ) +{ + for(; *s; ++s) + if(!isdigit(*s)) + return false; + return true; +} + +bool cw::isReal( const char* s) +{ + unsigned decN = 0; + for(; *s; ++s) + if( *s == '.' ) + { + if( ++decN > 1) + return false; + } + else + { + if(!isdigit(*s)) + return false; + } + + return true; +} + +bool cw::isIdentifier( const char* s ) +{ + if( !isalpha(*s) && *s != '_' ) + return false; + + for(++s; *s; ++s) + if( !isalnum(*s) && *s != '_' ) + return false; + + return true; +} + char* cw::textJoin( const char* s0, const char* s1 ) { diff --git a/cwText.h b/cwText.h index 5adb82c..081ee21 100644 --- a/cwText.h +++ b/cwText.h @@ -7,7 +7,7 @@ namespace cw // Return 0 if s is null. unsigned textLength( const char* s ); - // if both s0 and s1 are nullptr then a match is indicated + // Note: if both s0 and s1 are nullptr then a match is indicated int textCompare( const char* s0, const char* s1 ); int textCompare( const char* s0, const char* s1, unsigned n); @@ -36,6 +36,11 @@ namespace cw // or nullptr if 's' is null. const char* nextNonWhiteCharEOS( const char* s ); + + bool isInteger( const char* ); // text contains only [0-9] + bool isReal( const char* ); // text contains only [0-9] with one decimal place + bool isIdentifier( const char* ); // text is a legal id [0-9,A-Z,a-z,_] w/o leading number + // Join s0 and s1 to form one long string. Release the returned string with mem::free() char* textJoin( const char* s0, const char* s1 );