#include "StringUtils.h"
#include <locale>
#include <algorithm>
#include <sstream>
#include <iomanip>

#ifdef _WIN32
#include "Windows.h"
#endif

bool StringUtils::IsAlphaNumeric(char c)
{
    std::locale loc;
    return std::isalnum(c, loc);
}

bool StringUtils::IsSpace(char c)
{
    std::locale loc;
    return std::isspace(c, loc);
}

std::string StringUtils::ToLower(const std::string& s)
{
    std::string ret;
    std::transform(s.begin(), s.end(), ret.begin(),
            [](unsigned char c){ return std::tolower(c); });
    return ret;
}

std::string StringUtils::convert(const std::wstring& input)
{
    if (input.empty())
    {
        return std::string();
    }

#ifdef _WIN32
    const auto size = ::WideCharToMultiByte(CP_UTF8, 0, &input[0], 
        (int)input.size(), nullptr, 0, nullptr, nullptr);

    std::string result(size, 0);
    ::WideCharToMultiByte(CP_UTF8, 0, &input[0], (int)input.size(), 
        &result[0], size, nullptr, nullptr);
    return result;
#else
    throw std::logic_error("Not implemented");
#endif
}

std::string StringUtils::ToPaddedString(unsigned numBytes, unsigned entry)
{
    std::stringstream sstr;
    sstr << std::setfill('0') << std::setw(numBytes) << entry;
    return sstr.str();
}
