GGS(GenericGEANT4Simulation)Software  2.6.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
Functions
GGSStringUtils Namespace Reference

A namespace with some utility methods for strings. More...

Functions

Tokens Tokenize (const std::string &str, char delimiter= ' ')
 Extracts words from a string. More...
 
std::string Trim (const std::string &str)
 Trims a string. More...
 
bool IsInteger (const std::string &str)
 Checks if a string is an a integer. More...
 
bool IsReal (const std::string &str)
 Checks if a string is a real number. More...
 
bool IsGlobExpression (const std::string &str)
 Check if the given string is a glob expression. More...
 
std::string RegexFromGlob (const std::string &str)
 Build a regex starting from a glob expression. More...
 

Detailed Description

A namespace with some utility methods for strings.

Function Documentation

bool GGSStringUtils::IsGlobExpression ( const std::string &  str)

Check if the given string is a glob expression.

A glob expression is recognized by looking at '*' or '?' characters in the string. Other glob wildcards are ignored.

Parameters
strThe string to be checked.
Returns
true if the string is a glob expression.

Definition at line 128 of file GGSStringUtils.cpp.

128  {
129  if (str.find('*') != std::string::npos || str.find('?') != std::string::npos) {
130  return true;
131  }
132  else {
133  return false;
134  }
135 }
bool GGSStringUtils::IsInteger ( const std::string &  str)

Checks if a string is an a integer.

Checks if the trimmed input string represents an integer value, possibly with sign.

Parameters
strThe input string.
Returns
true if the input string represents an integer value.

Definition at line 82 of file GGSStringUtils.cpp.

82  {
83  std::string trimmedStr = Trim(str);
84  if (trimmedStr.empty() || ((!isdigit(trimmedStr[0])) && (trimmedStr[0] != '-') && (trimmedStr[0] != '+')))
85  return false;
86  for (unsigned int iChar = 1; iChar < trimmedStr.size(); iChar++) {
87  if (!isdigit(trimmedStr[iChar])) {
88  return false;
89  }
90  }
91  return true;
92 }
std::string Trim(const std::string &str)
Trims a string.
bool GGSStringUtils::IsReal ( const std::string &  str)

Checks if a string is a real number.

Checks if the trimmed input string represents a real value, possibly with sign, in the standard notation (e.g. 10.2, not 1.02e1). Note that integer numbers e.g. 10 are real numbers so they are recognized as real by this function.

Parameters
strThe input string.
Returns
true if the input string represents a real number in standard notation.

Definition at line 94 of file GGSStringUtils.cpp.

94  {
95  std::string trimmedStr = Trim(str);
96  if (trimmedStr.empty() || ((!isdigit(trimmedStr[0])) && (trimmedStr[0] != '-') && (trimmedStr[0] != '+')))
97  return false;
98  bool hasPoint = false;
99  bool hase = false;
100  for (unsigned int iChar = 1; iChar < trimmedStr.size(); iChar++) {
101  if (!isdigit(trimmedStr[iChar])) {
102  if (trimmedStr[iChar] == '.') {
103  if (hasPoint) {
104  return false;
105  }
106  hasPoint = true;
107  }
108  else if (trimmedStr[iChar] == 'e') {
109  if (hase) {
110  return false;
111  }
112  hase = true;
113  // After 'e' there must be a digit or a sign
114  ++iChar;
115  if (iChar >= trimmedStr.size()
116  || (!isdigit(trimmedStr[iChar]) && (trimmedStr[iChar] != '-') && (trimmedStr[iChar] != '+'))) {
117  return false;
118  }
119  }
120  else {
121  return false;
122  }
123  }
124  }
125  return true;
126 }
std::string Trim(const std::string &str)
Trims a string.
std::string GGSStringUtils::RegexFromGlob ( const std::string &  str)

Build a regex starting from a glob expression.

This function manages only the '*' and '?' glob wildcards.

Parameters
strThe glob expression.
Returns
The regex string corresponding to the given glob expression.

Definition at line 137 of file GGSStringUtils.cpp.

137  {
138  static std::string regexStr;
139  regexStr = str;
140  std::string::size_type n = 0;
141  while ((n = regexStr.find(".", n)) != std::string::npos) {
142  regexStr.replace(n, 1, "\\.");
143  n += 2;
144  }
145  n = 0;
146  while ((n = regexStr.find("*", n)) != std::string::npos) {
147  regexStr.replace(n, 1, ".*");
148  n += 2;
149  }
150  std::replace(regexStr.begin(), regexStr.end(), '?', '.');
151  return regexStr;
152 }
Tokens GGSStringUtils::Tokenize ( const std::string &  str,
char  delimiter = ' ' 
)

Extracts words from a string.

The string must be a sequence of words separated by a given delimiter. A string portion enclosed within double quotation marks will be treated as a single word (the quotation marks will be removed). If the closing double quotation is missing then a std::runtime_error is thrown.

Parameters
strThe string to tokenize.
delimiterA vector used to return the words in the string.
Returns
A collection of tokens (i.e. strings) in which the input string has been broken into.
Exceptions
std::runtime_errorif a non-terminated double-quotation-enclosed sequence is found.

Definition at line 13 of file GGSStringUtils.cpp.

13  {
14  Tokens tokens;
15 
16  size_t iChar = 0;
17  char endliteralDelimiter = '\0';
18  // Discard trailing spaces and tabs
19  while (str[iChar] == ' ' || str[iChar] == '\t')
20  iChar++;
21 
22  // Tokenize the string
23  std::string token;
24  tokens.clear();
25  while (iChar < str.size()) {
26 
27  size_t iLast = 0;
28  bool quotedText = false;
29  if (str[iChar] == '\"') {
30  quotedText = true;
31  endliteralDelimiter = '\"';
32  iChar++;
33  iLast = str.find_first_of('\"', iChar);
34  }
35  else if (str[iChar] == '{') {
36  quotedText = true;
37  endliteralDelimiter = '}';
38  iChar++;
39  iLast = str.find_first_of('}', iChar);
40  }
41  else {
42  iLast = str.find_first_of(delimiter, iChar + 1);
43  }
44  if (iLast != std::string::npos) {
45  // Delimiter character found
46  token = str.substr(iChar, iLast - iChar);
47  if (str[iLast] == endliteralDelimiter) {
48  iLast++;
49  quotedText = false;
50  }
51  }
52  else {
53  // Delimiter character not found.
54  if (quotedText) {
55  // Missing closing double quotation.
56  throw std::runtime_error(std::string("[GGSStringUtils::Tokenize] Missing closing ") + endliteralDelimiter);
57  }
58  else {
59  // Take all the characters up to the end of the string
60  token = str.substr(iChar);
61  }
62  }
63  tokens.push_back(token);
64 
65  iChar = str.find_first_not_of(delimiter, iLast);
66  }
67 
68  return tokens;
69 }
std::string GGSStringUtils::Trim ( const std::string &  str)

Trims a string.

Removes all leading and trailing invisible characters (spaces, tabs etc.).

Parameters
strThe input string.
Returns
a new string containing the trimmed input string.

Definition at line 71 of file GGSStringUtils.cpp.

71  {
72  std::string retStr = str;
73  retStr.erase(retStr.begin(), std::find_if(retStr.begin(), retStr.end(), [](int ch) {
74  return !(std::isblank(static_cast<unsigned int>(ch)));
75  }));
76  retStr.erase(std::find_if(retStr.rbegin(), retStr.rend(), [](int ch) {
77  return !(std::isblank(static_cast<unsigned int>(ch)));
78  }).base(), retStr.end());
79  return retStr;
80 }