From d4d325c41364a16c029dfaf26f5e1987a89341df Mon Sep 17 00:00:00 2001 From: kevin Date: Sun, 19 Nov 2023 15:03:42 -0500 Subject: [PATCH] cwText.h/cpp : Added firstMatchChar(0 and lastMatchChar(). --- cwText.cpp | 29 +++++++++++++++++++++++++++++ cwText.h | 5 +++++ 2 files changed, 34 insertions(+) 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