From 321fec8c1694288832113b0275fcf51f03f501a2 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Fri, 19 Nov 2021 01:39:50 +0100 Subject: [PATCH] Added getStringBetween() --- src/strutils.cpp | 16 ++++++++++++++++ src/strutils.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/src/strutils.cpp b/src/strutils.cpp index b8359c9..b26f363 100644 --- a/src/strutils.cpp +++ b/src/strutils.cpp @@ -115,4 +115,20 @@ std::string stringReplaceAll(std::string_view search, std::string_view replace, } return str; } + +std::optional getStringBetween(std::string_view search, std::string_view prefix, std::string_view suffix) +{ + auto beginIndex = search.find(prefix); + if (beginIndex == std::string_view::npos) + return std::nullopt; + + auto restView = search.substr(beginIndex + prefix.size()); + + auto endIndex = restView.find(suffix); + if (endIndex == std::string_view::npos) + return std::nullopt; + + return restView.substr(0, endIndex); +} + } // namespace cpputils diff --git a/src/strutils.h b/src/strutils.h index 6eb474b..64d74bd 100644 --- a/src/strutils.h +++ b/src/strutils.h @@ -47,6 +47,8 @@ std::string stringReplaceAll(char search, std::string_view replace, std::string_ //std::string stringReplaceAll(std::string_view search, char replace, std::string_view subject); std::string stringReplaceAll(std::string_view search, std::string_view replace, std::string_view subject); +std::optional getStringBetween(std::string_view search, std::string_view prefix, std::string_view suffix); + constexpr const std::string_view allDigitsAndCharacters = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"