GGS(GenericGEANT4Simulation)Software  2.7.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 122 of file GGSStringUtils.cpp.

122  {
123  if (str.find('*') != std::string::npos || str.find('?') != std::string::npos) {
124  return true;
125  } else {
126  return false;
127  }
128 }
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 78 of file GGSStringUtils.cpp.

78  {
79  std::string trimmedStr = Trim(str);
80  if (trimmedStr.empty() || ((!isdigit(trimmedStr[0])) && (trimmedStr[0] != '-') && (trimmedStr[0] != '+')))
81  return false;
82  for (unsigned int iChar = 1; iChar < trimmedStr.size(); iChar++) {
83  if (!isdigit(trimmedStr[iChar])) {
84  return false;
85  }
86  }
87  return true;
88 }
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 90 of file GGSStringUtils.cpp.

90  {
91  std::string trimmedStr = Trim(str);
92  if (trimmedStr.empty() || ((!isdigit(trimmedStr[0])) && (trimmedStr[0] != '-') && (trimmedStr[0] != '+')))
93  return false;
94  bool hasPoint = false;
95  bool hase = false;
96  for (unsigned int iChar = 1; iChar < trimmedStr.size(); iChar++) {
97  if (!isdigit(trimmedStr[iChar])) {
98  if (trimmedStr[iChar] == '.') {
99  if (hasPoint) {
100  return false;
101  }
102  hasPoint = true;
103  } else if (trimmedStr[iChar] == 'e') {
104  if (hase) {
105  return false;
106  }
107  hase = true;
108  // After 'e' there must be a digit or a sign
109  ++iChar;
110  if (iChar >= trimmedStr.size() ||
111  (!isdigit(trimmedStr[iChar]) && (trimmedStr[iChar] != '-') && (trimmedStr[iChar] != '+'))) {
112  return false;
113  }
114  } else {
115  return false;
116  }
117  }
118  }
119  return true;
120 }
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 130 of file GGSStringUtils.cpp.

130  {
131  static std::string regexStr;
132  regexStr = str;
133  std::string::size_type n = 0;
134  while ((n = regexStr.find(".", n)) != std::string::npos) {
135  regexStr.replace(n, 1, "\\.");
136  n += 2;
137  }
138  n = 0;
139  while ((n = regexStr.find("*", n)) != std::string::npos) {
140  regexStr.replace(n, 1, ".*");
141  n += 2;
142  }
143  std::replace(regexStr.begin(), regexStr.end(), '?', '.');
144  return regexStr;
145 }
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  } else if (str[iChar] == '{') {
35  quotedText = true;
36  endliteralDelimiter = '}';
37  iChar++;
38  iLast = str.find_first_of('}', iChar);
39  } else {
40  iLast = str.find_first_of(delimiter, iChar + 1);
41  }
42  if (iLast != std::string::npos) {
43  // Delimiter character found
44  token = str.substr(iChar, iLast - iChar);
45  if (str[iLast] == endliteralDelimiter) {
46  iLast++;
47  quotedText = false;
48  }
49  } else {
50  // Delimiter character not found.
51  if (quotedText) {
52  // Missing closing double quotation.
53  throw std::runtime_error(std::string("[GGSStringUtils::Tokenize] Missing closing ") + endliteralDelimiter);
54  } else {
55  // Take all the characters up to the end of the string
56  token = str.substr(iChar);
57  }
58  }
59  tokens.push_back(token);
60 
61  iChar = str.find_first_not_of(delimiter, iLast);
62  }
63 
64  return tokens;
65 }
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 67 of file GGSStringUtils.cpp.

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