+/EE312

software design

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

Joseph RyanProject 40c27270

master
5.0 KiB177 linesraw
1// String.c
2#include <stdio.h>
3#include <stdlib.h>
4
5#include "MyString.h"
6
7/* private functions, internal to the development of the String abstract type */
8
9/* copy 'len' characters from 'src' to 'dst'. If there is a zero in the src array
10 * it is copied (i.e., we don't assume null-terminated strings */
11// true C aficionados would use the standard library function 'memcpy' instead of this function
12static void copyChars(char dst[], const char src[], int len) {
13    int k = 0;
14    while (k < len) {
15        dst[k] = src[k];
16        k = k + 1;
17    }
18}
19
20
21/* public functions, declarations of these functions are included in String.h */
22
23/*
24 * c_string is a normal string (using C conventions)
25 * initialize a String object and return it
26 */
27String StringCreate(const char* c_string) {
28    String retVal;
29
30    /* Step 1: determine the length of the source string */
31    int k = 0;
32    while (c_string[k] != 0) {
33        k = k + 1;
34    }
35
36    /* Step 2: populate our struct's components */
37    retVal.len = k;
38    retVal.ptr = (char*) malloc(retVal.len * sizeof(char));
39
40    /* Step 3: don't forget! to copy the string into the buffer */
41    copyChars(retVal.ptr, c_string, retVal.len);
42
43    return retVal;
44}
45
46/*
47 * initialize a String object that is an exact duplicate of src and return that string
48 * (NOTE: this is a deep copy)
49 */
50String StringDup(String* src) {
51    String retVal;
52
53    retVal.len = (*src).len;
54    retVal.ptr = (char*) malloc(retVal.len * sizeof(char));
55
56    /* Step 3: don't forget! to copy the string into the buffer */
57    copyChars(retVal.ptr, (*src).ptr, retVal.len);
58
59    return retVal;
60}
61
62
63void StringDestroy(String* s) {
64    free((*s).ptr);
65}
66
67void StringPrint(String* s) {
68    int k;
69    for (k = 0; k < (*s).len; k += 1) {
70        putchar((*s).ptr[k]);
71    }
72}
73
74
75/*
76 * Replace old_str with a copy of new_str
77 * This function differs from StringDup in that here we assume old_str is already
78 * a String (has previously been initialized). The StringDup function would be appropriate
79 * (instead of this function) if you have an uninitialized String struct */
80void StringReAssign(String* old_str, String* new_str) {
81    if (old_str == new_str) { // huh? don't know why someone would do this
82        /* old_str and new_str are already the same, so, just get out of town */
83        return;
84    }
85
86    StringDestroy(old_str);
87    *old_str = StringDup(new_str);
88}
89
90/*
91 * replace the contents of s with the concatenation of s and suffix
92 * NOTE: we must reallocate a new array on the heap to have sufficient storage to hold
93 * thew new and longer string that results from concatenating */
94void StringAppend(String* s, String* suffix) {
95    int new_len = (*s).len + (*suffix).len;
96    char* new_ptr = (char*) malloc(new_len * sizeof(char));
97
98    copyChars(new_ptr, (*s).ptr, (*s).len);
99    copyChars(new_ptr + (*s).len, (*suffix).ptr, (*suffix).len);
100
101    free((*s).ptr);
102    (*s).len = new_len;
103    (*s).ptr = new_ptr;
104}
105
106/*
107 * return a newly created string that is initialized as a copy of the concatenation of a and b */
108String StringConcat(String* a, String* b) {
109    int j; // indexes into a source string (a or b)
110    int k; // indexes into the result string
111    String retVal;
112    retVal.len = (*a).len + (*b).len;
113    retVal.ptr = (char*) malloc(retVal.len * sizeof(
114                                    char)); // plus one byte for the terminating zero
115
116    j = 0;
117    k = 0;
118    while (j < (*a).len) {
119        retVal.ptr[k] = (*a).ptr[j];
120        j += 1;
121        k += 1;
122    }
123
124    j = 0;
125    while (j < (*b).len) {
126        retVal.ptr[k] = (*b).ptr[j];
127        j += 1;
128        k += 1;
129    }
130
131    return retVal;
132}
133
134
135bool StringIsEqualTo(String* s1, String* s2) {
136    int k = 0;
137
138    if ((*s1).len != (*s2).len) {
139        return false;    // lengths must be the same, otherwise, don't bother
140    }
141
142    while (k < (*s1).len) {
143        if ((*s1).ptr[k] !=
144                (*s2).ptr[k]) { // kth character differs, so strings are not the same, quit now
145            return false;
146        }
147        k = k + 1;
148    }
149    // if we finish the loop, then all characters are equal
150    return true;
151}
152
153bool StringIsLessThan(String* s1, String* s2) {
154    int k = 0;
155
156    while (k != (*s1).len
157            && k != (*s2).len) {
158        if ((*s1).ptr[k] <
159                (*s2).ptr[k]) { // the next character in s1 < the character from s2, so return true
160            return true;
161        } else if ((*s1).ptr[k] > (*s2).ptr[k]) { // s1 > s2, return false
162            return false;
163        } else { // the characters must be the same, so just keep going
164            k = k + 1;
165        }
166    }
167
168    if ((*s1).len == (*s2).len) { // strings are equal (s1 not less than s2)
169        return false;
170    } else if ((*s1).len > (*s2).len) { /* s2 is a prefix of s1, i.e., s2 < s1 */
171        return false;
172    } else { /* s1 is a prefix of s2, so s1 < s2 */
173        return true;
174    }
175}
176
177