1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Michael Andres <ma@suse.de>
16 Maintainer: Michael Andres <ma@suse.de>
18 Purpose: Contains 'std::string form(const char * format, ...)' for
19 printf style creation of strings and some more string utility
36 * Utility functions for std::strings. Most of them based on stringutil::form.
38 ///////////////////////////////////////////////////////////////////
39 namespace stringutil {
40 ;//////////////////////////////////////////////////////////////////
42 /** \brief read one line from a stream
44 * like above but with allows to specify trimming direction
46 extern std::string getline( std::istream & str, const Trim trim_r );
49 * Split line into words
51 * <b>singlesep_r = false</b>: Separator is any nonenmpty sequence of characters listed in sep_t.
52 * Leading trailing separators are ignored.
54 * <b>Example:</b> singlesep_r = false, sep_t = ":"
59 * "::a" -> words 1 |a|
60 * "::a::" -> words 1 |a|
61 * ":a::b:c:"-> words 3 |a|b|c|
64 * <b>singlesep_r = true</b>: Separator is any single character occuring in sep_t.
65 * Leading trailing separators are not ignored (i.e will cause an empty word).
67 * <b>Example:</b> singlesep_r = true, sep_t = ":"
72 * ":a" -> words 2 ||a|
73 * "a:" -> words 2 |a||
74 * ":a:" -> words 3 ||a||
78 extern unsigned split( const std::string line_r,
79 std::vector<std::string> & words_r,
80 const std::string & sep_t = " \t",
81 const bool singlesep_r = false );
84 * Join strinngs in words_r using separator sep_r
86 extern std::string join( const std::vector<std::string> & words_r,
87 const std::string & sep_r = " " );
91 * Split string into a list of lines using <b>any<\b> char in sep_r as line
92 * delimiter. The delimiter is stripped from the line.
95 * splitToLines( "start\n\nend" ) -> { "start", "", "end" }
98 inline std::list<std::string> splitToLines( const std::string text_r, const std::string & sep_r = "\n" )
100 std::vector<std::string> lines;
101 stringutil::split( text_r, lines, "\n", true );
102 std::list<std::string> ret;
103 for ( unsigned i = 0; i < lines.size(); ++i ) {
104 ret.push_back( lines[i] );
110 * Strip the first word (delimited by blank or tab) from value, and return it.
111 * Adjust value to start with the second word afterwards.
113 * If value starts with blank or tab, the <b>first word is empty</b> and value will be
114 * ltrimmed afterwards.
116 * If ltrim_first is true, value will be ltrimmed before stripping the first word. Thus
117 * first word is empty, iff value is empty or contains whitespace only.
120 * stripFirstWord( "1st" ) == "1st" and value truncated to ""
121 * stripFirstWord( "1st word" ) == "1st" and value truncated to "word"
122 * stripFirstWord( " 1st word" ) == "" and value truncated to "1st word"
123 * stripFirstWord( " 1st word", true ) == "1st" and value truncated to "word"
126 extern std::string stripFirstWord( std::string & value, const bool ltrim_first = false );
129 * Return string with leading/trailing/surrounding whitespace removed
131 extern std::string ltrim( const std::string & s );
132 extern std::string rtrim( const std::string & s );
133 inline std::string trim( const std::string & s, const Trim trim_r = TRIM ) {
140 return ltrim( rtrim( s ) );
147 ///////////////////////////////////////////////////////////////////
148 } // namespace stringutil
149 ///////////////////////////////////////////////////////////////////
151 #endif // stringutil_h