summaryrefslogtreecommitdiff
path: root/src/utils/string.cpp
blob: 9727f3fe8ca9fb8d5e4a9813bbf36e2f804e65c8 (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
#include "string.hpp"

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

namespace bot {
  namespace utils {
    namespace string {
      std::vector<std::string> split_text(const std::string &text,
                                          char delimiter) {
        std::vector<std::string> parts;

        std::istringstream iss(text);
        std::string part;

        while (std::getline(iss, part, delimiter)) {
          parts.push_back(part);
        }

        return parts;
      }

      std::string join_vector(const std::vector<std::string> &vec,
                              char delimiter) {
        if (vec.empty()) {
          return "";
        }

        std::string str;

        for (auto i = vec.begin(); i != vec.end() - 1; i++) {
          str += *i + delimiter;
        }

        str += vec[vec.size() - 1];

        return str;
      }

      std::string join_vector(const std::vector<std::string> &vec) {
        std::string str;

        for (const auto &e : vec) {
          str += e;
        }

        return str;
      }
    }
  }
}