From de73d25e0b27f4bee2de116d19cab32800785d64 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 2 Oct 2015 14:56:07 +0200 Subject: 7.2 --- zen/string_tools.h | 79 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 31 deletions(-) (limited to 'zen/string_tools.h') diff --git a/zen/string_tools.h b/zen/string_tools.h index 03094c96..c04adf96 100644 --- a/zen/string_tools.h +++ b/zen/string_tools.h @@ -30,10 +30,16 @@ template bool startsWith(const S& str, const T& prefix); // template bool endsWith (const S& str, const T& postfix); //both S and T can be strings or char/wchar_t arrays or simple char/wchar_t template bool contains (const S& str, const T& term); // -template S afterLast (const S& str, const T& term); //returns the whole string if term not found -template S beforeLast (const S& str, const T& term); //returns empty string if term not found -template S afterFirst (const S& str, const T& term); //returns empty string if term not found -template S beforeFirst(const S& str, const T& term); //returns the whole string if term not found +enum FailureReturnVal +{ + IF_MISSING_RETURN_ALL, + IF_MISSING_RETURN_NONE +}; + +template S afterLast (const S& str, const T& term, FailureReturnVal rv); +template S beforeLast (const S& str, const T& term, FailureReturnVal rv); +template S afterFirst (const S& str, const T& term, FailureReturnVal rv); +template S beforeFirst(const S& str, const T& term, FailureReturnVal rv); template std::vector split(const S& str, const T& delimiter); template void trim ( S& str, bool fromLeft = true, bool fromRight = true); @@ -96,6 +102,7 @@ template <> inline bool isAlpha(wchar_t ch) { return std::iswalpha(ch) != 0; } template inline bool startsWith(const S& str, const T& prefix) { + static_assert(IsSameType::Type, typename GetCharType::Type>::value, ""); const size_t pfLen = strLength(prefix); if (strLength(str) < pfLen) return false; @@ -109,6 +116,7 @@ bool startsWith(const S& str, const T& prefix) template inline bool endsWith(const S& str, const T& postfix) { + static_assert(IsSameType::Type, typename GetCharType::Type>::value, ""); const size_t strLen = strLength(str); const size_t pfLen = strLength(postfix); if (strLen < pfLen) @@ -123,6 +131,7 @@ bool endsWith(const S& str, const T& postfix) template inline bool contains(const S& str, const T& term) { + static_assert(IsSameType::Type, typename GetCharType::Type>::value, ""); const size_t strLen = strLength(str); const size_t termLen = strLength(term); if (strLen < termLen) @@ -137,77 +146,83 @@ bool contains(const S& str, const T& term) } -//returns the whole string if term not found template inline -S afterLast(const S& str, const T& term) +S afterLast(const S& str, const T& term, FailureReturnVal rv) { + static_assert(IsSameType::Type, typename GetCharType::Type>::value, ""); const size_t termLen = strLength(term); const auto* const strFirst = strBegin(str); const auto* const strLast = strFirst + strLength(str); const auto* const termFirst = strBegin(term); - const auto* iter = search_last(strFirst, strLast, - termFirst, termFirst + termLen); - if (iter == strLast) - return str; + const auto* it = search_last(strFirst, strLast, + termFirst, termFirst + termLen); + if (it == strLast) + return rv == IF_MISSING_RETURN_ALL ? str : S(); - iter += termLen; - return S(iter, strLast - iter); + it += termLen; + return S(it, strLast - it); } -//returns empty string if term not found template inline -S beforeLast(const S& str, const T& term) +S beforeLast(const S& str, const T& term, FailureReturnVal rv) { + static_assert(IsSameType::Type, typename GetCharType::Type>::value, ""); const auto* const strFirst = strBegin(str); const auto* const strLast = strFirst + strLength(str); const auto* const termFirst = strBegin(term); - const auto* iter = search_last(strFirst, strLast, - termFirst, termFirst + strLength(term)); - if (iter == strLast) - return S(); + const auto* it = search_last(strFirst, strLast, + termFirst, termFirst + strLength(term)); + if (it == strLast) + return rv == IF_MISSING_RETURN_ALL ? str : S(); - return S(strFirst, iter - strFirst); + return S(strFirst, it - strFirst); } -//returns empty string if term not found template inline -S afterFirst(const S& str, const T& term) +S afterFirst(const S& str, const T& term, FailureReturnVal rv) { + static_assert(IsSameType::Type, typename GetCharType::Type>::value, ""); const size_t termLen = strLength(term); const auto* const strFirst = strBegin(str); const auto* const strLast = strFirst + strLength(str); const auto* const termFirst = strBegin(term); - const auto* iter = std::search(strFirst, strLast, - termFirst, termFirst + termLen); - if (iter == strLast) - return S(); - iter += termLen; + const auto* it = std::search(strFirst, strLast, + termFirst, termFirst + termLen); + if (it == strLast) + return rv == IF_MISSING_RETURN_ALL ? str : S(); - return S(iter, strLast - iter); + it += termLen; + return S(it, strLast - it); } -//returns the whole string if term not found template inline -S beforeFirst(const S& str, const T& term) +S beforeFirst(const S& str, const T& term, FailureReturnVal rv) { + static_assert(IsSameType::Type, typename GetCharType::Type>::value, ""); const auto* const strFirst = strBegin(str); + const auto* const strLast = strFirst + strLength(str); const auto* const termFirst = strBegin(term); - return S(strFirst, std::search(strFirst, strFirst + strLength(str), - termFirst, termFirst + strLength(term)) - strFirst); + auto it = std::search(strFirst, strLast, + termFirst, termFirst + strLength(term)); + if (it == strLast) + return rv == IF_MISSING_RETURN_ALL ? str : S(); + + return S(strFirst, it - strFirst); } template inline std::vector split(const S& str, const T& delimiter) { + static_assert(IsSameType::Type, typename GetCharType::Type>::value, ""); std::vector output; const size_t delimLen = strLength(delimiter); @@ -253,6 +268,8 @@ typename EnableIf::value>::Type stringAppend(S& str, const template inline S replaceCpy(const S& str, const T& oldTerm, const U& newTerm, bool replaceAll) { + static_assert(IsSameType::Type, typename GetCharType::Type>::value, ""); + static_assert(IsSameType::Type, typename GetCharType::Type>::value, ""); const size_t oldLen = strLength(oldTerm); if (oldLen == 0) { -- cgit