From 05d0c316f4f6b60b297c0d01b9b8e603f5cd0dd2 Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 19 Sep 2024 15:06:27 -0400 Subject: [PATCH] cwText.h/cpp : Added removeTrailingWhitespace() --- cwText.cpp | 24 ++++++++++++++++++++++++ cwText.h | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/cwText.cpp b/cwText.cpp index 2ae2f89..811a8de 100644 --- a/cwText.cpp +++ b/cwText.cpp @@ -185,6 +185,30 @@ const char* cw::lastMatchChar( const char* s, char c ) return lastMatchChar((char*)s,c); } +char* cw::removeTrailingWhitespace( char* s ) +{ + char* s0; + unsigned sn; + + if( s == nullptr ) + return nullptr; + + if((sn = textLength(s)) == 0 ) + return s; + + s0 = s + (sn-1); + + for(; s0>=s; --s0) + { + if( !isspace(*s0) ) + break; + *s0 = 0; + } + + return s; +} + + bool cw::isInteger( const char* s ) { for(; *s; ++s) diff --git a/cwText.h b/cwText.h index 89a8bf1..f749882 100644 --- a/cwText.h +++ b/cwText.h @@ -70,7 +70,9 @@ namespace cw // Find the last occurrent of 'c' in s[]. char* lastMatchChar( char* s, char c ); - const char* lastMatchChar( const char* s, char c ); + const char* lastMatchChar( const char* s, char c ); + + char* removeTrailingWhitespace( char* s ); bool isInteger( const char* ); // text contains only [0-9] bool isReal( const char* ); // text contains only [0-9] with one decimal place