~ubuntu-branches/ubuntu/raring/openwalnut/raring

« back to all changes in this revision

Viewing changes to src/core/common/WStringUtils.h

  • Committer: Package Import Robot
  • Author(s): Sebastian Eichelbaum
  • Date: 2012-12-12 11:26:32 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20121212112632-xhiuwkxuz5h0idkh
Tags: 1.3.1+hg5849-1
* Minor changes compared to 1.3.0 but included several bug fixes.
* See http://www.openwalnut.org/versions/4

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
#include <string>
36
36
#include <vector>
37
37
 
38
 
#include <boost/lexical_cast.hpp>
39
 
 
40
 
#include "WExportCommon.h"
 
38
#include "exceptions/WTypeMismatch.h"
41
39
 
42
40
/**
43
41
 * Some utilities for string manipulation and output operations. Please note
59
57
 */
60
58
namespace string_utils
61
59
{
 
60
    /**
 
61
     * Conversion class to convert a string to a given target type. We place this in separate classes as we are not allowed to specialize
 
62
     * function templates. But we need specializations for certain cases.
 
63
     *
 
64
     * \tparam Target target type
 
65
     */
 
66
    template< typename Target >
 
67
    struct fromStringImpl
 
68
    {
 
69
        /**
 
70
         * Convert a given string to the target type. If this fails, a exception is thrown.
 
71
         *
 
72
         * \throw WTypeMismatch if source string cannot be converted to target type value.
 
73
         *
 
74
         * \param from source string
 
75
         *
 
76
         * \return target value
 
77
         */
 
78
        static Target fromString( const std::string& from )
 
79
        {
 
80
            std::stringstream ss( from );
 
81
            Target value;
 
82
            ss >> value;
 
83
            if( ss.fail() )
 
84
            {
 
85
                throw WTypeMismatch( "Specified string could not be converted to target type." );
 
86
            }
 
87
 
 
88
            return value;
 
89
        }
 
90
    };
 
91
 
 
92
    /**
 
93
     * Conversion class to convert a string to a given target type. This is a specialization whenever a string is given as input type
 
94
     */
 
95
    template<>
 
96
    struct fromStringImpl< std::string >
 
97
    {
 
98
        /**
 
99
         * Convert a given string to the target type. Never fails.
 
100
         *
 
101
         * \param from source string
 
102
         *
 
103
         * \return copy of the source string
 
104
         */
 
105
        static std::string fromString( const std::string& from )
 
106
        {
 
107
            return from;
 
108
        }
 
109
    };
 
110
 
 
111
    /**
 
112
     * Convert a given value to a string. The input value must provide a operator<< or be a standard scalar type.
 
113
     *
 
114
     * \tparam T the source type. You do not need to specify this directly as it can be deducted from the given parameter
 
115
     * \param value the value to cast to string
 
116
     *
 
117
     * \return the string.
 
118
     */
 
119
    template< typename T >
 
120
    inline std::string toString( const T& value )
 
121
    {
 
122
        std::stringstream ss;
 
123
        ss << value;
 
124
        return ss.str();
 
125
    }
 
126
 
 
127
    /**
 
128
     * Convert a given value to a string. The input value must provide a operator<< or be a standard scalar type.
 
129
     *
 
130
     * \param value the value to cast to string
 
131
     *
 
132
     * \return the string.
 
133
     */
 
134
    inline std::string toString( const unsigned char& value )   // NOLINT: stylechecker complains about non const ref!?
 
135
    {
 
136
        std::stringstream ss;
 
137
        // NOTE: unsigned chars are interpreted as ASCII chars. We want it to be used as number.
 
138
        ss << static_cast< int >( value );
 
139
        return ss.str();
 
140
    }
 
141
 
 
142
    /**
 
143
     * Convert a given value to a string. The input value must provide a operator<< or be a standard scalar type. This method additionally allows
 
144
     * setting width and precision flags of the used std::stringstream.
 
145
     *
 
146
     * \tparam T the source type. You do not need to specify this directly as it can be deducted from the given parameter
 
147
     * \param value the value to cast to string
 
148
     * \param precision the precision
 
149
     * \param width the width
 
150
     *
 
151
     * \return the string.
 
152
     */
 
153
    template< typename T >
 
154
    inline std::string toString( const T& value, const size_t width, const size_t precision )
 
155
    {
 
156
        std::stringstream ss;
 
157
        ss.width( width );
 
158
        ss.precision( precision );
 
159
        ss << value;
 
160
        return ss.str();
 
161
    }
 
162
 
 
163
    /**
 
164
     * Convert a given string to a value of a certain type. The target type must provide a operator>> to work or be a standard scalar type.
 
165
     *
 
166
     * \tparam T the source type.
 
167
     * \param str the value to cast to string
 
168
     *
 
169
     * \throw WTypeMismatch if the string cannot be converted properly.
 
170
     * \return the string.
 
171
     */
 
172
    template< typename T >
 
173
    inline T fromString( const std::string& str )
 
174
    {
 
175
        return fromStringImpl< T >::fromString( str );
 
176
    }
 
177
 
62
178
    /** We consider the following characters as whitespace:
63
179
     *  - <tt>\\r</tt> carriage return
64
180
     *  - <tt>\\n</tt> newline
65
181
     *  - <tt>\\t</tt> tab
66
182
     *  - <tt>' '</tt> space
67
183
     */
68
 
    extern OWCOMMON_EXPORT const std::string WHITESPACE;
 
184
    extern const std::string WHITESPACE;
69
185
 
70
186
    /**
71
187
     * Trims any occurence of each character given in parameter t from the end
76
192
     * \return A copy of the trimmed string
77
193
     */
78
194
 
79
 
    std::string OWCOMMON_EXPORT rTrim( const std::string& source, const std::string& t = WHITESPACE );
 
195
    std::string rTrim( const std::string& source, const std::string& t = WHITESPACE );
80
196
 
81
197
    /**
82
198
     * Trims any occurence of each character given in parameter t from the
86
202
     * \param t String representing a set containg all trimmable characters
87
203
     * \return A copy of the trimmed string
88
204
     */
89
 
    std::string OWCOMMON_EXPORT lTrim( const std::string& source, const std::string& t =
 
205
    std::string lTrim( const std::string& source, const std::string& t =
90
206
            WHITESPACE );
91
207
 
92
208
    /**
97
213
     * \param t String representing a set containg all trimmable characters
98
214
     * \return A copy of the trimmed string
99
215
     */
100
 
    std::string OWCOMMON_EXPORT trim( const std::string& source, const std::string& t = WHITESPACE );
 
216
    std::string trim( const std::string& source, const std::string& t = WHITESPACE );
101
217
 
102
218
    /**
103
219
     * Transforms all characters in the given string into upper case
106
222
     * \param source String to transpose.
107
223
     * \return A copy of the upper case only string
108
224
     */
109
 
    std::string OWCOMMON_EXPORT toUpper( const std::string& source );
 
225
    std::string toUpper( const std::string& source );
110
226
 
111
227
    /**
112
228
     * Transforms all characters in the given string into lower case
115
231
     * \param source String to transpose.
116
232
     * \return A copy of the lower case only string
117
233
     */
118
 
    std::string OWCOMMON_EXPORT toLower( const std::string& source );
 
234
    std::string toLower( const std::string& source );
119
235
 
120
236
    /**
121
237
     * Splits the given string into a vector of strings (so called tokens).
122
238
     *
123
239
     * \param source String to tokenize
124
 
     * \param compress If true, charactes matching between two tokens are
 
240
     * \param compress If true, characters matching between two tokens are
125
241
     * collapsed and handled as just one character.
126
 
     * \param delim String representing a set containg all characters considered
 
242
     * \param delim String representing a set containing all characters considered
127
243
     * as whitespace.
128
244
     * \return A vector of strings containing the tokens.
129
245
     */
130
 
    std::vector< std::string > OWCOMMON_EXPORT tokenize( const std::string& source,
131
 
                                                       const std::string& delim = WHITESPACE,
132
 
                                                       bool compress = true );
 
246
    std::vector< std::string > tokenize( const std::string& source,
 
247
                                         const std::string& delim = WHITESPACE,
 
248
                                         bool compress = true );
133
249
 
134
250
    /**
135
251
     * Writes every vector to an output stream such as cout, if its elements
153
269
     * Also wrapping brackets '[' ']' are expected. In general this is the opposite of the
154
270
     * output operator above.
155
271
     * \warning The inputstream is first written into a string then the convertion into T
156
 
     * via boost::lexical_cast takes place.
 
272
     * via fromString takes place.
157
273
     * \warning The delimiter should not be in an elements string representation since then
158
274
     * the tokenizer may gets confused
159
275
     *
172
288
        v.reserve( tokens.size() );
173
289
        for( size_t i = 0; i < tokens.size(); ++i )
174
290
        {
175
 
            v.push_back( boost::lexical_cast< T >( tokens[i] ) );
 
291
            v.push_back( fromString< T >( tokens[i] ) );
176
292
        }
177
293
        return in;
178
294
    }