-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.cpp
96 lines (74 loc) · 1.7 KB
/
String.cpp
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
#include "String.h"
#include <string.h>
#include "utils.h"
namespace ylib {
String::String() : m_data(nullptr)
{
}
String::String(const String& s) : m_data(nullptr) {
*this = s;
}
String::String(const char* c) : m_data(nullptr) {
*this = c;
}
String::~String()
{
this->clear();
}
void String::clear() {
SAFE_DELETE_ARRAY(m_data);
}
bool String::finishWith(const char* value) const {
size_t len = length();
size_t vLen = strlen(value);
if (len < vLen) {
return false;
}
const char* end = &m_data[len - vLen];
return strcmp(end, value);
}
size_t String::length() const {
if (m_data != nullptr) {
return strlen(m_data);
}
return 0;
}
String& String::operator=(String const& s) {
return *this = s.toCharArray();
}
String& String::operator=(const char* c) {
clear();
size_t len;
if (c != nullptr && (len = strlen(c)) != 0) {
m_data = new char[len + 1];
strcpy(m_data, c);
}
return *this;
}
String& String::operator+=(String const& s) {
return operator+=(s.toCharArray());
}
String& String::operator+=(const char* c) {
size_t cLen;
if (c == nullptr || (cLen = strlen(c)) == 0) {
return *this;
}
size_t currentLen = length();
size_t size = currentLen + cLen + 1;
char* data = new char[size];
strncpy(data, m_data, currentLen);
strcpy(&data[currentLen], c);
clear();
m_data = data;
return *this;
}
bool operator==(String const& a, String const& b) {
return strcmp(a.toCharArray(), b.toCharArray()) == 0;
}
String operator+(String const& a, String const& b) {
String result;
result += a;
result += b;
return result;
}
}