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

#include <algorithm>
#include <cctype>
#include <locale>
#include <sstream>

namespace silly::editor::utils {
  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;
  }

  void ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
              return !std::isspace(ch);
            }));
  }

  void rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(),
                         [](unsigned char ch) { return !std::isspace(ch); })
                .base(),
            s.end());
  }

  void trim(std::string &s) {
    rtrim(s);
    ltrim(s);
  }
}