#pragma once
#include <string>
#include <string_view>

namespace stringUtils
{

    /// <summary>
    /// Case insensitive compare
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    inline bool iequals(const std::string& a, const std::string& b)
    {
        return std::equal(a.begin(), a.end(),
            b.begin(), b.end(),
            [](char a, char b) {
                return tolower(a) == tolower(b);
            });
    }

    /// <summary>
    /// left trim white space (\r\n etc)
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    inline std::string_view ltrim(std::string_view s)
    {
        s.remove_prefix(std::distance(s.cbegin(), std::find_if(s.cbegin(), s.cend(),
                                                      [](int c) { return !std::isspace(c); })));

        return s;
    }

    /// <summary>
    /// Right trim white space (\r\n etc)
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    inline std::string_view rtrim(std::string_view s)
    {
        s.remove_suffix(std::distance(s.crbegin(), std::find_if(s.crbegin(), s.crend(),
                                                       [](int c) { return !std::isspace(c); })));

        return s;
    }

    /// <summary>
    /// Left Right trim
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    inline std::string_view trim(std::string_view s)
    {
        return ltrim(rtrim(s));
    }
}