C++模拟Python功能【二】

新博客地址:http://gorthon.sinaapp.com/



新博客地址:http://gorthon.sinaapp.com/

// file:function.h
// author: shuji
// bh2#qq.com

#ifndef __PYTHON__FUNCTION__H__
#define __PYTHON__FUNCTION__H__

#include <ctime>
#include <string>
#include <sstream>
#include <cctype>
#include <vector>
#include <cmath>
#include <cstdarg>

//#include <climits>
//#include <cstdlib>
#include <algorithm>
#include <functional>

#include <iostream>

//using namespace std;
using std::string;
using std::stringstream;
using std::vector;
using std::pair;
using std::cout;
using std::endl;

namespace __rand__
{
	// 已经实现的功能,线程安全未测试
	double random();
	int randint(int start, int stop);
	int randint(int stop);
	double uniform(double start, double stop);
	string choice(const string &sequence);
	string sample(const string &sequence, size_t num);

	// 以下功能尚未实现
	template <typename T>
	T choice(T sequence);
	template <typename T>
	T sample(T sequence);
	template <typename T>
	void shuffle(T sequence);

} // end namespace __rand__


namespace __string__
{
	const	string whitespace(" \t\n\r\v\f");
	const string lowercase("abcdefghijklmnopqrstuvwxyz");
	const string uppercase("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	const string ascii_lowercase(lowercase);
	const string ascii_uppercase(uppercase);
	const string letters(ascii_lowercase + ascii_uppercase);
	const string digits("0123456789");
	const	string hexdigits(digits + "abcdef" + "ABCDEF");
	const	string octdigits ("01234567");
	const string punctuation("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
	const	string printable(digits + letters + punctuation + whitespace);

	string::size_type count(const string &s, string sub, int start=0, int stop=NULL);
	string::size_type count(const string &s, char c, int start=0, int stop=NULL);
	bool endswith(const string &s, string suffix, int start=0, int stop=NULL);
	bool endswith(const string &s, char c, int start=0, int stop=NULL);
	bool startswith(const string &s, string prefix, int start=0, int stop=NULL);
	bool startswith(const string &s, char c, int start=0, int stop=NULL);
	
	// 对于islower和isupper函数,我不知道Python的官方人员为什么要这样设计
	// 在我看来islower应该是字符串s中全部是小写就返回true否则返回false
	// 但是Python中却不是这样,他的规则为:
	// 返回True:有小写且无大写
	// 返回False:有大写或无小写
	// "abc你好".islower()返回True
	// "gcdisstupid!@#$%{1}quot;.islower()也是返回True
	// 但是为什么这样设计?为什么事先”过滤掉”非字母呢?
	bool isalnum(const string &s);
	bool isalnum(char c);
	bool isalpha(const string &s);
	bool isalpha(char c);
	bool isdigit(const string &s);
	bool isdigit(char c);
	bool islower(const string &s);
	bool islower(char c);
	bool isupper(const string &s);
	bool isupper(char c);
	bool isspace(const string &s);
	bool isspace(char c);
	bool istitle(const string &s);
	bool istitle(char c);

	string capitalize(const string &s);
	string capwords(const string &s, string sep=whitespace);
	string expandtabs(const string &s, int tabsize=8);
	string lower(const string &s);
	string lower(char c);
	string title(const string &s);
	string upper(const string &s);
	string upper(char c);

	string join(vector<string> &words, string sep=" ");
	string lstrip(const string &s, string chars=whitespace);
	string ljust(const string &s, string::size_type width, char fillchar=' ');
	string rjust(const string &s, string::size_type width, char fillchar=' ');
	string rstrip(const string &s, string chars=whitespace);
	string strip(const string &s, string chars=whitespace);
	string replace(const string &s, string old_str, string new_str, size_t maxreplace=0);
	string replace(const string &s, char c, string new_str, size_t maxreplace=0);
	vector<string> split(const string &s, string sep=whitespace, size_t maxsplit=0);
	string swapcase(const string &s);

	string::size_type rfind(const string &s, string sub, int start=0, int stop=NULL);
	string::size_type rfind(const string &s, char c, int start=0, int stop=NULL);
	string::size_type find(const string &s, string sub, int start=0, int stop=NULL);
	string::size_type find(const string &s, char c, int start=0, int stop=NULL);
	string zfill(const string &s, string::size_type width);

	// 其他一些功能
	int toInt(const string &s);
	float toFloat(const string &s);
	template <typename T>
	string str( const T &integer)
	{
		string ret;
		stringstream ss;
		ss << integer;
		ss >> ret;
		return ret;
	}
} // end namespace __string__

namespace __builtin__
{
	vector<int> range(int stop);
	vector<int> range(int start, int stop, int step=1);
	template <typename T>
	T sum(vector<T> iterable, T start=T(0))
	{
		for (vector<T>::iterator it=iterable.begin(); it!=iterable.end(); ++it)
			start += *it;
		return start;
	}

	template<class T>
	T sum(int count, T ...)
	{
		// count为元素个数
		va_list arg_ptr; // va_list类型用来依次指向各个实参
		va_start(arg_ptr, count); // va_start用来使arg_ptr指向函数的第一个实参count
		T s(0);
		for (int i=0; i<count; i++)
			s += va_arg(arg_ptr, T);
		va_end(arg_ptr);
		return s;
	}

	template <typename T1, typename T2>
	vector<pair<T1, T2> > zip(vector<T1> t1, vector<T2> t2)
	{
		// 仅支持两个iterable的zip
		if(t1.empty() || t2.empty()) return vector<pair<T1, T2> >();
		size_t length(t1.size() > t2.size() ? t2.size() : t1.size());
		vector<pair<T1, T2> > ret;
		ret.reserve(length);
		vector<T1>::iterator it1=t1.begin();
		vector<T2>::iterator it2=t2.begin();
		for (;it1!=t1.begin() + length;)
			ret.push_back(pair<T1, T2>(*it1++, *it2++));
		return ret;
	}

} // end namespace __builtin__ 

namespace deelx
{
	#include "deelx.h"
} // end namespace deelx 

namespace __re__
{
	enum REGEX_FLAGS
	{
		NO_FLAG        = deelx::NO_FLAG,
		SINGLELINE     = deelx::SINGLELINE,
		MULTILINE      = deelx::MULTILINE,
		GLOBAL         = deelx::GLOBAL,
		IGNORECASE     = deelx::IGNORECASE,
		RIGHTTOLEFT    = deelx::RIGHTTOLEFT,
		EXTENDED       = deelx::EXTENDED
	};
	typedef deelx::MatchResultT <0> Match;
	class Pattern:public deelx::CRegexpT<char>
	{
	public:
		Pattern(const string &pattern, int flags=NO_FLAG):CRegexpT(pattern.c_str(), flags)
		{
			//text=pattern.c_str();
		}
		deelx::MatchResultT <0> match(const string &pattern, int start=-1)
		{
			return this->Match(pattern.c_str(), start);
		}
		//void compile(const string &pattern, int flags=0);
	private:
		//const char *text;
	};
// 	class Match:public deelx::MatchResultT <0>
// 	{
// 	public:
// 		Match(const string &pattern, int start=-1):deelx::MatchResultT <0>(pattern.c_str(), start)
// 		{
// 
// 		}
// 
// 	};
} // end namespace __re__ 

#endif



// file:function.cpp
// author: shuji
// bh2#qq.com

#include "function.h"

static bool has_seeded(false);

template <typename T>
inline static void _swap_(T &t1, T &t2)
{
	T tmp = t1;
	t1 = t2;
	t2 = tmp;
}

inline static void _initSrand_()
{
	/**初始化种子*/
	if (!has_seeded)
	{
		has_seeded = true;
		srand((unsigned)time(NULL));
	}
	rand(); // 0 <= rand() < RAND_MAX(32767=0x7fff)
}

inline static void _checkStartEnd_(int &start, int &stop, int size)
{
	start = start < 0 ? 0 : start;
	stop = stop == NULL ? size : stop;
}

inline static string _getCurrentRandStr_(string &ascii, string &unicode)
{
	/**此函数供choice和sample使用,返回ascii或unicode中的一个随机字符*/
	string result("");
	int index(0);
	// randint(1) = 0返回中文
	// randint(1) = 1返回英文
	if (__rand__::randint(1))
	{
		index = __rand__::randint(ascii.size() - 1);
		result = ascii[index];
	}
	else // 返回中文
	{
		index = __rand__::randint(unicode.size() - 1);
		index = (index&0x1)? index - 1 : index;  // 不要取到中文的第二个字节,index&0x1 = 1表示index为奇数,应将其变为偶数(注意下标从0开始,故中文的第一个字节下标为偶数)
		result = unicode.substr(index, 2);
	}
	return result;
}

inline static void _allocate_(const string &sequence, string &ascii, string &unicode)
{
	ascii.clear();
	unicode.clear();
	/**将sequence分为中文和英文*/
	for (size_t index(0); index<sequence.size(); index++)
	{
		if ((sequence[index]&0x80)&&(unsigned)sequence[index]>=0)
			unicode += sequence.substr(index++, 2);
		else
			ascii += sequence[index];
	}
}

inline static void _swapcase_(char &c)
{
	if(__string__::islower(c))
		c = std::toupper(c);
	else if(__string__::isupper(c))
		c = std::tolower(c);
}

inline static bool _in_(char c, string &chars)
{
	return chars.find(c) != string::npos ? true : false;
}

inline static bool _in_(string sub, string &chars)
{
	return __string__::find(chars, sub) != string::npos ? true : false;
}

inline static bool _isWhat_(const string &s, string table)
{
	if(s.empty()) return false;
	for(size_t i=0; i<s.size(); i++)
		if (table.find(s.at(i)) == string::npos)
			return false;
	return true;
}

static vector<string> _search_(const string &s, string sub, bool is_countting=true, const size_t maxsplit=0)
{
	/**用于count和split函数is_countting==true表示count,此时不用把找到的字串插入ret中,如果是is_countting==false表示split,则不要resize(num)*/
	vector<string> ret;
	string str(s);
	string::size_type pos(0), step(sub.size()), stop(str.size()), num(0);
	while(true)
	{
		pos = str.find(sub);
		if (pos != string::npos && pos < stop)
		{
			num++;
			if(!is_countting)
			{
				if(num > maxsplit) break;
				ret.push_back(str.substr(0, pos));
			}
			str = str.substr(pos + step, stop - pos);
		}
		else break;
	}
	if(is_countting) 
		ret.resize(num);
	else
		ret.push_back(str); // 把剩下的字串添加进去
	return ret;
}
/************************************************************************/
/*                                                                     rand                                                              */
/************************************************************************/
double __rand__::random()
{
	/**返回闭区间[0, 1]上的一个浮点数*/
	_initSrand_();
	return rand() / (double)(RAND_MAX - 1);
}

int __rand__::randint(int start, int stop)
{
	/**返回闭区间[start, stop]或[stop, start]上的一个整数*/
	int istart = start;
	int istop = stop;
	if(istart > istop)
		_swap_(istart, istop);
	//_initSrand_();
	return int(random() * (istop + 1 - istart)) + istart;
}

int __rand__::randint(int stop)
{
	/**返回闭区间[0, stop]或[stop, 0]上的一个整数*/
	return randint(0, stop);
}

double __rand__::uniform(double start, double stop)
{
	/**返回闭区间[start, stop]或[stop, start]上的一个浮点数*/
	return random() * (stop - start) + start;
}

string __rand__::choice(const string &sequence)
{ 
	/**返回sequence中的一个随机字符*/
	string ascii(""); // 存储所有英文
	string unicode(""); // 存储所有中文
	_allocate_(sequence, ascii, unicode);
	return _getCurrentRandStr_(ascii, unicode);
}

string __rand__::sample(const string &sequence, size_t num)
{
	/**返回sequence中的一个采样序列,采样长度不限*/
	string ascii(""); // 存储所有英文
	string unicode(""); // 存储所有中文
	string result(""); // 返回值
	_allocate_(sequence, ascii, unicode);
	for(size_t i=0; i < num; i++)
		result += _getCurrentRandStr_(ascii, unicode);
	return result;
}
/************************************************************************/
/*                                                                    string                                                            */
/************************************************************************/
string __string__::capitalize(const string &s)
{
	// 不会改变原s的值
	if(s.empty()) return s;
	string cap_str(s);
	cap_str[0] = std::toupper(cap_str[0]);
	return cap_str;
	// 返回局部变量string没有问题,因为string是分配在堆中的,与分配在栈中的char []不同
}

inline static void _capitalize_(string &s)
{
	// 此函数只被capwords使用
	s = __string__::capitalize(s);
}

string __string__::capwords(const string &s, string sep)
{
	vector<string> vs = split(s, sep);
	for_each(vs.begin(), vs.end(), _capitalize_);
	return  join(vs, sep == whitespace ? " " : sep);
}

string::size_type __string__::count(const string &s, string sub, int start, int stop)
{
	/**返回半开半闭区间[start, stop)上找到的sub子串的个数*/
	_checkStartEnd_(start, stop, s.size());
	if(start >= stop) return 0;
	return _search_(s.substr(start, stop - start), sub).size();
}

string::size_type __string__::count(const string &s, char c, int start, int stop)
{
	string chr;
	chr.insert(chr.begin(), c);
	return count(s, chr, start, stop);
}

bool __string__::endswith(const string &s, string suffix, int start, int stop)
{
	_checkStartEnd_(start, stop, s.size());
	if(start >= stop) return false;

	int length(suffix.size());
	int duration(stop - start);
	return suffix == s.substr(start, duration ).substr(duration - length, length);
}

bool __string__::endswith(const string &s, char c, int start, int stop)
{
	string chr;
	chr.insert(chr.begin(), c);
	return endswith(s, chr, start, stop);
}

bool __string__::startswith(const string &s, string prefix, int start, int stop)
{
	_checkStartEnd_(start, stop, s.size());
	if(start >= stop) return false;

	int length(prefix.size());
	int duration(stop - start);
	return prefix == s.substr(start, duration ).substr(0, length);
}

bool __string__::startswith(const string &s, char c, int start, int stop)
{
	string chr;
	chr.insert(chr.begin(), c);
	return startswith(s, chr, start, stop);
}

string __string__::expandtabs(const string &s, int tabsize)
{
	return replace(s, "\t", string(tabsize, ' '));
}

string::size_type __string__::find(const string &s, string sub, int start, int stop)
{
		_checkStartEnd_(start, stop, s.size());
 		if(start >= stop) return string::npos;

		string str(s.substr(start, stop - start));
		return str.find(sub);
}

string::size_type __string__::find(const string &s, char c, int start, int stop)
{
	string chr;
	chr.insert(chr.begin(), c);
	return find(s, chr, start, stop);
}

bool __string__::isalnum(const string &s)
{
	/**判断是否为全部字母或者数字*/
	return _isWhat_(s, letters + digits);
}

bool __string__::isalnum(char c)
{
	string chr;
	chr.insert(chr.begin(), c);
	return isalnum(chr);
}

bool __string__::isalpha(const string &s)
{
	/**判断是否为字母*/
	return _isWhat_(s, letters);
}

bool __string__::isalpha(char c)
{
	string chr;
	chr.insert(chr.begin(), c);
	return isalpha(chr);
}

bool __string__::isdigit(const string &s)
{
	/**判断是否为自然数[0, 1, 2, ..., ]*/
	return _isWhat_(s, digits);
}

bool __string__::isdigit(char c)
{
	string chr;
	chr.insert(chr.begin(), c);
	return isdigit(chr);
}

bool __string__::islower(const string &s)
{
	/**判断是否全部为小写字母*/
	return _isWhat_(s, ascii_lowercase);
}

bool __string__::islower(char c)
{
	string chr;
	chr.insert(chr.begin(), c);
	return islower(chr);
}

bool __string__::isupper(const string &s)
{
	/**判断是否全部为大写字母*/
	return _isWhat_(s, ascii_uppercase);
}

bool __string__::isupper(char c)
{
	string chr;
	chr.insert(chr.begin(), c);
	return isupper(chr);
}

bool __string__::isspace(const string &s)
{
	/**判断是否为空白字符(注意是空白字符而不是空格!)*/
	return _isWhat_(s, whitespace);
}

bool __string__::isspace(char c)
{
	string chr;
	chr.insert(chr.begin(), c);
	return isspace(chr);
}

bool __string__::istitle(const string &s)
{
	/**判断是否全部为大写字母*/
	return s == title(s);
}

bool __string__::istitle(char c)
{
	string chr;
	chr.insert(chr.begin(), c);
	return istitle(chr);
}

string::size_type __string__::rfind(const string &s, string sub, int start, int stop)
{
	_checkStartEnd_(start, stop, s.size());
	if(start >= stop) return string::npos;

	string str(s.substr(start, stop - start));
	return str.rfind(sub);
}

string::size_type __string__::rfind(const string &s, char c, int start, int stop)
{
	string chr;
	chr.insert(chr.begin(), c);
	return rfind(s, chr, start, stop);
}

string __string__::title(const string &s)
{
	/**如果当前字符前一个字符isalpha, 那么将他tolower,否则toupper*/
	if(s.empty()) return s;
	string str(s);
	str = capitalize(str);
	if(str.size() == 1) return str;
	string tmp("");
	for(size_t i=1; i<s.size(); i++)
	{
		tmp.clear();
		tmp.insert(tmp.begin(), s.at(i - 1));
		str[i] = isalpha(tmp) ? str[i] = std::tolower(str.at(i)) : str[i] = std::toupper(str.at(i));	
	}
	return str;
}

static string _lowerORupper_(const string &s, bool type)
{
	string str(s);
	if (type)
		for(size_t i=0; i<s.size(); i++)
			str[i] = std::toupper(str.at(i));
	else 
		for(size_t i=0; i<s.size(); i++)
			str[i] = std::tolower(str.at(i));
	return str;
}
string __string__::lower(const string &s)
{
	if(s.empty()) return s;
	return _lowerORupper_(s, false);
}

string __string__::lower(char c)
{
	string chr;
	chr.insert(chr.begin(), c);
	return lower(chr);
}

string __string__::upper(const string &s)
{
	if(s.empty()) return s;
	return _lowerORupper_(s, true);
}

string __string__::upper(char c)
{
	string chr;
	chr.insert(chr.begin(), c);
	return upper(chr);
}

string __string__::replace(const string &s, string old_str, string new_str, size_t maxreplace)
{
	string str(s);
	string::size_type pos(0), length(old_str.size());
	size_t num(0);
	maxreplace = !maxreplace ? s.size() : maxreplace;
	while (true)
	{
		pos = str.find(old_str, pos);
		if(pos == string::npos || ++num > maxreplace) break;
		str.replace(pos, length, new_str);
	}
	return str;
}

string __string__::replace(const string &s, char c, string new_str, size_t maxreplace)
{
	string chr;
	chr.insert(chr.begin(), c);
	return replace(s, chr, new_str, maxreplace);
}

vector<string> __string__::split(const string &s, string sep, size_t maxsplit)
{
	if(s.empty()) return vector<string>();
	if(sep == whitespace)
	{
		// 先将所有空白字符替换为空格
		string str(s);
		for(string::size_type i=1; i<whitespace.size(); ++i)
			str = replace(str, whitespace[i], " ");
		return _search_(str, " ", false, s.size()); // 不支持maxsplit参数
	}
	return _search_(s, sep, false, !maxsplit ? s.size() : maxsplit);
}

string __string__::join(vector<string> &words, string sep)
{
	if(words.empty()) return "";
	string ret;
	ret.reserve(sep.size() * (words.size() - 1) + sizeof words);
	for (vector<string>::iterator it=words.begin(); it!=words.end() - 1;++it)
		ret += *it + sep;
	ret += words.at(words.size() - 1);
	string(ret).swap(ret); // 去除多余的空间
	return ret;
}

string __string__::swapcase(const string &s)
{
	string ret(s);
	for_each(ret.begin(), ret.end(), _swapcase_);
	return ret;
}

string __string__::lstrip(const string &s, string chars)
{
	string ret(s);
	for(string::iterator it=ret.begin(); it!=ret.end();)
		if ((*it&0x80) && (unsigned)*it>=0) // 中文
			if(_in_(string(it, it + 2), chars))
				it = ret.erase(it++, it + 2); // 保持迭代器有效
			else break; // 遇到第一个不是chars里面的字串时就停止
		else // 英文
			if(_in_(*it, chars))
				it = ret.erase(it);
			else break;
	return ret;
}

string __string__::rstrip(const string &s, string chars)
{
	string ret(s), curr_str("");
	string::size_type length;
	for(string::const_reverse_iterator it=s.rbegin(); it!=s.rend();++it)
	{
		if ((*it&0x80) && (unsigned)*it>=0) // 中文
		{
			length = 2;
			curr_str = *(it+1) + "" + *it++;
		}
		else // 英文
		{
			length = 1;
			curr_str = *it;
		}
		if(_in_(curr_str, chars)) ret.resize(ret.size() - length);
		else break; // 遇到第一个不是chars里面的字串时就停止
	}
	return ret;
}

string __string__::strip(const string &s, string chars)
{
	return rstrip(lstrip(s, chars), chars);
}

string __string__::ljust(const string &s, string::size_type width, char fillchar)
{
	if(width <= s.size()) return s;
	return s + string(width - s.size(), fillchar);
}

string __string__::rjust(const string &s, string::size_type width, char fillchar)
{
	if(width <= s.size()) return s;
	return string(width - s.size(), fillchar) + s;
}

string __string__::zfill(const string &s, string::size_type width)
{
	return rjust(s, width, '0');
}

int __string__::toInt(const string &s)
{
	int ret;
	stringstream ss;
	ss << s;
	ss >> ret;
	return ret;
}

float __string__::toFloat(const string &s)
{
	float ret;
	stringstream ss;
	ss << s;
	ss >> ret;
	return ret;
}
/************************************************************************/
/*                                                                    builtin                                                            */
/************************************************************************/
vector<int> __builtin__::range(int start, int stop, int step)
{
	if(stop <= start || !step || (start < stop && step <0))return vector<int>();
	vector<int> vi;
	vi.reserve(1 + (stop - start) / step);
	for(int i=start; i<stop; i+=step) vi.push_back(i);
	vector<int>(vi).swap(vi); // 去除多余的空间
	return vi;
}

vector<int> __builtin__::range(int stop)
{
	return range(0, stop);
}


测试程序:

// file:main.cpp
// author: shuji
// bh2#qq.com

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

#include "function.h"

using namespace std;

template <typename T>
void show(T t)
{
	cout << t << endl;
}

template <typename T>
void showArray(vector<T> &v, string sep=", ")
{
	cout <<"[";
	if(v.empty())
	{
		cout<<"]\n";
		return;
	}
	copy(v.begin(), v.end(), ostream_iterator<T>(cout, sep.c_str()));
	cout <<"\b\b]\n";
}

template <typename T1, typename T2>
void show(pair<T1, T2> p)
{
	cout << "(" << p.first << ", " << p.second << ")" << endl;
}

int main()
{
	/************************************************************************/
	/*                                                                     rand                                                              */
	/************************************************************************/
	show(__rand__::random());
	show(__rand__::randint(3));
	show(__rand__::randint(3, 0));
	show(__rand__::uniform(5.2, 4.8));
	string  str("^和9谐a中b华c人d民e共f和g国{1}quot;);
	show(__rand__::choice(str));
	show(__rand__::sample(str, 22));
	/************************************************************************/
	/*                                                                    string                                                            */
	/************************************************************************/
	show(__string__::lowercase);
	show(__string__::uppercase);
	show(__string__::ascii_lowercase);
	show(__string__::ascii_uppercase);
	show(__string__::letters);
	show(__string__::digits);
	show(__string__::hexdigits);
	show(__string__::octdigits);
	show(__string__::punctuation);
	show(__string__::printable);
	show(__string__::whitespace);

	show(__string__::capwords("gfw fu和谐ck gcd's\ttail\n."));
	show(__string__::title("gfw fu和谐ck gcd's\ttail\n."));
	string greet = "an Neon Ha sae Yo?和\t谐gfW";
	show(__string__::capitalize(greet));
	show(__string__::count(greet, "a"));//0, 9, 12有a共3个
	show(__string__::endswith(greet, "谐gfW"));
	show(__string__::startswith(greet, "an N"));
	show(__string__::expandtabs(greet, 0));
	show(__string__::find(greet, "和\t谐"));
	show(__string__::rfind(greet, "和\t谐"));
	show(__string__::isalnum("110isStupid"));
	show(__string__::isalpha("GCDisMoreStupid"));
	show(__string__::isdigit("45345435345345435583655333338"));
	show(__string__::islower("how"));
	show(__string__::isupper("GCDDIE"));
	show(__string__::isspace("      \t     "));
	show(__string__::istitle(__string__::title(greet)));
	show(__string__::lower(greet));
	show(__string__::upper(greet));
	show(__string__::replace("GFW is stupid. GFW is too frail to stand a gust of wind.", "GFW", "GCD", 1));
	showArray(__string__::split("123##456##7890##1234", "##", 2));
	showArray(__string__::split("123 456\t7890\n1234"));
	// string.join(string.split(s, sep), sep) == s
	show(__string__::join(__string__::split("1好d2好d3好d4", "好d"), "好d"));
	show(__string__::swapcase("F和u谐C.k@G和c谐D"));
	string monster("gcd和谐monstrous和谐gcd和");
	show(__string__::lstrip(monster, "gcd和"));
	show(__string__::rstrip(monster, "gcd和"));
	show(__string__::strip(monster, "gcd和"));
	show(__string__::ljust(monster, 45, '@'));
	show(__string__::rjust(monster, 45, '@'));
	show(__string__::zfill(monster, 45));
	show(__string__::str(-65536));
	show(__string__::toInt("-65536fsl"));
	show(__string__::str(-3.14159f));
	show(__string__::toFloat("-3.14159fu和谐ckgfw"));

	/************************************************************************/
	/*                                                                    builtin                                                            */
	/************************************************************************/
	showArray(__builtin__::range(10));
	showArray(__builtin__::range(-10, 11));
	showArray(__builtin__::range(-10, 11, 2));
	float sum_data[] = {1.1f, 2.4f};
	vector<float> sum_v(sum_data, sum_data+sizeof(sum_data )/ sizeof(sum_data[0]));
	show(__builtin__::sum(sum_v, 5.0f));
	show(__builtin__::sum(5, 1.5, 2.0, 3.0, 4.0, 5.0));
	vector<string> gfw_v;
	gfw_v.push_back("stupid");gfw_v.push_back("gcd");gfw_v.push_back("和谐");
	vector<pair<float, string> > ret = __builtin__::zip(sum_v, gfw_v);
	for_each(ret.begin(), ret.end(), show<float, string>);

	/************************************************************************/
	/*                                                                        re                                                                */
	/************************************************************************/
// 	string sdf("\\d+");
// 	char dfd[] = "sdf";
// 	__re__::regex <char> regexp(sdf, __re__::NO_FLAG);
// 	deelx::CRegexpT <char> regexp2(dfd, deelx::NO_FLAG);
// 	cout << sdf<<endl;
// 	cout << dfd<<endl;
//	char * text = "12.5, a1.1, 0.123, 178";
	string data = "12.5, a1.1, 0.123, 178";
	__re__::Pattern pattern("\\b\\d+\\.\\d+");
	__re__::Match match = pattern.match(data);
	int length;
	while(match.IsMatched())
	{
		length = match.GetEnd() - match.GetStart();
		cout << data.substr(match.GetStart(), length)<<endl;
		match = pattern.match(data, match.GetEnd());
	}

	return EXIT_SUCCESS;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值