-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.h
executable file
·114 lines (93 loc) · 2.92 KB
/
String.h
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
#ifndef _String_h
#define _String_h
#include <stdint.h>
#include <stdlib.h>
struct String {
private:
char* ptr;
uint32_t len;
public:
String(const char* src) { construct(src); }
String(void) { construct(""); }
String(const String& that) { copy(that); }
~String(void) { destroy(); }
String& operator=(const String& that) {
if (this != &that) {
destroy();
copy(that);
}
return *this;
}
String& operator=(const char* src) {
destroy();
construct(src);
return *this;
}
/*
* clients can gain access to the C-compatible string (i.e., the char*) by
* invoking myString.c_str()
* This is helpful, for example, if you want to print a string with printf
*
* printf("Customer with name %s has purchased %d bottles\n",
* myCustomer.name.c_str(), myCustomer.bottles);
*/
const char* c_str(void) const { return ptr; }
uint32_t size(void) const { return len; }
bool operator==(String s2) const {
const String& s1 = *this;
if (s1.size() != s2.size()) { return false; }
int k = 0;
while (k < s1.size()) {
if (s1.ptr[k] != s2.ptr[k]) { return false; }
k += 1;
}
return true;
}
bool operator<(String s2) const {
const String& s1 = *this;
int k = 0;
while (s1.ptr[k] == s2.ptr[k]) {
if (s1.ptr[k] == 0) { return false; } // strings are equal
k += 1;
}
return s1.ptr[k] < s2.ptr[k];
}
bool operator!=(String s2) const { const String& s1 = *this; return !(s1 == s2); }
bool operator>(String s2) const { const String& s1 = *this; return s2 < s1; }
bool operator<=(String s2) const { const String& s1 = *this; return ! (s2 < s1); }
bool operator>=(String s2) const { const String& s1 = *this; return ! (s1 < s2); }
private:
/* for internal use only, clients really shouldn't be calling these functions
* in future versions of this String object type, I might remove these functions entirely or
* dramatically change their behavior. */
/* WARNING: DO NOT CALL ANY FUNCTION BELOW THIS LINE */
void construct(const char* src) {
len = 0;
while (src[len] != 0) {
len += 1;
}
ptr = (char*) malloc(len + 1);
copy(src);
}
/* copy(char*) assumes that ptr has been allocated and holds sufficient capacity for src */
void copy(const char* src) {
int k = len;
while (k >= 0) {
ptr[k] = src[k];
k -= 1;
}
}
/* copy(String&) is my "real" copy constructor. I always name this function "copy" and its purpose
* is to construct a "deep copy" (i.e., an independent object that shares no pointers with the original object) */
void copy(const String& that) {
len = that.len;
ptr = (char*) malloc(len + 1);
copy(that.ptr);
}
/* destroy(void) is my "real" destructor. I always name this function destroy and my official destructor is always
* just a simple one-line call to this function */
void destroy(void) {
free(ptr);
}
};
#endif /* _String_h */