ArmNN
 24.08
armnn::stringUtils Namespace Reference

Functions

std::vector< std::string > StringTokenizer (const std::string &str, const char *delimiters, bool tokenCompression=true)
 Function to take a string and a list of delimiters and split the string into tokens based on those delimiters This assumes that tokens are also to be split by newlines Enabling tokenCompression merges adjacent delimiters together, preventing empty tokens. More...
 
std::string & StringStartTrim (std::string &str, const std::string &chars="\t\n\v\f\r ")
 Trim from the start of a string. More...
 
std::string & StringEndTrim (std::string &str, const std::string &chars="\t\n\v\f\r ")
 Trim for the end of a string. More...
 
std::string & StringTrim (std::string &str, const std::string &chars="\t\n\v\f\r ")
 Trim from both the start and the end of a string. More...
 
std::string StringTrimCopy (const std::string &str, const std::string &chars="\t\n\v\f\r ")
 Trim from both the start and the end of a string, returns a trimmed copy of the string. More...
 
std::string StringConcat (const std::vector< std::string > &strings, std::string seperator="")
 Takes a vector of strings and concatenates them together into one long std::string with an optional seperator between each. More...
 
void StringReplaceAll (std::string &str, const std::string &oldStr, const std::string &newStr)
 Iterates over a given str and replaces all instance of substring oldStr with newStr. More...
 
bool StringToBool (const std::string &s, bool throw_on_error=true)
 Converts a string to bool. More...
 

Function Documentation

◆ StringConcat()

std::string armnn::stringUtils::StringConcat ( const std::vector< std::string > &  strings,
std::string  seperator = "" 
)
inline

Takes a vector of strings and concatenates them together into one long std::string with an optional seperator between each.

Definition at line 96 of file StringUtils.hpp.

97 {
98  std::stringstream ss;
99  for (auto string : strings)
100  {
101  ss << string << seperator;
102  }
103  return ss.str();
104 }

◆ StringEndTrim()

std::string& armnn::stringUtils::StringEndTrim ( std::string &  str,
const std::string &  chars = "\t\n\v\f\r " 
)
inline

Trim for the end of a string.

Definition at line 71 of file StringUtils.hpp.

72 {
73  str.erase(str.find_last_not_of(chars) + 1);
74  return str;
75 }

Referenced by StringTrim(), and StringTrimCopy().

◆ StringReplaceAll()

void armnn::stringUtils::StringReplaceAll ( std::string &  str,
const std::string &  oldStr,
const std::string &  newStr 
)
inline

Iterates over a given str and replaces all instance of substring oldStr with newStr.

Definition at line 109 of file StringUtils.hpp.

112 {
113  std::string::size_type pos = 0u;
114  while ((pos = str.find(oldStr, pos)) != std::string::npos)
115  {
116  str.replace(pos, oldStr.length(), newStr);
117  pos += newStr.length();
118  }
119 }

◆ StringStartTrim()

std::string& armnn::stringUtils::StringStartTrim ( std::string &  str,
const std::string &  chars = "\t\n\v\f\r " 
)
inline

Trim from the start of a string.

Definition at line 62 of file StringUtils.hpp.

63 {
64  str.erase(0, str.find_first_not_of(chars));
65  return str;
66 }

Referenced by StringTrim(), and StringTrimCopy().

◆ StringToBool()

bool armnn::stringUtils::StringToBool ( const std::string &  s,
bool  throw_on_error = true 
)
inline

Converts a string to bool.

Accepts "true", "false" (case-insensitive) and numbers, 1 (true) or 0 (false).

Parameters
sString to convert to bool
throw_on_errorBool variable to suppress error if conversion failed (Will return false in that case)
Returns
bool value

Definition at line 129 of file StringUtils.hpp.

130 {
131  // in case of failure to convert returns false
132  auto result = false;
133 
134  // isstringstream fails if parsing didn't work
135  std::istringstream is(s);
136 
137  // try integer conversion first. For the case s is a number
138  is >> result;
139 
140  if (is.fail())
141  {
142  // transform to lower case to make case-insensitive
143  std::string s_lower = s;
144  std::transform(s_lower.begin(),
145  s_lower.end(),
146  s_lower.begin(),
147  [](unsigned char c){ return std::tolower(c); });
148  is.str(s_lower);
149  // try boolean -> s="false" or "true"
150  is.clear();
151  is >> std::boolalpha >> result;
152  }
153 
154  if (is.fail() && throw_on_error)
155  {
156  throw armnn::InvalidArgumentException(s + " is not convertable to bool");
157  }
158 
159  return result;
160 }

◆ StringTokenizer()

std::vector<std::string> armnn::stringUtils::StringTokenizer ( const std::string &  str,
const char *  delimiters,
bool  tokenCompression = true 
)
inline

Function to take a string and a list of delimiters and split the string into tokens based on those delimiters This assumes that tokens are also to be split by newlines Enabling tokenCompression merges adjacent delimiters together, preventing empty tokens.

Definition at line 23 of file StringUtils.hpp.

26 {
27  std::stringstream stringStream(str);
28  std::string line;
29  std::vector<std::string> tokenVector;
30  while (std::getline(stringStream, line))
31  {
32  std::size_t prev = 0;
33  std::size_t pos;
34  while ((pos = line.find_first_of(delimiters, prev)) != std::string::npos)
35  {
36  // Ignore adjacent tokens
37  if (pos > prev)
38  {
39  tokenVector.push_back(line.substr(prev, pos - prev));
40  }
41  // Unless token compression is disabled
42  else if (!tokenCompression)
43  {
44  tokenVector.push_back(line.substr(prev, pos - prev));
45  }
46  prev = pos + 1;
47  }
48  if (prev < line.length())
49  {
50  tokenVector.push_back(line.substr(prev, std::string::npos));
51  }
52  }
53  return tokenVector;
54 }

Referenced by DynamicBackendUtils::GetBackendPathsImpl().

◆ StringTrim()

std::string& armnn::stringUtils::StringTrim ( std::string &  str,
const std::string &  chars = "\t\n\v\f\r " 
)
inline

Trim from both the start and the end of a string.

Definition at line 80 of file StringUtils.hpp.

81 {
82  return StringStartTrim(StringEndTrim(str, chars), chars);
83 }

References StringEndTrim(), and StringStartTrim().

◆ StringTrimCopy()

std::string armnn::stringUtils::StringTrimCopy ( const std::string &  str,
const std::string &  chars = "\t\n\v\f\r " 
)
inline

Trim from both the start and the end of a string, returns a trimmed copy of the string.

Definition at line 88 of file StringUtils.hpp.

89 {
90  std::string strCopy = str;
91  return StringStartTrim(StringEndTrim(strCopy, chars), chars);
92 }

References StringEndTrim(), and StringStartTrim().

armnn::stringUtils::StringStartTrim
std::string & StringStartTrim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Trim from the start of a string.
Definition: StringUtils.hpp:62
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::stringUtils::StringEndTrim
std::string & StringEndTrim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Trim for the end of a string.
Definition: StringUtils.hpp:71