扩充std::string 类,format 重载,md5,base64,正则表达式等

#ifndef _MY_STRINGEX_FILE_
#define _MY_STRINGEX_FILE_

#include <stdio.h>
#include <iostream>
#include <stdarg.h>
#include <algorithm>
#include <vector>
#include <fstream>
#include <assert.h>
#include <boost/regex.hpp>
using namespace std;

#ifndef MD5_H
#define MD5_H

/* MD5 declaration. */
class MD5 {
public:
	MD5()
	{
		reset();
	}
	MD5(const void* input, size_t length)
	{
		reset();
		update(input, length);
	}
	
	MD5(const std::string& str)
	{
		reset();
		update(str);
	}
	
	MD5(std::ifstream& in)
	{
		reset();
		update(in);
	}
	
	void update(const void* input, size_t length)
	{
		update((const unsigned char*)input, length);
	}
	void update(const std::string& str)
	{
		update((const unsigned char*)str.c_str(), str.length());
	}
	void update(std::ifstream& in)
	{
		if (!in) {
			return;
		}
		
		std::streamsize length;
		char buffer[BUFFER_SIZE];
		while (!in.eof()) {
			in.read(buffer, BUFFER_SIZE);
			length = in.gcount();
			if (length > 0) {
				update(buffer, length);
			}
		}
		in.close();
	}
	const unsigned char* digest()
	{
		if (!_finished) {
			_finished = true;
			final();
		}
		return _digest;
	}
	
	std::string toString()
	{
		return bytesToHexString(digest(), 16);
	}
	void reset()
	{
		
		_finished = false;
		/* reset number of bits. */
		_count[0] = _count[1] = 0;
		/* Load magic initialization constants. */
		_state[0] = 0x67452301;
		_state[1] = 0xefcdab89;
		_state[2] = 0x98badcfe;
		_state[3] = 0x10325476;
	}
	
private:
	void update(const unsigned char* input, size_t length)
	{
		unsigned int i, index, partLen;
		
		_finished = false;
		
		/* Compute number of unsigned chars mod 64 */
		index = (unsigned int)((_count[0] >> 3) & 0x3f);
		
		/* update number of bits */
		if ((_count[0] += ((unsigned int)length << 3)) < ((unsigned int)length << 3)) {
			++_count[1];
		}
		_count[1] += ((unsigned int)length >> 29);
		
		partLen = 64 - index;
		
		/* transform as many times as possible. */
		if (length >= partLen) {
			
			memcpy(&_buffer[index], input, partLen);
			transform(_buffer);
			
			for (i = partLen; i + 63 < length; i += 64) {
				transform(&input[i]);
			}
			index = 0;
			
		} else {
			i = 0;
		}
		
		/* Buffer remaining input */
		memcpy(&_buffer[index], &input[i], length - i);
	}
	
	void final()
	{
		
		unsigned char PADDING[64] = { 0x80 };
		
		unsigned char bits[8];
		unsigned int oldState[4];
		unsigned int oldCount[2];
		unsigned int index, padLen;
		
		/* Save current state and count. */
		memcpy(oldState, _state, 16);
		memcpy(oldCount, _count, 8);
		
		/* Save number of bits */
		encode(_count, bits, 8);
		
		/* Pad out to 56 mod 64. */
		index = (unsigned int)((_count[0] >> 3) & 0x3f);
		padLen = (index < 56) ? (56 - index) : (120 - index);
		update(PADDING, padLen);
		
		/* Append length (before padding) */
		update(bits, 8);
		
		/* Store state in digest */
		encode(_state, _digest, 16);
		
		/* Restore current state and count. */
		memcpy(_state, oldState, 16);
		memcpy(_count, oldCount, 8);
	}
	void transform(const unsigned char block[64])
	{
		
		
		/* Constants for MD5Transform routine. */
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
		
		
		/* F, G, H and I are basic MD5 functions.
		*/
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
		
		/* ROTATE_LEFT rotates x left n bits.
		*/
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
		
		/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
		Rotation is separate from addition to prevent recomputation.
		*/
#define FF(a, b, c, d, x, s, ac) { \
	(a) += F ((b), (c), (d)) + (x) + ac; \
	(a) = ROTATE_LEFT ((a), (s)); \
	(a) += (b); \
	}
#define GG(a, b, c, d, x, s, ac) { \
	(a) += G ((b), (c), (d)) + (x) + ac; \
	(a) = ROTATE_LEFT ((a), (s)); \
	(a) += (b); \
	}
#define HH(a, b, c, d, x, s, ac) { \
	(a) += H ((b), (c), (d)) + (x) + ac; \
	(a) = ROTATE_LEFT ((a), (s)); \
	(a) += (b); \
	}
#define II(a, b, c, d, x, s, ac) { \
	(a) += I ((b), (c), (d)) + (x) + ac; \
	(a) = ROTATE_LEFT ((a), (s)); \
	(a) += (b); \
	}
		
		unsigned int a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16];
		
		decode(block, x, 64);
		
		/* Round 1 */
		FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
		FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
		FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
		FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
		FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
		FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
		FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
		FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
		FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
		FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
		FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
		FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
		FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
		FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
		FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
		FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
		
		/* Round 2 */
		GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
		GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
		GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
		GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
		GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
		GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
		GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
		GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
		GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
		GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
		GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
		GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
		GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
		GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
		GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
		GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
		
		/* Round 3 */
		HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
		HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
		HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
		HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
		HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
		HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
		HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
		HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
		HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
		HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
		HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
		HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
		HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
		HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
		HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
		HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
		
		/* Round 4 */
		II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
		II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
		II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
		II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
		II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
		II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
		II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
		II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
		II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
		II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
		II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
		II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
		II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
		II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
		II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
		II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
		
		_state[0] += a;
		_state[1] += b;
		_state[2] += c;
		_state[3] += d;
	}
	void encode(const unsigned int* input, unsigned char* output, size_t length)
	{
		for (size_t i = 0, j = 0; j < length; ++i, j += 4) {
			output[j]= (unsigned char)(input[i] & 0xff);
			output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff);
			output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff);
			output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff);
		}
	}
	void decode(const unsigned char* input, unsigned int* output, size_t length)
	{
		
		for (size_t i = 0, j = 0; j < length; ++i, j += 4) {
			output[i] = ((unsigned int)input[j]) | (((unsigned int)input[j + 1]) << 8) |
				(((unsigned int)input[j + 2]) << 16) | (((unsigned int)input[j + 3]) << 24);
		}
	}
	std::string bytesToHexString(const unsigned char* input, size_t length)
	{
		const char HEX[16] = {
			'0', '1', '2', '3',
				'4', '5', '6', '7',
				'8', '9', 'a', 'b',
				'c', 'd', 'e', 'f'
		};
		
		std::string str;
		str.reserve(length << 1);
		for (size_t i = 0; i < length; ++i) {
			int t = input[i];
			int a = t / 16;
			int b = t % 16;
			str.append(1, HEX[a]);
			str.append(1, HEX[b]);
		}
		
		return str;
	}
	
	/* class uncopyable */
	MD5(const MD5&);
	MD5& operator=(const MD5&);
	
private:
	unsigned int _state[4];	/* state (ABCD) */
	unsigned int _count[2];	/* number of bits, modulo 2^64 (low-order word first) */
	unsigned char _buffer[64];	/* input buffer */
	unsigned char _digest[16];	/* message digest */
	bool _finished;		/* calculate finished ? */
	
	//static const unsigned char PADDING[64];	/* padding for calculate */
	//static const char HEX[16];
	enum { BUFFER_SIZE = 1024 };
};


#endif /*MD5_H*/

#ifndef STRINGEX_H
#define STRINGEX_H


template<typename _CharT,typename _Traits,typename _Allocator>  
class _StringEx:public basic_string<char, char_traits<char>, allocator<char> >
{
public:
	TCHAR*	m_pBuffer;
	_StringEx(const _CharT __c)
	{
		this->assign(1, __c);
	}
	
	_StringEx(wchar_t* __ws)
	{
		wstring ws; ws.assign(__ws);

		string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
		setlocale(LC_ALL, "chs");
		const wchar_t* _Source = ws.c_str();
		size_t _Dsize = 2 * ws.size() + 1;
		char *_Dest = new char[_Dsize];
		memset(_Dest,0,_Dsize);
		wcstombs(_Dest,_Source,_Dsize);
		string result = _Dest;
		delete []_Dest;
		setlocale(LC_ALL, curLocale.c_str());

		this->assign(result);

	}

	_StringEx(wchar_t __wc)
	{
		wstring ws; ws.assign(1,__wc);

		string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
		setlocale(LC_ALL, "chs");
		const wchar_t* _Source = ws.c_str();
		size_t _Dsize = 2 * ws.size() + 1;
		char *_Dest = new char[_Dsize];
		memset(_Dest,0,_Dsize);
		wcstombs(_Dest,_Source,_Dsize);
		string result = _Dest;
		delete []_Dest;
		setlocale(LC_ALL, curLocale.c_str());

		this->assign(result);
		return *this;

	}

	_StringEx(const unsigned char* _ucs)
	{
		this->assign((char*)_ucs);
 
	}

	_StringEx(const unsigned char _ucc)
	{
		this->assign(1,_ucc);
	}

	_StringEx(const _CharT* __s)
	{
		this->assign(__s);
	}
	
	_StringEx()
	{
		this->assign("");
	}
	
	_StringEx& operator=(const string __str)
	{
		this->assign(__str);
		return *this;
	}
	_StringEx& operator=(const _StringEx& __str)
	{
		this->assign(__str);
		return *this;
	}
	
	_StringEx& operator=(const _CharT* __s)
	{
		this->assign(__s);
		return *this;
	}
	
	_StringEx& operator=(_CharT __c)
	{
 
		this->assign(1, __c);
		return *this;
	}

	_StringEx& operator=(const unsigned char* _ucs)
	{
		this->assign((char*)_ucs);
		return *this;
	}

	_StringEx& operator=(const unsigned char _ucc)
	{
		this->assign(1,(char)_ucc);
		return *this;
	}

	_StringEx& operator=(wchar_t __wc)
	{
		wstring ws; ws.assign(1,__wc);

		string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
		setlocale(LC_ALL, "chs");
		const wchar_t* _Source = ws.c_str();
		size_t _Dsize = 2 * ws.size() + 1;
		char *_Dest = new char[_Dsize];
		memset(_Dest,0,_Dsize);
		wcstombs(_Dest,_Source,_Dsize);
		string result = _Dest;
		delete []_Dest;
		setlocale(LC_ALL, curLocale.c_str());

		this->assign(result);
		return *this;


	}

	_StringEx& operator=(wchar_t* __ws)
	{
		wstring ws; ws.assign(__ws);

		string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
		setlocale(LC_ALL, "chs");
		const wchar_t* _Source = ws.c_str();
		size_t _Dsize = 2 * ws.size() + 1;
		char *_Dest = new char[_Dsize];
		memset(_Dest,0,_Dsize);
		wcstombs(_Dest,_Source,_Dsize);
		string result = _Dest;
		delete []_Dest;
		setlocale(LC_ALL, curLocale.c_str());

		this->assign(result);
		return *this;
	}
	
	_StringEx& Format(CHAR* fmt,...)
	{
		CHAR* buffer = new (std::nothrow) CHAR[BUFFER_SIZE];
		if(buffer!=NULL) 
		{
			va_list args; 
			va_start(args,fmt); 

			memset(buffer,0,BUFFER_SIZE);
			vsprintf(buffer, fmt,args); 
			va_end(args); 

			StringEx s(buffer);delete[] buffer;
			this->assign(s.c_str());
			
		}
		
		return *this;
	}
	
	_StringEx& Trim()
	{
		this->erase(0,this->find_first_not_of("\r\t\n "));
		this->erase(this->.find_last_not_of("\r\t\n ")+1);
		return *this;
	}
	
	template<class T> int CompareNoCase(T __str)
	{
		_StringEx s1(__str); s1.ToLower();
		return this->ToLower().compare(s1);
	}
	
	_StringEx& ToLower()
	{
		transform(this->begin(), this->end(), this->begin(), tolower); 
		return *this;
	}
	
	_StringEx& ToUpper()
	{
		transform(this->begin(), this->end(), this->begin(), toupper); 
		return *this;
	}
	
	_StringEx& Reverse()
	{
		this->reserve(this->begin(),this->end());
		return *this;
	}
	
	char GetAt(int i)
	{
		assert(i>=0 && i<this->size());
		char* p = (char*)this->c_str();
		return *(p+i);
	}
	
	wstring ToWStr()
	{
		setlocale(LC_ALL, "chs"); 
		const char* _Source = this->c_str();
		size_t _Dsize = this->size() + 1;
		wchar_t *_Dest = new wchar_t[_Dsize];
		wmemset(_Dest, 0, _Dsize);
		mbstowcs(_Dest,_Source,_Dsize);
		std::wstring result = _Dest;
		delete []_Dest;
		setlocale(LC_ALL, "C");
		return result;
	}
	
	template<class T>int Split(T space,vector<string>* ret)
	{
		std::string s = this->c_str();
		int nCount = 0;size_t last = 0;
		
		string item;
		
		size_t index=s.find_first_of(space,last);
		while (index!=std::string::npos)
		{
			item = s.substr(last,index-last);
			if(item.size()>0){
				ret->push_back(item); nCount++;}
			last=index+1;
			index=s.find_first_of(space,last);
		}
		
		if (index-last>0)
		{
			item = s.substr(last,index-last);
			if(item.size()>0){
				ret->push_back(item); nCount++;}
		}
		return nCount;
	}
	
	_StringEx& ToMd5()
	{
		string s; s.assign(this->c_str());
		MD5 m5;
		m5.update(s);
		this->assign(m5.toString());
		return *this;
	}
	
	_StringEx& ToBase64()
	{
		int DataByte = this->size();
		const unsigned char* Data = (const unsigned char*)this->c_str();
		
		//编码表
		const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
		//返回值
		std::string strEncode;
		unsigned char Tmp[4]={0};
		int LineLength=0;
		for(int i=0;i<(int)(DataByte / 3);i++)
		{
			Tmp[1] = *Data++;
			Tmp[2] = *Data++;
			Tmp[3] = *Data++;
			strEncode+= EncodeTable[Tmp[1] >> 2];
			strEncode+= EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];
			strEncode+= EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];
			strEncode+= EncodeTable[Tmp[3] & 0x3F];
			if(LineLength+=4,LineLength==76) {strEncode+="\r\n";LineLength=0;}
		}
		//对剩余数据进行编码
		int Mod=DataByte % 3;
		if(Mod==1)
		{
			Tmp[1] = *Data++;
			strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];
			strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4)];
			strEncode+= "==";
		}
		else if(Mod==2)
		{
			Tmp[1] = *Data++;
			Tmp[2] = *Data++;
			strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];
			strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];
			strEncode+= EncodeTable[((Tmp[2] & 0x0F) << 2)];
			strEncode+= "=";
		}
		
		
		this->assign(strEncode);
		return *this;
		
	}
	
	_StringEx& FromBase64()
	{
		int DataByte = this->size();
		const char* Data = (const char*) this->c_str();
		int OutByte = 0;
		
		//解码表
		const char DecodeTable[] =
		{
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				62, // '+'
				0, 0, 0,
				63, // '/'
				52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
				0, 0, 0, 0, 0, 0, 0,
				0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
				13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
				0, 0, 0, 0, 0, 0,
				26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
				39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
		};
		//返回值
		std::string strDecode;
		int nValue;
		int i= 0;
		while (i < DataByte)
		{
			if (*Data != '\r' && *Data!='\n')
			{
				nValue = DecodeTable[*Data++] << 18;
				nValue += DecodeTable[*Data++] << 12;
				strDecode+=(nValue & 0x00FF0000) >> 16;
				OutByte++;
				if (*Data != '=')
				{
					nValue += DecodeTable[*Data++] << 6;
					strDecode+=(nValue & 0x0000FF00) >> 8;
					OutByte++;
					if (*Data != '=')
					{
						nValue += DecodeTable[*Data++];
						strDecode+=nValue & 0x000000FF;
						OutByte++;
					}
				}
				i += 4;
			}
			else// 回车换行,跳过
			{
				Data++;
				i++;
			}
		}
		
		this->assign(strDecode);
		return *this;
	}

	int	ToInt()
	{
		return atoi(this->c_str());
	}

	int ToLong()
	{
		return atol(this->c_str());
	}

	float ToFloat()
	{
		return atof(this->c_str());
	}

	double ToDouble()
	{
		return ToFloat();
	}

	bool  ToBoolean()
	{
		return ToInt()!=0;
	}

	long ToHex()
	{
		return strtol(this->c_str(),NULL,16);
	}

	_StringEx& ToGB2312()
	{
 		int len = MultiByteToWideChar(CP_UTF8, 0, this->c_str(), -1, NULL, 0);
		wchar_t* wstr = new(std::nothrow) wchar_t[len+1];
		if(wstr!=NULL){
		memset(wstr, 0, len+1);
		MultiByteToWideChar(CP_UTF8, 0, this->c_str(), -1, wstr, len);
		len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
		char* str = new(std::nothrow) char[len+1];
		if(str!=NULL){
		memset(str, 0, len+1);
		WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
		if(wstr) delete[] wstr;
		this->assign(str); delete[] str;
		}
		}
		return *this;
	}

	_StringEx& ToUTF8()
	{
		int len = MultiByteToWideChar(CP_ACP, 0, this->c_str(), -1, NULL, 0);
		wchar_t* wstr = new (std::nothrow)wchar_t[len+1];
		if(wstr!=NULL){
		memset(wstr, 0, len+1);
		MultiByteToWideChar(CP_ACP, 0, this->c_str(), -1, wstr, len);
		len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
		char* str = new(std::nothrow) char[len+1];
		if(str!=NULL){
		memset(str, 0, len+1);
		WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
		if(wstr) delete[] wstr;
		this->assign(str);  delete[] str;
		}

		}
		return *this;

	}

	template<class T>_StringEx& FromHex(T _tv)
	{
		return Format("%02X",_tv);
	}
	
	_StringEx& FromBoolean(bool b)
	{
		this->assign(b?"true":"false");
	}

	_StringEx& FromInt(int n)
	{
		return Format("%d",n);
	}

	_StringEx& FromFloat(float f)
	{
		return Format("%f",f);
	}

	_StringEx& FromLong(long l)
	{
		return Format("%d",l);
	}

	_StringEx& FromDouble(double d)
	{
		return Format("%g",d);
	}

	template<class T> bool regex_Match(T _pattern,bool ignorecase=true)
	{
		string pattern(_pattern);
		std::string s= this->c_str();
		int nType = boost::regex::icase|boost::regex::perl;
		if(!ignorecase) nType = boost::regex::perl;
		
		boost::regex reg(pattern,nType); 
		return boost::regex_match(s,reg);
	}

	template<class T> _StringEx& regex_Replace(T _pattern,T _rpl,bool ignorecase=true)
	{
		string pattern(_pattern),rpl(_rpl);
		std::string s= this->c_str();
		int nType = boost::regex::icase|boost::regex::perl;
		if(!ignorecase) nType = boost::regex::perl;
		boost::regex reg(pattern,nType); 
		this->assign( boost::regex_replace(s,reg,rpl) );
		return *this;
	}

	template<class T> int regex_Search(T _pattern,vector<string>* ret,bool ignorecase=true)
	{
		string pattern(_pattern);
		std::string s= this->c_str();
		int nType = boost::regex::icase|boost::regex::perl;
		if(!ignorecase) nType = boost::regex::perl;
		boost::regex reg(pattern,nType); 

		boost::smatch what; 
		
		std::string::const_iterator it=s.begin(); 
		std::string::const_iterator end=s.end(); 
		
		int nCount = 0;		
		while (boost::regex_search(it,end,what,reg)) { 
			
			int size  = what.size();
			std::string msg(what[1].first, what[1].second);
			ret->push_back(what.str());
			it=what[0].second; nCount++;
		}

		return nCount;
	}
	

	
	private:
		enum { BUFFER_SIZE = 10240 };
	
	
};


typedef  _StringEx< char, char_traits<char>, allocator<char> > StringEx;  

#endif
#endif
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值