diff --git a/cwText.cpp b/cwText.cpp index d9009df..4ed9a69 100644 --- a/cwText.cpp +++ b/cwText.cpp @@ -82,6 +82,35 @@ const char* cw::nextNonWhiteChar( const char* s ) const char* cw::nextNonWhiteCharEOS( const char* s ) { return _nextNonWhiteChar(s,true); } +const char* cw::firstMatchChar( const char* s, char c ) +{ + if( s == nullptr ) + return nullptr; + + for(; *s; ++s) + if(*s == c) + return s; + return nullptr; +} + +const char* cw::lastMatchChar( const char* s, char c ) +{ + unsigned sn; + + if( s == nullptr ) + return nullptr; + + sn = textLength(s); + if( sn == 0 ) + return nullptr; + + for(const char* s1=s+(sn-1); s<=s1; --s1) + if( *s1 == c ) + return s1; + + return nullptr; +} + bool cw::isInteger( const char* s ) { for(; *s; ++s) diff --git a/cwText.h b/cwText.h index 081ee21..64f579d 100644 --- a/cwText.h +++ b/cwText.h @@ -36,6 +36,11 @@ namespace cw // or nullptr if 's' is null. const char* nextNonWhiteCharEOS( const char* s ); + // Return a pointer to the first occurrence of 'c' in s[] or nullptr + // if 'c' does not occur in s[] + const char* firstMatchChar( const char* s, char c ); + // Find the last occurrent of 'c' in s[]. + const char* lastMatchChar( const char* s, char c ); bool isInteger( const char* ); // text contains only [0-9] bool isReal( const char* ); // text contains only [0-9] with one decimal place