add padding function lpad and rpad to strutils

This commit is contained in:
Torsten Dreyer
2010-08-12 13:02:16 +02:00
parent 49887ff06e
commit c0e20ad56b
2 changed files with 36 additions and 0 deletions

View File

@@ -183,5 +183,23 @@ namespace simgear {
return do_strip( s, BOTHSTRIP );
}
string
rpad( const string & s, string::size_type length, char c )
{
string::size_type l = s.length();
if( l >= length ) return s;
string reply = s;
return reply.append( length-l, c );
}
string
lpad( const string & s, size_t length, char c )
{
string::size_type l = s.length();
if( l >= length ) return s;
string reply = s;
return reply.insert( 0, length-l, c );
}
} // end namespace strutils
} // end namespace simgear

View File

@@ -64,6 +64,24 @@ namespace simgear {
std::string rstrip( const std::string& s );
std::string strip( const std::string& s );
/**
* Right-padding of a string to a given length
* @param s String to pad
* @param length The total length of the resulting string
* @param c The character to pad with
* @return The padded string
*/
std::string rpad( const std::string & s, size_t length, char c );
/**
* Left-padding of a string to a given length
* @param s String to pad
* @param length The total length of the resulting string
* @param c The character to pad with
* @return The padded string
*/
std::string lpad( const std::string & s, size_t length, char c );
/**
* Split a string into a words using 'sep' as the delimiter string.
* Produces a result similar to the perl and python functions of the