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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
#ifndef ALA_TEXT_H
#define ALA_TEXT_H
#include<cstddef>
#include<cstring>
#include<cstdlib>
#include<sstream>
#include<algorithm>
typedef char s_char;
typedef const char cs_char;
typedef const size_t size_tc;
template<typename unkwn>
cs_char*_to_string(unkwn item){
std::stringstream ss;
ss<<std::fixed<<item;
std::string str=ss.str();
return str.c_str();
};
template<class unkwn,unkwn def>
struct def_value{
unkwn value;
def_value():value(def){};
def_value(unkwn v):value(v){};
operator def_value()const{return value;};
};
class tale{
private:
size_t _length;
s_char* _buffer;
public:
//properties
def_value<bool,false>_temporary;
//method
bool empty(){
return _length==0;
};
size_tc length(){
return _length;
};
cs_char*gets(){
return(cs_char*)_buffer;
};
void clear(){
if(_length<1)return;
_length=0;
delete[]_buffer;
};
//operator
template<typename unkwn>
tale&operator+(unkwn item){
if(_temporary.value){
*this+=_to_string(item);
return*this;
};
tale*res=new tale[1];
*res+=*this;
*res+=_to_string(item);
(*res)._temporary=true;
return*res;
};
tale&operator+(tale&item){
if(_temporary.value){
*this+=item;
if(item._temporary.value){
delete&item;
};
return*this;
};
tale*res=new tale[1];
*res+=*this;
*res+=item.gets();
(*res)._temporary=true;
if(item._temporary.value){
delete&item;
};
return*res;
};
tale&operator+(cs_char*item){
if(_temporary.value){
*this+=_to_string(item);
return*this;
};
tale*res=new tale[1];
*res+=*this;
*res+=item;
(*res)._temporary=true;
return*res;
};
tale&operator+(cs_char&item){
if(_temporary.value){
*this+=_to_string(item);
return*this;
};
tale*res=new tale[1];
*res+=*this;
*res+=item;
(*res)._temporary=true;
return*res;
};
tale&operator+=(tale&item){
size_tc len=item.length();
_buffer=(s_char*)realloc(_buffer,_length+len+1);
memcpy(&_buffer[_length],item.gets(),len);
_buffer[_length+=len]=0;
if(item._temporary.value){
delete&item;
};
return*this;
};
tale&operator+=(cs_char*text){
size_tc len=strlen(text);
_buffer=(s_char*)realloc(_buffer,_length+len+1);
memcpy(&_buffer[_length],text,len);
_buffer[_length+=len]=0;
return*this;
};
tale&operator+=(cs_char&chr){
_buffer=(s_char*)realloc(_buffer,_length+2);
_buffer[_length]=chr;
_buffer[_length+=1]=0;
return*this;
};
tale&operator=(tale&item){
delete[]_buffer;
_length=item.length();
_buffer=new s_char[_length];
memcpy(_buffer,item.gets(),_length);
_buffer[_length]=0;
if(item._temporary.value){
delete&item;
};
return*this;
};
tale&operator=(cs_char*text){
delete[]_buffer;
_length=strlen(text);
_buffer=new s_char[_length];
memcpy(_buffer,text,_length);
_buffer[_length]=0;
return*this;
};
tale&operator=(cs_char&chr){
delete[]_buffer;
_length=1;
_buffer=new s_char[_length];
_buffer[0]=chr;
_buffer[_length]=0;
return*this;
};
//constructor
tale(cs_char*text){
_length=strlen(text);
_buffer=new s_char[_length];
memcpy(_buffer,text,_length);
_buffer[_length]=0;
};
tale(cs_char&chr){
_length=1;
_buffer=new s_char[_length];
_buffer[0]=chr;
_buffer[_length]=0;
};
tale(){
_length=0;
_buffer=new s_char[_length];
_buffer[0]=0;
};
//destructor
~tale(){
delete[]_buffer;
};
};
#endif
|