cwText.h/cpp : Added textAppend() and textJoin().

This commit is contained in:
kevin 2020-08-19 20:12:19 -04:00
parent bb479512ef
commit ca4487d09b
2 changed files with 46 additions and 10 deletions

View File

@ -30,7 +30,23 @@ namespace cw
return eosFl ? s : nullptr; 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); } { 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 ) if( s0 == nullptr && s1 == nullptr )
return 0; return nullptr;
buf[0] = v;
return 1; unsigned s0n = textLength(s0);
unsigned s1n = textLength(s1);
unsigned sn = s0n + s1n + 1;
char* s = mem::alloc<char>(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 ) if( s0 == nullptr && s1==nullptr)
return 0; return nullptr;
buf[0] = v;
return 1; return mem::appendStr(s0,s1);
} }
unsigned cw::toText( char* buf, unsigned bufN, bool v ) unsigned cw::toText( char* buf, unsigned bufN, bool v )
{ return toText( buf, bufN, v ? "true" : "false" ); } { return toText( buf, bufN, v ? "true" : "false" ); }

View File

@ -30,6 +30,11 @@ namespace cw
// or nullptr if 's' is null. // or nullptr if 's' is null.
const char* nextNonWhiteCharEOS( const char* s ); 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, bool v );
unsigned toText( char* buf, unsigned bufN, unsigned char v ); unsigned toText( char* buf, unsigned bufN, unsigned char v );