📄 cs_string.cpp
字号:
char buffer [ 2049 ];
va_list args;
va_start ( args, fmt );
int len = _vsnprintf ( buffer, 2048, fmt, args );
va_end ( args );
buffer [ 2048 ] = 0;
if ( len > 0 )
{
set_length ( len );
::memcpy ( chars(), buffer, len );
}
else
clear();
return *this;
}
string string::format ( const char *fmt, ... )
{
char buffer [ 2049 ];
va_list args;
va_start ( args, fmt );
int len = _vsnprintf( buffer, 2048, fmt, args );
va_end ( args );
buffer [ 2048 ] = 0;
return buffer;
}
/****************************************************************************/
string &
string::to_upper ()
{
make_unique();
#ifdef _WIN32
_strupr ( chars() );
#else
for ( register char *p = chars(); *p; p++ )
*p = toupper ( *p );
#endif
return *this;
}
/****************************************************************************/
string &
string::to_lower ()
{
make_unique();
#ifdef _WIN32
_strlwr ( chars() );
#else
for ( register char *p = chars(); *p; p++ )
*p = tolower ( *p );
#endif
return *this;
}
/****************************************************************************/
int
string::index_of ( const char *s, int start_index ) const
{
// a negative index specifies an index from the right of the string.
if ( start_index < 0 )
start_index += length();
if ( start_index < 0 || start_index >= length() )
return -1;
//invalid_index_error("index_of()");
const char *index;
if ( !( index = strstr ( &chars() [ start_index ], s ) ) )
return -1;
else
return index - chars();
}
/****************************************************************************/
int
string::index_of ( char c, int start_index ) const
{
// a negative index specifies an index from the right of the string.
if ( start_index < 0 )
start_index += length();
if ( start_index < 0 || start_index >= length() )
return -1;
const char *index;
if ( c == '\0' )
return -1;
else if ( !( index = (char *) ::memchr ( &chars() [ start_index ], c,
length() - start_index ) ) )
return -1;
else
return index - chars();
}
/****************************************************************************/
int string::last_index_of ( char c, int start_index ) const
{
// a negative index specifies an index from the right of the string.
if ( start_index < 0 )
start_index = length() - 1;
if ( start_index < 0 || start_index >= length() )
return -1;
if ( c == '\0' )
return -1;
const char *p = chars();
for ( int i = start_index; i >= 0; i-- )
if( p [ i ] == c )
return i;
return -1;
}
#ifndef NO_ARRAY
array<string>
string::tokens ( const char *separators ) const
{
array<string> list;
int token_length, index = 0;
do
{
index += ::strspn ( &chars() [ index ], separators );
token_length = ::strcspn ( &chars() [ index ], separators );
if ( token_length > 0 )
list.push ( substr ( index, token_length ) );
index += token_length;
}
while ( token_length > 0 );
return list;
}
#endif
/****************************************************************************/
#ifndef NO_ARRAY
array<string>
string::tokens ( char separator ) const
{
char separators [ 2 ];
separators [ 0 ] = separator;
separators [ 1 ] = '\0';
return tokens ( separators );
}
#endif
/****************************************************************************/
bool
string::read_until ( istream &stream, const char *separators )
{
const int num_of_separators = ::strlen ( separators );
char buffer [ 256 ];
bool found_end;
char c;
set_length ( 0 );
if ( stream.eof() )
return false;
do
{
unsigned int i = 0;
do
{
stream.get ( c );
found_end = !stream || ::memchr ( separators, c, num_of_separators );
if ( !found_end && i < sizeof ( buffer ) - 1 )
buffer [ i++ ] = c;
}
while ( !found_end && i < sizeof ( buffer ) - 1 );
buffer [ i ] = '\0';
*this += buffer;
}
while ( !found_end );
if ( stream )
stream.putback ( c );
return true;
}
/****************************************************************************/
bool
string::read_token ( istream &stream, const char *separators )
{
return read_until ( stream, separators );
}
/****************************************************************************/
int
string::replace ( const char *from, const char *to )
{
int to_length = strlen ( to );
int from_length = strlen ( from );
int count = 0, idx = 0;
while ( true )
{
idx = index_of ( from, idx ); // + to_length
if ( idx < 0 )
break;
if ( to_length )
replace_substr ( to, idx, from_length );
else
cut ( idx, from_length );
++count;
idx += to_length;
}
return count;
}
/****************************************************************************/
//
// idea was taken from Konstantin Knizhnik's FastDB
// see http://www.garret.ru/
// extended by [] operations
//
const char AnySubstring = '*';
const char AnyOneChar = '?';
int
string::match ( const char *pattern ) const
{
struct charset
{
bool codes [ 0x100 ];
void set ( int from, int to, bool v )
{
for ( int i = from;
i <= to;
i++)
codes[i]=v;
}
void parse ( const char **pp )
{
const unsigned char *p = (const unsigned char *) *pp;
bool inv = *p == '^';
if ( inv )
{
++p;
}
set ( 0, 0xff, inv );
if ( *p == '-' )
codes [ '-' ] = !inv;
while ( *p )
{
if ( p [ 0 ] == ']' )
{
p++;
break;
}
if ( p [ 1 ] == '-' && p [ 2 ] != 0 )
{
set ( p [ 0 ], p [ 2 ], !inv );
p += 3;
}
else
codes [ *p++ ] = !inv;
}
*pp = (const char *) p;
}
bool valid ( unsigned char c )
{
return codes [ c ];
}
};
const char *str = chars();
const char *wildcard = 0;
const char *strpos = 0;
const char *matchpos = 0;
charset cset;
while ( true )
{
if ( *pattern == AnySubstring )
{
wildcard = ++pattern;
strpos = str;
if ( !matchpos )
matchpos = str;
}
else if ( *str == '\0' )
{
return ( *pattern == '\0' ) ? ( matchpos - chars() ) : -1;
}
else if ( *pattern == '[' )
{
cset.parse ( &pattern );
if ( !cset.valid ( (unsigned char) *str ) )
return -1;
if ( !matchpos )
matchpos = str;
str += 1;
}
else if ( *str == *pattern || *pattern == AnyOneChar )
{
if ( !matchpos )
matchpos = str;
str += 1;
pattern += 1;
}
else if ( wildcard )
{
str = ++strpos;
pattern = wildcard;
}
else
{
break;
}
}
return -1;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -