-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcharacter.cc
104 lines (87 loc) · 2.35 KB
/
character.cc
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
//
// Created by wlanjie on 2018/2/7.
//
#include "character.h"
#include "utils.h"
namespace mp4 {
// TODO name
char String::EmptyString = 0;
String::String() : m_Chars(&EmptyString), m_Length(0) {}
String::String(const char* s) {
if (s == NULL) {
m_Chars = &EmptyString;
m_Length = 0;
return;
}
m_Length = (Size)StringLength(s);
m_Chars = new char[m_Length+1];
CopyMemory(m_Chars, s, m_Length+1);
}
String::String(const char* s, Size size) :
m_Chars(new char[size+1]),
m_Length(size)
{
m_Chars[size] = 0;
CopyMemory(m_Chars, s, size);
}
String::String(const String& s) {
m_Length = s.m_Length;
m_Chars = new char[m_Length+1];
CopyMemory(m_Chars, s.m_Chars, m_Length+1);
}
String::String(Size size) {
m_Length = size;
m_Chars = new char[m_Length+1];
for (unsigned int i=0; i<size+1; i++) m_Chars[i] = 0;
}
String::~String() {
if (m_Chars != &EmptyString) delete[] m_Chars;
}
const String& String::operator=(const String& s) {
if (&s == this) return s;
if (m_Chars != &EmptyString) delete[] m_Chars;
m_Length = s.m_Length;
m_Chars = new char[m_Length+1];
CopyMemory(m_Chars, s.m_Chars, m_Length+1);
return *this;
}
const String& String::operator=(const char* s) {
if (s == NULL) {
if (m_Chars != &EmptyString) delete[] m_Chars;
m_Chars = &EmptyString;
m_Length = 0;
} else {
assign(s, (Size) StringLength(s));
}
return *this;
}
bool String::operator==(const String& s) const {
if (m_Length != s.m_Length) return false;
for (unsigned int i=0; i<m_Length; i++) {
if (m_Chars[i] != s.m_Chars[i]) return false;
}
return true;
}
bool String::operator==(const char* s) const {
Size s_length = (Size)StringLength(s);
if (m_Length != s_length) return false;
for (unsigned int i=0; i<s_length; i++) {
if (m_Chars[i] != s[i]) return false;
}
return true;
}
void String::assign(const char *s, Size size) {
if (m_Chars != &EmptyString) delete[] m_Chars;
m_Length = size;
m_Chars = new char[m_Length+1];
CopyMemory(m_Chars, s, m_Length);
m_Chars[size] = '\0';
}
int String::find(char c, unsigned int start) const {
const char* chars = getChars();
for (unsigned int i=start; i<m_Length; i++) {
if (chars[i] == c) return i;
}
return -1;
}
}