+/EE312
software design
git clone https://git.julia.blue/+/EE312
6241299
master
1#ifndef _String_h 2#define _String_h 3#include <stdlib.h> 4#include <stdint.h> 5// #include "MemHeap.h" 6 7struct String { 8private : 9char * ptr ; 10uint32_t len; 11 12public : 13String( const char * src ) { 14construct( src ); 15} 16 17String ( void ) { 18construct ( "" ); 19} 20 21String ( const String & that ) { 22copy ( that ); 23} 24~ String ( void ) { 25destroy (); 26} 27String & operator = ( const String & that ) { 28if ( this != & that ) { 29destroy (); 30copy ( that ); 31} 32return * this ; 33} 34 35String & operator = ( const char * src ) { 36destroy (); 37construct ( src ); 38return * 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*/ 50const char * c_str ( void ) const { 51return ptr ; 52} 53 54uint32_t size ( void ) const { 55return len ; 56} 57 58bool operator == ( String s2 ) const { 59const String & s1 = * this ; 60if ( s1 . size () != s2 . size ()) { 61return false; 62} 63int k = 0 ; 64while ( k < s1 . size ()) { 65if ( s1 .ptr[ k ] != s2 .ptr[ k ]) { 66return false; 67} 68k += 1 ; 69} 70return true; 71} 72 73bool operator < ( String s2 ) const { 74const String & s1 = * this ; 75int k = 0 ; 76while ( s1 .ptr[ k ] == s2 .ptr[ k ]) { 77if ( s1 .ptr[ k ] == 0 ) { 78return false; // strings are equal 79} 80k += 1 ; 81} 82return s1 .ptr[ k ] < s2 .ptr[ k ]; 83} 84 85 86bool operator != ( String s2 ) const { 87const String & s1 = * this ; 88return !( s1 == s2 ); 89} 90bool operator > ( String s2 ) const { 91const String & s1 = * this ; 92return s2 < s1 ; 93} 94bool operator <=( String s2 ) const { 95const String & s1 = * this ; 96return ! ( s2 < s1 ); 97} 98bool operator >=( String s2 ) const { 99const String & s1 = * this ; 100return ! ( 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 110void construct ( const char * src ) { 111len = 0 ; 112while ( src [ len ] != 0 ) { 113len += 1 ; 114} 115ptr = ( char * ) malloc ( len + 1 ); 116copy ( src ); 117} 118 119/* copy(char*) assumes that ptr has been allocated and holds sufficient capacity for src */ 120void copy ( const char * src ) { 121int k = len ; 122while ( k >= 0 ) { 123ptr [ k ] = src [ k ]; 124k -= 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) */ 130void copy ( const String & that ) { 131len = that .len; 132ptr = ( char * ) malloc ( len + 1 ); 133copy ( 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 */ 138void destroy ( void ) { 139free ( ptr ); 140} 141}; 142 143 144 145 146#endif /* _String_h */