DP3
StringTools.h
Go to the documentation of this file.
1 // StringTools.h: useful string manipulation methods.
2 //
3 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
6 #ifndef LOFAR_COMMON_STRINGUTIL_H
7 #define LOFAR_COMMON_STRINGUTIL_H
8 
9 #include <cstdint>
10 #include <string>
11 #include <vector>
12 
13 namespace dp3 {
14 namespace common {
15 
17 namespace stringtools {
18 
28 //
34 std::vector<std::string> tokenize(const std::string& str,
35  const std::string& delims);
36 
43 class Compare {
44  public:
46  enum Mode { NORMAL, NOCASE };
47 
50  Compare(Mode mode = NORMAL) : itsMode(mode) {}
51 
53  bool operator()(const std::string& s1, const std::string& s2) const {
54  if (itsMode == NORMAL)
55  return s1 < s2;
56  else
57  return lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
58  nocaseCompare);
59  }
60 
61  private:
63  static bool nocaseCompare(char c1, char c2) {
64  return toupper(c1) < toupper(c2);
65  }
66 
68  Mode itsMode;
69 };
70 
71 } // namespace stringtools
72 
73 //
74 // formatString(format, ...) --> string up to 10Kb
75 //
76 // The function formatString accepts printf-like arguments and returns a
77 // formatted string. It can be used e.g. in stream constructions:
78 // stream << formatString("Opening connection with host %%s", hostName);
79 // In real life this must be %s ofcourse but doxygen need a double %%.
80 const std::string formatString(const char* format, ...);
81 
82 // Skip leading whitespace (blanks and horizontal tabs) starting at st.
83 // It returns the position of the first non-whitespace character.
84 // It returns end if all whitespace.
85 // It can be used in combination with rskipws.
86 unsigned int lskipws(const std::string& value, unsigned int st,
87  unsigned int end);
88 
89 // Skip trailing whitespace (blanks and horizontal tabs) starting at end.
90 // It returns the position after the last non-whitespace character, thus
91 // value.substr(st, end-st) extracts the significant value.
92 // It returns st if all whitespace.
93 unsigned int rskipws(const std::string& value, unsigned int st,
94  unsigned int end);
95 
96 // Skip past a quoted string.
97 // The quote character is the first character (at position st).
98 // Usually the quote character is ' or ", but it could be any other character.
99 // An exception is thrown if no ending quote character is found.
100 unsigned int skipQuoted(const std::string& str, unsigned int st);
101 
102 // Skip past the end of a balanced pair of delimiters where nested pairs
103 // are also skipped. Delimiters in quoted parts are ignored.
104 // The starting delimiter is the first character in the string (at position st).
105 // The ending delimiter is given as an argument.
106 // The function also works fine if starting and ending delimiter are the same.
108 // end delimiter is found before position end.
110 unsigned int skipBalanced(const std::string& str, unsigned int st,
111  unsigned int end, char endChar);
112 
114 // Convert a string to the value of any of the fundamental arithmetic data
115 // types. It checks if the entire string is used for the conversion.
116 // An integer value can also be given in hexadecimal format (e.g. 0x123).
117 // Leading and trailing whitespace is allowed.
118 // It checks if an integer value does not exceed the data type range.
120 bool strToBool(const std::string& aString);
121 long strToLong(const std::string& aString);
122 int strToInt(const std::string& aString);
123 int32_t strToInt32(const std::string& aString);
124 int16_t strToInt16(const std::string& aString);
125 unsigned long strToUlong(const std::string& aString);
126 unsigned int strToUint(const std::string& aString);
127 uint32_t strToUint32(const std::string& aString);
128 uint16_t strToUint16(const std::string& aString);
129 int64_t strToInt64(const std::string& aString);
130 uint64_t strToUint64(const std::string& aString);
131 float strToFloat(const std::string& aString);
132 double strToDouble(const std::string& aString);
134 
136 // Array specifications are often entered by the user with ranges
137 // like 3..32,55..58 For converting such a string to a real vector the spec must
138 // be expanded so that it contains all elements instead of the ranges. Likewise,
139 // when you present a array to the user you often want to show a spec with the
140 // ranges instead of all individual elements. See the ParameterSet document for
141 // a detailed description of the syntax.
142 
144 // Given a string 'xx..xx, xx' this utility expands the string
145 // by replacing ranges with the fill series.
146 // Eg. "lii001..003xx, lii005" --> "lii001xx,lii002xx,lii003xx, lii005"
147 std::string expandRangeString(const std::string&);
148 
149 // Given a string like '3*str' this utility expands the string
150 // by replacing the string 3 times.
151 // Eg. "3*0" --> "0,0,0"
152 std::string expandMultString(const std::string&);
153 
154 // Apply expandMultString and expandRangeString (in that order) for an array
155 // string which must be enclosed in square brackets.
156 std::string expandArrayString(const std::string&);
158 
166 std::string PatternToRegex(const std::string& pattern);
167 
168 } // namespace common
169 } // namespace dp3
170 
171 #endif
Functor to compare two strings. Strings can be compared case sensitive (NORMAL) and case insensitive ...
Definition: StringTools.h:43
Compare(Mode mode=NORMAL)
Definition: StringTools.h:50
bool operator()(const std::string &s1, const std::string &s2) const
The comparison operator.
Definition: StringTools.h:53
Mode
String comparison mode.
Definition: StringTools.h:46
@ NORMAL
Definition: StringTools.h:46
@ NOCASE
Definition: StringTools.h:46
std::vector< std::string > tokenize(const std::string &str, const std::string &delims)
unsigned int rskipws(const std::string &value, unsigned int st, unsigned int end)
int strToInt(const std::string &aString)
unsigned int strToUint(const std::string &aString)
long strToLong(const std::string &aString)
unsigned int lskipws(const std::string &value, unsigned int st, unsigned int end)
unsigned long strToUlong(const std::string &aString)
std::string expandMultString(const std::string &)
int16_t strToInt16(const std::string &aString)
unsigned int skipBalanced(const std::string &str, unsigned int st, unsigned int end, char endChar)
An exception is thrown if the delimiters are not balanced, thus if no.
bool strToBool(const std::string &aString)
int64_t strToInt64(const std::string &aString)
std::string expandRangeString(const std::string &)
std::string PatternToRegex(const std::string &pattern)
unsigned int skipQuoted(const std::string &str, unsigned int st)
const std::string formatString(const char *format,...)
std::string expandArrayString(const std::string &)
int32_t strToInt32(const std::string &aString)
uint64_t strToUint64(const std::string &aString)
uint32_t strToUint32(const std::string &aString)
uint16_t strToUint16(const std::string &aString)
float strToFloat(const std::string &aString)
double strToDouble(const std::string &aString)
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53