142 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			142 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
 | 
						|
#pragma once
 | 
						|
 | 
						|
#if !defined(MIJIN_UTIL_STRING_HPP_INCLUDED)
 | 
						|
#define MIJIN_UTIL_STRING_HPP_INCLUDED 1
 | 
						|
 | 
						|
#include <iterator>
 | 
						|
#include <ranges>
 | 
						|
#include <sstream>
 | 
						|
#include <string>
 | 
						|
#include <string_view>
 | 
						|
#include <vector>
 | 
						|
 | 
						|
#include "./iterators.hpp"
 | 
						|
 | 
						|
namespace mijin
 | 
						|
{
 | 
						|
 | 
						|
//
 | 
						|
// public defines
 | 
						|
//
 | 
						|
 | 
						|
//
 | 
						|
// public constants
 | 
						|
//
 | 
						|
 | 
						|
//
 | 
						|
// public types
 | 
						|
//
 | 
						|
 | 
						|
//
 | 
						|
// public functions
 | 
						|
//
 | 
						|
 | 
						|
template <typename TRange, typename TValue = typename TRange::value_type>
 | 
						|
[[nodiscard]] std::string join(const TRange& elements, const char* const delimiter)
 | 
						|
{
 | 
						|
    std::ostringstream oss;
 | 
						|
    auto first = std::begin(elements);
 | 
						|
    auto last = std::end(elements);
 | 
						|
 | 
						|
    if (first != last)
 | 
						|
    {
 | 
						|
        std::copy(first, std::prev(last), std::ostream_iterator<TValue>(oss, delimiter));
 | 
						|
        first = std::prev(last);
 | 
						|
    }
 | 
						|
    if (first != last)
 | 
						|
    {
 | 
						|
        oss << *first;
 | 
						|
    }
 | 
						|
 | 
						|
    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>
 | 
						|
bool equalsIgnoreCaseImpl(std::basic_string_view<TChar, TTraitsA> stringA, std::basic_string_view<TChar, TTraitsB> stringB) noexcept
 | 
						|
{
 | 
						|
    if (stringA.size() != stringB.size())
 | 
						|
    {
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
 | 
						|
    for (const auto [charA, charB] : zip(stringA, stringB))
 | 
						|
    {
 | 
						|
        if (std::tolower(charA) != std::tolower(charB))
 | 
						|
        {
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    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
 | 
						|
{
 | 
						|
struct Join
 | 
						|
{
 | 
						|
    const char* delimiter;
 | 
						|
 | 
						|
    explicit Join(const char* delimiter_) noexcept : delimiter(delimiter_) {}
 | 
						|
};
 | 
						|
 | 
						|
template<typename TIterable>
 | 
						|
auto operator|(TIterable&& iterable, const Join& joiner)
 | 
						|
{
 | 
						|
    return join(std::forward<TIterable>(iterable), joiner.delimiter);
 | 
						|
}
 | 
						|
}
 | 
						|
} // namespace mijin
 | 
						|
 | 
						|
#endif // !defined(MIJIN_UTIL_STRING_HPP_INCLUDED)
 |