summaryrefslogtreecommitdiff
path: root/src/CommonAPI/Utils.cpp
blob: a2058c0a391fd62230ccfb4b80a54085bd2e1038 (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
// Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <algorithm>
#include <sstream>
#include <functional>

#include <CommonAPI/Utils.hpp>

namespace CommonAPI {

std::vector<std::string>& split(const std::string& s, char delim, std::vector<std::string>& elems) {
    std::istringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}

std::vector<std::string> split(const std::string& s, char delim) {
    std::vector<std::string> elems;
    return split(s, delim, elems);
}

void trim(std::string& toTrim) {
    toTrim.erase(
        toTrim.begin(),
        std::find_if(
            toTrim.begin(),
            toTrim.end(),
            std::not1(std::ptr_fun(isspace))
        )
    );

    toTrim.erase(
        std::find_if(
            toTrim.rbegin(),
            toTrim.rend(),
            std::not1(std::ptr_fun(isspace))).base(),
            toTrim.end()
    );
}

}//namespace CommonAPI