From c0e20ad56b259434c2e1d9287d735fd5e9e49488 Mon Sep 17 00:00:00 2001 From: Torsten Dreyer Date: Thu, 12 Aug 2010 13:02:16 +0200 Subject: [PATCH] add padding function lpad and rpad to strutils --- simgear/misc/strutils.cxx | 18 ++++++++++++++++++ simgear/misc/strutils.hxx | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/simgear/misc/strutils.cxx b/simgear/misc/strutils.cxx index d33b0a9d..be05d692 100644 --- a/simgear/misc/strutils.cxx +++ b/simgear/misc/strutils.cxx @@ -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 diff --git a/simgear/misc/strutils.hxx b/simgear/misc/strutils.hxx index 162cdb28..ea44d654 100644 --- a/simgear/misc/strutils.hxx +++ b/simgear/misc/strutils.hxx @@ -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 -- 2.39.5