uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

xstring.hpp (4928B)


      1 #ifndef XSTRING_HPP
      2 #define XSTRING_HPP
      3 
      4 #include <algorithm>
      5 #include <cstdlib>
      6 #include <cstring>
      7 #include <iostream>
      8 
      9 namespace lab {
     10 class xstring
     11 {
     12     private:
     13         char *str;
     14         std::size_t len;
     15 
     16     public:
     17         xstring();
     18         xstring(const char *s);
     19         xstring(const xstring& s);
     20         ~xstring();
     21         
     22         xstring  operator=  (const xstring& s);
     23         xstring  operator=  (const char *s);
     24         xstring  operator+  (const xstring& s);
     25         xstring  operator+  (char c);
     26         xstring  operator+  (const char *s);
     27         xstring& operator+= (const xstring& s);
     28         xstring& operator+= (const char *s);
     29         xstring& operator+= (char c);
     30         constexpr bool operator== (const xstring& s) const
     31             {return std::strcmp(this->str, s.str) == 0;}
     32         constexpr bool operator== (const char *s)    const
     33             {return std::strcmp(this->str, s) == 0;}
     34         constexpr bool operator!= (const xstring& s) const
     35             {return std::strcmp(this->str, s.str) != 0;}
     36         constexpr bool operator!= (const char *s)    const
     37             {return std::strcmp(this->str, s) != 0;}
     38         constexpr bool operator<  (const xstring& s) const
     39             {return std::strcmp(this->str, s.str) < 0;}
     40         constexpr bool operator<  (const char *s)    const
     41             {return std::strcmp(this->str, s) < 0;}
     42         constexpr bool operator<= (const xstring& s) const
     43             {return std::strcmp(this->str, s.str) <= 0;}
     44         constexpr bool operator<= (const char *s)    const
     45             {return std::strcmp(this->str, s) <= 0;}
     46         constexpr bool operator>  (const xstring& s) const
     47             {return std::strcmp(this->str, s.str) > 0;}
     48         constexpr bool operator>  (const char *s)    const
     49             {return std::strcmp(this->str, s) > 0;}
     50         constexpr bool operator>= (const xstring& s) const
     51             {return std::strcmp(this->str, s.str) >= 0;}
     52         constexpr bool operator>= (const char *s)    const
     53             {return std::strcmp(this->str, s) >= 0;}
     54         char& operator[] (std::size_t i) const;
     55         friend std::ostream& operator<< (std::ostream& stream, const xstring& s);
     56         friend std::istream& operator>> (std::istream& stream, const xstring& s);
     57 
     58         xstring& append(const xstring& s);
     59         xstring& append(const char *s);
     60         xstring& append(char c);
     61         xstring& insert(const xstring& s, std::size_t i);
     62         xstring& insert(const char *s, std::size_t i);
     63         void push_back(char c);
     64         void pop_back();
     65         void replace(std::size_t i, char c);
     66         void clear();
     67         bool find(const xstring& s) const;
     68         bool find(const char *s)    const;
     69         constexpr char *cstr()  const {return str;}
     70         constexpr char& front() const {return str[0];}
     71         constexpr char& back()  const
     72             {return (!empty()) ? str[len-1] : str[0];};
     73         constexpr bool empty() const {return len == 0;}
     74         constexpr std::size_t length() const {return std::strlen(str);}
     75     
     76     private:
     77         void resize(std::size_t n);
     78         char *conv(const char *s) const;
     79         constexpr bool strempty(const char *s) const
     80             {return std::strlen(s) == 0;}
     81 };
     82 
     83 std::istream& getline(std::istream& stream, xstring& s, char delim = '\n');
     84 template<typename T> xstring to_xstr(T val);
     85 template<typename T> xstring to_xstr(const char *fs, T val);
     86 template<typename T> constexpr const char *getformat();
     87 
     88 template<typename T> xstring
     89 to_xstr(T val)
     90 {
     91     char buf[25];
     92     std::snprintf(buf, sizeof(buf), getformat<T>(), val);
     93     return xstring(buf);
     94 }
     95 
     96 template<typename T> xstring
     97 to_xstr(const char *fs, T val)
     98 {
     99     char buf[25];
    100     std::snprintf(buf, sizeof(buf), fs, val);
    101     return xstring(buf);
    102 }
    103 
    104 template<typename T> constexpr const char *
    105 getformat()
    106 {
    107     if constexpr (std::is_same_v<T, char>)           return "%c";
    108     if constexpr (std::is_same_v<T, unsigned char>)  return "%c";
    109     if constexpr (std::is_same_v<T, short>)          return "%hi";
    110     if constexpr (std::is_same_v<T, int>)            return "%d";
    111     if constexpr (std::is_same_v<T, long>)           return "%ld";
    112     if constexpr (std::is_same_v<T, long long>)      return "%lld";
    113     if constexpr (std::is_same_v<T, unsigned short>) return "%hu";
    114     if constexpr (std::is_same_v<T, unsigned>)       return "%u";
    115     if constexpr (std::is_same_v<T, unsigned int>)   return "%u";
    116     if constexpr (std::is_same_v<T, unsigned long>)  return "%lu";
    117     if constexpr (std::is_same_v<T, unsigned long>)  return "%llu";
    118     if constexpr (std::is_same_v<T, float>)          return "%f";
    119     if constexpr (std::is_same_v<T, double>)         return "%f";
    120     if constexpr (std::is_same_v<T, long double>)    return "%Lf";
    121     if constexpr (std::is_same_v<T, void *>)         return "%p";
    122     if constexpr (std::is_same_v<T, char *>)         return "%s";
    123 }
    124 }
    125 
    126 #endif /* XSTRING_HPP */