Added split() and trim() string utilty functions.
This commit is contained in:
parent
9337ad7ddb
commit
def91ac1bf
@ -5,8 +5,11 @@
|
|||||||
#define MIJIN_UTIL_STRING_HPP_INCLUDED 1
|
#define MIJIN_UTIL_STRING_HPP_INCLUDED 1
|
||||||
|
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <ranges>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "./iterators.hpp"
|
#include "./iterators.hpp"
|
||||||
|
|
||||||
@ -49,8 +52,19 @@ template <typename TRange, typename TValue = typename TRange::value_type>
|
|||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
template<typename TChar, typename TTraits>
|
||||||
|
std::vector<std::basic_string_view<TChar, TTraits>> splitImpl(std::basic_string_view<TChar, TTraits> stringView,
|
||||||
|
std::basic_string_view<TChar, TTraits> seperator)
|
||||||
|
{
|
||||||
|
return std::views::split(stringView, seperator)
|
||||||
|
| std::views::transform([](auto val) { return std::string_view(val); })
|
||||||
|
| std::ranges::to<std::vector>();
|
||||||
|
}
|
||||||
|
|
||||||
template<typename TChar, typename TTraitsA, typename TTraitsB>
|
template<typename TChar, typename TTraitsA, typename TTraitsB>
|
||||||
[[nodiscard]] bool equalsIgnoreCase(std::basic_string_view<TChar, TTraitsA> stringA, std::basic_string_view<TChar, TTraitsB> stringB) noexcept
|
bool equalsIgnoreCaseImpl(std::basic_string_view<TChar, TTraitsA> stringA, std::basic_string_view<TChar, TTraitsB> stringB) noexcept
|
||||||
{
|
{
|
||||||
if (stringA.size() != stringB.size())
|
if (stringA.size() != stringB.size())
|
||||||
{
|
{
|
||||||
@ -68,6 +82,45 @@ template<typename TChar, typename TTraitsA, typename TTraitsB>
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename TChar>
|
||||||
|
static const TChar SPACE = TChar(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TLeft, typename TRight>
|
||||||
|
[[nodiscard]] auto split(TLeft&& left, TRight&& right)
|
||||||
|
{
|
||||||
|
return detail::splitImpl(std::basic_string_view(std::forward<TLeft>(left)), std::basic_string_view(std::forward<TRight>(right)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits>
|
||||||
|
std::basic_string_view<TChar, TTraits> trimPrefix(std::basic_string_view<TChar, TTraits> stringView,
|
||||||
|
std::basic_string_view<TChar, TTraits> charsToTrim = {&detail::SPACE<TChar>, &detail::SPACE<TChar> + 1})
|
||||||
|
{
|
||||||
|
stringView.remove_prefix(std::min(stringView.find_first_not_of(charsToTrim), stringView.size()));
|
||||||
|
return stringView;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits>
|
||||||
|
std::basic_string_view<TChar, TTraits> trimSuffix(std::basic_string_view<TChar, TTraits> stringView,
|
||||||
|
std::basic_string_view<TChar, TTraits> charsToTrim = {&detail::SPACE<TChar>, &detail::SPACE<TChar> + 1})
|
||||||
|
{
|
||||||
|
stringView.remove_suffix(stringView.size() - std::min(stringView.find_last_not_of(charsToTrim) + 1, stringView.size()));
|
||||||
|
return stringView;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TChar, typename TTraits>
|
||||||
|
std::basic_string_view<TChar, TTraits> trim(std::basic_string_view<TChar, TTraits> stringView,
|
||||||
|
std::basic_string_view<TChar, TTraits> charsToTrim = {&detail::SPACE<TChar>, &detail::SPACE<TChar> + 1})
|
||||||
|
{
|
||||||
|
return trimPrefix(trimSuffix(stringView, charsToTrim), charsToTrim);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TLeft, typename TRight>
|
||||||
|
[[nodiscard]] bool equalsIgnoreCase(TLeft&& left, TRight&& right) noexcept
|
||||||
|
{
|
||||||
|
return detail::equalsIgnoreCaseImpl(std::string_view(left), std::string_view(right));
|
||||||
|
}
|
||||||
|
|
||||||
namespace pipe
|
namespace pipe
|
||||||
{
|
{
|
||||||
struct Join
|
struct Join
|
||||||
|
Loading…
x
Reference in New Issue
Block a user