From a3012db1653b1f247b93169d2244fe5b7741c6df Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 22 Apr 2024 15:56:11 -0400 Subject: [PATCH] cwText.h/cpp : Added first/lastMatchChar() for non-const strings. --- cwText.cpp | 16 +++++++++++++--- cwText.h | 3 +++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cwText.cpp b/cwText.cpp index f6b4e7a..2ae2f89 100644 --- a/cwText.cpp +++ b/cwText.cpp @@ -146,7 +146,7 @@ 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 ) +char* cw::firstMatchChar( char* s, char c ) { if( s == nullptr ) return nullptr; @@ -157,7 +157,12 @@ const char* cw::firstMatchChar( const char* s, char c ) return nullptr; } -const char* cw::lastMatchChar( const char* s, char c ) +const char* cw::firstMatchChar( const char* s, char c ) +{ + return firstMatchChar((char*)s,c); +} + +char* cw::lastMatchChar( char* s, char c ) { unsigned sn; @@ -168,13 +173,18 @@ const char* cw::lastMatchChar( const char* s, char c ) if( sn == 0 ) return nullptr; - for(const char* s1=s+(sn-1); s<=s1; --s1) + for(char* s1=s+(sn-1); s<=s1; --s1) if( *s1 == c ) return s1; return nullptr; } +const char* cw::lastMatchChar( const char* s, char c ) +{ + return lastMatchChar((char*)s,c); +} + bool cw::isInteger( const char* s ) { for(; *s; ++s) diff --git a/cwText.h b/cwText.h index cc44f06..9c4b7de 100644 --- a/cwText.h +++ b/cwText.h @@ -65,8 +65,11 @@ namespace cw // Return a pointer to the first occurrence of 'c' in s[] or nullptr // if 'c' does not occur in s[] + char* firstMatchChar( char* s, char c ); const char* firstMatchChar( const char* s, char c ); + // Find the last occurrent of 'c' in s[]. + char* lastMatchChar( char* s, char c ); const char* lastMatchChar( const char* s, char c ); bool isInteger( const char* ); // text contains only [0-9]