Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "string.hpp"
- // Maximum # of chars in the string
- int String::capacity() const {
- return STRING_SIZE - 1;
- }
- // ENSURE THAT str == ""
- String::String() {
- str[0] = 0;
- }
- // String('x')
- String::String(const char ch) {
- str[0] = ch;
- str[1] = 0;
- }
- String::String(const char* cstr) {
- int i = 0;
- while (cstr[i] != 0) {
- str[i] = cstr[i];
- ++i;
- if (i >= capacity()) break;
- }
- str[i] = 0;
- }
- // ENSURE: Return value == i where str[i] = 0
- // # of chars in string
- int String::length() const {
- int i = 0;
- while (str[i] != 0)
- ++i;
- return i;
- }
- // REQUIRES: 0 <= i < length()
- // ENSURES: return value == str[i]
- char & String::operator[](int index) {
- assert(index >= 0);
- assert(index < length());
- return str[index];
- }
- //RELATIONAL OPS
- bool String::operator==(const String& rhs) const {
- int i = 0;
- while ( (str[i] != 0) && (str[i] == rhs.str[i]) ) {
- ++i;
- }
- return str[i] == rhs.str[i];
- }
- bool String::operator<(const String& rhs) const {
- int i = 0;
- while ( (str[i] != 0) && (rhs[i] != 0)
- && (str[i] == rhs.str[i]) ) {
- ++i;
- }
- return str[i] < rhs.str[i];
- }
- bool operator==(const char* lhs, const String& rhs) {
- return String(lhs) == rhs;
- }
- bool operator==(const char lhs, const String& rhs) {
- return String(lhs) == rhs;
- }
- bool operator<(const char* lhs, const String& rhs) {
- return String(lhs) < rhs;
- }
- bool operator<(const char lhs, const String& rhs) {
- return String(lhs) < rhs;
- }
- bool operator<=(const String& lhs, const String& rhs) {
- return !(rhs < lhs);
- }
- bool operator!=(const String& lhs, const String& rhs) {
- return !(lhs == rhs);
- }
- bool operator>=(const String& lhs, const String& rhs) {
- return !(lhs < rhs);
- }
- bool operator>(const String& lhs, const String& rhs){
- return (rhs < lhs);
- }
- //Concatenation
- //ENSURES: RETVAL == str + rhs.str
- String String::operator+(const String& rhs) const {
- int offset = length();
- String result(str);
- int i = 0;
- while (rhs.str[i] != 0) {
- if ( (offset + i) >= capacity() ) break;
- result.str[offset + i] = rhs.str[i];
- ++i;
- }
- result.str[offset + 1] = 0;
- return result;
- }
- String& String::operator+=(const String& rhs) {
- int start = length();
- int i = 0;
- while(rhs.str[i] != 0) {
- if ((start + i) >= capacity() ) break;
- str[start + i] = rhs.str[i];
- ++i;
- }
- str[start + i] = 0;
- return *this;
- }
- //REQUIRES: 0 <= start <= length
- //ENSURES: RETVAL == str[start .. end]
- String String::substr(int start, int end) const {
- if (start < 0) start = 0;
- if (start > end) return String();
- if (end >= length()) end = length() - 1;
- if (end < 0) return String();
- String result;
- int i;
- for (i = start; i <= end; ++i) {
- result.str[i - start] = str[i];
- }
- result.str[i - start] = 0; // 日本語を書きます
- return result;
- }
- //REQUIRES: 0 <= pos < length
- //ENSURES: Returns i where s[i] == ch, i = pos, pos+1, pos+2 ...
- // Returns -1 if ch not in String.
- int String::findChar(char ch, int pos) const {
- if (pos < 0) pos = 0;
- if (pos > length() - 1) return - 1;
- int i = pos;
- while (str[i] != 0) {
- if (ch == str[i]) return i;
- ++i;
- }
- return -1;
- }
- //REQUIRES: 0 <= pos < length
- //ENSURES: Returns i where str[i,..,j] == s,
- // Returns -1 is ch not in String.
- int String::findSubstr(const String& s, const int pos) const {
- if (pos < 0) pos = 0;
- if (pos > length() - s.length()) return -1;
- int len = s.length();
- int i = pos;
- while (str[i] != 0) {
- if (substr(i, i + len - 1) == s) return i;
- ++i;
- }
- return -1;
- }
- std::ostream& operator<<(std::ostream& out, const String& rhs) {
- out << rhs.str;
- return out;
- }
- std::istream& operator>>(std::istream& in, String& rhs) {
- char temp[STRING_SIZE];
- in >> temp;
- rhs = String(temp);
- return in;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement