mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
update cppcommon for splitStr
This commit is contained in:
parent
2319fd9307
commit
a8374bd2b7
@ -6,227 +6,231 @@
|
|||||||
|
|
||||||
namespace CPPCOMMON
|
namespace CPPCOMMON
|
||||||
{
|
{
|
||||||
//http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
|
//http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
|
||||||
string string_format(const string fmt, ...)
|
string string_format(const string fmt, ...)
|
||||||
{
|
{
|
||||||
int size = 100;
|
int size = 100;
|
||||||
std::string str;
|
std::string str;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
while (1) {
|
while (1) {
|
||||||
str.resize(size);
|
str.resize(size);
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
int n = vsnprintf((char *)str.c_str(), size, fmt.c_str(), ap);
|
int n = vsnprintf((char *)str.c_str(), size, fmt.c_str(), ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
if (n > -1 && n < size) {
|
if (n > -1 && n < size) {
|
||||||
str.resize(n);
|
str.resize(n);
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
if (n > -1)
|
if (n > -1)
|
||||||
size = n + 1;
|
size = n + 1;
|
||||||
else
|
else
|
||||||
size *= 2;
|
size *= 2;
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
string joinStr(const vector<string>& src, const string& connectorStr)
|
string joinStr(const vector<string>& src, const string& connectorStr)
|
||||||
{
|
{
|
||||||
string res = "";
|
string res = "";
|
||||||
int len = src.size();
|
int len = src.size();
|
||||||
if(0 == len)
|
if(0 == len)
|
||||||
{
|
{
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
for(int i = 0; i < len - 1; i++)
|
for(int i = 0; i < len - 1; i++)
|
||||||
{
|
{
|
||||||
res += stripStr(src[i]);
|
res += stripStr(src[i]);
|
||||||
res += connectorStr;
|
res += connectorStr;
|
||||||
}
|
}
|
||||||
res += stripStr(src[len-1]);
|
res += stripStr(src[len-1]);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
vector<string> splitStr(const string& source, const string& pattern)
|
|
||||||
{
|
|
||||||
vector<string> res;
|
|
||||||
splitStr(source, res, pattern);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
void splitStr(const string& source, vector<string>& out_vec, const string& pattern)
|
|
||||||
{
|
|
||||||
if(source.empty())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
out_vec.clear();
|
|
||||||
string s = source + pattern;
|
|
||||||
string::size_type pos;
|
|
||||||
uint length = s.size();
|
|
||||||
|
|
||||||
for(uint i = 0; i < length; i++)
|
vector<string> splitStr(const string& source, const string& pattern)
|
||||||
{
|
{
|
||||||
pos = s.find(pattern, i);
|
vector<string> res;
|
||||||
if(pos < length)
|
splitStr(source, res, pattern);
|
||||||
{
|
return res;
|
||||||
string tmp = stripStr(s.substr(i, pos - i));
|
}
|
||||||
if("" != tmp)
|
|
||||||
{
|
|
||||||
out_vec.push_back(tmp);
|
|
||||||
}
|
|
||||||
i = pos + pattern.size() - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
string stripStr(const string& str, const string& patternStr)
|
bool splitStr(const string& source, vector<string>& res, const string& pattern)
|
||||||
{
|
{
|
||||||
if(str.empty())
|
if(source.empty())
|
||||||
{
|
{
|
||||||
return str;
|
return false;
|
||||||
}
|
}
|
||||||
string::size_type posL = str.find_first_not_of(patternStr);
|
res.clear();
|
||||||
if(string::npos == posL)
|
|
||||||
{
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
string::size_type posR = str.find_last_not_of(patternStr);
|
|
||||||
return str.substr(posL, posR - posL + 1);
|
|
||||||
|
|
||||||
}
|
size_t start = source.find_first_not_of(pattern);
|
||||||
|
size_t end;
|
||||||
|
if(string::npos == start)
|
||||||
//http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
|
|
||||||
// trim from start
|
|
||||||
std::string <rim(std::string &s)
|
|
||||||
{
|
|
||||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
// trim from end
|
|
||||||
std::string &rtrim(std::string &s)
|
|
||||||
{
|
|
||||||
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
// trim from both ends
|
|
||||||
std::string &trim(std::string &s)
|
|
||||||
{
|
|
||||||
return ltrim(rtrim(s));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool splitStrMultiPatterns(
|
|
||||||
const string& strSrc,
|
|
||||||
vector<string>& outVec,
|
|
||||||
const vector<string>& patterns
|
|
||||||
)
|
|
||||||
{
|
|
||||||
char transChar = '#';
|
|
||||||
uint transLenThreshold = 10;
|
|
||||||
string transStr;
|
|
||||||
transStr += transChar;
|
|
||||||
while(strSrc.find(transStr) != string::npos)
|
|
||||||
{
|
|
||||||
transStr += transChar;
|
|
||||||
if(transStr.size() > transLenThreshold)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
string strSrcMutable = strSrc;
|
|
||||||
for(uint i = 0; i < patterns.size(); i++)
|
|
||||||
{
|
|
||||||
strSrcMutable = replaceStr(strSrcMutable, patterns[i], transStr);
|
|
||||||
}
|
|
||||||
splitStr(strSrcMutable, outVec, transStr);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
string upperStr(const string& strIn)
|
|
||||||
{
|
|
||||||
string str = strIn;
|
|
||||||
transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper);
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
string lowerStr(const string& strIn)
|
|
||||||
{
|
|
||||||
string str = strIn;
|
|
||||||
transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower);
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count)
|
|
||||||
{
|
|
||||||
string strRet = strSrc;
|
|
||||||
size_t pos = 0;
|
|
||||||
int l_count = 0;
|
|
||||||
if(-1 == count)
|
|
||||||
count = strRet.size();
|
|
||||||
while((pos = strRet.find(oldStr, pos)) != string::npos)
|
|
||||||
{
|
|
||||||
strRet.replace(pos, oldStr.size(), newStr);
|
|
||||||
if(++l_count >= count)
|
|
||||||
break;
|
|
||||||
pos += newStr.size();
|
|
||||||
}
|
|
||||||
return strRet;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int countStrDistance(const string& A, const string& B)
|
|
||||||
{
|
|
||||||
unsigned int lenA = A.size();
|
|
||||||
unsigned int lenB = B.size();
|
|
||||||
unsigned int len = (lenA < lenB ? lenA : lenB);
|
|
||||||
unsigned int res = lenA + lenB - 2 * len;
|
|
||||||
for(size_t i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
if(A[i] != B[i])
|
|
||||||
res++;
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int countStrSimilarity(const string& A, const string& B)
|
|
||||||
{
|
|
||||||
unsigned int lenA = A.size();
|
|
||||||
unsigned int lenB = B.size();
|
|
||||||
unsigned int len = (lenA < lenB ? lenA : lenB);
|
|
||||||
unsigned int res = 0;
|
|
||||||
for(size_t i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
if(A[i] == B[i])
|
|
||||||
res++;
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
//unicode str to vec
|
|
||||||
bool uniStrToVec(const string& str, Unicode& vec)
|
|
||||||
{
|
|
||||||
vec.clear();
|
|
||||||
if(str.empty() || str.size() % 2)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for(uint i = 0; i < str.size(); i+=2)
|
while(string::npos != start)
|
||||||
{
|
{
|
||||||
vec.push_back(twocharToUint16(str[i], str[i + 1]));
|
end = source.find_first_of(pattern, start);
|
||||||
|
if(string::npos == end)
|
||||||
|
{
|
||||||
|
res.push_back(source.substr(start));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
res.push_back(source.substr(start, end - start));
|
||||||
|
start = source.find_first_not_of(pattern, end);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
string stripStr(const string& str, const string& patternStr)
|
||||||
}
|
{
|
||||||
|
if(str.empty())
|
||||||
|
{
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
string::size_type posL = str.find_first_not_of(patternStr);
|
||||||
|
if(string::npos == posL)
|
||||||
|
{
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
string::size_type posR = str.find_last_not_of(patternStr);
|
||||||
|
return str.substr(posL, posR - posL + 1);
|
||||||
|
|
||||||
//unicode vec to str
|
}
|
||||||
string uniVecToStr(const Unicode& vec)
|
|
||||||
{
|
|
||||||
string res("");
|
//http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
|
||||||
for(uint i = 0; i < vec.size(); i++)
|
// trim from start
|
||||||
{
|
std::string <rim(std::string &s)
|
||||||
pair<char,char> pa = uint16ToChar2(vec[i]);
|
{
|
||||||
res += pa.first;
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
|
||||||
res += pa.second;
|
return s;
|
||||||
}
|
}
|
||||||
return res;
|
|
||||||
}
|
// trim from end
|
||||||
|
std::string &rtrim(std::string &s)
|
||||||
|
{
|
||||||
|
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// trim from both ends
|
||||||
|
std::string &trim(std::string &s)
|
||||||
|
{
|
||||||
|
return ltrim(rtrim(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool splitStrMultiPatterns(
|
||||||
|
const string& strSrc,
|
||||||
|
vector<string>& outVec,
|
||||||
|
const vector<string>& patterns
|
||||||
|
)
|
||||||
|
{
|
||||||
|
char transChar = '#';
|
||||||
|
uint transLenThreshold = 10;
|
||||||
|
string transStr;
|
||||||
|
transStr += transChar;
|
||||||
|
while(strSrc.find(transStr) != string::npos)
|
||||||
|
{
|
||||||
|
transStr += transChar;
|
||||||
|
if(transStr.size() > transLenThreshold)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
string strSrcMutable = strSrc;
|
||||||
|
for(uint i = 0; i < patterns.size(); i++)
|
||||||
|
{
|
||||||
|
strSrcMutable = replaceStr(strSrcMutable, patterns[i], transStr);
|
||||||
|
}
|
||||||
|
splitStr(strSrcMutable, outVec, transStr);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
string upperStr(const string& strIn)
|
||||||
|
{
|
||||||
|
string str = strIn;
|
||||||
|
transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
string lowerStr(const string& strIn)
|
||||||
|
{
|
||||||
|
string str = strIn;
|
||||||
|
transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count)
|
||||||
|
{
|
||||||
|
string strRet = strSrc;
|
||||||
|
size_t pos = 0;
|
||||||
|
int l_count = 0;
|
||||||
|
if(-1 == count)
|
||||||
|
count = strRet.size();
|
||||||
|
while((pos = strRet.find(oldStr, pos)) != string::npos)
|
||||||
|
{
|
||||||
|
strRet.replace(pos, oldStr.size(), newStr);
|
||||||
|
if(++l_count >= count)
|
||||||
|
break;
|
||||||
|
pos += newStr.size();
|
||||||
|
}
|
||||||
|
return strRet;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int countStrDistance(const string& A, const string& B)
|
||||||
|
{
|
||||||
|
unsigned int lenA = A.size();
|
||||||
|
unsigned int lenB = B.size();
|
||||||
|
unsigned int len = (lenA < lenB ? lenA : lenB);
|
||||||
|
unsigned int res = lenA + lenB - 2 * len;
|
||||||
|
for(size_t i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if(A[i] != B[i])
|
||||||
|
res++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int countStrSimilarity(const string& A, const string& B)
|
||||||
|
{
|
||||||
|
unsigned int lenA = A.size();
|
||||||
|
unsigned int lenB = B.size();
|
||||||
|
unsigned int len = (lenA < lenB ? lenA : lenB);
|
||||||
|
unsigned int res = 0;
|
||||||
|
for(size_t i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if(A[i] == B[i])
|
||||||
|
res++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
//unicode str to vec
|
||||||
|
bool uniStrToVec(const string& str, Unicode& vec)
|
||||||
|
{
|
||||||
|
vec.clear();
|
||||||
|
if(str.empty() || str.size() % 2)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for(uint i = 0; i < str.size(); i+=2)
|
||||||
|
{
|
||||||
|
vec.push_back(twocharToUint16(str[i], str[i + 1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//unicode vec to str
|
||||||
|
string uniVecToStr(const Unicode& vec)
|
||||||
|
{
|
||||||
|
string res("");
|
||||||
|
for(uint i = 0; i < vec.size(); i++)
|
||||||
|
{
|
||||||
|
pair<char,char> pa = uint16ToChar2(vec[i]);
|
||||||
|
res += pa.first;
|
||||||
|
res += pa.second;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,78 +240,20 @@ using namespace CPPCOMMON;
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
//string s = " \t\n1 a h \n";
|
vector<string> vec;
|
||||||
//cout<<"["<<stripStr(s)<<"]"<<endl;
|
splitStr("1 3 4", vec);
|
||||||
//cout<<countStrDistance("Aheheh","heheh1212")<<endl;
|
for(uint i =0;i < vec.size(); i++)
|
||||||
//cout<<joinStr(splitStr(s), ",")<<endl;
|
{
|
||||||
//vector<string> vec;
|
cout<<vec[i]<<endl;
|
||||||
//splitStr("1 3 4", vec);
|
}
|
||||||
//char * a[] = {"3","jaj","ads"};
|
cout<<strStartsWith("--help","--")<<endl;
|
||||||
//vector<string> pats(a,a+3);
|
cout<<strStartsWith("--help","-")<<endl;
|
||||||
//vec.clear();
|
cout<<strStartsWith("--help","he")<<endl;
|
||||||
//splitStrMultiPattern("1 #3 jajs5 asdf3ads 4", vec, pats);
|
cout<<strStartsWith("help","help")<<endl;
|
||||||
//for(int i=0;i<vec.size();i++)
|
cout<<strStartsWith("","help")<<endl;
|
||||||
//{
|
cout<<strStartsWith("hel","")<<endl;
|
||||||
// cout<<vec[i]<<endl;
|
cout<<strEndsWith("hel","")<<endl;
|
||||||
//}
|
cout<<strEndsWith("hel","el")<<endl;
|
||||||
//string s = "1111aaafasfa,asdj.sadhashfhaha";
|
return 0;
|
||||||
//upperStr(s);
|
|
||||||
//cout<<s<<endl;
|
|
||||||
//
|
|
||||||
//s = "ab1ba2ab3";
|
|
||||||
//cout<<replaceStr(s,"ab","###")<<endl;
|
|
||||||
//ifstream ifile("testdata/dict.txt");
|
|
||||||
//string line;
|
|
||||||
//while(getline(ifile, line))
|
|
||||||
//{
|
|
||||||
// uint16_t strbuf[1024];
|
|
||||||
|
|
||||||
// size_t unilen = utf8ToUnicode(line.c_str(), line.size(), strbuf);
|
|
||||||
// for(int i = 0; i < unilen; i++)
|
|
||||||
// {
|
|
||||||
// // printf("%x\n", strbuf[i]);
|
|
||||||
// }
|
|
||||||
// char utf8str[512]={0};
|
|
||||||
// unicodeToUtf8(strbuf, unilen, utf8str);
|
|
||||||
// //cout<<strlen(utf8str);
|
|
||||||
// cout<<utf8str<<endl;
|
|
||||||
//}
|
|
||||||
//cout<<string_format("hehe%s11asd%dasf","[here]",2);
|
|
||||||
//ifstream ifile("testdata/dict.gbk");
|
|
||||||
//string line;
|
|
||||||
//Unicode unicode;
|
|
||||||
//while(getline(ifile, line))
|
|
||||||
//{
|
|
||||||
// cout<<line<<endl;
|
|
||||||
// utf8ToUnicode(line, unicode);
|
|
||||||
// printUnicode(unicode);
|
|
||||||
// cout<<unicodeToUtf8(unicode)<<endl;;
|
|
||||||
//}
|
|
||||||
//vector<string> tmp;
|
|
||||||
//tmp.push_back("1");
|
|
||||||
////tmp.push_back("2");
|
|
||||||
////tmp.clear();
|
|
||||||
//cout<<joinStr(tmp, ",")<<endl;
|
|
||||||
//ifstream ifile("testdata/dict.gbk");
|
|
||||||
//string line;
|
|
||||||
//while(getline(ifile, line))
|
|
||||||
//{
|
|
||||||
// cout<<line<<endl;
|
|
||||||
// string s = gbkToUtf8(line);
|
|
||||||
// s = utf8ToGbk(s);
|
|
||||||
// cout<<s<<endl;
|
|
||||||
//}
|
|
||||||
cout<<strStartsWith("--help","--")<<endl;
|
|
||||||
cout<<strStartsWith("--help","-")<<endl;
|
|
||||||
cout<<strStartsWith("--help","he")<<endl;
|
|
||||||
cout<<strStartsWith("help","help")<<endl;
|
|
||||||
cout<<strStartsWith("","help")<<endl;
|
|
||||||
cout<<strStartsWith("hel","")<<endl;
|
|
||||||
cout<<strEndsWith("hel","")<<endl;
|
|
||||||
cout<<strEndsWith("hel","el")<<endl;
|
|
||||||
//string s(" helloword heh\t");
|
|
||||||
//string b;
|
|
||||||
//cout<<trim(b)<<"11"<<endl;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -21,66 +21,66 @@
|
|||||||
#include <locale>
|
#include <locale>
|
||||||
namespace CPPCOMMON
|
namespace CPPCOMMON
|
||||||
{
|
{
|
||||||
using namespace std;
|
using namespace std;
|
||||||
string string_format(const string fmt, ...) ;
|
string string_format(const string fmt, ...) ;
|
||||||
string joinStr(const vector<string>& source, const string& connector);
|
string joinStr(const vector<string>& source, const string& connector);
|
||||||
vector<string> splitStr(const string& source, const string& pattern = " \t\n");
|
vector<string> splitStr(const string& source, const string& pattern = " \t\n");
|
||||||
void splitStr(const string& source, vector<string>& out_vec, const string& pattern = " \t\n");
|
bool splitStr(const string& source, vector<string>& res, const string& pattern = " \t\n");
|
||||||
bool splitStrMultiPatterns(
|
bool splitStrMultiPatterns(
|
||||||
const string& strSrc,
|
const string& strSrc,
|
||||||
vector<string>& outVec,
|
vector<string>& outVec,
|
||||||
const vector<string>& patterns
|
const vector<string>& patterns
|
||||||
);
|
);
|
||||||
string upperStr(const string& str);
|
string upperStr(const string& str);
|
||||||
string lowerStr(const string& str);
|
string lowerStr(const string& str);
|
||||||
string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count = -1);
|
string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count = -1);
|
||||||
string stripStr(const string& str, const string& patternstr = " \n\t");
|
string stripStr(const string& str, const string& patternstr = " \n\t");
|
||||||
std::string <rim(std::string &s) ;
|
std::string <rim(std::string &s) ;
|
||||||
std::string &rtrim(std::string &s) ;
|
std::string &rtrim(std::string &s) ;
|
||||||
std::string &trim(std::string &s) ;
|
std::string &trim(std::string &s) ;
|
||||||
unsigned int countStrDistance(const string& A, const string& B);
|
unsigned int countStrDistance(const string& A, const string& B);
|
||||||
unsigned int countStrSimilarity(const string& A, const string& B);
|
unsigned int countStrSimilarity(const string& A, const string& B);
|
||||||
|
|
||||||
|
|
||||||
bool uniStrToVec(const string& str, Unicode& vec);
|
bool uniStrToVec(const string& str, Unicode& vec);
|
||||||
string uniVecToStr(const Unicode& vec);
|
string uniVecToStr(const Unicode& vec);
|
||||||
|
|
||||||
inline uint16_t twocharToUint16(char high, char low)
|
inline uint16_t twocharToUint16(char high, char low)
|
||||||
{
|
{
|
||||||
return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff));
|
return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline pair<char, char> uint16ToChar2(uint16_t in)
|
inline pair<char, char> uint16ToChar2(uint16_t in)
|
||||||
{
|
{
|
||||||
pair<char, char> res;
|
pair<char, char> res;
|
||||||
res.first = (in>>8) & 0x00ff; //high
|
res.first = (in>>8) & 0x00ff; //high
|
||||||
res.second = (in) & 0x00ff; //low
|
res.second = (in) & 0x00ff; //low
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void printUnicode(const Unicode& unicode)
|
inline void printUnicode(const Unicode& unicode)
|
||||||
{
|
{
|
||||||
cout<<uniVecToStr(unicode)<<endl;
|
cout<<uniVecToStr(unicode)<<endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool strStartsWith(const string& str, const string& prefix)
|
inline bool strStartsWith(const string& str, const string& prefix)
|
||||||
{
|
{
|
||||||
//return str.substr(0, prefix.size()) == prefix;
|
//return str.substr(0, prefix.size()) == prefix;
|
||||||
if(prefix.length() > str.length())
|
if(prefix.length() > str.length())
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return 0 == str.compare(0, prefix.length(), prefix);
|
return 0 == str.compare(0, prefix.length(), prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool strEndsWith(const string& str, const string& suffix)
|
inline bool strEndsWith(const string& str, const string& suffix)
|
||||||
{
|
{
|
||||||
if(suffix.length() > str.length())
|
if(suffix.length() > str.length())
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return 0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix);
|
return 0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user