- added ByteCount and Unit
[platform/upstream/libzypp.git] / zypp / @Review / stringutil.h
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       stringutil.h
14
15   Author:     Michael Andres <ma@suse.de>
16   Maintainer: Michael Andres <ma@suse.de>
17
18   Purpose: Contains 'std::string form(const char * format, ...)' for
19   printf style creation of strings and some more string utility
20   functions.
21
22 /-*/
23
24 #ifndef stringutil_h
25 #define stringutil_h
26
27 #include <cstdio>
28 #include <cstdarg>
29
30 #include <iosfwd>
31 #include <vector>
32 #include <string>
33 #include <list>
34
35 /**
36  * Utility functions for std::strings. Most of them based on stringutil::form.
37  **/
38 ///////////////////////////////////////////////////////////////////
39 namespace stringutil {
40 ;//////////////////////////////////////////////////////////////////
41
42 /** \brief read one line from a stream
43  *
44  * like above but with allows to specify trimming direction
45  * */
46 extern std::string getline( std::istream & str, const Trim trim_r );
47
48 /**
49  * Split line into words
50  *
51  * <b>singlesep_r = false</b>: Separator is any nonenmpty sequence of characters listed in sep_t.
52  * Leading trailing separators are ignored.
53  *
54  * <b>Example:</b> singlesep_r = false, sep_t = ":"
55  * <PRE>
56  * ""        -> words 0
57  * ":"       -> words 0
58  * "a"       -> words 1  |a|
59  * "::a"     -> words 1  |a|
60  * "::a::"   -> words 1  |a|
61  * ":a::b:c:"-> words 3  |a|b|c|
62  * </PRE>
63  *
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).
66  *
67  * <b>Example:</b> singlesep_r = true, sep_t = ":"
68  * <PRE>
69  * ""        -> words 0
70  * ":"       -> words 2  |||
71  * "a"       -> words 1  |a|
72  * ":a"      -> words 2  ||a|
73  * "a:"      -> words 2  |a||
74  * ":a:"     -> words 3  ||a||
75  * </PRE>
76  *
77  **/
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 );
82
83 /**
84  * Join strinngs in words_r using separator sep_r
85  **/
86 extern std::string join( const std::vector<std::string> & words_r,
87                          const std::string & sep_r = " " );
88
89
90 /**
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.
93  *
94  * <PRE>
95  * splitToLines( "start\n\nend" ) -> { "start", "", "end" }
96  * </PRE>
97  **/
98 inline std::list<std::string> splitToLines( const std::string text_r, const std::string & sep_r = "\n" )
99 {
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] );
105   }
106   return ret;
107 }
108
109 /**
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.
112  *
113  * If value starts with blank or tab, the <b>first word is empty</b> and value will be
114  * ltrimmed afterwards.
115  *
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.
118  *
119  * <PRE>
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"
124  * </PRE>
125  **/
126 extern std::string stripFirstWord( std::string & value, const bool ltrim_first = false );
127
128 /**
129  * Return string with leading/trailing/surrounding whitespace removed
130  **/
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 ) {
134   switch ( trim_r ) {
135   case L_TRIM:
136     return ltrim( s );
137   case R_TRIM:
138     return rtrim( s );
139   case TRIM:
140     return ltrim( rtrim( s ) );
141   case NO_TRIM:
142     break;
143   }
144   return s;
145 }
146
147 ///////////////////////////////////////////////////////////////////
148 }  // namespace stringutil
149 ///////////////////////////////////////////////////////////////////
150
151 #endif // stringutil_h