+/EE312
software design
git clone https://git.julia.blue/+/EE312
0c27270
master
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 ) { 13int k = 0 ; 14while (k < len ) { 15dst [k ]= src [k ]; 16k = 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 ) { 28String retVal ; 29 30/* Step 1: determine the length of the source string */ 31int k = 0 ; 32while (c_string [k ]!= 0 ) { 33k = k + 1 ; 34 } 35 36/* Step 2: populate our struct's components */ 37retVal .len = k ; 38retVal .ptr = (char * )malloc (retVal .len * sizeof (char )); 39 40/* Step 3: don't forget! to copy the string into the buffer */ 41copyChars (retVal .ptr ,c_string ,retVal .len ); 42 43return 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 ) { 51String retVal ; 52 53retVal .len = (* src ).len ; 54retVal .ptr = (char * )malloc (retVal .len * sizeof (char )); 55 56/* Step 3: don't forget! to copy the string into the buffer */ 57copyChars (retVal .ptr , (* src ).ptr ,retVal .len ); 58 59return retVal ; 60} 61 62 63void StringDestroy (String * s ) { 64free ((* s ).ptr ); 65} 66 67void StringPrint (String * s ) { 68int k ; 69for (k = 0 ;k < (* s ).len ;k += 1 ) { 70putchar ((* 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 ) { 81if (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 */ 83return ; 84 } 85 86StringDestroy (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 ) { 95int new_len = (* s ).len + (* suffix ).len ; 96char * new_ptr = (char * )malloc (new_len * sizeof (char )); 97 98copyChars (new_ptr , (* s ).ptr , (* s ).len ); 99copyChars (new_ptr + (* s ).len , (* suffix ).ptr , (* suffix ).len ); 100 101free ((* 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 ) { 109int j ;// indexes into a source string (a or b) 110int k ;// indexes into the result string 111String retVal ; 112retVal .len = (* a ).len + (* b ).len ; 113retVal .ptr = (char * )malloc (retVal .len * sizeof ( 114char ));// plus one byte for the terminating zero 115 116j = 0 ; 117k = 0 ; 118while (j < (* a ).len ) { 119retVal .ptr [k ]= (* a ).ptr [j ]; 120j += 1 ; 121k += 1 ; 122 } 123 124j = 0 ; 125while (j < (* b ).len ) { 126retVal .ptr [k ]= (* b ).ptr [j ]; 127j += 1 ; 128k += 1 ; 129 } 130 131return retVal ; 132} 133 134 135bool StringIsEqualTo (String * s1 ,String * s2 ) { 136int k = 0 ; 137 138if ((* s1 ).len != (* s2 ).len ) { 139return false;// lengths must be the same, otherwise, don't bother 140 } 141 142while (k < (* s1 ).len ) { 143if ((* s1 ).ptr [k ]!= 144 (* s2 ).ptr [k ]) {// kth character differs, so strings are not the same, quit now 145return false; 146 } 147k = k + 1 ; 148 } 149// if we finish the loop, then all characters are equal 150return true; 151} 152 153bool StringIsLessThan (String * s1 ,String * s2 ) { 154int k = 0 ; 155 156while (k != (* s1 ).len 157&& k != (* s2 ).len ) { 158if ((* s1 ).ptr [k ]< 159 (* s2 ).ptr [k ]) {// the next character in s1 < the character from s2, so return true 160return true; 161 }else if ((* s1 ).ptr [k ]> (* s2 ).ptr [k ]) {// s1 > s2, return false 162return false; 163 }else {// the characters must be the same, so just keep going 164k = k + 1 ; 165 } 166 } 167 168if ((* s1 ).len == (* s2 ).len ) {// strings are equal (s1 not less than s2) 169return false; 170 }else if ((* s1 ).len > (* s2 ).len ) {/* s2 is a prefix of s1, i.e., s2 < s1 */ 171return false; 172 }else {/* s1 is a prefix of s2, so s1 < s2 */ 173return true; 174 } 175} 176 177