cwText.h/cpp : Added nextNonWhiteChar() functions and toText() functions.

This commit is contained in:
kpl 2020-03-23 10:42:16 -04:00
parent 5cda838a93
commit bcb6b37df4
2 changed files with 133 additions and 0 deletions

View File

@ -4,6 +4,36 @@
#include "cwCommonImpl.h" #include "cwCommonImpl.h"
#include "cwMem.h" #include "cwMem.h"
namespace cw
{
const char* _nextWhiteChar( const char* s, bool eosFl )
{
if( s == nullptr )
return nullptr;
for(; *s; ++s )
if( isspace(*s) )
return s;
return eosFl ? s : nullptr;
}
const char* _nextNonWhiteChar( const char* s, bool eosFl )
{
if( s == nullptr )
return nullptr;
for(; *s; ++s )
if( !isspace(*s) )
return s;
return eosFl ? s : nullptr;
}
}
unsigned cw::textLength( const char* s ) unsigned cw::textLength( const char* s )
{ return s == nullptr ? 0 : strlen(s); } { return s == nullptr ? 0 : strlen(s); }
@ -23,3 +53,70 @@ int cw::textCompare( const char* s0, const char* s1, unsigned n)
return strncmp(s0,s1,n); return strncmp(s0,s1,n);
} }
const char* cw::nextWhiteChar( const char* s )
{ return _nextWhiteChar(s,false); }
const char* cw::nextWhiteCharEOS( const char* s )
{ return _nextWhiteChar(s,true); }
const char* cw::nextNonWhiteChar( const char* s )
{ return _nextNonWhiteChar(s,false); }
const char* cw::nextNonWhiteCharEOS( const char* s )
{ return _nextNonWhiteChar(s,true); }
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;
}
unsigned cw::toText( char* buf, unsigned bufN, bool v )
{ return toText( buf, bufN, v ? "true" : "failse" ); }
unsigned cw::toText( char* buf, unsigned bufN, unsigned short v )
{ return snprintf(buf,bufN,"%i",v); }
unsigned cw::toText( char* buf, unsigned bufN, short v )
{ return snprintf(buf,bufN,"%i",v); }
unsigned cw::toText( char* buf, unsigned bufN, unsigned int v )
{ return snprintf(buf,bufN,"%i",v); }
unsigned cw::toText( char* buf, unsigned bufN, int v )
{ return snprintf(buf,bufN,"%i",v); }
unsigned cw::toText( char* buf, unsigned bufN, float v )
{ return snprintf(buf,bufN,"%f",v); }
unsigned cw::toText( char* buf, unsigned bufN, double v )
{ return snprintf(buf,bufN,"%f",v); }
unsigned cw::toText( char* buf, unsigned bufN, const char* v )
{
unsigned sn = strlen(v) + 1;
// bufN must be greater than the length of v[]
if( sn >= bufN )
return 0;
strncpy(buf,v,sn);
return sn-1;
}

View File

@ -4,11 +4,47 @@
namespace cw namespace cw
{ {
// Return 0 if s is null.
unsigned textLength( const char* s ); unsigned textLength( const char* s );
// if both s0 and s1 are nullptr then a match is indicated // if both s0 and s1 are nullptr then a match is indicated
int textCompare( const char* s0, const char* s1 ); int textCompare( const char* s0, const char* s1 );
int textCompare( const char* s0, const char* s1, unsigned n); int textCompare( const char* s0, const char* s1, unsigned n);
// Return a pointer to the next white space char
// or nullptr if 's' is null are there are no whitespace char's.
const char* nextWhiteChar( const char* s );
// Return a pointer to the next white space char,
// a pointer to the EOS if there are no white space char's,
// or nullptr if 's' is null.
const char* nextWhiteCharEOS( const char* s );
// Return a pointer to the next non-white space char
// or nullptr if 's' is null are there are no non-whitespace char's.
const char* nextNonWhiteChar( const char* s );
// Return a pointer to the next non-white space char,
// a pointer to the EOS if there are no non-white space char's,
// or nullptr if 's' is null.
const char* nextNonWhiteCharEOS( const char* s );
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 );
unsigned toText( char* buf, unsigned bufN, unsigned short v );
unsigned toText( char* buf, unsigned bufN, short v );
unsigned toText( char* buf, unsigned bufN, unsigned int v );
unsigned toText( char* buf, unsigned bufN, int v );
unsigned toText( char* buf, unsigned bufN, float v );
unsigned toText( char* buf, unsigned bufN, double v );
unsigned toText( char* buf, unsigned bufN, const char* v );
} }
#endif #endif