uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

xstring.cpp (4624B)


      1 #include "xstring.hpp"
      2 
      3 namespace lab {
      4 xstring::xstring()
      5 {
      6     str = new char[1];
      7     str[0] = '\0';
      8     len = 0;
      9 }
     10 
     11 xstring::xstring(const char *s)
     12     :str(conv(s)), len(std::strlen(s)) {}
     13 
     14 xstring::xstring(const xstring& s)
     15 {
     16     if (!s.empty())
     17     {
     18         str = conv(s.str);
     19         len = s.len;
     20     }
     21     else clear();
     22 }
     23 
     24 xstring::~xstring()
     25 {
     26     delete[] str;
     27 }
     28 
     29 xstring
     30 xstring::operator= (const xstring& s)
     31 {
     32     if (this == &s) return *this;
     33     if (!s.empty())
     34     {
     35         if (!empty()) delete[] str;
     36         str = conv(s.str);
     37         len = s.len;
     38     }
     39     else clear();
     40     return *this;
     41 }
     42 
     43 xstring
     44 xstring::operator= (const char *s)
     45 {
     46     if (!empty()) delete[] str;
     47     str = conv(s);
     48     len = length();
     49     return *this;
     50 }
     51 
     52 xstring
     53 xstring::operator+ (const xstring& s)
     54 {
     55     return xstring(append(s));
     56 }
     57 
     58 xstring
     59 xstring::operator+ (const char *s)
     60 {
     61     return xstring(append(s));
     62 }
     63 
     64 xstring
     65 xstring::operator+ (char c)
     66 {
     67     return xstring(append(c));
     68 }
     69 
     70 xstring&
     71 xstring::operator+= (const xstring& s)
     72 {
     73     append(s.str);
     74     return *this;   
     75 }
     76 
     77 xstring&
     78 xstring::operator+= (const char *s)
     79 {
     80     append(s);
     81     return *this;
     82 }
     83 
     84 xstring&
     85 xstring::operator+= (char c)
     86 {
     87     push_back(c);
     88     return *this;
     89 }
     90 
     91 char&
     92 xstring::operator[] (std::size_t i) const
     93 {
     94     if (i >= len) throw std::runtime_error("Out of bounds.");
     95     return str[i];  
     96 }
     97 
     98 std::ostream&
     99 operator<< (std::ostream& stream, const xstring& s)
    100 {
    101     return stream << s.str;
    102 }
    103 
    104 std::istream&
    105 operator>> (std::istream& stream, const xstring& s)
    106 {
    107     return stream >> s.str;
    108 }
    109 
    110 xstring&
    111 xstring::append(const xstring& s)
    112 {
    113     if (!s.empty())
    114     {
    115         resize(s.len);
    116         std::strcat(str, s.str);
    117     }
    118     len = length();
    119     return *this;
    120 }
    121 
    122 xstring&
    123 xstring::append(const char *s)
    124 {
    125     if (!strempty(s))
    126     {
    127         resize(std::strlen(s));
    128         std::strcat(str, s);
    129     }
    130     len = length();
    131     return *this;
    132 }
    133 
    134 xstring&
    135 xstring::append(char c)
    136 {
    137     push_back(c);
    138     return *this;
    139 }
    140 
    141 xstring&
    142 xstring::insert(const xstring& s, std::size_t i)
    143 {
    144     if (!s.empty() && i < len)
    145     {
    146         char *tmp1 = new char[i + 1];
    147         char *tmp2 = new char[len - i + 1];
    148         std::copy(str, str + i + 1, tmp1);
    149         std::copy(str + i, str + len + 1, tmp2);
    150         resize(s.len);
    151         std::copy(tmp1, tmp1 + i + 1, str);
    152         std::copy(s.str, s.str + s.len + 1, str + i);
    153         std::copy(tmp2, tmp2 + len - i + 1, str + s.len + i); 
    154         len = length();
    155         str[len] = '\0';
    156         delete[] tmp1;
    157         delete[] tmp2;
    158     }
    159     return *this;
    160 }
    161 
    162 xstring&
    163 xstring::insert(const char *s, std::size_t i)
    164 {
    165     if (!strempty(s) && i < len)
    166     {
    167         char *tmp1 = new char[i + 1];
    168         char *tmp2 = new char[len - i + 1];
    169         std::copy(str, str + i + 1, tmp1);
    170         std::copy(str + i, str + len + 1, tmp2);
    171         resize(std::strlen(s));
    172         std::copy(tmp1, tmp1 + i + 1, str);
    173         std::copy(s, s + std::strlen(s) + 1, str + i);
    174         std::copy(tmp2, tmp2 + len - i + 1, str + std::strlen(s) + i); 
    175         len = length();
    176         str[len] = '\0';
    177         delete[] tmp1;
    178         delete[] tmp2;
    179     }
    180     return *this;
    181 }
    182 
    183 void
    184 xstring::push_back(char c)
    185 {
    186     resize(1);
    187     str[len] = c;
    188     str[len+1] = '\0';
    189     len = length();
    190 }
    191 
    192 void
    193 xstring::pop_back()
    194 {
    195     if (len - 1 > 0)
    196     {
    197         char *tmp = new char[len];
    198         std::copy(str, str + len, tmp);
    199         delete[] str;
    200         tmp[len-1] = '\0';
    201         str = tmp;
    202         len--;
    203     }
    204     else return;
    205 }
    206 
    207 void
    208 xstring::replace(std::size_t i, char c)
    209 {
    210     if (i < len) str[i] = c;    
    211 }
    212 
    213 void
    214 xstring::clear()
    215 {
    216     if (!this->empty()) delete[] str;
    217     str = new char[1];
    218     str[0] = '\0';
    219     len = 0;
    220 }
    221 
    222 bool
    223 xstring::find(const xstring& s) const
    224 {
    225     return (std::strstr(this->cstr(), s.cstr()) != nullptr);
    226 }
    227 
    228 bool
    229 xstring::find(const char *s) const
    230 {
    231     return (std::strstr(this->cstr(), s) != nullptr);
    232 }
    233 
    234 void
    235 xstring::resize(std::size_t n)
    236 {
    237     if (!this->empty())
    238     {
    239         std::size_t l = len + n;
    240         char *tmp = new char[l + 1];
    241         std::copy(str, str + len + 1, tmp);
    242         delete[] str;
    243         tmp[l] = '\0';
    244         str = tmp;
    245         len = length();
    246     }
    247 }
    248 
    249 char *
    250 xstring::conv(const char *s) const
    251 {
    252     std::size_t l = std::strlen(s);
    253     char *tmp = new char[l + 1];
    254     std::copy(s, s + l+1, tmp);
    255     tmp[l] = '\0';
    256     return tmp;
    257 }
    258 
    259 std::istream&
    260 getline(std::istream& stream, xstring& s, char delim)
    261 {
    262     char c;
    263     s.clear();
    264     while (stream.get(c) && c != '\n')
    265     {
    266         if (c == delim) break;
    267         else s.push_back(c);
    268     }
    269     return stream;
    270 }
    271 }