From 40e231bc29e453ecb685b02204cec46c458b80d7 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 24 Jan 2015 09:39:14 +0100 Subject: StringUtils: Added string vector manipulation. --- src/StringUtils.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/StringUtils.h | 11 ++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp index a63525356..48486a762 100644 --- a/src/StringUtils.cpp +++ b/src/StringUtils.cpp @@ -905,3 +905,47 @@ bool SplitZeroTerminatedStrings(const AString & a_Strings, AStringVector & a_Out + +AStringVector MergeStringVectors(const AStringVector & a_Strings1, const AStringVector & a_Strings2) +{ + // Initialize the resulting vector by the first vector: + AStringVector res = a_Strings1; + + // Add each item from strings2 that is not already present: + for (auto item : a_Strings2) + { + if (std::find(res.begin(), res.end(), item) != res.end()) + { + res.push_back(item); + } + } // for item - a_Strings2[] + + return res; +} + + + + + +AString StringsConcat(const AStringVector & a_Strings, char a_Separator) +{ + // If the vector is empty, return an empty string: + if (a_Strings.empty()) + { + return ""; + } + + // Concatenate the strings in the vector: + AString res; + res.append(a_Strings[0]); + for (auto itr = a_Strings.cbegin() + 1, end = a_Strings.cend(); itr != end; ++itr) + { + res.push_back(a_Separator); + res.append(*itr); + } + return res; +} + + + + diff --git a/src/StringUtils.h b/src/StringUtils.h index bfe2a41fa..bc3bb7a2c 100644 --- a/src/StringUtils.h +++ b/src/StringUtils.h @@ -115,7 +115,16 @@ a_Output is first cleared and then each separate string is pushed back into a_Ou Returns true if there are at least two strings in a_Output (there was at least one \0 separator). */ extern bool SplitZeroTerminatedStrings(const AString & a_Strings, AStringVector & a_Output); -/// Parses any integer type. Checks bounds and returns errors out of band. +/** Merges the two vectors of strings, removing duplicate entries from the second vector. +The resulting vector contains items from a_Strings1 first, then from a_Strings2. +The order of items doesn't change, only the duplicates are removed. +If a_Strings1 contains duplicates, the result will still contain those duplicates. */ +extern AStringVector MergeStringVectors(const AStringVector & a_Strings1, const AStringVector & a_Strings2); + +/** Concatenates the specified strings into a single string, separated by the specified separator. */ +extern AString StringsConcat(const AStringVector & a_Strings, char a_Separator); + +/** Parses any integer type. Checks bounds and returns errors out of band. */ template bool StringToInteger(const AString & a_str, T & a_Num) { -- cgit v1.2.3