#include "VrStringUtils.h"
#include <sstream>
#include <algorithm>
#include <iomanip>

/// \brief
/// 将数字转换为字符串
/// \param nNumber[in] 数字
/// \return 返回字符串
std::string CVrStringUtils::Int2Str(int nNum)
{
	std::stringstream ss;
	ss << nNum;
	return ss.str();
}

std::string CVrStringUtils::Int2HexStr(int nNum)
{
	std::stringstream ss;
	ss << std::hex << std::setw(2) << std::setfill('0') << std::uppercase << nNum;  // 使用 std::hex 标志将整数以十六进制形式输出到流中
	return ss.str();  // 使用 str() 方法获取流中的字符串表示
}

std::string CVrStringUtils::Int2Str(unsigned int nNum)
{
	std::stringstream ss;
	ss << nNum;
	return ss.str();
}

std::string CVrStringUtils::Int2Str(unsigned long long nNum)
{
	std::stringstream ss;
	ss << nNum;
	return ss.str();
}

std::string CVrStringUtils::Int2str(long num)
{
	std::stringstream ss;
	ss << num;
	return ss.str();
}

std::vector<std::string> CVrStringUtils::SplitString(std::string srcStr, std::string delimStr, bool repeatedCharIgnored)
{
	std::vector<std::string> resultStringVector;
	std::replace_if(srcStr.begin(), srcStr.end(), [&](const char& c) {if (delimStr.find(c) != std::string::npos) { return true; } else { return false; }}/*pred*/, delimStr.at(0));//将出现的所有分隔符都替换成为一个相同的字符（分隔符字符串的第一个）
	size_t pos = srcStr.find(delimStr.at(0));
	std::string addedString = "";
	while (pos != std::string::npos) {
		addedString = srcStr.substr(0, pos);
		if (!addedString.empty() || !repeatedCharIgnored) {
			resultStringVector.push_back(addedString);
		}
		srcStr.erase(srcStr.begin(), srcStr.begin() + pos + 1);
		pos = srcStr.find(delimStr.at(0));
	}
	addedString = srcStr;
	if (!addedString.empty() || !repeatedCharIgnored) {
		resultStringVector.push_back(addedString);
	}
	return resultStringVector;
}


int CVrStringUtils::StringUpper(char *sData, int nLen)
{
	if (sData != nullptr && nLen != 0)
	{
		for (int i = 0; i < nLen; i++)
		{
			sData[i] = toupper(sData[i]);
		}
	}
	return 0;
}

int CVrStringUtils::StringLower(char *sData, int nLen)
{
	if (sData != nullptr && nLen != 0)
	{
		for (int i = 0; i < nLen; i++)
		{
			sData[i] = tolower(sData[i]);
		}
	}
	return 0;
}

/// 替换字符串
void CVrStringUtils::ReplaceString(std::string& src, const std::string& repSrc, const std::string& repDest)
{
	size_t pos = 0;
	// 找到子字符串的起始位置
	while ((pos = src.find(repSrc, pos)) != std::string::npos)
	{
		// 替换找到的子字符串
		src.replace(pos, repSrc.length(), repDest);
		// 移动到替换后的位置，避免重新替换已经替换的部分
		pos += repDest.length();
	}
}

