From ca4487d09b0cef56aa7be0e36782672703e447ef Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 19 Aug 2020 20:12:19 -0400 Subject: [PATCH] cwText.h/cpp : Added textAppend() and textJoin(). --- cwText.cpp | 51 +++++++++++++++++++++++++++++++++++++++++---------- cwText.h | 5 +++++ 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/cwText.cpp b/cwText.cpp index c17ebf4..7ec0a88 100644 --- a/cwText.cpp +++ b/cwText.cpp @@ -30,7 +30,23 @@ namespace cw return eosFl ? s : nullptr; } + /* + unsigned _toText( char* buf, unsigned bufN, unsigned char v ) + { + if( bufN < 1 ) + return 0; + buf[0] = v; + return 1; + } + unsigned _toText( char* buf, unsigned bufN, char v ) + { + if( bufN < 1 ) + return 0; + buf[0] = v; + return 1; + } + */ } @@ -71,22 +87,37 @@ const char* cw::nextNonWhiteCharEOS( const char* s ) { return _nextNonWhiteChar(s,true); } -unsigned toText( char* buf, unsigned bufN, unsigned char v ) +char* cw::textJoin( const char* s0, const char* s1 ) { - if( bufN < 1 ) - return 0; - buf[0] = v; - return 1; + if( s0 == nullptr && s1 == nullptr ) + return nullptr; + + unsigned s0n = textLength(s0); + unsigned s1n = textLength(s1); + unsigned sn = s0n + s1n + 1; + + char* s = mem::alloc(sn+1); + s[0] = 0; + + if( s0 != nullptr ) + strcpy(s,mem::duplStr(s0)); + + if( s0 != nullptr && s1 != nullptr ) + strcpy(s + strlen(s0), mem::duplStr(s1) ); + + return s; } -unsigned toText( char* buf, unsigned bufN, char v ) +char* cw::textAppend( char* s0, const char* s1 ) { - if( bufN < 1 ) - return 0; - buf[0] = v; - return 1; + if( s0 == nullptr && s1==nullptr) + return nullptr; + + return mem::appendStr(s0,s1); } + + unsigned cw::toText( char* buf, unsigned bufN, bool v ) { return toText( buf, bufN, v ? "true" : "false" ); } diff --git a/cwText.h b/cwText.h index d64a83e..5f5eeba 100644 --- a/cwText.h +++ b/cwText.h @@ -30,7 +30,12 @@ namespace cw // or nullptr if 's' is null. const char* nextNonWhiteCharEOS( const char* s ); + // Join s0 and s1 to form one long string. Release the returned string with mem::free() + char* textJoin( const char* s0, const char* s1 ); + // Realloc s0 and append s1. + char* textAppend( char* s0, const char* s1 ); + unsigned toText( char* buf, unsigned bufN, bool v ); unsigned toText( char* buf, unsigned bufN, unsigned char v ); unsigned toText( char* buf, unsigned bufN, char v );