summaryrefslogtreecommitdiff
path: root/src/stringutils.cpp
blob: 3d30943ec0a1aed511f88eccdb3185368558918b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/***************************************************************************
                               stringutils.cpp
                             -------------------
    begin                : Tue Nov 06 2007
    copyright            : (C) 2007 - 2012 by Roland Riegel
    email                : feedback@roland-riegel.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "stringutils.h"

using namespace std;

string trim(const string& s)
{
    // find end of whitespace at the beginning
    string::size_type posBegin = s.find_first_not_of(" \011\012\015");
    if(posBegin == string::npos)
        return string();

    // find beginning of whitespace at the end
    string::size_type posEnd = s.find_last_not_of(" \011\012\015");

    return s.substr(posBegin, posEnd - posBegin + 1);
}

vector<string> split(const string& s, const string& separators)
{
    vector<string> words;
    if(s.empty())
        return words;

    string::size_type pos = s.find_first_of(separators);
    string::size_type posOld = 0;
    while(pos != string::npos)
    {
        words.push_back(s.substr(posOld, pos - posOld));
        posOld = pos + 1;

        pos = s.find_first_of(separators, posOld);
    }

    words.push_back(s.substr(posOld));

    return words;
}

vector<string> splitQuoted(const string& s, const string& separators, const string& quotes)
{
    if(s.empty())
        return vector<string>();

    vector<string> words(1);
    string::size_type pos = s.find_first_of(separators + quotes);
    string::size_type posOld = 0;
    bool quoted = false;

    while(true)
    {
        words.back() += s.substr(posOld, pos - posOld);
        posOld = pos + 1;

        if(pos == string::npos)
            return words;

        if(separators.find(s[pos]) != string::npos)
        {
            // separator found

            words.push_back(string());

            pos = s.find_first_of(separators + quotes, posOld);
        }
        else
        {
            // quote found

            posOld = pos + 1;

            quoted = !quoted;

            if(quoted)
                pos = s.find_first_of(quotes, posOld);
            else
                pos = s.find_first_of(separators + quotes, posOld);
        }
    }
}