rohdeschwarz  0.2.0
TCPIP socket library for Rohde & Schwarz instrument control
helpers.cpp
Go to the documentation of this file.
1 
8 
9 
10 // std lib
11 #include <algorithm>
12 #include <cctype>
13 #include <cstddef>
14 #include <sstream>
15 
16 
17 // constants
18 const char SINGLE_QUOTE = '\'';
19 const char DOUBLE_QUOTE = '\"';
20 
21 
22 // lambdas
23 
24 auto not_a_space = [](unsigned char ch)
25 {
26  return !std::isspace(ch);
27 };
28 
29 
30 auto is_quote_char = [](char character)
31 {
32  return character == SINGLE_QUOTE || character == DOUBLE_QUOTE;
33 };
34 
35 
36 // helpers
37 
38 
39 std::string rohdeschwarz::leftTrim(std::string text)
40 {
41  const auto end = std::find_if(text.begin(), text.end(), not_a_space);
42  text.erase(text.begin(), end);
43  return text;
44 }
45 
46 
47 std::string rohdeschwarz::rightTrim(std::string text)
48 {
49  const auto rbegin = std::find_if(text.rbegin(), text.rend(), not_a_space);
50  const auto begin = rbegin.base();
51  text.erase(begin, text.end());
52  return text;
53 }
54 
55 
56 std::string rohdeschwarz::trim(const std::string& text)
57 {
58  return leftTrim(rightTrim(text));
59 }
60 
61 
62 bool rohdeschwarz::isLeftQuote(const char* text)
63 {
64  const std::string text_str(text);
65  return isLeftQuote(text_str);
66 }
67 
68 
69 bool rohdeschwarz::isLeftQuote(const std::string& text)
70 {
71  if (text.empty())
72  {
73  return false;
74  }
75 
76  return !text.empty() && is_quote_char(text.front());
77 }
78 
79 
80 bool rohdeschwarz::isRightQuote(const char* text)
81 {
82  const std::string text_str(text);
83  return isRightQuote(text_str);
84 }
85 
86 
87 bool rohdeschwarz::isRightQuote(const std::string& text)
88 {
89  return !text.empty() && is_quote_char(text.back());
90 }
91 
92 
93 bool rohdeschwarz::isQuoted(const char* text)
94 {
95  const std::string text_str(text);
96  return isQuoted(text_str);
97 }
98 
99 
100 bool rohdeschwarz::isQuoted(const std::string& text)
101 {
102  return isLeftQuote(text) && isRightQuote(text);
103 }
104 
105 
106 std::string rohdeschwarz::quote(const char* text, char quote_character)
107 {
108  const std::string text_str(text);
109  return quote(text_str, quote_character);
110 }
111 
112 
113 std::string rohdeschwarz::quote(const std::string& text, char quote_character)
114 {
115  std::stringstream quoted_text;
116  quoted_text << quote_character << text << quote_character;
117  return quoted_text.str();
118 }
119 
120 
121 std::string rohdeschwarz::unquote(const char* text)
122 {
123  const std::string text_str(text);
124  return unquote(text_str);
125 }
126 
127 
128 std::string rohdeschwarz::unquote(const std::string& text)
129 {
130  return isQuoted(text)?
131  text.substr(1, text.size() - 2)
132  : text;
133 }
134 
135 
136 std::vector<std::string> rohdeschwarz::split(const char* csvList, const char separator)
137 {
138  const std::string csv_list_str(csvList);
139  return split(csv_list_str, separator);
140 }
141 
142 
143 std::vector<std::string> rohdeschwarz::split(const std::string& csvList, const char separator)
144 {
145  std::vector<std::string> parts;
146  if (csvList.empty())
147  {
148  // no parts
149  return parts;
150  }
151 
152  // find initial values
153  std::size_t start = 0;
154  std::size_t end = csvList.find(separator);
155  while (end != std::string::npos)
156  {
157  // append part
158  const std::size_t length = end - start;
159  const std::string part = csvList.substr(start, length);
160  parts.emplace_back(part);
161 
162  // find next part
163  // starting at next character
164  start = end + 1;
165  end = csvList.find(separator, start);
166  }
167 
168  // find last value
169  const std::string part = csvList.substr(start);
170  parts.emplace_back(part);
171  return parts;
172 }
auto not_a_space
Definition: helpers.cpp:24
const char DOUBLE_QUOTE
Definition: helpers.cpp:19
const char SINGLE_QUOTE
Definition: helpers.cpp:18
auto is_quote_char
Definition: helpers.cpp:30
rohdeschwarz helper function definition
std::string unquote(const char *text)
Removes quotes from beginning and end of string.
Definition: helpers.cpp:121
bool isRightQuote(const char *text)
Checks for a quote at the end of the string.
Definition: helpers.cpp:80
std::string trim(const std::string &text)
Trims whitespace beginning and end of string.
Definition: helpers.cpp:56
std::string quote(const char *text, char quote_character='\'')
Surrounds string in quotes.
Definition: helpers.cpp:106
bool isLeftQuote(const char *text)
Checks for a quote at the beginning of the string.
Definition: helpers.cpp:62
std::vector< std::string > split(const char *csvList, const char separator=',')
Splits string on separator.
Definition: helpers.cpp:136
std::string leftTrim(std::string text)
Trims whitespace from left (beginning) of string.
Definition: helpers.cpp:39
bool isQuoted(const char *text)
Checks for quotes at the beginning and end of the string.
Definition: helpers.cpp:93
std::string rightTrim(std::string text)
Trims whitespace from right (end) of string.
Definition: helpers.cpp:47