| 149 | |
| 150 | |
| 151 | class String { |
| 152 | private: |
| 153 | char* _str; |
| 154 | |
| 155 | public: |
| 156 | String(const char* str = "") { |
| 157 | _str = strdup(str); |
| 158 | } |
| 159 | |
| 160 | String(const String& other) { |
| 161 | _str = strdup(other._str); |
| 162 | } |
| 163 | |
| 164 | ~String() { |
| 165 | free(_str); |
| 166 | } |
| 167 | |
| 168 | String& operator=(const String& other) { |
| 169 | if (this == &other) return *this; |
| 170 | |
| 171 | free(_str); |
| 172 | _str = strdup(other._str); |
| 173 | return *this; |
| 174 | } |
| 175 | |
| 176 | const char* str() const { |
| 177 | return _str; |
| 178 | } |
| 179 | |
| 180 | bool operator==(const char* other) const { |
| 181 | return strcmp(_str, other) == 0; |
| 182 | } |
| 183 | |
| 184 | bool operator==(const String& other) const { |
| 185 | return strcmp(_str, other._str) == 0; |
| 186 | } |
| 187 | |
| 188 | String& operator<<(const char* tail) { |
| 189 | size_t len = strlen(_str); |
| 190 | _str = (char*)realloc(_str, len + strlen(tail) + 1); |
| 191 | strcpy(_str + len, tail); |
| 192 | return *this; |
| 193 | } |
| 194 | |
| 195 | String& operator<<(String& tail) { |
| 196 | return operator<<(tail._str); |
| 197 | } |
| 198 | |
| 199 | String& operator<<(int n) { |
| 200 | char buf[16]; |
| 201 | snprintf(buf, sizeof(buf), "%d", n); |
| 202 | return operator<<(buf); |
| 203 | } |
| 204 | |
| 205 | String& replace(char c, const char* rep) { |
| 206 | const char* start = _str; |
| 207 | const char* p; |
| 208 | while ((p = strchr(start, c)) != NULL) { |
no test coverage detected