summaryrefslogtreecommitdiffstats
path: root/string.cpp
blob: e75b50e0052be9c12039f518b5bb25ab4500a4ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* string.cpp
 *
 * Note: http://msdn.microsoft.com/en-us/magazine/cc163742.aspx
 *
 */

#include "string.h"
#include <sstream>

MyString::MyString(int len) : str_(new char[len + 1]), length_(len)
{
    this->count++;
}

MyString::MyString(const char *str) : str_(new char[strlen(str)+1]), length_(strlen(str))
{
    strcpy(str_, str);
    this->count++;
}

MyString::MyString(const MyString& str) : str_(new char[str.length_ + 1]),
                                          length_(str.length_)
{
    strcpy(str_, str.str_);
    this->count++;
}

MyString& MyString::operator=(const MyString& rhs)
{
    /* don't do anything if object assigns itself */
    if (this == &rhs)
        return *this;

    length_ = rhs.length_;
    delete[] str_;
    str_ = new char[ length_ + 1];
    strcpy(str_, rhs.str_);

    /* assignment operator requires this */
    return *this;
}

MyString::~MyString()
{
    delete[] str_;
    this->count--;
}

int MyString::indexOf(char ch, int pos) const
{
    while (pos < length_)
    {
        if (str_[pos] == ch)
            return pos;

        pos++;
    }

    return -1;
}

/* define this field only once, .h is not the best place */
size_t MyString::count = 0;

bool MyString::isSubstring(const MyString& str) const
{
    if (length_ < str.length_)
        return false;

    int i, j, temp;

    for (i = 0; i < length_; i++)
    {
        j = 0;
        temp = i;
        while ((j < str.length_) && (str_[temp] == str.str_[j]))
        {
            temp++;
            j++;
        }

        if (j == str.length_)
        return true;
    }

    return false;
}

/* might seem redundant since constructor can create an object from char array
 * and call other isSubstring method that takes in MyString type
 */
bool MyString::isSubstring(const char *str) const
{
    int i, j, k;
    for (i = 0; i < length_; i++)
    {
        j = i;
        k = 0;
        while (str_[j++] == str[k++])
            ;

        if (str[k-1] == '\0')
            return true;
    }

    return false;
}

/* there are few ways to implement this */
MyString& MyString::concat(const MyString& str)
{
    /* right way to do this would be to use realloc and double the buffer each
     * we append something (if it's full)
     */
    MyString ret(length_ + str.length_);
    strcpy(ret.str_, str_);
    strcat(ret.str_, str.str_);

    return (*this = ret);
}

MyString& MyString::concat(const char *str)
{
    char *newstr = new char[length_ + strlen(str) + 1];

    strcpy(newstr, str_);
    strcpy(newstr + length_, str);
    delete[] str_;

    length_ += strlen(str);
    str_ = newstr;

    return *this;
}

bool operator==(const MyString& lhs, const MyString& rhs)
{
    if (lhs.length_ != rhs.length_)
        return false;

    if (strcmp(lhs.str_, rhs.str_) == 0)
        return true;
    else
        return false;
}

bool operator!=(const MyString& lhs, const MyString& rhs)
{
    return !(lhs == rhs);
}

/* right now I think the reference is used to avoid returning copy of ostream
 * so when calls are chained up we work on one ostream object only
 *
 * this function is in global scope, it is friend'ed to allow us access to data
 * members of calss MyString
 */
std::ostream& operator<<(std::ostream& ostr, const MyString& rhs)
{
    return (ostr << rhs.str_);
}

/* unary operator operate on one operand, itself! */
bool MyString::operator!(void) const
{
    return (this->length_ == 0);
}

char& MyString::operator[](int i)
{
    /* references need to point to valid objects, all hell could breaks loose */
    return this->str_[i];
}

char MyString::operator[](int i) const
{
    if (i < 0 || i > this->length_ - 1)
        return 0;
    else
        return this->str_[i];
}

MyString::operator char*()
{
    char *ret = new char[this->length_ + 1];
    if (ret)
    {
        strcpy(ret, this->str_);
        return ret;
    }
    else
        return NULL;
}

MyString::operator const char*() const
{
    return this->str_;
}