00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef __XVR2_ENABLE_BASIC_STRING__
00015 #error "DO NOT INCLUDE THIS FILE DIRECTLY"
00016 #endif
00017 #ifndef __XVR2_BASIC_STRING_H__
00018 #define __XVR2_BASIC_STRING_H__
00019 #include<xvr2/Object.h>
00020 #include<xvr2/CoreExceptions.h>
00021 #include<string>
00022
00023 namespace xvr2 {
00026 template<class _charT>
00027 class BasicString : public Object, public std::basic_string<_charT> {
00028 protected:
00029 BasicString() : xvr2::Object(){
00030 }
00031 BasicString(const _charT *s) : xvr2::Object(){
00032 std::basic_string<_charT>::assign(s);
00033 }
00034 public:
00035 ~BasicString(){}
00042 virtual int index(const _charT *s){
00043 return std::basic_string<_charT>::find(s);
00044 }
00051 virtual int index(const _charT *s) const{
00052 return std::basic_string<_charT>::find(s);
00053 }
00060 virtual int index(const BasicString<_charT> &s){
00061 return std::basic_string<_charT>::find(s.c_str());
00062 }
00069 virtual int rindex(const _charT *s){
00070 return std::basic_string<_charT>::find_last_of(s);
00071 }
00078 virtual int rindex(const _charT *s) const{
00079 return std::basic_string<_charT>::find_last_of(s);
00080 }
00087 virtual int rindex(const BasicString<_charT> &s){
00088 return std::basic_string<_charT>::find_last_of(s.c_str());
00089 }
00094 BasicString<_charT> &biteLeft(UInt32 howmany = 1){
00095 if(std::basic_string<_charT>::size() == howmany){
00096 std::basic_string<_charT>::clear();
00097 }
00098 else{
00099 std::basic_string<_charT>::erase(
00100 std::basic_string<_charT>::begin(),
00101 std::basic_string<_charT>::begin() + howmany
00102 );
00103 }
00104 return *this;
00105 }
00111 BasicString<_charT> &biteRight(UInt32 howmany = 1){
00112 if(std::basic_string<_charT>::size() == howmany){
00113 std::basic_string<_charT>::clear();
00114 }
00115 else{
00116 std::basic_string<_charT>::erase(
00117 std::basic_string<_charT>::end() - howmany,
00118 std::basic_string<_charT>::end()
00119 );
00120 }
00121 return *this;
00122 }
00125 BasicString<_charT> &deleteFirst(){
00126 return biteLeft(1);
00127 }
00130 BasicString<_charT> &deleteLast(){
00131 return biteRight(1);
00132 }
00137 virtual BasicString<_charT> &trimLeft(const _charT c = ' '){
00138 while(std::basic_string<_charT>::size() > 0 &&
00139 std::basic_string<_charT>::at(0) == c){
00140 std::basic_string<_charT>::erase(
00141 std::basic_string<_charT>::begin(),
00142 std::basic_string<_charT>::begin() + 1
00143 );
00144 }
00145 return *this;
00146 }
00151 virtual BasicString<_charT> &trimRight(const _charT c = ' '){
00152 while(std::basic_string<_charT>::size() > 0 &&
00153 std::basic_string<_charT>::at(std::basic_string<_charT>::size() - 1) == c){
00154 std::basic_string<_charT>::erase(
00155 std::basic_string<_charT>::end() - 1,
00156 std::basic_string<_charT>::end()
00157 );
00158 }
00159 return *this;
00160 }
00165 virtual BasicString<_charT> &trim(const _charT c = ' '){
00166 trimLeft(c);
00167 trimRight(c);
00168 return *this;
00169 }
00174 virtual bool equals(const _charT *s){
00175 return (std::basic_string<_charT>::compare(s) == 0)?true:false;
00176 }
00181 virtual bool equals(const BasicString<_charT> &s){
00182 return (std::basic_string<_charT>::compare(s) == 0)?true:false;
00183 }
00188 virtual bool equals(const BasicString<_charT> &s) const{
00189 return (std::basic_string<_charT>::compare(s) == 0)?true:false;
00190 }
00195 virtual bool operator==(const _charT *s){
00196 return (std::basic_string<_charT>::compare(s) == 0)?true:false;
00197 }
00202 virtual bool operator==(const BasicString<_charT> &s){
00203 return (std::basic_string<_charT>::compare(s) == 0)?true:false;
00204 }
00206 void deleteString(){
00207 std::basic_string<_charT>::clear();
00208 }
00211 const _charT *toCharPtr() const {
00212 return std::basic_string<_charT>::c_str();
00213 }
00216 const _charT *toCharPtr(){
00217 return std::basic_string<_charT>::c_str();
00218 }
00223 const _charT charAt(int i) const {
00224 return std::basic_string<_charT>::at(i);
00225 }
00230 const _charT charAt(int i) {
00231 return std::basic_string<_charT>::at(i);
00232 }
00237 const _charT operator[](int i){
00238 return std::basic_string<_charT>::at(i);
00239 }
00244 const _charT operator[](int i) const {
00245 return std::basic_string<_charT>::at(i);
00246 }
00251 bool startsWith(const _charT *s) const {
00252 return (std::basic_string<_charT>::find(s) == 0)?true:false;
00253 }
00258 bool endsWith(const _charT *s) const {
00259 std::basic_string<_charT> tmp = s;
00260
00261 return (std::basic_string<_charT>::size() -
00262 (std::basic_string<_charT>::find_last_of(s) + 1)
00263 == 0)?true:false;
00264 }
00269 BasicString<_charT> &deleteCharAt(int pos){
00270 std::basic_string<_charT>::erase(pos);
00271 return *this;
00272 }
00277 BasicString<_charT> &insertCharAt(const int pos, const _charT c) {
00278 std::string::insert(pos, 1, c);
00279 return *this;
00280 }
00287 BasicString<_charT> getSubstr(int start, int end){
00288 return BasicString<_charT>::substr(start, end);
00289 }
00296 BasicString<_charT> &replace(const _charT from, const _charT *to){
00297 for(unsigned int i = 0; i < std::basic_string<_charT>::size(); i++){
00298 if(charAt(i) == from){
00299 std::basic_string<_charT>::erase(i, 1);
00300 std::basic_string<_charT>::insert(i, to);
00301 i++;
00302 }
00303 }
00304 return *this;
00305 }
00306 };
00307 }
00308 #endif