Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include <cassert>
  2. #include <ctype.h>
  3. #include <cstring>
  4. #include <string>
  5.  
  6. class CIString {
  7.     std::string _String;
  8. public:
  9.     CIString(std::string s)
  10.         : _String(s)
  11.     { }
  12.  
  13.     char& operator[] (int i) {
  14.         return _String[i];
  15.     }
  16.  
  17.     template<class L, class R> friend bool operator==(L l, R r);
  18.     template<class L, class R> friend bool operator<(L l, R r);
  19.     template<class L, class R> friend bool operator>(L l, R r);
  20.     template<class L, class R> friend bool operator!=(L l, R r);
  21.     template<class L, class R> friend bool operator>=(L l, R r);
  22.     template<class L, class R> friend bool operator<=(L l, R r);
  23.     template<class T> friend int get_size(T other);
  24. };
  25.  
  26.     template<class L, class R>
  27.     bool operator==(L l, R r) {
  28.         return !cmp(l, r);
  29.     }
  30.  
  31.     template<class L, class R>
  32.     bool operator<(L l, R r) {
  33.         return cmp(l,r) < 0;
  34.     }
  35.  
  36.     template<class L, class R>
  37.     bool operator>(L l, R r) {
  38.         return cmp(l,r) > 0;
  39.     }
  40.  
  41.     template<class L, class R>
  42.     bool operator!=(L l, R r) {
  43.         return cmp(l,r) != 0;
  44.     }
  45.  
  46.     template<class L, class R>
  47.     bool operator>=(L l, R r) {
  48.         return cmp(l,r) >= 0;
  49.     }
  50.  
  51.     template<class L, class R>
  52.     bool operator<=(L l, R r) {
  53.         return cmp(l,r) <= 0;
  54.     }
  55.  
  56.     template<class T>
  57.     int get_size(T other);
  58.  
  59.  
  60.     template<class L, class R>
  61.     int cmp(L l, R r) {
  62.         int s1 = get_size(l);
  63.         int s2 = get_size(r);
  64.  
  65.         size_t i = 0;
  66.         for (i = 0; i < s1 && i < s2; ++i) {
  67.             char c1 = tolower(l[i]);
  68.             char c2 = tolower(r[i]);
  69.             if (c1 == c2) {
  70.                 continue;
  71.             }
  72.             if (c1 < c2) {
  73.                 return -1;
  74.             }
  75.             if (c1 > c2) {
  76.                 return 1;
  77.             }
  78.         }
  79.         if (s1 == s2) {
  80.             return 0;
  81.         }
  82.         return (s1 < s2) ? -1 : 1;
  83.     }
  84.  
  85.     template<>
  86.     int get_size<const char*>(const char* other) {
  87.         return strlen(other);
  88.     }
  89.  
  90.     template<>
  91.     int get_size<CIString>(CIString other) {
  92.         return other._String.size();
  93.     }
  94.  
  95.  
  96.  
  97. int main(int, const char**) {
  98.     //const char* operators
  99.     assert(CIString("") == "");
  100.     assert(CIString("") != "a" && CIString("") < "a" && CIString("") <= "a" );
  101.     assert(CIString("a") != "" && CIString("a") > "" && CIString("a") >= "" );
  102.  
  103.     assert(CIString("AbC") == "aBc");
  104.     assert(CIString("abCd") != "aBc");
  105.     assert(CIString("abCd") > "aBc" && CIString("abcd") >= "abc");
  106.     assert(CIString("aBc") < "abCd" && CIString("abc") <= "abcd");
  107.  
  108.     assert(CIString("abC") != "abD");
  109.     assert(CIString("aBc") < "abD");
  110.  
  111.  
  112.     ////CIString operators
  113.     assert(CIString("") == CIString(""));
  114.     assert(CIString("") != CIString("a") && CIString("") < CIString("a") && CIString("") <= CIString("a") );
  115.     assert(CIString("a") != CIString("") && CIString("a") > CIString("") && CIString("a") >= CIString("") );
  116.  
  117.     assert(CIString("AbC") == CIString("aBc"));
  118.     assert(CIString("AbCd") != CIString("Abc"));
  119.     assert(CIString("abcd") > CIString("abc") && CIString("abcd") >= CIString("abc"));
  120.     assert(CIString("abc") < CIString("abcd") && CIString("abc") <= CIString("abcd"));
  121.  
  122.     assert(CIString("abc") != CIString("abd"));
  123.     assert(CIString("abc") < CIString("abd"));
  124.  
  125.     ////last
  126.     assert("" == CIString(""));
  127.     assert("a" != CIString("") && "a" > CIString("") && "a" >= CIString("") );
  128.     assert("" != CIString("a") && "" < CIString("a")  && "" <= CIString("a"));
  129.  
  130.     assert("aBc" == CIString("AbC"));
  131.     assert("aBc" != CIString("abCd"));
  132.     assert("aBc" < CIString("abCd") && "aBc" <= CIString("abCd"));
  133.     assert("abCd" > CIString("aBc") && "abCd" >= CIString("abc"));
  134.  
  135.     assert("abD" != CIString("abC"));
  136.     assert("abD" > CIString("aBc"));
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement