ArmNN
 21.02
StringUtils.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <iostream>
9 #include <sstream>
10 
11 namespace armnn
12 {
13 
14 namespace stringUtils
15 {
16 
17 /// Function to take a string and a list of delimiters and split the string into tokens based on those delimiters
18 /// This assumes that tokens are also to be split by newlines
19 /// Enabling tokenCompression merges adjacent delimiters together, preventing empty tokens
20 inline std::vector<std::string> StringTokenizer(const std::string& str,
21  const char* delimiters,
22  bool tokenCompression = true)
23 {
24  std::stringstream stringStream(str);
25  std::string line;
26  std::vector<std::string> tokenVector;
27  while (std::getline(stringStream, line))
28  {
29  std::size_t prev = 0;
30  std::size_t pos;
31  while ((pos = line.find_first_of(delimiters, prev)) != std::string::npos)
32  {
33  // Ignore adjacent tokens
34  if (pos > prev)
35  {
36  tokenVector.push_back(line.substr(prev, pos - prev));
37  }
38  // Unless token compression is disabled
39  else if (!tokenCompression)
40  {
41  tokenVector.push_back(line.substr(prev, pos - prev));
42  }
43  prev = pos + 1;
44  }
45  if (prev < line.length())
46  {
47  tokenVector.push_back(line.substr(prev, std::string::npos));
48  }
49  }
50  return tokenVector;
51 }
52 
53 // Set of 3 utility functions for trimming std::strings
54 // Default char set for common whitespace characters
55 
56 ///
57 /// Trim from the start of a string
58 ///
59 inline std::string& StringStartTrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
60 {
61  str.erase(0, str.find_first_not_of(chars));
62  return str;
63 }
64 
65 ///
66 /// Trim for the end of a string
67 ///
68 inline std::string& StringEndTrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
69 {
70  str.erase(str.find_last_not_of(chars) + 1);
71  return str;
72 }
73 
74 ///
75 /// Trim from both the start and the end of a string
76 ///
77 inline std::string& StringTrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
78 {
79  return StringStartTrim(StringEndTrim(str, chars), chars);
80 }
81 
82 ///
83 /// Trim from both the start and the end of a string, returns a trimmed copy of the string
84 ///
85 inline std::string StringTrimCopy(const std::string& str, const std::string& chars = "\t\n\v\f\r ")
86 {
87  std::string strCopy = str;
88  return StringStartTrim(StringEndTrim(strCopy, chars), chars);
89 }
90 
91 /// Takes a vector of strings and concatenates them together into one long std::string with an optional
92 /// seperator between each.
93 inline std::string StringConcat(const std::vector<std::string>& strings, std::string seperator = "")
94 {
95  std::stringstream ss;
96  for (auto string : strings)
97  {
98  ss << string << seperator;
99  }
100  return ss.str();
101 }
102 
103 ///
104 /// Iterates over a given str and replaces all instance of substring oldStr with newStr
105 ///
106 inline void StringReplaceAll(std::string& str,
107  const std::string& oldStr,
108  const std::string& newStr)
109 {
110  std::string::size_type pos = 0u;
111  while ((pos = str.find(oldStr, pos)) != std::string::npos)
112  {
113  str.replace(pos, oldStr.length(), newStr);
114  pos += newStr.length();
115  }
116 }
117 
118 } // namespace stringUtils
119 
120 } // namespace armnn
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 de...
Definition: StringUtils.hpp:20
std::string & StringEndTrim(std::string &str, const std::string &chars="\\\")
Trim for the end of a string.
Definition: StringUtils.hpp:68
Copyright (c) 2021 ARM Limited and Contributors.
std::string StringTrimCopy(const std::string &str, const std::string &chars="\\\")
Trim from both the start and the end of a string, returns a trimmed copy of the string.
Definition: StringUtils.hpp:85
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.
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 s...
Definition: StringUtils.hpp:93
std::string & StringStartTrim(std::string &str, const std::string &chars="\\\")
Trim from the start of a string.
Definition: StringUtils.hpp:59
std::string & StringTrim(std::string &str, const std::string &chars="\\\")
Trim from both the start and the end of a string.
Definition: StringUtils.hpp:77