~ubuntu-branches/ubuntu/wily/opencollada/wily-proposed

« back to all changes in this revision

Viewing changes to Externals/MathMLSolver/src/MathMLString.cpp

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2015-05-14 17:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20150514172327-f862u8envms01fra
Tags: upstream-0.1.0~20140703.ddf8f47+dfsg1
ImportĀ upstreamĀ versionĀ 0.1.0~20140703.ddf8f47+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "MathMLSolverStableHeaders.h"
 
2
#include "MathMLString.h"
 
3
 
 
4
#ifdef _WIN32
 
5
# include <windows.h>
 
6
#endif
 
7
 
 
8
namespace MathML
 
9
{
 
10
    //-----------------------------------------------------------------------
 
11
    String::size_type StringUtil::lastIndexOf( const String& str, const String& pattern )
 
12
    {
 
13
        String::size_type pos = String::npos;
 
14
 
 
15
        for ( String::size_type i = str.find( pattern );
 
16
                i != std::string::npos;
 
17
                i = str.find( pattern, i + 1 )
 
18
            )
 
19
        {
 
20
            pos = i;
 
21
        }
 
22
 
 
23
        return pos;
 
24
    }
 
25
 
 
26
    //-----------------------------------------------------------------------
 
27
    String::size_type StringUtil::lastIndexOf( const String& str, const char character )
 
28
    {
 
29
        String::size_type pos = String::npos;
 
30
 
 
31
        for ( String::size_type i = str.find( character );
 
32
                i != std::string::npos;
 
33
                i = str.find( character, i + 1 )
 
34
            )
 
35
        {
 
36
            pos = i;
 
37
        }
 
38
 
 
39
        return pos;
 
40
    }
 
41
 
 
42
    //-----------------------------------------------------------------------
 
43
    bool StringUtil::startsWith( const String& str, const String& pattern )
 
44
    {
 
45
        String::size_type pos = str.find( pattern );
 
46
        return pos == 0 ? true : false;
 
47
    }
 
48
 
 
49
    //-----------------------------------------------------------------------
 
50
    bool StringUtil::startsWith( const String& str, char character )
 
51
    {
 
52
        String::size_type pos = str.find( character );
 
53
        return pos == 0 ? true : false;
 
54
    }
 
55
 
 
56
    //-----------------------------------------------------------------------
 
57
    bool StringUtil::endsWith( const String& str, const String& pattern )
 
58
    {
 
59
        String::size_type pos = str.find( pattern );
 
60
 
 
61
        if ( pos == String::npos )
 
62
            return false;
 
63
 
 
64
        return pos == ( str.size() - pattern.size() ) ? true : false;
 
65
    }
 
66
 
 
67
    //-----------------------------------------------------------------------
 
68
    bool StringUtil::endsWith( const String& str, char character )
 
69
    {
 
70
        String::size_type pos = str.find( character );
 
71
        return pos == ( str.size() - 1 ) ? true : false;
 
72
    }
 
73
 
 
74
    //-----------------------------------------------------------------
 
75
    String StringUtil::replaceAll( const String& str, char character, const String& replacement )
 
76
    {
 
77
        String string = str;
 
78
        String::size_type position = string.find_last_of( character );
 
79
 
 
80
        while ( position != String::npos )
 
81
        {
 
82
            string.replace( position, 1, replacement );
 
83
            position = string.find_last_of( character );
 
84
        }
 
85
 
 
86
        return string;
 
87
    }
 
88
 
 
89
    //-----------------------------------------------------------------
 
90
    String StringUtil::replaceAll( const String& str, const String& pattern, const String& replacement )
 
91
    {
 
92
        String string = str;
 
93
 
 
94
        size_t position = string.find( pattern );
 
95
 
 
96
        while ( position != String::npos )
 
97
        {
 
98
            string.replace( position, pattern.size(), replacement );
 
99
            position = string.find( pattern );
 
100
        }
 
101
 
 
102
        return string;
 
103
    }
 
104
 
 
105
    //-----------------------------------------------------------------
 
106
    String StringUtil::replaceAll( const String& str, const String& pattern, char replacement )
 
107
    {
 
108
        String replaceStr;
 
109
        replaceStr += replacement;
 
110
        return replaceAll( str, pattern, replaceStr );
 
111
    }
 
112
 
 
113
    //-----------------------------------------------------------------------
 
114
    MathML::String StringUtil::replaceAll( const String& str, char pattern, char replacement )
 
115
    {
 
116
        String string = str;
 
117
 
 
118
        size_t position = string.find( pattern );
 
119
 
 
120
        while ( position != String::npos )
 
121
        {
 
122
            string[ position ] = replacement;
 
123
            position = string.find( pattern );
 
124
        }
 
125
 
 
126
        return string;
 
127
    }
 
128
 
 
129
    //-----------------------------------------------------------------------
 
130
    MathML::String StringUtil::valueOf( unsigned char value )
 
131
    {
 
132
        std::stringstream ss;
 
133
        ss << value << std::ends;
 
134
        return ss.str();
 
135
    }
 
136
 
 
137
    //-----------------------------------------------------------------------
 
138
    MathML::String StringUtil::valueOf( unsigned short value )
 
139
    {
 
140
        std::stringstream ss;
 
141
        ss << value << std::ends;
 
142
        return ss.str();
 
143
    }
 
144
 
 
145
    //-----------------------------------------------------------------------
 
146
    MathML::String StringUtil::valueOf( unsigned int value )
 
147
    {
 
148
        std::stringstream ss;
 
149
        ss << value << std::ends;
 
150
        return ss.str();
 
151
    }
 
152
 
 
153
    //-----------------------------------------------------------------------
 
154
    MathML::String StringUtil::valueOf( unsigned long value )
 
155
    {
 
156
        std::stringstream ss;
 
157
        ss << value << std::ends;
 
158
        return ss.str();
 
159
    }
 
160
 
 
161
    //-----------------------------------------------------------------------
 
162
    MathML::String StringUtil::valueOf( unsigned long long value )
 
163
    {
 
164
        std::stringstream ss;
 
165
        ss << value << std::ends;
 
166
        return ss.str();
 
167
    }
 
168
 
 
169
    //-----------------------------------------------------------------------
 
170
    MathML::String StringUtil::valueOf( char value )
 
171
    {
 
172
        std::stringstream ss;
 
173
        ss << value << std::ends;
 
174
        return ss.str();
 
175
    }
 
176
 
 
177
    //-----------------------------------------------------------------------
 
178
    MathML::String StringUtil::valueOf( short value )
 
179
    {
 
180
        std::stringstream ss;
 
181
        ss << value << std::ends;
 
182
        return ss.str();
 
183
    }
 
184
 
 
185
    //-----------------------------------------------------------------------
 
186
    MathML::String StringUtil::valueOf( int value )
 
187
    {
 
188
        std::stringstream ss;
 
189
        ss << value << std::ends;
 
190
        return ss.str();
 
191
    }
 
192
 
 
193
    //-----------------------------------------------------------------------
 
194
    MathML::String StringUtil::valueOf( long value )
 
195
    {
 
196
        std::stringstream ss;
 
197
        ss << value << std::ends;
 
198
        return ss.str();
 
199
    }
 
200
 
 
201
    //-----------------------------------------------------------------------
 
202
    MathML::String StringUtil::valueOf( long long value )
 
203
    {
 
204
        std::stringstream ss;
 
205
        ss << value << std::ends;
 
206
        return ss.str();
 
207
    }
 
208
 
 
209
    //-----------------------------------------------------------------------
 
210
    MathML::String StringUtil::valueOf( float value )
 
211
    {
 
212
        std::stringstream ss;
 
213
        ss << value << std::ends;
 
214
        return ss.str();
 
215
    }
 
216
 
 
217
    //-----------------------------------------------------------------------
 
218
    MathML::String StringUtil::valueOf( double value )
 
219
    {
 
220
        std::stringstream ss;
 
221
        ss << value << std::ends;
 
222
        return ss.str();
 
223
    }
 
224
 
 
225
    //-----------------------------------------------------------------------
 
226
    int StringUtil::caseCompare( const String& str1, const String& str2 )
 
227
    {
 
228
        String tmp1( str1 );
 
229
        toLowerCase( tmp1 );
 
230
        String tmp2( str2 );
 
231
        toLowerCase( tmp2 );
 
232
 
 
233
        return tmp1.compare( str2 );
 
234
 
 
235
    }
 
236
 
 
237
    //-----------------------------------------------------------------------
 
238
    void StringUtil::toLowerCase( String& str )
 
239
    {
 
240
        std::transform(
 
241
            str.begin(),
 
242
            str.end(),
 
243
            str.begin(),
 
244
            tolower );
 
245
    }
 
246
 
 
247
    //-----------------------------------------------------------------------
 
248
    void StringUtil::toUpperCase( String& str )
 
249
    {
 
250
        std::transform(
 
251
            str.begin(),
 
252
            str.end(),
 
253
            str.begin(),
 
254
            toupper );
 
255
    }
 
256
 
 
257
    //-----------------------------------------------------------------------
 
258
    unsigned short StringUtil::parseUnsignedShort( const String& str )
 
259
    {
 
260
        unsigned short value;
 
261
        std::istringstream inStream( str );
 
262
        inStream >> value;
 
263
        return value;
 
264
    }
 
265
 
 
266
    //-----------------------------------------------------------------------
 
267
    unsigned int StringUtil::parseUnsignedInt( const String& str )
 
268
    {
 
269
        unsigned int value;
 
270
        std::istringstream inStream( str );
 
271
        inStream >> value;
 
272
        return value;
 
273
    }
 
274
 
 
275
    //-----------------------------------------------------------------------
 
276
    unsigned long StringUtil::parseUnsignedLong( const String& str )
 
277
    {
 
278
        unsigned long value;
 
279
        std::istringstream inStream( str );
 
280
        inStream >> value;
 
281
        return value;
 
282
    }
 
283
 
 
284
    //-----------------------------------------------------------------------
 
285
    short StringUtil::parseShort( const String& str )
 
286
    {
 
287
        short value;
 
288
        std::istringstream inStream( str );
 
289
        inStream >> value;
 
290
        return value;
 
291
    }
 
292
 
 
293
    //-----------------------------------------------------------------------
 
294
    int StringUtil::parseInt( const String& str )
 
295
    {
 
296
        int value;
 
297
        std::istringstream inStream( str );
 
298
        inStream >> value;
 
299
        return value;
 
300
    }
 
301
 
 
302
    //-----------------------------------------------------------------------
 
303
    long StringUtil::parseLong( const String& str )
 
304
    {
 
305
        long value;
 
306
        std::istringstream inStream( str );
 
307
        inStream >> value;
 
308
        return value;
 
309
    }
 
310
 
 
311
    //-----------------------------------------------------------------------
 
312
    double StringUtil::parseDouble( const String& str )
 
313
    {
 
314
        double value;
 
315
        std::istringstream inStream( str );
 
316
        inStream >> value;
 
317
        return value;
 
318
    }
 
319
 
 
320
    //-----------------------------------------------------------------
 
321
    String StringUtil::removeWhitespaces( const String &string )
 
322
    {
 
323
        String whitespaces ( " \t\f\v\n\r" );
 
324
        String result = string;
 
325
 
 
326
        String::size_type found = string.find_first_of( whitespaces );
 
327
 
 
328
        while ( found != String::npos )
 
329
        {
 
330
            result.erase( found, 1 );
 
331
            found = result.find_first_of( whitespaces );
 
332
        }
 
333
 
 
334
        return result;
 
335
    }
 
336
 
 
337
    //-----------------------------------------------------------------
 
338
    MathML::String StringUtil::valueOfUnfinalized( int value )
 
339
    {
 
340
        std::stringstream ss;
 
341
        ss << value;
 
342
        return ss.str();
 
343
    }
 
344
}