#include "../pch/pch.h" #include "CLewaimaiString.h" #include #include #include #include #include #include using namespace std; using namespace boost::archive::iterators; unsigned char ToHex(unsigned char x) { return x > 9 ? x + 55 : x + 48; } unsigned char FromHex(unsigned char x) { unsigned char y; if(x >= 'A' && x <= 'Z') { y = x - 'A' + 10; } else if(x >= 'a' && x <= 'z') { y = x - 'a' + 10; } else if(x >= '0' && x <= '9') { y = x - '0'; } else { assert(0); } return y; } CLewaimaiString::CLewaimaiString() { } CLewaimaiString::~CLewaimaiString() { } bool CLewaimaiString::base64_encode(const string& input, string* output) { typedef base64_from_binary> Base64EncodeIterator; stringstream result; try { copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), ostream_iterator(result)); } catch(...) { return false; } size_t equal_count = (3 - input.length() % 3) % 3; for(size_t i = 0; i < equal_count; i++) { result.put('='); } *output = result.str(); return output->empty() == false; } bool CLewaimaiString::base64_decode(const string& input, string* output) { typedef transform_width, 8, 6> Base64DecodeIterator; stringstream result; try { copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), ostream_iterator(result)); } catch(...) { return false; } *output = result.str(); return output->empty() == false; } void CLewaimaiString::trim(string& s) { if(!s.empty()) { s.erase(0, s.find_first_not_of(" ")); s.erase(s.find_last_not_of(" ") + 1); } } std::string CLewaimaiString::UrlEncode(const std::string& str) { std::string strTemp = ""; size_t length = str.length(); for(size_t i = 0; i < length; i++) { if(isalnum((unsigned char)str[i]) || (str[i] == '-') || (str[i] == '_') || (str[i] == '.') || (str[i] == '~')) { strTemp += str[i]; } else if(str[i] == ' ') { strTemp += "+"; } else { strTemp += '%'; strTemp += ToHex((unsigned char)str[i] >> 4); strTemp += ToHex((unsigned char)str[i] % 16); } } return strTemp; } std::string CLewaimaiString::UrlDecode(const std::string& str) { std::string strTemp = ""; size_t length = str.length(); for(size_t i = 0; i < length; i++) { if(str[i] == '+') { strTemp += ' '; } else if(str[i] == '%') { assert(i + 2 < length); unsigned char high = FromHex((unsigned char)str[++i]); unsigned char low = FromHex((unsigned char)str[++i]); strTemp += high * 16 + low; } else { strTemp += str[i]; } } return strTemp; } std::string CLewaimaiString::UnicodeToUTF8(const std::wstring& wstr) { std::string ret; try { std::wstring_convert< std::codecvt_utf8 > wcv; ret = wcv.to_bytes(wstr); } catch(const std::exception& e) { std::cerr << e.what() << std::endl; } return ret; } std::wstring CLewaimaiString::UTF8ToUnicode(const std::string& str) { std::wstring ret; try { std::wstring_convert< std::codecvt_utf8 > wcv; ret = wcv.from_bytes(str); } catch(const std::exception& e) { std::cerr << e.what() << std::endl; } return ret; } std::string CLewaimaiString::UnicodeToANSI(const std::wstring& wstr) { unsigned len = wstr.size() * 4; if (len == 0) { return ""; } setlocale(LC_CTYPE, ""); char *p = new char[len]; wcstombs(p, wstr.c_str(), len); std::string str1(p); delete[] p; return str1; } std::wstring CLewaimaiString::ANSIToUnicode(const std::string& str) { unsigned len = str.size() * 2;// 预留字节数 if (len == 0) { return L""; } setlocale(LC_CTYPE, ""); //必须调用此函数 wchar_t *p = new wchar_t[len];// 申请一段内存存放转换后的字符串 mbstowcs(p, str.c_str(), len);// 转换 std::wstring str1(p); delete[] p;// 释放申请的内存 return str1; } std::string CLewaimaiString::UTF8ToANSI(const std::string& str) { return UnicodeToANSI(UTF8ToUnicode(str)); } std::string CLewaimaiString::ANSIToUTF8(const std::string& str) { return UnicodeToUTF8(ANSIToUnicode(str)); } std::string CLewaimaiString::DoubleToString(const double value, unsigned int precisionAfterPoint) { char str[256]; std::string pre = "%." + to_string(precisionAfterPoint) + "f"; sprintf(str, pre.c_str(), value); string result = str; return result; } bool CLewaimaiString::isIPAddressValid(const char* pszIPAddr) { if(!pszIPAddr) { return false; //若pszIPAddr为空 } char IP1[100], cIP[4]; int len = strlen(pszIPAddr); int i = 0, j = len - 1; int k, m = 0, n = 0, num = 0; //去除首尾空格(取出从i-1到j+1之间的字符): while(pszIPAddr[i++] == ' '); while(pszIPAddr[j--] == ' '); for(k = i - 1; k <= j + 1; k++) { IP1[m++] = *(pszIPAddr + k); } IP1[m] = '\0'; char* p = IP1; while(*p != '\0') { if(*p == ' ' || *p < '0' || *p > '9') { return false; } cIP[n++] = *p; //保存每个子段的第一个字符,用于之后判断该子段是否为0开头 int sum = 0; //sum为每一子段的数值,应在0到255之间 while(*p != '.' && *p != '\0') { if(*p == ' ' || *p < '0' || *p > '9') { return false; } sum = sum * 10 + *p - 48; //每一子段字符串转化为整数 p++; } if(*p == '.') { if((*(p - 1) >= '0' && *(p - 1) <= '9') && (*(p + 1) >= '0' && *(p + 1) <= '9')) //判断"."前后是否有数字,若无,则为无效IP,如“1.1.127.” { num++; //记录“.”出现的次数,不能大于3 } else { return false; } }; if((sum > 255) || (sum > 0 && cIP[0] == '0') || num > 3) { return false; //若子段的值>255或为0开头的非0子段或“.”的数目>3,则为无效IP } if(*p != '\0') { p++; } n = 0; } if(num != 3) { return false; } return true; } vector CLewaimaiString::Split(const string& in, const string& delim) { vector ret; try { regex re{ delim }; return vector { sregex_token_iterator(in.begin(), in.end(), re, -1), sregex_token_iterator() }; } catch(const std::exception& e) { cout << "error:" << e.what() << std::endl; } return ret; } /* *nNum是表示替换几个,默认为1表示替换第1个 **/ int CLewaimaiString::Replace(std::wstring& strContent, std::wstring strReplace, std::wstring strDest, int nNum) { int nCount = 0; while(true) { size_t pos = strContent.find(strReplace); if(pos != std::wstring::npos) { WCHAR pBuf[1] = { L'\0' }; strContent.replace(pos, strReplace.length(), pBuf, 0); strContent.insert(pos, strDest); nCount++; if(nCount == nNum) { break; } } else { break; } } return 0; }