+/EE312
software design
git clone https://git.julia.blue/+/EE312
796bbdc
master
1// String ADT Project3.cpp 2// EE 312 Project 3 submission by 3// Joseph Ryan 4// jbr2558 5// Slip days used: 0 6// Spring 2018 7 8#include <assert.h> 9#include <stdbool.h> 10#include <stdlib.h> 11#include <stdint.h> 12#include <string.h> 13#include "String.h" 14 15#define SIGNATURE (~0xdeadbeef) 16 17/* 18* Helper macro to find the signature of a UTString 19* Treat this like a function that takes a UTString* 20* Accesses a uint32_t from where the signature should be located 21*/ 22#define CHECK (s ) (*(uint32_t*) ((s)->string + (s)->length + 1)) 23 24/* 25* Checks if a UTString is valid or not. 26*/ 27bool isOurs (UTString * s ) { 28if (CHECK (s )== SIGNATURE ) { 29return true; 30 }else { 31return false; 32 } 33} 34 35UTString * addCheck (UTString * s ) { 36s -> string [s -> length ]= '\0' ; 37CHECK (s )= ~0xdeadbeef ; 38return s ; 39} 40 41/* 42* Allocate a utstring on the heap. 43* Initialize the string correctly by copying src. 44* Return a pointer to the UTString. 45*/ 46UTString * utstrdup (const char * src ) { 47UTString * newstr = (UTString * )malloc (sizeof (UTString )); 48newstr -> length = strlen (src ); 49newstr -> capacity = newstr -> length ; 50newstr -> string = strdup (src ); 51addCheck (newstr ); 52return newstr ; 53} 54 55/* 56* Returns the length of s. 57* s must be a valid UTString. 58*/ 59uint32_t utstrlen (const UTString * s ) { 60assert (isOurs ((UTString * )s )); 61return s -> length ; 62} 63 64/* 65* Append the string suffix to the UTString s. 66* s must be a valid UTString. 67* Do not allocate any additional storage: only append as many characters 68* as will actually fit in s. 69* Update the length of s. 70* Return s with the above changes. */ 71UTString * utstrcat (UTString * s ,const char * suffix ) { 72assert (isOurs (s )); 73int32_t len = strlen (suffix ); 74for (int32_t i = 0 ;s -> length < s -> capacity && i < len ;++ i ,++ s -> length ) { 75s -> string [s -> length ]= suffix [i ]; 76 } 77addCheck (s ); 78return s ; 79} 80 81/* 82* Copy src into dst. 83* dst must be a valid UTString. 84* src is an ordinary string. 85* Do not allocate any additional storage: only copy as many characters 86* as will actually fit in dst. 87* Update the length of dst. 88* Return dst with the above changes. 89*/ 90UTString * utstrcpy (UTString * dst ,const char * src ) { 91assert (isOurs (dst )); 92dst -> length = 0 ; 93utstrcat (dst ,src ); 94return dst ; 95} 96 97/* 98* Free all memory associated with the given UTString. 99*/ 100void utstrfree (UTString * self ) { 101assert (isOurs (self )); 102free (self -> string ); 103free (self ); 104} 105 106/* 107* Make s have at least as much capacity as new_capacity. 108* s must be a valid UTString. 109* If s has enough capacity already, do nothing. 110* If s does not have enough capacity, then allocate enough capacity, 111* copy the old string and the null terminator, free the old string, 112* and update all relevant metadata. 113* Return s with the above changes. 114*/ 115UTString * utstrrealloc (UTString * s ,uint32_t new_capacity ) { 116assert (isOurs (s )); 117if (new_capacity <=s -> capacity ) { 118return s ; 119 } 120s -> capacity = new_capacity ; 121s -> string = (char * )realloc (s -> string ,new_capacity + 5 ); 122addCheck (s ); 123return s ; 124}