From a6c9e64eb559a58d31f5ea68b8947f460ed8f04b Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sat, 12 Feb 2022 03:07:11 +0100 Subject: Better directory structure naming --- Src/StringTools/CMakeLists.txt | 6 +++ Src/StringTools/StringTools.cpp | 100 ++++++++++++++++++++++++++++++++++++++++ Src/StringTools/StringTools.h | 29 ++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 Src/StringTools/CMakeLists.txt create mode 100644 Src/StringTools/StringTools.cpp create mode 100644 Src/StringTools/StringTools.h (limited to 'Src/StringTools') diff --git a/Src/StringTools/CMakeLists.txt b/Src/StringTools/CMakeLists.txt new file mode 100644 index 0000000..885710c --- /dev/null +++ b/Src/StringTools/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.16) +project(Stringtools) + +set(CMAKE_CXX_STANDARD 17) + +add_library(Stringtools StringTools.cpp) diff --git a/Src/StringTools/StringTools.cpp b/Src/StringTools/StringTools.cpp new file mode 100644 index 0000000..bb5faf3 --- /dev/null +++ b/Src/StringTools/StringTools.cpp @@ -0,0 +1,100 @@ +#include "StringTools.h" +#include + +std::string StringTools::Replace(const std::string& str, const char find, const std::string& subst) +{ + std::stringstream ss; + + for (std::size_t i = 0; i < str.length(); i++) + { + if (str[i] != find) + ss << str[i]; + else + ss << subst; + } + + return ss.str(); +} + +std::string StringTools::Replace(const std::string& str, const std::string& find, const std::string& subst) +{ + if (find.length() == 0) + return str; + + std::stringstream ss; + + std::size_t posFound = 0; + std::size_t lastFound = 0; + + while (posFound != std::string::npos) + { + lastFound = posFound; + posFound = str.find(find, posFound); + + if (posFound != std::string::npos) + { + ss << str.substr(lastFound, posFound - lastFound) << subst; + posFound += find.length(); + } + else + { + ss << str.substr(lastFound, (str.length()) - lastFound); + } + } + + return ss.str(); +} + +std::string StringTools::Replace(const std::string& str, const char find, const char subst) +{ + std::stringstream ss; + ss << subst; + + return Replace(str, find, ss.str()); +} + +std::string StringTools::Replace(const std::string& str, const std::string& find, const char subst) +{ + std::stringstream ss; + ss << subst; + + return Replace(str, find, ss.str()); +} + +std::string StringTools::Lower(const std::string& str) +{ + std::stringstream ss; + + for (std::size_t i = 0; i < str.size(); i++) + { + const char c = str[i]; + + // Quick-accept: regular letters + if ((c >= 'A') && (c <= 'Z')) + ss << (char)(c | 32); + + // Else: keep the character as is + else ss << c; + } + + return ss.str(); +} + +std::string StringTools::Upper(const std::string& str) +{ + std::stringstream ss; + + for (std::size_t i = 0; i < str.size(); i++) + { + const char c = str[i]; + + // Quick-accept: regular letters + if ((c >= 'a') && (c <= 'z')) + ss << (char)(c & ~32); + + // Else: keep the character as is + else ss << c; + } + + return ss.str(); +} diff --git a/Src/StringTools/StringTools.h b/Src/StringTools/StringTools.h new file mode 100644 index 0000000..589c047 --- /dev/null +++ b/Src/StringTools/StringTools.h @@ -0,0 +1,29 @@ +#pragma once +#include + +/* Handy utensils to manipulate strings */ +class StringTools +{ +public: + //! Will replace every occurence of `find` in `str` by `subst`. + static std::string Replace(const std::string& str, const char find, const std::string& subst); + + //! Will replace every occurence of `find` in `str` by `subst`. + static std::string Replace(const std::string& str, const std::string& find, const std::string& subst); + + //! Will replace every occurence of `find` in `str` by `subst`. + static std::string Replace(const std::string& str, const char find, const char subst); + + //! Will replace every occurence of `find` in `str` by `subst`. + static std::string Replace(const std::string& str, const std::string& find, const char subst); + + //! Will make a string all-lowercase. Only works with latin and german umlautes, plus some extras. + static std::string Lower(const std::string& str); + + //! Will make a string all-uppercase. Only works with latin and german umlautes, plus some extras. + static std::string Upper(const std::string& str); + +private: + // No instanciation! >:( + StringTools(); +}; -- cgit v1.2.3