+/EE312

software design

git clone https://git.julia.blue/+/EE312

Joseph RyanProject 76241299

master
3.6 KiB146 linesraw
1#ifndef _String_h
2#define _String_h
3#include <stdlib.h>
4#include <stdint.h>
5// #include "MemHeap.h"
6
7struct String {
8private:
9    char* ptr;
10    uint32_t len;
11
12public:
13    String(const char* src) {
14        construct(src);
15    }
16
17    String(void) {
18        construct("");
19    }
20
21    String(const String &that) {
22        copy(that);
23    }
24    ~String(void) {
25        destroy();
26    }
27    String &operator=(const String &that) {
28        if (this != &that) {
29            destroy();
30            copy(that);
31        }
32        return *this;
33    }
34
35    String &operator=(const char* src) {
36        destroy();
37        construct(src);
38        return *this;
39    }
40
41
42    /*
43     * clients can gain access to the C-compatible string (i.e., the char*) by
44     * invoking myString.c_str()
45     * This is helpful, for example, if you want to print a string with printf
46     *
47     *   printf("Customer with name %s  has purchased %d bottles\n",
48     *			    myCustomer.name.c_str(), myCustomer.bottles);
49     */
50    const char* c_str(void) const {
51        return ptr;
52    }
53
54    uint32_t size(void) const {
55        return len;
56    }
57
58    bool operator==(String s2) const {
59        const String &s1 = *this;
60        if (s1.size() != s2.size()) {
61            return false;
62        }
63        int k = 0;
64        while (k < s1.size()) {
65            if (s1.ptr[k] != s2.ptr[k]) {
66                return false;
67            }
68            k += 1;
69        }
70        return true;
71    }
72
73    bool operator<(String s2) const {
74        const String &s1 = *this;
75        int k = 0;
76        while (s1.ptr[k] == s2.ptr[k]) {
77            if (s1.ptr[k] == 0) {
78                return false;    // strings are equal
79            }
80            k += 1;
81        }
82        return s1.ptr[k] < s2.ptr[k];
83    }
84
85
86    bool operator!=(String s2) const {
87        const String &s1 = *this;
88        return !(s1 == s2);
89    }
90    bool operator>(String s2) const {
91        const String &s1 = *this;
92        return s2 < s1;
93    }
94    bool operator<=(String s2) const {
95        const String &s1 = *this;
96        return ! (s2 < s1);
97    }
98    bool operator>=(String s2) const {
99        const String &s1 = *this;
100        return ! (s1 < s2);
101    }
102
103private:
104    /* for internal use only, clients really shouldn't be calling these functions
105     * in future versions of this String object type, I might remove these functions entirely or
106     * dramatically change their behavior. */
107
108    /* WARNING: DO NOT CALL ANY FUNCTION BELOW THIS LINE */
109
110    void construct(const char* src) {
111        len = 0;
112        while (src[len] != 0) {
113            len += 1;
114        }
115        ptr = (char*) malloc(len + 1);
116        copy(src);
117    }
118
119    /* copy(char*) assumes that ptr has been allocated and holds sufficient capacity for src */
120    void copy(const char* src) {
121        int k = len;
122        while (k >= 0) {
123            ptr[k] = src[k];
124            k -= 1;
125        }
126    }
127
128    /* copy(String&) is my "real" copy constructor. I always name this function "copy" and its purpose
129     * is to construct a "deep copy" (i.e., an independent object that shares no pointers with the original object) */
130    void copy(const String &that) {
131        len = that.len;
132        ptr = (char*) malloc(len + 1);
133        copy(that.ptr);
134    }
135
136    /* destroy(void) is my "real" destructor. I always name this function destroy and my official destructor is always
137     * just a simple one-line call to this function */
138    void destroy(void) {
139        free(ptr);
140    }
141};
142
143
144
145
146#endif /* _String_h */