utils.cpp
Go to the documentation of this file.
1 
28 #include <log_view/utils.h>
29 
30 #include <cstdlib>
31 #include <sstream>
32 
33 #include <boost/algorithm/string/find.hpp>
34 
35 namespace log_view {
36 
37 std::string toString(double val, int precision) {
38  std::ostringstream ss;
39  ss.precision(precision);
40  ss << std::fixed << val;
41  return ss.str();
42 }
43 
44 std::vector<std::string> split(const std::string &text, char sep) {
45  if (text.empty()) {
46  return {};
47  }
48 
49  std::vector<std::string> tokens;
50  size_t start = 0;
51  size_t end = 0;
52  while ((end = text.find(sep, start)) != std::string::npos) {
53  if (end != start) {
54  tokens.push_back(text.substr(start, end - start));
55  }
56  start = end + 1;
57  }
58  if (end != start) {
59  auto token = text.substr(start);
60  if (!token.empty()) {
61  tokens.push_back(text.substr(start));
62  }
63  }
64  return tokens;
65 }
66 
67 bool contains(const std::string& text, const std::string& substr, bool case_insensitive) {
68  if (substr.empty()) {
69  return true;
70  }
71 
72  if (case_insensitive) {
73  auto it = std::search(
74  text.begin(), text.end(),
75  substr.begin(), substr.end(),
76  [](char ch1, char ch2) { return std::toupper(ch1) == std::toupper(ch2); }
77  );
78  return it != text.end();
79  }
80  else {
81  return text.find(substr) != std::string::npos;
82  }
83 }
84 
85 std::vector<size_t> find(const std::string& text, const std::string& substr, bool case_insensitive) {
86  if (substr.empty()) {
87  return {};
88  }
89 
90  std::vector<size_t> indices;
91 
92  if (case_insensitive) {
93  auto it = std::search(
94  text.begin(), text.end(),
95  substr.begin(), substr.end(),
96  [](char ch1, char ch2) { return std::toupper(ch1) == std::toupper(ch2); }
97  );
98 
99  while (it != text.end()) {
100  size_t index = std::distance(text.begin(), it);
101  indices.push_back(index);
102  it = std::search(
103  text.begin() + index + 1, text.end(),
104  substr.begin(), substr.end(),
105  [](char ch1, char ch2) { return std::toupper(ch1) == std::toupper(ch2); }
106  );
107  }
108  }
109  else {
110  size_t loc = text.find(substr, 0);
111  while (loc != std::string::npos) {
112  indices.push_back(loc);
113  loc = text.find(substr, loc + 1);
114  }
115  }
116 
117  return indices;
118 }
119 
120 
121 int ctrl(char key) {
122  return key & 0x1f;
123 }
124 
125 void toClipboard(const std::string& text) {
126  FILE* pipe = popen("xclip -sel clip", "w");
127  if (!pipe) {
128  return;
129  }
130  fwrite(text.data(), sizeof(char), text.size(), pipe);
131  pclose(pipe);
132 }
133 
134 } // namespace log_view
ROSCPP_DECL void start()
std::vector< size_t > find(const std::string &text, const std::string &substr, bool case_insensitive)
Definition: utils.cpp:85
std::vector< std::string > split(const std::string &text, char sep)
Definition: utils.cpp:44
std::string toString(double val, int precision)
Definition: utils.cpp:37
bool contains(const std::string &text, const std::string &substr, bool case_insensitive)
Definition: utils.cpp:67
void toClipboard(const std::string &text)
Definition: utils.cpp:125
int ctrl(char key)
Definition: utils.cpp:121


log_view
Author(s): Marc Alban
autogenerated on Thu Mar 4 2021 03:21:52