Advertisement
MolSno

string.cpp

Oct 4th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include "string.hpp"
  2.  
  3. // Maximum # of chars in the string
  4. int String::capacity() const {
  5.   return STRING_SIZE - 1;
  6. }
  7.  
  8. // ENSURE THAT str == ""
  9. String::String() {
  10.   str[0] = 0;
  11. }
  12.  
  13. // String('x')
  14. String::String(const char ch) {
  15.   str[0] = ch;
  16.   str[1] = 0;
  17. }
  18.  
  19. String::String(const char* cstr) {
  20.   int i = 0;
  21.   while (cstr[i] != 0) {
  22.     str[i] = cstr[i];
  23.     ++i;
  24.     if (i >= capacity()) break;
  25.   }
  26.   str[i] = 0;
  27. }
  28.  
  29. // ENSURE: Return value == i where str[i] = 0
  30. // # of chars in string
  31. int String::length() const {
  32.   int i = 0;
  33.   while (str[i] != 0)
  34.     ++i;
  35.   return i;
  36. }
  37.  
  38. // REQUIRES: 0 <= i < length()
  39. // ENSURES: return value == str[i]
  40. char & String::operator[](int index) {
  41.   assert(index >= 0);
  42.   assert(index < length());
  43.   return str[index];
  44. }
  45.  
  46. //RELATIONAL OPS
  47. bool String::operator==(const String& rhs) const {
  48.   int i = 0;
  49.   while ( (str[i] != 0) && (str[i] == rhs.str[i]) ) {
  50.     ++i;
  51.   }
  52.   return str[i] == rhs.str[i];
  53. }
  54.  
  55. bool String::operator<(const String& rhs) const {
  56.   int i = 0;
  57.   while ( (str[i] != 0) && (rhs[i] != 0)
  58.           && (str[i] == rhs.str[i]) ) {
  59.     ++i;
  60.   }
  61.   return str[i] < rhs.str[i];
  62. }
  63.  
  64. bool operator==(const char* lhs, const String& rhs) {
  65.   return String(lhs) == rhs;
  66. }
  67. bool operator==(const char lhs, const String& rhs) {
  68.   return String(lhs) == rhs;
  69. }
  70. bool operator<(const char* lhs, const String& rhs) {
  71.   return String(lhs) < rhs;
  72. }
  73. bool operator<(const char lhs, const String& rhs) {
  74.   return String(lhs) < rhs;
  75. }
  76.  
  77. bool operator<=(const String& lhs, const String& rhs) {
  78.   return !(rhs < lhs);
  79. }
  80. bool operator!=(const String& lhs, const String& rhs) {
  81.   return !(lhs == rhs);
  82. }
  83. bool operator>=(const String& lhs, const String& rhs) {
  84.   return !(lhs < rhs);
  85. }
  86. bool operator>(const String& lhs, const String& rhs){
  87.   return (rhs < lhs);
  88. }
  89.  
  90. //Concatenation
  91. //ENSURES: RETVAL == str + rhs.str
  92. String String::operator+(const String& rhs) const {
  93.   int offset = length();
  94.   String result(str);
  95.   int i = 0;
  96.   while (rhs.str[i] != 0) {
  97.     if ( (offset + i) >= capacity() ) break;
  98.     result.str[offset + i] = rhs.str[i];
  99.     ++i;
  100.   }
  101.   result.str[offset + 1] = 0;
  102.   return result;
  103. }
  104.  
  105. String& String::operator+=(const String& rhs) {
  106.   int start = length();
  107.   int i = 0;
  108.   while(rhs.str[i] != 0) {
  109.     if ((start + i) >= capacity() ) break;
  110.     str[start + i] = rhs.str[i];
  111.     ++i;
  112.   }
  113.   str[start + i] = 0;
  114.   return *this;
  115. }
  116.  
  117. //REQUIRES: 0 <= start <= length
  118. //ENSURES: RETVAL == str[start .. end]
  119. String String::substr(int start, int end) const {
  120.   if (start < 0) start = 0;
  121.   if (start > end) return String();
  122.   if (end >= length()) end = length() - 1;
  123.   if (end < 0) return String();
  124.  
  125.   String result;
  126.   int i;
  127.   for (i = start; i <= end; ++i) {
  128.     result.str[i - start] = str[i];
  129.   }
  130.   result.str[i - start] = 0; // 日本語を書きます
  131.   return result;
  132. }
  133.  
  134. //REQUIRES: 0 <= pos < length
  135. //ENSURES: Returns i where s[i] == ch, i = pos, pos+1, pos+2 ...
  136. //         Returns -1 if ch not in String.
  137. int String::findChar(char ch, int pos) const {
  138.   if (pos < 0) pos = 0;
  139.   if (pos > length() - 1) return - 1;
  140.  
  141.   int i = pos;
  142.   while (str[i] != 0) {
  143.     if (ch == str[i]) return i;
  144.     ++i;
  145.   }
  146.   return -1;
  147. }
  148.  
  149. //REQUIRES: 0 <= pos < length
  150. //ENSURES: Returns i where str[i,..,j] == s,
  151. //         Returns -1 is ch not in String.
  152. int String::findSubstr(const String& s, const int pos) const {
  153.   if (pos < 0) pos = 0;
  154.   if (pos > length() - s.length()) return -1;
  155.  
  156.   int len = s.length();
  157.   int i = pos;
  158.   while (str[i] != 0) {
  159.     if (substr(i, i + len - 1) == s) return i;
  160.     ++i;
  161.   }
  162.   return -1;
  163. }
  164.  
  165. std::ostream& operator<<(std::ostream& out, const String& rhs) {
  166.   out << rhs.str;
  167.   return out;
  168. }
  169.  
  170. std::istream& operator>>(std::istream& in, String& rhs) {
  171.   char temp[STRING_SIZE];
  172.   in >> temp;
  173.   rhs = String(temp);
  174.   return in;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement