📄 string.hpp
字号:
/*******************************************************************************************
*
* Version 1.0 Beta- April 19th, 2006
*
*
* http://www.freewebs.com/coos/2006-04-19-01.hpp
*
* Copyright (c) 2006
* Carl Ge : carol8421@hotmail.com
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer,
* must be included in all copies of the Software, in whole or in part, and
* all derivative works of the Software, unless such copies or derivative
* works are solely in the form of machine-executable object code generated by
* a source language processor.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*
*******************************************************************************************/
#include <string>
#include <algorithm>
#ifndef _COOLECTION_STRINGC_
#define _COOLECTION_STRINGC_
#define is_upper(ch) ((ch >= 'A')&&(ch <= 'Z'))
#define is_lower(ch) ((ch >= 'a')&&(ch <= 'z'))
#define is_num(ch) ((ch >= '0')&&(ch <= '9'))
#define upper2lower(ch) (is_upper(ch)?(ch - 'A' + 'a'):ch)
#define lower2upper(ch) (is_lower(ch)?(ch - 'a' + 'A'):ch)
using namespace std;
namespace collection{
std::string vPrint(std::string fmt,va_list* marker){ char temp_buffer[513];std::string value_return(""); size_t length = fmt.size(); char flag = upper2lower(fmt[length - 1]); int base = 10; int error= 0; switch(flag) { default: error = true; goto isTranslate; case 'x': base = 16; goto isTranslate; case 'o': base = 8; goto isTranslate; case 'i': case 'd':goto isTranslate; case 'b': base = 2; goto isTranslate; case 'c': value_return = std::string(1,va_arg(*marker,const char)); goto isEnd; case 's': value_return = va_arg(*marker,const char*); goto isEnd; }isTranslate:if(error)return value_return;flag = upper2lower(fmt[length - 2]);if(flag == 'l') { value_return = _i64toa(va_arg(*marker,__int64),temp_buffer, base);}else if(flag == 'u'){value_return = _ui64toa(va_arg(*marker,unsigned __int64),temp_buffer, base);}else{value_return = _itoa(va_arg(*marker, int),temp_buffer, base); }isEnd: char* out_fmt = (char*)fmt.c_str() + 1; int out_len = atoi(out_fmt); while(out_len > value_return.size()) {if(*out_fmt == '0'){value_return.insert(0,'0'); } else{value_return.insert(0,' ');}}if(fmt[length - 1] == 'U')for(int i = 0; i < value_return.size(); i++) value_return[i] = lower2upper(value_return[i]);return value_return;}
template<typename T = std::basic_string< char > >
class StringC: T
{
protected:
typedef T traits_type;
typedef T::reference reference;
typedef T::const_reference const_reference;
typedef T::size_type size_type;
typedef T::pointer pointer;
typedef T::const_pointer const_pointer;
public:
StringC(){ };
StringC(traits_type val){ this->swap(val);};
const StringC<traits_type>& operator=(const_reference val){ return *this = traits_type(1,val); }
const StringC<traits_type>& operator=(traits_type val){ *this = StringC<traits_type>(val); return *this; }
const StringC<traits_type>& operator+=(const_reference val){ return *this += traits_type(1,val); }
const StringC<traits_type>& operator+=(traits_type val){ (*this).append(val); return *this; }
const StringC<traits_type>& operator+(const_reference val){ return *this + traits_type(1,val); }
const StringC<traits_type>& operator+(traits_type val){ (*this).append(val); return *this; }
const traits_type& c_str(){ return (*this); }
const_reference operator [](size_type pos) const { return (*this)[pos]; };
bool operator ==(const_reference val){ return *this == traits_type(1,val); }
bool operator ==( StringC s2 ){ return (*this).compare(s2) == 0);}
bool operator <=(const_reference val){ return *this <= traits_type(1,val); }
bool operator <=( StringC s2 ){ return (*this).compare(s2) <= 0);}
bool operator >=(const_reference val){ return *this >= traits_type(1,val); }
bool operator >=( StringC s2 ){ return (*this).compare(s2) >= 0);}
bool operator !=(const_reference val){ return *this != traits_type(1,val); }
bool operator !=( StringC s2 ){ return (*this).compare(s2) != 0);}
bool operator <(const_reference val){ return *this < traits_type(1,val); }
bool operator <( StringC s2 ){ return (*this).compare(s2) < 0);}
bool operator >(const_reference val){ return *this > traits_type(1,val); }
bool operator >( StringC s2 ){ return (*this).compare(s2) > 0);}
StringC<traits_type>& ToLower(){ size_type offset = GetLength() - 1;while(offset--) SetAt(offset,upper2lower((*this)[offset])); return *this;}
StringC<traits_type>& ToUpper(){ size_type offset = GetLength() - 1;while(offset--) SetAt(offset,lower2upper((*this)[offset])); return *this;}
StringC<traits_type> Mid(size_type nFirst, size_type nCount){return StringC<traits_type>((*this).substr(nFirst,nCount)); }
StringC<traits_type> Mid(size_type nFirst){return StringC<traits_type>((*this).substr(nFirst,(*this).size() - nFirst)); }
StringC<traits_type> Left(size_type nCount){return Mid(0,nCount); }
StringC<traits_type> Right(size_type nCount){return Mid((*this).size() - nCount,nCount); }
size_type Compare(StringC s2){ return value.compare(s2);}
size_type CompareNoCase(StringC s2){ s2.ToLower();ToLower();return (*this).compare(s2.c_str());}
size_type CompareNoCase(traits_type s2){return CompareNoCase(StringC(s2));}
const_pointer CHAR(){ return (*this).c_str().c_str(); }
StringC<traits_type>& Reverse(){ std::reverse(begin(), end()); return *this;}
size_type Replace(const_reference old_text, const_reference new_text){ return Replace(traits_type(1,old_text),traits_type(1,new_text));}
size_type Replace(traits_type old_text, traits_type new_text){ size_type offset = 0, repeated= 0 ; while((offset = this->rfind(old_text)) != traits_type::npos) this->replace(offset,old_text.size(),new_text), repeated++; return repeated;}
StringC<traits_type>& Remove(const_reference text){return Remove(traits_type(1,text));}
StringC<traits_type>& Remove(traits_type text){ size_type offset = 0; while((offset = this->rfind(text)) != traits_type::npos) this->erase(offset,text.size()); return *this;}
StringC<traits_type>& Insert(size_type index, const_reference text){return Insert(index, traits_type(1,text));}
StringC<traits_type>& Insert(size_type index, traits_type text){ if(index != traits_type::npos) this->insert(index,text); return *this;}
StringC<traits_type>& Delete(int index, int count = 1){ if(index != traits_type::npos) this->erase(index,count); return *this; }
StringC<traits_type>& TrimLeft(){ size_type offset = 0;do{ offset = this->find_first_not_of('\t');offset = (offset > (this->find_first_not_of('\n')))?offset:this->find_first_not_of('\n');offset = (offset > (this->find_first_not_of(' ')))?offset:this->find_first_not_of(' ');if(offset){ this->erase(0, offset); } }while(offset); return *this; }
StringC<traits_type>& TrimLeft(const_reference text){return TrimLeft(traits_type(1,text));}
StringC<traits_type>& TrimLeft(traits_type text){ size_type offset = 0;if((offset = this->find_first_not_of(text)) != traits_type::npos) this->erase(0,offset); return *this; }
StringC<traits_type>& TrimRight(){ size_type offset = 0;do{ offset = this->find_last_not_of('\t'); offset = (offset < (this->find_last_not_of('\n')))?offset:this->find_last_not_of('\n'); offset = (offset < (this->find_last_not_of(' ')))?offset:this->find_last_not_of(' '); if(offset != (this->size()-1)){ this->erase(offset, this->size() - offset); } }while(offset != (this->size()-1)); return *this; }
StringC<traits_type>& TrimRight(const_reference text){return TrimRight(traits_type(1,text));}
StringC<traits_type>& TrimRight(traits_type text){ size_type offset = 0;if((offset = this->find_last_not_of(text)) != traits_type::npos) this->erase(offset,this->size() - offset); return *this; }
size_type Find(traits_type text, size_type start){ return ((size_type pos = this->find_first_of(text,start)) != traits_type::npos)? pos: -1; }
size_type Find(traits_type text){ return Find(text, 0);}
size_type Find(const_reference text){ return Find(traits_type(1,text), 0);}
size_type Find(const_reference text, size_type start){ return Find(traits_type(1,text), start);}
size_type FindOneOf(traits_type text){ return Find(text, 0); }
size_type ReverseFind(traits_type text, size_type last){ return ((size_type pos = this->find_last_of(text,last)) != traits_type::npos)? pos: -1; }
size_type ReverseFind(traits_type text){ return ReverseFind(text, 0);}
size_type ReverseFind(const_reference text){ return ReverseFind(traits_type(1,text), 0);}
size_type ReverseFind(const_reference text, size_type last){ return ReverseFind(traits_type(1,text), last);}
StringC<traits_type>& Format(traits_type format,...){ std::string value_return("");va_list marker;va_start( marker, format ); for(size_type offset = 0;offset < format.size(); offset++){ if(format[offset] != '%'){value_return += format[offset];continue;} else{size_t offset_of_sign = 0; ++offset;while(is_num(format[offset])) offset_of_sign++, offset++;value_return += vPrint(format.substr(offset- offset_of_sign - 1,offset_of_sign + 2),&marker); format.erase(offset- offset_of_sign - 1,offset_of_sign + 2);offset -= offset_of_sign + 2;}} va_end( marker );*this = value_return;return *this;}
traits_type& GetBuffer(){ this->c_str(); }
traits_type& GetBufferSetLength(size_type length){ this->resize(length, '\0');}
void ReleaseBuffer() { GetBufferSetLength(0); }
const size_type GetLength(){ return (*this).size(); }
bool IsEmpty(){ return (*this).empty(); }
void Empty(){ (*this).resize(0); }
const_reference GetAt(size_type pos){ return (*this)[pos]; }
void SetAt(size_type pos, const_reference val){ (*this)[pos] = val;}
};
};
typedef collection::StringC<> String;
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -