This repository has been archived by the owner on Mar 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apstring.h
231 lines (201 loc) · 8.69 KB
/
apstring.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#ifndef _APSTRING_H
#define _APSTRING_H
#include <iostream>
using namespace std;
// *******************************************************************
// APCS string class
//
// string class consistent with a subset of the standard C++ string class
// as defined in the draft ANSI standard
// *******************************************************************
extern const int npos; // used to indicate not a position in the string
class apstring
{
public:
// constructors/destructor
apstring( ); // construct empty string ""
apstring( const char * s ); // construct from string literal
apstring( const apstring & str ); // copy constructor
~apstring( ); // destructor
// assignment
const apstring & operator = ( const apstring & str ); // assign str
const apstring & operator = ( const char * s ); // assign s
const apstring & operator = ( char ch ); // assign ch
// accessors
int length( ) const; // number of chars
int find( const apstring & str ) const; // index of first occurrence of str
int find( char ch ) const; // index of first occurrence of ch
apstring substr( int pos, int len ) const; // substring of len chars
// starting at pos
const char * c_str( ) const; // explicit conversion to char *
// indexing
char operator[ ]( int k ) const; // range-checked indexing
char & operator[ ]( int k ); // range-checked indexing
// modifiers
const apstring & operator += ( const apstring & str );// append str
const apstring & operator += ( char ch ); // append char
// case conversion added by R Harlan, 11/18/97
void UpCase();
//description: convert lower case chars to upper case, leave
// nonalphabetic characters unaffected
//precondition: this string represents c0, c1, ..., c(n-1)
// 0 <= pos < n.
//postcondition: returns the string that represents
// c(0), c(1), ..., c(len-1) with all lower case
// letters converted to uppercase
void DownCase();
//description: convert upper case chars to lower case, leave
// nonalphabetic characters unaffected
//precondition: this string represents c0, c1, ..., c(n-1)
// 0 <= pos < n.
//postcondition: returns the string that represents
// c(0), c(1), ..., c(len-1) with all upper case
// letters converted to lower case
//
//
private:
int myLength; // length of string (# of characters)
int myCapacity; // capacity of string
char * myCstring; // storage for characters
};
// The following free (non-member) functions operate on strings
//
// I/O functions
ostream & operator << ( ostream & os, const apstring & str );
istream & operator >> ( istream & is, apstring & str );
istream & getline( istream & is, apstring & str );
// comparison operators:
bool operator == ( const apstring & lhs, const apstring & rhs );
bool operator != ( const apstring & lhs, const apstring & rhs );
bool operator < ( const apstring & lhs, const apstring & rhs );
bool operator <= ( const apstring & lhs, const apstring & rhs );
bool operator > ( const apstring & lhs, const apstring & rhs );
bool operator >= ( const apstring & lhs, const apstring & rhs );
// concatenation operator +
apstring operator + ( const apstring & lhs, const apstring & rhs );
apstring operator + ( char ch, const apstring & str );
apstring operator + ( const apstring & str, char ch );
// *******************************************************************
// Specifications for string functions
//
// Any violation of a function's precondition will result in an error
// message followed by a call to abort.
//
// constructors / destructor
//
// string( )
// postcondition: string is empty
//
// string( const char * s )
// description: constructs a string object from a literal string
// such as "abcd"
// precondition: s is '\0'-terminated string as used in C
// postcondition: copy of s has been constructed
//
// string( const string & str )
// description: copy constructor
// postcondition: copy of str has been constructed
//
// ~string( );
// description: destructor
// postcondition: string is destroyed
//
// assignment
//
// string & operator = ( const string & rhs )
// postcondition: normal assignment via copying has been performed
//
// string & operator = ( const char * s )
// description: assignment from literal string such as "abcd"
// precondition: s is '\0'-terminated string as used in C
// postcondition: assignment via copying of s has been performed
//
// string & operator = ( char ch )
// description: assignment from character as though single char string
// postcondition: assignment of one-character string has been performed
//
// accessors
//
// int length( ) const;
// postcondition: returns # of chars in string
//
// int find( const string & str) const;
// description: find the first occurrence of the string str within this
// string and return the index of the first character. If
// str does not occur in this string, then return npos.
// precondition: this string represents c0, c1, ..., c(n-1)
// str represents s0, s1, ...,s(m-1)
// postcondition: if s0 == ck0, s1 == ck1, ..., s(m-1) == ck(m-1) and
// there is no j < k0 such that s0 = cj, ...., sm == c(j+m-1),
// then returns k0;
// otherwise returns npos
//
// int find( char ch ) const;
// description: finds the first occurrence of the character ch within this
// string and returns the index. If ch does not occur in this
// string, then returns npos.
// precondition: this string represents c0, c1, ..., c(n-1)
// postcondition: if ch == ck, and there is no j < k such that ch == cj
// then returns k;
// otherwise returns npos
//
// string substr( int pos, int len ) const;
// description: extract and return the substring of length len starting
// at index pos
// precondition: this string represents c0, c1, ..., c(n-1)
// 0 <= pos <= pos + len - 1 < n.
// postcondition: returns the string that represents
// c(pos), c(pos+1), ..., c(pos+len-1)
//
// const char * c_str( ) const;
// description: convert string into a '\0'-terminated string as
// used in C for use with functions
// that have '\0'-terminated string parameters.
// postcondition: returns the equivalent '\0'-terminated string
//
// indexing
//
// char operator [ ]( int k ) const;
// precondition: 0 <= k < length()
// postcondition: returns copy of the kth character
//
// char & operator [ ]( int k )
// precondition: 0 <= k < length()
// postcondition: returns reference to the kth character
//
// modifiers
//
// const string & operator += ( const string & str )
// postcondition: concatenates a copy of str onto this string
//
// const string & operator += ( char ch )
// postcondition: concatenates a copy of ch onto this string
//
//
// non-member functions
//
// ostream & operator << ( ostream & os, const string & str)
// postcondition: str is written to output stream os
//
// istream & operator >> ( istream & is, string & str )
// precondition: input stream is open for reading
// postcondition: the next string from input stream is has been read
// and stored in str
//
// istream & getline( istream & is, string & str )
// description: reads a line from input stream is into the string str
// precondition: input stream is open for reading
// postcondition: chars from input stream is up to '\n' have been read
// and stored in str; the '\n' has been read but not stored
//
// string operator + ( const string & lhs, const string & rhs )
// postcondition: returns concatenation of lhs with rhs
//
// string operator + ( char ch, const string & str )
// postcondition: returns concatenation of ch with str
//
// string operator + ( const string & str, char ch )
// postcondition: returns concatenation of str with ch
//
//***************************************************************
#endif