Guest User

pistring.h

a guest
Dec 19th, 2010
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.20 KB | None | 0 0
  1. /*Created by: PiMaster
  2.  
  3. Member Functions:
  4.     inline const char* c_str() const - return the data of the string as a const c string
  5.     clear() - clear the contents of the string (no return)
  6.     insert(const char c,unsigned pos) - inserts a character at pos (returns new length)
  7.     insert(const char* str,unsigned pos) - inserts a c-string at pos (returns new length)
  8.     insert(const string& str,unsigned pos) - inserts a string at pos (returns new length)
  9.     count(const char c) - returns the number of occurrences of c
  10.     count(const char* str) - returns the number of occurrences of str
  11.     count(const string& str) - returns the number of occurrences of str
  12.     replace(const char* str1,const char* str2) - replace all occurrences of str1 with str2 (returns number of occurrences)
  13.     replace(const string& str1,const char* str2) - replace all occurrences of str1 with str2 (returns number of occurrences)
  14.     replace(const char* str1,const string& str2) - replace all occurrences of str1 with str2 (returns number of occurrences)
  15.     replace(const string& str1,const string& str2) - replace all occurrences of str1 with str2 (returns number of occurrences)
  16.     erase(unsigned start,unsigned end) - erase data between start and end (returns new length)
  17.     find(const char c,unsigned n,unsigned start=0) - returns the position of the nth occurrence of c starting at start
  18.     find(const char* str,unsigned n,unsigned start=0) - returns the position of the nth occurrence of str starting at start
  19.     find(const string& str,unsigned n,unsigned start=0) - returns the position of the nth occurrence of str starting at start
  20.     rfind(const char c,unsigned n,unsigned start) - returns the position of the nth occurrence of c going backwards starting at start
  21.     rfind(const char* str,unsigned n,unsigned start) - returns the position of the nth occurrence of str going backwards starting at start
  22.     rfind(const string& str,unsigned n,unsigned start) - returns the position of the nth occurrence of str going backwards starting at start
  23.     reserve(unsigned newsize) - request a new maximum size for the string (returns new size)
  24.         NOTE: If newsize is less than the current size, the reallocation will NOT happen
  25.     upper() - make all letters in the string uppercase (no return)
  26.     lower() - make all letters in the string lowercase (no return)
  27.     length() - returns the length of the string
  28.     capacity() - returns the capacity of the string (size of the buffer)
  29.     reverse() - reverse the characters in the string (no return)
  30.  
  31. Member Operators:
  32.     inline operator char*() - converts the string to a c string
  33.     inline operator const char*() - converts the string to a const c string
  34.     inline char& operator[](const int x) - returns the character at index x
  35.         NOTE: If x is greater than the string length, it will return a reference to stroob
  36.     inline const char& operator[](const int x) const - returns the const character at index x
  37.         NOTE: If x is greater than the string length, it will return a reference to stroob
  38.     string& operator=(const char c) - sets the string equal to c (NULL terminated) (returns the string)
  39.     string& operator=(const char* str) - sets the string equal to str (returns the string)
  40.     string& operator=(const string& str) - sets the string equal to str (returns the string)
  41.     string& operator+=(const char c) - appends c to the string (returns the string)
  42.     string& operator+=(const char* str) - appends str to the string (returns the string)
  43.     string& operator+=(cosnt string& str) - appends str to the string (returns the string)
  44.  
  45. Global Operators:
  46.     bool operator==(const string& str1,const string& str2) - returns if str1 is equal to str2
  47.     bool operator==(const string& str1,const char* str2) - returns if str1 is equal to str2
  48.     bool operator==(const char* str1,const string& str2) - returns if str1 is equal to str2
  49.     bool operator!=(const string& str1,const string& str2) - returns if str1 is not equal to str2
  50.     bool operator!=(const string& str1,const char* str2) - returns if str1 is not equal to str2
  51.     bool operator!=(const char* str1,const string& str2) - returns if str1 is not equal to str2
  52.     bool operator>(const string& str1,const string& str2) - returns if the length of str1 is greater than the length of str2
  53.     bool operator>(const string& str1,const char* str2) - returns if the length of str1 is greater than the length of str2
  54.     bool operator>(const char* str1,const string& str2) - returns if the length of str1 is greater than the length of str2
  55.     bool operator<(const string& str1,const string& str2) - returns if the length of str1 is less than the length of str2
  56.     bool operator<(const string& str1,const char* str2) - returns if the length of str1 is less than the length of str2
  57.     bool operator<(const char* str1,const string& str2) - returns if the length of str1 is less than the length of str2
  58.     bool operator>=(const string& str1,const string& str2) - returns if the length of str1 is greater than or equal to the length of str2
  59.     bool operator>=(const string& str1,const char* str2) - returns if the length of str1 is greater than or equal to the length of str2
  60.     bool operator>=(const char* str1,const string& str2) - returns if the length of str1 is greater than or equal to the length of str2
  61.     bool operator<=(const string& str1,const string& str2) - returns if the length of str1 is less than or equal to the length of str2
  62.     bool operator<=(const string& str1,const char* str2) - returns if the length of str1 is less than or equal to the length of str2
  63.     bool operator<=(const char* str1,const string& str2) - returns if the length of str1 is less than or equal to the length of str2
  64.  
  65. */
  66.  
  67. #pragma once
  68. #ifndef PISTRING
  69. #define PISTRING
  70.  
  71. #ifndef NULL
  72. #define NULL 0
  73. #endif
  74.  
  75. char stroob=0;
  76.  
  77. struct iterator;
  78.  
  79. struct string{
  80.     private:
  81.     //memory related functions
  82.         //resize the string data array
  83.         void resizedata(unsigned newsize){
  84.             //minimum buffer size
  85.             if(newsize<minbuffsize){
  86.                 newsize=minbuffsize;}
  87.             char* tmp=new char[datasize+newsize];
  88.             if(data!=NULL){
  89.                 memcpy(tmp,data,datalen+1);
  90.                 delete[] data;}
  91.             data=tmp;
  92.             datasize+=newsize;}
  93.         //doesn't check for memory overlapping
  94.         void* memcpy(void* dst,const void* src,unsigned len){
  95.             if(dst==NULL || src==NULL || len==0){
  96.                 return NULL;}
  97.             char* d=(char*)dst;
  98.             char* s=(char*)src;
  99.             unsigned n=0;
  100.             while(n<len){
  101.                 *d=*s;
  102.                 ++d;
  103.                 ++s;
  104.                 ++n;}
  105.             return dst;}
  106.         //does check for memory overlapping
  107.         void* memmove(void* dst,const void* src,unsigned len){
  108.             if(dst==NULL || src==NULL || len==0){
  109.                 return NULL;}
  110.             char* d=(char*)dst;
  111.             char* s=(char*)src;
  112.             unsigned n=0;
  113.             //if dst is potentially overlapping src
  114.             if(d>s){
  115.                 d+=len;
  116.                 s+=len;
  117.                 while(n<len){
  118.                     *d=*s;
  119.                     ++d;
  120.                     ++s;
  121.                     ++n;}}
  122.             else{
  123.                 while(n<len){
  124.                     *d=*s;
  125.                     ++d;
  126.                     ++s;
  127.                     ++n;}}
  128.             return dst;}
  129.         unsigned strlen(const char* str) const{
  130.             unsigned x=0;
  131.             while(str[x]!=0){
  132.                 ++x;}
  133.             return x;}
  134.     //functions that are called by other functions
  135.         //count the number of occurrences of str
  136.         unsigned countstr(const char* str,unsigned size) const{
  137.             unsigned x,pos=0,n=0;
  138.             while(pos<=datalen){
  139.                 //check if we found the string
  140.                 for(x=0;x<size && pos<=datalen;++x,++pos){
  141.                     if(data[pos]!=str[x]){
  142.                         break;}}
  143.                 if(x==size){
  144.                     ++n;}
  145.                 else{
  146.                     ++pos;}}
  147.             return n;}
  148.         //replace every occurrence of str1 with str2 a maximum of num times
  149.         unsigned replacestr(const char* str1,const char* str2,unsigned size1,unsigned size2,unsigned num){
  150.             unsigned c=countstr(str1,size1);
  151.             if(datalen+(size2-size1)*c>datasize){
  152.                 resizedata(((size2-size1)*c)<<1);}
  153.             unsigned x,pos=0,n=0;
  154.             while(pos<=datalen && n<num){
  155.                 for(x=0;x<size1 && pos<=datalen;++x,++pos){
  156.                     if(data[pos]!=str1[x]){
  157.                         break;}}
  158.                 if(x==size1){
  159.                     memmove(data+pos+size2-size1,data+pos,datalen+size2-size1);
  160.                     memcpy(data+pos-size1,str2,size2);
  161.                     ++n;}
  162.                 else{
  163.                     ++pos;}}
  164.             datalen+=(size2-size1)*n;
  165.             return n;}
  166.         //find the nth occurrence of str starting at x
  167.         unsigned findstr(const char* str,unsigned s,unsigned x,unsigned n) const{
  168.             unsigned pos,c=0;
  169.             for(x=0;x<datalen;++x){
  170.                 for(pos=0;pos<s;++pos){
  171.                     if(data[x+pos]!=str[pos]){
  172.                         break;}}
  173.                 if(pos==s){
  174.                     if(c==n){
  175.                         return x;}
  176.                     else{
  177.                         ++c;}}}
  178.             return datalen+1;}
  179.         //find the nth occurrence of str starting at x going backwards
  180.         unsigned rfindstr(const char* str,unsigned s,unsigned x,unsigned n) const{
  181.             unsigned pos,c=0;
  182.             for(;x<datalen;--x){
  183.                 for(pos=0;pos<s;++pos){
  184.                     if(data[x+pos]!=str[pos]){
  185.                         break;}}
  186.                 if(pos==s){
  187.                     if(c==n){
  188.                         return x;}
  189.                     else{
  190.                         ++c;}}
  191.                 else{
  192.                     x-=pos;}}
  193.             return datalen+1;}
  194.         int comparestr(const char* str,unsigned first,unsigned last,unsigned start,unsigned end){
  195.             unsigned pos=first;
  196.             unsigned spos=start;
  197.             while(pos<=last && spos<=end && data[pos]==str[spos]){
  198.                 ++pos;
  199.                 ++spos;}
  200.             if(pos==last+1){
  201.                 return 0;}
  202.             return str[spos]-data[pos];}
  203.     //constants
  204.         //maximum unsigned integer
  205.         const static unsigned maxint=~0;
  206.         //difference between 'a' and 'A' (used in upper() and lower() functions)
  207.         const static unsigned aA_diff='a'-'A';
  208.         //the maximum buffer size
  209.         const static unsigned minbuffsize=32;
  210.     //friends
  211.         //operators
  212.         //equal
  213.         friend bool operator==(const string&,const string&);
  214.         friend bool operator==(const string&,const char*);
  215.         friend bool operator==(const char*,const string&);
  216.         //not equal
  217.         friend bool operator!=(const string&,const string&);
  218.         friend bool operator!=(const string&,const char*);
  219.         friend bool operator!=(const char*,const string&);
  220.         //greater than
  221.         friend bool operator>(const string&,const string&);
  222.         friend bool operator>(const string&,const char*);
  223.         friend bool operator>(const char*,const string&);
  224.         //less than
  225.         friend bool operator<(const string&,const string&);
  226.         friend bool operator<(const string&,const char*);
  227.         friend bool operator<(const char*,const string&);
  228.         //greater than or equal to
  229.         friend bool operator>=(const string&,const string&);
  230.         friend bool operator>=(const string&,const char*);
  231.         friend bool operator>=(const char*,const string&);
  232.         //less than or equal to
  233.         friend bool operator<=(const string&,const string&);
  234.         friend bool operator<=(const string&,const char*);
  235.         friend bool operator<=(const char*,const string&);
  236.     //iterator
  237.         friend struct iterator;
  238.     protected:
  239.         //data
  240.         char* data;
  241.         unsigned datasize,datalen;
  242.     public:
  243.         string(){
  244.             data=new char[32];
  245.             data[0]=0;
  246.             datasize=32;
  247.             datalen=0;}
  248.         string(const char* str){
  249.             unsigned s=strlen(str);
  250.             data=new char[s+minbuffsize];
  251.             memcpy(data,str,s+1);
  252.             datasize=s+minbuffsize;
  253.             datalen=s;}
  254.         string(const string& str){
  255.             data=new char[str.datasize];
  256.             memcpy(data,str.data,str.datalen+1);
  257.             datasize=str.datasize;
  258.             datalen=str.datalen;}
  259.         ~string(){
  260.             delete[] data;}
  261.         inline operator const char*() const{
  262.             return data;}
  263.         inline const char* c_str() const{
  264.             return data;}
  265.         inline char& operator[](unsigned x){
  266.             if(x>datalen){
  267.                 return stroob;}
  268.             return data[x];}
  269.         inline const char& operator[](unsigned x) const{
  270.             if(x>datalen){
  271.                 return stroob;}
  272.             return data[x];}
  273.         string& operator=(const char c){
  274.             if(2>datasize){
  275.                 resizedata(minbuffsize+1-datasize);}
  276.             data[0]=c;
  277.             data[1]=0;
  278.             datalen=1;
  279.             return *this;}
  280.         string& operator=(const char* str){
  281.             unsigned s=strlen(str);
  282.             if(s+1>datasize){
  283.                 resizedata(s<<1-datasize);}
  284.             memcpy(data,str,s+1);
  285.             datalen=s;
  286.             return *this;}
  287.         string& operator=(const string& str){
  288.             datalen=str.datalen;
  289.             if(datalen+1>datasize){
  290.                 resizedata(datalen<<1-datasize);}
  291.             memcpy(data,str.data,datalen+1);
  292.             return *this;}
  293.         string& operator+=(const char c){
  294.             if(datalen+2>datasize){
  295.                 resizedata(minbuffsize);}
  296.             data[datalen]=c;
  297.             data[datalen+1]=0;
  298.             ++datalen;
  299.             return *this;}
  300.         string& operator+=(const char* str){
  301.             unsigned s=strlen(str);
  302.             if(datalen+s+1>datasize){
  303.                 resizedata(s<<1);}
  304.             memcpy(data+datalen,str,s+1);
  305.             datalen+=s;
  306.             return *this;}
  307.         string& operator+=(const string& str){
  308.             if(datalen+str.datalen+1>datasize){
  309.                 resizedata(str.datalen<<2);}
  310.             memcpy(data+datalen,str.data,str.datalen+1);
  311.             datalen+=str.datalen;
  312.             return *this;}
  313.         void clear(){
  314.             data[0]=0;}
  315.         unsigned insert(const char c,unsigned pos){
  316.             if(datalen+2>datasize){
  317.                 resizedata(minbuffsize);}
  318.             memmove(data+pos+1,data+pos,datalen-pos);
  319.             data[pos]=c;
  320.             ++datalen;
  321.             return datalen;}
  322.         unsigned insert(const char* str,unsigned pos){
  323.             unsigned s=strlen(str);
  324.             if(datalen+s+1>datasize){
  325.                 resizedata(s<<2);}
  326.             memmove(data+pos+s,data+pos,datalen-pos);
  327.             memcpy(data+pos,str,s);
  328.             datalen+=s;
  329.             return datalen;}
  330.         unsigned insert(const string& str,unsigned pos){
  331.             if(datalen+str.datalen+1>datasize){
  332.                 resizedata(str.datalen<<1);}
  333.             memmove(data+pos+str.datalen,data+pos,datalen-pos);
  334.             memcpy(data+pos,str.data,str.datalen);
  335.             datalen+=str.datalen;
  336.             return datalen;}
  337.         //count
  338.         unsigned count(const char c) const{
  339.             return countstr(&c,1);}
  340.         unsigned count(const char* str) const{
  341.             return countstr(str,strlen(str));}
  342.         unsigned count(const string& str) const{
  343.             return countstr(str.data,str.datalen);}
  344.         //replace
  345.         unsigned replace(const char* str1,const char* str2,unsigned num=maxint){
  346.             return replacestr(str1,str2,strlen(str1),strlen(str2),num);}
  347.         unsigned replace(const string& str1,const char* str2,unsigned num=maxint){
  348.             return replacestr(str1.data,str2,str1.datalen,strlen(str2),num);}
  349.         unsigned replace(const char* str1,const string& str2,unsigned num=maxint){
  350.             return replacestr(str1,str2.data,strlen(str1),str2.datalen,num);}
  351.         unsigned replace(const string& str1,const string& str2,unsigned num=maxint){
  352.             return replacestr(str1.data,str2.data,str1.datalen,str2.datalen,num);}
  353.         //erase
  354.         unsigned erase(unsigned start,unsigned end){
  355.             memmove(data+start,data+end,datalen-end+1);
  356.             datalen-=end-start;
  357.             return datalen;}
  358.         //find
  359.         unsigned find(const char c,unsigned n=0,unsigned start=0) const{
  360.             return findstr(&c,1,start,n);}
  361.         unsigned find(const char* str,unsigned n=0,unsigned start=0) const{
  362.             return findstr(str,strlen(str),start,n);}
  363.         unsigned find(const string& str,unsigned n=0,unsigned start=0) const{
  364.             return findstr(str.data,str.datalen,start,n);}
  365.         //rfind (reverse find)
  366.         unsigned rfind(const char c,unsigned n=0,unsigned start=maxint) const{
  367.             if(start==maxint){
  368.                 start=datalen;}
  369.             return rfindstr(&c,1,start,n);}
  370.         unsigned rfind(const char* str,unsigned n=0,unsigned start=maxint) const{
  371.             if(start==maxint){
  372.                 start=datalen;}
  373.             return rfindstr(str,strlen(str),start,n);}
  374.         unsigned rfind(const string& str,unsigned n=0,unsigned start=maxint) const{
  375.             if(start==maxint){
  376.                 start=datalen;}
  377.             return rfindstr(str,strlen(str),start,n);}
  378.         //reserve
  379.         int reserve(const int newsize){
  380.             int size=(newsize>datasize?newsize:datasize);
  381.             char* tmp=new char[size];
  382.             memcpy(tmp,data,datalen);
  383.             delete[] data;
  384.             data=tmp;
  385.             datasize=size;
  386.             return size;}
  387.         void upper(){
  388.             unsigned pos=0;
  389.             while(pos<=datalen){
  390.                 if(data[pos]>='a' && data[pos]<='z'){
  391.                     data[pos]-=aA_diff;}
  392.                 ++pos;}}
  393.         void lower(){
  394.             unsigned pos=0;
  395.             while(pos<=datalen){
  396.                 if(data[pos]>='A' && data[pos]<='Z'){
  397.                     data[pos]+=aA_diff;}
  398.                 ++pos;}}
  399.         unsigned length() const{
  400.             return datalen;}
  401.         unsigned capacity() const{
  402.             return datasize;}
  403.         void reverse(){
  404.             char* start=data;
  405.             char* end=data+datalen;
  406.             char buffer;
  407.             while(end>start){
  408.                 buffer=*start;
  409.                 *start=*end;
  410.                 *end=buffer;
  411.                 ++start;
  412.                 --end;}}
  413.         //compare
  414.         int compare(const char* str) const{
  415.             return comparestr(str,0,datalen,0,strlen(str));}
  416.         int compare(const string& str) const{
  417.             return comparestr(str.data,0,datalen,0,str.datalen);}
  418.         int compare(unsigned pos1,unsigned n1,const string& str) const{
  419.             return comparestr(str.data,pos1,pos1+n1,0,str.datalen);}
  420.         int compare(unsigned pos1,unsigned n1,const char* s) const{
  421.             return comparestr(s,pos1,pos1+n1,0,strlen(s));}
  422.         int compare(unsigned pos1,unsigned n1,const string& str,unsigned pos2,unsigned n2) const{
  423.             return comparestr(str.data,pos1,pos1+n1,pos2,pos2+n2);}
  424.         int compare(unsigned pos1,unsigned n1,const char* s,unsigned n2) const{
  425.             return comparestr(str.data,pos1,pos1+n1,0,n2);}
  426.         //iterator functions
  427.         struct iterator{
  428.             private:
  429.                 string* str;
  430.                 unsigned x;
  431.                 friend struct string;
  432.             public:
  433.                 iterator& operator++(){
  434.                     ++x;
  435.                     if(x>=str.datalen){
  436.                         x=str.datalen-1;}
  437.                     return *this;}
  438.                 iterator& operator++(int){
  439.                     ++x;
  440.                     if(x>=str.datalen){
  441.                         x=str.datalen-1;}
  442.                     return *this;}
  443.                 iterator& operator--(){
  444.                     --x;
  445.                     if(x==string::maxint){
  446.                         x=0;}
  447.                     return *this;}
  448.                 iterator& operator--(int){
  449.                     --x;
  450.                     if(x==string::maxint){
  451.                         x=0;}
  452.                     return *this;}
  453.                 iterator& operator+=(unsigned n){
  454.                     x+=n;
  455.                     if(x>=str.datalen){
  456.                         x=str.datalen-1;}
  457.                     return *this;}
  458.                 iterator& operator-=(unsigned n){
  459.                     x-=n;
  460.                     if(x==string::maxint){
  461.                         x=0;}
  462.                     return *this;}
  463.                 char& operator*(){
  464.                     return str.data[x];}};
  465.             struct const_iterator{
  466.                 private:
  467.                     string* str;
  468.                     unsigned x;
  469.                     friend struct string;
  470.                 public:
  471.                     iterator& operator++(){
  472.                         ++x;
  473.                         if(x>=str.datalen){
  474.                             x=str.datalen-1;}
  475.                         return *this;}
  476.                     iterator& operator++(int){
  477.                         ++x;
  478.                         if(x>=str.datalen){
  479.                             x=str.datalen-1;}
  480.                         return *this;}
  481.                     iterator& operator--(){
  482.                         --x;
  483.                         if(x==maxint){
  484.                             x=0;}
  485.                         return *this;}
  486.                     iterator& operator--(int){
  487.                         --x;
  488.                         if(x==maxint){
  489.                             x=0;}
  490.                         return *this;}
  491.                     iterator& operator+=(unsigned n){
  492.                         x+=n;
  493.                         if(x>=str.datalen){
  494.                             x=str.datalen-1;}
  495.                         return *this;}
  496.                     iterator& operator-=(unsigned n){
  497.                         x-=n;
  498.                         if(x==string::maxint){
  499.                             x=0;}
  500.                         return *this;}
  501.                     const char& operator*(){
  502.                         return str.data[x];}};
  503.         iterator begin(){
  504.             iterator ret;
  505.             ret.str=this;
  506.             ret.x=0;
  507.             return ret;}
  508.         const_iterator begin() const{
  509.             const_iterator ret;
  510.             ret.str=this;
  511.             ret.x=0;
  512.             return ret;}
  513.         iterator end(){
  514.             iterator ret;
  515.             ret.str=this;
  516.             ret.x=datalen-1;
  517.             return ret;}
  518.         const_iterator end() const{
  519.             const_iterator ret;
  520.             ret.str=this;
  521.             ret.x=datalen-1;
  522.             return ret;}};
  523.  
  524. //I couldn't use the ones defined in the string class, so I made an obscurely named namespace to hide them
  525. //(this isn't bad practice, is it?)
  526. namespace strexternalfuncs{
  527.     bool isequal(const char* str1,const char* str2,unsigned len1,unsigned len2){
  528.         if(len1!=len2){
  529.             return false;}
  530.         unsigned pos=0;
  531.         while(pos<len1){
  532.             if(str1[pos]!=str2[pos]){
  533.                 return false;}
  534.             ++pos;}
  535.         return true;}
  536.     unsigned strlen(const char* str){
  537.         unsigned x;
  538.         while(str[x]!=0){
  539.             ++x;}
  540.         return x;}};
  541.  
  542. //global operators
  543. //equal
  544. bool operator==(const string& str1,const string& str2){
  545.     return (str1.compare(str2.data,0,str1.datalen,0,str2.datalen)==0);}
  546.  
  547. bool operator==(const string& str1,const char* str2){
  548.     return (str1.compare(str2,0,str1.datalen,0,str1.strlen(str2))==0);}
  549.  
  550. bool operator==(const char* str1,const string& str2){
  551.     return (str2.compare(str1,0,str2.datalen,0,str2.strlen(str1))==0);}
  552.  
  553. //not equal
  554. bool operator!=(const string& str1,const string& str2){
  555.     return (str1.compare(str2.data,0,str1.datalen,0,str2.datalen)!=0);}
  556.  
  557. bool operator!=(const string& str1,const char* str2){
  558.     return (str1.compare(str2,0,str1.datalen,0,str1.strlen(str2))!=0);}
  559.  
  560. bool operator!=(const char* str1,const string& str2){
  561.     return (str2.compare(str1,0,str2.datalen,0,str2.strlen(str1))!=0);}
  562.  
  563. //greater than
  564. bool operator>(const string& str1,const string& str2){
  565.     return (str1.compare(str2.data,0,str1.datalen,0,str2.datalen)>0);}
  566.  
  567. bool operator>(const string& str1,const char* str2){
  568.     return (str1.compare(str2,0,str1.datalen,0,str1.strlen(str2))>0);}
  569.  
  570. bool operator>(const char* str1,const string& str2){
  571.     return (str2.compare(str1,0,str2.datalen,0,str2.strlen(str1))>0);}
  572.  
  573. //less than
  574. bool operator<(const string& str1,const string& str2){
  575.     return (str1.compare(str2.data,0,str1.datalen,0,str2.datalen)<0);}
  576.  
  577. bool operator<(const string& str1,const char* str2){
  578.     return (str1.compare(str2,0,str1.datalen,0,str1.strlen(str2))<0);}
  579.  
  580. bool operator<(const char* str1,const string& str2){
  581.     return (str2.compare(str1,0,str2.datalen,0,str2.strlen(str1))<0);}
  582.  
  583. //greater than or equal to
  584. bool operator>=(const string& str1,const string& str2){
  585.     return (str1.compare(str2.data,0,str1.datalen,0,str2.datalen)>=0);}
  586.  
  587. bool operator>=(const string& str1,const char* str2){
  588.     return (str1.compare(str2,0,str1.datalen,0,str1.strlen(str2))>=0);}
  589.  
  590. bool operator>=(const char* str1,const string& str2){
  591.     return (str2.compare(str1,0,str2.datalen,0,str2.strlen(str1))>=0);}
  592.  
  593. //less than or equal to
  594. bool operator<=(const string& str1,const string& str2){
  595.     return (str1.compare(str2.data,0,str1.datalen,0,str2.datalen)<=0);}
  596.  
  597. bool operator<=(const string& str1,const char* str2){
  598.     return (str1.compare(str2,0,str1.datalen,0,str1.strlen(str2))<=0);}
  599.  
  600. bool operator<=(const char* str1,const string& str2){
  601.     return (str2.compare(str1,0,str2.datalen,0,str2.strlen(str1))<=0);}
  602.  
  603. #endif
Advertisement
Add Comment
Please, Sign In to add comment