aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorDavid Monahan <david.monahan@arm.com>2020-04-16 10:01:56 +0100
committerDavid Monahan <david.monahan@arm.com>2020-04-20 12:20:18 +0100
commita8837bfcf45136f178a9884b7c6f6449b3e6ed41 (patch)
tree0fa2c4de3646d27f1029d5ebd2cf966c3005522c /include
parent9b89e0a5e4398672c3b9423b02b3a9257e0ea95f (diff)
downloadarmnn-a8837bfcf45136f178a9884b7c6f6449b3e6ed41.tar.gz
IVGCVSW-4513 Remove boost/algorithm/string *
* Removed split, classification, trim, string, join, contains * Added StringUtils.hpp to replace the removed Boost String functionality Signed-off-by: David Monahan <david.monahan@arm.com> Change-Id: I8aa938dc3942cb65c512cccb2c069da66aa24668
Diffstat (limited to 'include')
-rw-r--r--include/armnn/utility/StringUtils.hpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/include/armnn/utility/StringUtils.hpp b/include/armnn/utility/StringUtils.hpp
new file mode 100644
index 0000000000..c56a215b83
--- /dev/null
+++ b/include/armnn/utility/StringUtils.hpp
@@ -0,0 +1,120 @@
+//
+// Copyright © 2020 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#pragma once
+
+#include <iostream>
+#include <sstream>
+
+namespace armnn
+{
+
+namespace stringUtils
+{
+
+/// 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 token compression causes this to ignore multiple concurrent delimiters
+inline std::vector<std::string> StringTokenizer(const std::string& str,
+ const char* delimiters,
+ bool tokenCompression = true)
+{
+ std::stringstream stringStream(str);
+ std::string line;
+ std::vector<std::string> wordVector;
+ while (std::getline(stringStream, line))
+ {
+ std::size_t prev = 0;
+ std::size_t pos;
+ while ((pos = line.find_first_of(delimiters, prev)) != std::string::npos)
+ {
+ if (pos > prev)
+ {
+ // If token compression is enabled ignore delimiters that are next to each other
+ if (tokenCompression && (pos - prev == 1))
+ {
+ prev = pos + 1;
+ continue;
+ }
+ wordVector.push_back(line.substr(prev, pos - prev));
+ }
+ prev = pos + 1;
+ }
+ if (prev < line.length())
+ {
+ wordVector.push_back(line.substr(prev, std::string::npos));
+ }
+ }
+ return wordVector;
+}
+
+// Set of 3 utility functions for trimming std::strings
+// Default char set for common whitespace characters
+
+///
+/// Trim from the start of a string
+///
+inline std::string& StringStartTrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
+{
+ str.erase(0, str.find_first_not_of(chars));
+ return str;
+}
+
+///
+/// Trim for the end of a string
+///
+inline std::string& StringEndTrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
+{
+ str.erase(str.find_last_not_of(chars) + 1);
+ return str;
+}
+
+///
+/// Trim from both the start and the end of a string
+///
+inline std::string& StringTrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
+{
+ return StringStartTrim(StringEndTrim(str, chars), chars);
+}
+
+///
+/// Trim from both the start and the end of a string, returns a trimmed copy of the string
+///
+inline std::string StringTrimCopy(const std::string& str, const std::string& chars = "\t\n\v\f\r ")
+{
+ std::string strCopy = str;
+ return StringStartTrim(StringEndTrim(strCopy, chars), chars);
+}
+
+/// Takes a vector of strings and concatenates them together into one long std::string with an optional
+/// seperator between each.
+inline std::string StringConcat(const std::vector<std::string>& strings, std::string seperator = "")
+{
+ std::stringstream ss;
+ for (auto string : strings)
+ {
+ ss << string << seperator;
+ }
+ return ss.str();
+}
+
+///
+/// Iterates over a given str and replaces all instance of substring oldStr with newStr
+///
+inline void StringReplaceAll(std::string& str,
+ const std::string& oldStr,
+ const std::string& newStr)
+{
+ std::string::size_type pos = 0u;
+ while ((pos = str.find(oldStr, pos)) != std::string::npos)
+ {
+ str.replace(pos, oldStr.length(), newStr);
+ pos += newStr.length();
+ }
+}
+
+} // namespace stringUtils
+
+} // namespace armnn \ No newline at end of file