-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjstring.cpp
More file actions
114 lines (98 loc) · 2.01 KB
/
Copy pathjstring.cpp
File metadata and controls
114 lines (98 loc) · 2.01 KB
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
#define _CRT_SECURE_NO_WARNINGS
#include "jstring.hpp"
JString::JString()
{
m_str = new char[1];
m_str[0] = '\0';
m_length = 0;
}
JString::JString(const char* str)
{
m_length = strlen(str);
m_str = new char[m_length + 1];
strcpy(m_str, str);
m_str[m_length] = '\0';
}
JString::JString(const char* str, int start, int length)
{
m_length = length;
m_str = new char[m_length + 1];
strncpy(m_str, str + start, length);
m_str[m_length] = '\0';
}
JString::JString(const JString& other)
{
m_length = other.m_length;
m_str = new char[m_length + 1];
strcpy(m_str, other.m_str);
m_str[m_length] = '\0';
}
JString::~JString()
{
delete[] m_str;
}
JString& JString::operator=(const JString& other)
{
if (this != &other)
{
delete[] m_str;
m_length = other.m_length;
m_str = new char[m_length + 1];
strcpy(m_str, other.m_str);
m_str[m_length] = '\0';
}
return *this;
}
JString& JString::operator=(JString&& other)
{
if (this != &other)
{
delete[] m_str;
m_str = other.m_str;
m_length = other.m_length;
other.m_str = nullptr;
other.m_length = 0;
}
return *this;
}
JString& JString::operator=(const char* str)
{
delete[] m_str;
m_length = strlen(str);
m_str = new char[m_length + 1];
strcpy(m_str, str);
m_str[m_length] = '\0';
return *this;
}
bool JString::operator==(const JString& other) const
{
return strcmp(m_str, other.c_str()) == 0;
}
bool JString::operator==(const char* str) const
{
return strcmp(m_str, str) == 0;
}
bool JString::operator!=(const JString& other) const
{
return !(*this == other);
}
bool JString::operator!=(const char* str) const
{
return !(*this == str);
}
char& JString::operator[](int index)
{
return m_str[index];
}
const char& JString::operator[](int index) const
{
return m_str[index];
}
int JString::length() const
{
return m_length;
}
const char* JString::c_str() const
{
return m_str;
}