📄 xs.cpp
字号:
/************************************************************************/
/* AUTHOR: Leiming Hong */
/* INST.: South China Univ. of Tech. */
/* Date: 2005-10-25 */
/* FUNC: Attribute used for defining data column type */
/************************************************************************/
#pragma warning(disable: 4786) //: disable the warning of the debug length problem
#include "xs.h"
//: for testing
#include <iostream>
#include <ctime>
using namespace std;
//==========================================================
// FUNC: split the string by the specified delimit chars
// RETURN: string array vector splited
//==========================================================
vector<string> xs::split(const string& str,char* delimits)
{
vector<string> tokens;
vector<string>::size_type pos1, pos2;
pos1 = str.find_first_not_of(delimits);
if(pos1 == string::npos) tokens.push_back(str);
while(pos1 != string::npos)
{
pos2 = str.find_first_of(delimits,pos1);
if(pos2 == string::npos) pos2 = str.length();
string token = str.substr(pos1,pos2-pos1);
tokens.push_back(token);
pos1 = str.find_first_not_of(delimits,pos2);
}
return tokens;
}
//==========================================================
// FUNC: split the string by the specified delimit chars
// RETURN: string array vector splited
//==========================================================
bool xs::start_with(const string& str,string pre,bool sensitive /* = true */)
{
int len = pre.length();
if(len > str.length()) return false;
string pstr = str.substr(0,len);
if( !sensitive )
{
lower(pstr);
lower(pre);
}
return pstr==pre;
}
void xs::lower(string& str)
{
int len =str.length();
for(int i=0;i<len;i++) if(str[i]>='A' && str[i]<='Z') str[i] += 0x20;
}
void xs::upper(string& str)
{
int len =str.length();
for(int i=0;i<len;i++) if(str[i]>='a' && str[i]<='z') str[i] -= 0x20;
}
string xs::trim(const string& str,string omit /* = " " */)
{
string::size_type first,last;
first = str.find_first_not_of(omit);
last = str.find_last_not_of(omit);
if(first == string::npos || last == string::npos) return "";
return str.substr(first,last-first+1);
}
/************************************************************************
// TESTING CODE by L.M.Hong SCUT
void main()
{
time_t start,end;
clock_t s,e;
s = clock();
time(&start);
int i=0;
int max = 1000000;
char* test = " test test test";
while(i++<max)
{
xs::split(test," ");
}
e = clock();
time(&end);
cout<<start<<" "<<end<<endl;
cout<<s<<" "<<e<<endl;
cout<<(end-start)/1000.0<<endl;
cout<<(e-s)/1000.0<<endl;
}
/************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -