Title: C String, Proper Type, and String Objects
1C String, Proper Type, and String Objects
- Andy Wang
- COP 4530 Data Structures, Algorithms, and
Generic Programming
2Lecture Overview
- Goal
- Design a string class that overcomes the
disadvantages of C strings - Roadmap
- Critique of C strings
- The need for proper type
- String objects
3In-Class Exercise
- Write a C function that copies one string to
another. - void strcpy(char dest, char src)
-
-
4Assumptions of C Strings
- Allocated memory
- char str
- str malloc(sizeof(char)11)
- Null termination
- str10 \0
- strcpy(str, 0123456789)
5What can go wrong?
- void strcpy(char copy_cat, char cat)
- int j
- for (j 0 catj ! \0 j)
- copy_catj catj
-
- copy_catj \0
6Other String Functions
- Still assume null-terminated strings
- Still assume allocated memory
- Security holes
- Internet worms
- System break-ins
7The Concept of Proper Type
- A proper type should be
- Responsible for its own data
- Data protected from clients and other programs
- Responsible for its own behavior
- Make behavior available to clients
- Implementation hidden from the public view
- Responsible for its own existence
- Automatic memory management
8C Class Objects
- Responsible for their own data
- Protected or private data
- Controlled access through public methods
- Responsible for their own behavior
- Public methods
- Responsible for their own existence
- Constructors/deconstructors
9In-Class Exercise String Class
- namespace rcl
- class String
- // friend
- public
- //
- private
- //
-
- //
10String Class
- class String
- // friend iostream operators
- public
- // constructors/destructors
- // operators
- // builders
- // data accessors (const)
- // string comparison function
- private
- // data
- // helper methods
-
- // equality and order comparison operators
11Private Data
- char str
- unsigned int size // equal to C strlen()
class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
12Data Accessors
- unsigned int Size() const
- unsigned int Length() const
- char Element(unsigned int n) const
class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
13Data Accessors
- unsigned int StringSize() const
- return size
-
- unsigned int StringLength() const
- // note strlen takes char therefore, we need
to implement an operator that converts String to
char. - return strlen(this)
14In-Class Exercise Element
- Return the nth character of the String. Watch
out for boundary cases. - char StringElement(unsigned int n) const
-
-
15Element
- char StringElement(unsigned int n) const
- char ch
- if ((size 0) (n gt size))
- return \0
- else
- return strn
-
16String Comparison Function
- static int StrCmp(const String, const String)
class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
17StrCmp
- int StringStrCmp(const String S1, const
String S2) - if ((S1.Size() 0) (S2.Size() 0))
- return 0
- else if ((S1.Size() 0) (S2.Size() ! 0))
- return 1
- else if ((S1.Size() ! 0) (S2.Size() 0))
- return 1
- else
- return (strcmp(S1.str, S2.str)
-
18Equality Operators
- int operator(const String S1, const String
S2) - int operator!(const String S1, const String
S2) - int operatorlt(const String S1, const String
S2) - int operatorlt(const String S1, const String
S2) - int operatorgt(const String S1, const String
S2) - int operatorgt(const String S1,
- const String S2)
class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
19Comparison Operators
- int operator(const String S1, const String
S2) - return (StringStrCmp(S1, S2) 0)
-
- int operator!(const String S1, const String
S2) - return (StringStrCmp(S1, S2) ! 0)
-
- int operatorgt(const String S1, const String S2)
- return (StringStrCmp(S1, S2) gt 0)
-
20Comparison Operators
- int operatorgt(const String S1, const String
S2) - return (StringStrCmp(S1, S2) gt 0)
-
- int operatorlt(const String S1, const String S2)
- return (StringStrCmp(S1, S2) lt 0)
-
- int operatorlt(const String S1, const String
S2) - return (StringStrCmp(S1, S2) lt 0)
-
21Helper Methods
- static void Error(const char)
- static char newstr(int n)
- static int xstrlen(const char)
- static void xstrcpy(char , const char)
- void Clear()
- void Clone(const String S)
class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
22Error
- void StringError(const char msg)
- cerr ltlt String error ltlt msg ltlt \n
- exit(EXIT_FAILURE)
23In-Class Exercise newstr
- It creates a new String of size n (C String with
an array size of n 1). - void Stringnewstr(int n)
-
-
24newstr
- char Stringnewstr(int n)
- char Cptr 0
- if (n gt 0)
- Cptr new charn 1
- if (Cptr 0)
- Error(memory allocation failure)
-
- Cptrn \0
-
- return Cptr
25xstrlen
- int Stringxstrlen(const char s)
- int len
-
- if (s ! 0) // check for null pointer
- for (len 0 slen ! \0 len)
-
- return len
26xstrcpy
- void Stringxstrcpy(char dest, const char src)
- if (src ! 0) // check for null pointer
- if (dest ! 0) // check for null pointer
- int j
- for (j 0 srcj ! \0 j)
- destj srcj
-
- destj \0
- else
- Error(xstrcpy null destination)
-
-
27In-Class Exercise Clear
- If the String has storage allocated, delete it,
and set the storage pointer and size to zero. - void Clear()
-
-
28Clear
- void StringClear()
- if (str)
- delete str
- str 0
- size 0
-
29In-Class Exercise Clone
- If makes a copy of the parameter S.
- void StringClone(const String S)
-
-
30Clone
- void StringClone(const String S)
- size S.Size()
- if (size gt 0)
- str newstr(size)
- xstrcpy(str, S.str)
- else
- str 0
-
31Operators
- String operator(const String S)
- char operator (unsigned int I) const
- operator const char () const
class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
32Operator
- String Stringoperator(const String S)
- if (this ! S)
- Clear()
- Clone(S)
-
- return this
33Operator
- char Stringoperator (unsigned int n) const
- if ((size 0) (n gt size))
- Error(index out of range)
-
- return strn
34Operator const char
- Stringoperator const char () const
- return str
35Builder Functions
- void Wrap(const char Cptr)
- void GetLine(istream inl)
- int SetSize(unsigned int sz, char fill)
class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
36Wrap
- void StringWrap(const char Cptr)
- Clear()
- if (Cptr)
- size xstrlen(Cptr)
- str newstr(size)
- xstrcpy(str, Cptr)
-
37GetLine
- void StringGetLine(istream is)
- unsigned int curr_size 0, buff_size
init_buff_size - char buffer new charbuff_size 1
- for (char x is.get() ((x ! \n)
(!is.eof()) x is.get()) - if (curr_size buff_size)
- buff_size 2
- char newbuffer new charbuff_size 1
- // copy buffer to newbuffer
- delete buffer
- buffer newbuffer
-
- buffercurr_size x
-
- buffercurr_size \0
- Wrap(buffer)
- delete buffer
38SetSize
- int StringSetSize(unsigned int sz, char fill)
- if (sz ! Size())
- char newdata newstr(sz)
- if (newdata 0) return 0
- unsigned int j
- if (sz lt Size())
- for (j 0 j lt sz j)
- newdataj strj
-
- else
- for (j 0 j lt Size() j)
- newdataj strj
-
- for (j Size() j lt sz j)
- newdataj fill
-
- delete str
- str newdata
- size sz
39Constructors/Destructors
- String()
- explicit String(const char Cptr)
- String()
- String (const String S)
class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
40Constructors /Deconstructors
- StringString() str(0), size(0)
- StringString(const char Cptr) str(0),
size(0) - Wrap(Cptr)
-
- StringString(const String S)
- Clone(S)
-
- StringString()
- if (str) delete str
41I/O Operators
- friend ostream operatorltlt(ostream os, const
String S) - friend istream operatorgtgt(istream is, String
S)
class String // friend iostream
operators public // constructors/destructors
// operators // builders // data accessors
(const) // String comparison function private
// data // helper methods // equality and
order comparison operators
42Operatorltlt
- ostream operatorltlt(ostream os, const String S)
- os ltlt S.str
- return os
-
43Operatorgtgt
- istream operatorgtgt(istream is, String S)
- unsigned int curr_size 0 buff_size
init_buff_size - char x
- // skip clear space
- char buffer Stringnewstr(buff_size)
- buffercurr_size x
- for (x is.peek() (// x is not white space or
eof) x is.peek()) - if (curr_size buff_size)
- buff_size 2
- char newbuffer Stringnewstr(buff_size)
- // copy buffer into newbuffer
- delete buffer
- buffer newbuffer
-
- buffercurr_size x
- is.get()
-
- buffercurr_size \0
- S.Wrap(buffer)