-
Notifications
You must be signed in to change notification settings - Fork 0
/
strlib.h
201 lines (163 loc) · 5.91 KB
/
strlib.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
/*
* strlib.h
*
* Created on: Mar 15, 2015
* Author: Aurelia
*/
#ifndef STRLIB_H_
#define STRLIB_H_
#include <iostream>
#include <string>
/*
* Function: integerToString
* Usage: string s = integerToString(n);
* -------------------------------------
* Converts an integer into the corresponding string of digits. For
* example, calling integerToString(123) returns the string "123".
*/
std::string integerToString(int n);
/*
* Function: stringToInteger
* Usage: int n = stringToInteger(str);
* ------------------------------------
* Converts a string of digits into an integer. If the string is not a
* legal integer or contains extraneous characters other than whitespace,
* stringToInteger calls error with an appropriate message.
*/
int stringToInteger(std::string str);
/*
* Function: realToString
* Usage: string s = realToString(d);
* ----------------------------------
* Converts a floating-point number into the corresponding string form.
* For example, calling realToString(23.45) returns the string "23.45".
*/
std::string realToString(double d);
/*
* Function: stringToReal
* Usage: double d = stringToReal(str);
* ------------------------------------
* Converts a string representing a real number into its corresponding
* value. If the string is not a legal floating-point number or contains
* extraneous characters other than whitespace, stringToReal calls error
* with an appropriate message.
*/
double stringToReal(std::string str);
/*
* Function: toUpperCase
* Usage: string s = toUpperCase(str);
* -----------------------------------
* Returns a new string in which all lowercase characters have been
* converted into their uppercase equivalents.
*/
std::string toUpperCase(std::string str);
/*
* Function: toLowerCase
* Usage: string s = toLowerCase(str);
* -----------------------------------
* Returns a new string in which all uppercase characters have been
* converted into their lowercase equivalents.
*/
std::string toLowerCase(std::string str);
/*
* Function: equalsIgnoreCase
* Usage: if (equalsIgnoreCase(s1, s2)) ...
* ----------------------------------------
* Returns true if s1 and s2 are equal discounting differences in case.
*/
bool equalsIgnoreCase(std::string s1, std::string s2);
/*
* Function: startsWith
* Usage: if (startsWith(str, prefix)) ...
* ---------------------------------------
* Returns true if the string str starts with the specified prefix, which
* may be either a string or a character.
*/
bool startsWith(std::string str, std::string prefix);
bool startsWith(std::string str, char prefix);
/*
* Function: endsWith
* Usage: if (endsWith(str, suffix)) ...
* -------------------------------------
* Returns true if the string str ends with the specified suffix, which may
* be either a string or a character.
*/
bool endsWith(std::string str, std::string suffix);
bool endsWith(std::string str, char suffix);
/*
* Function: trim
* Usage: string trimmed = trim(str);
* ----------------------------------
* Returns a new string after removing any whitespace characters from the
* beginning and end of the argument.
*/
std::string trim(std::string str);
/* Private section */
/**********************************************************************/
/* Note: Everything below this point in the file is logically part */
/* of the implementation and should not be of interest to clients. */
/**********************************************************************/
/*
* Friend function: writeQuotedString
* Usage: writeQuotedString(outfile, str, forceQuotes);
* ----------------------------------------------------
* Writes the string str to outfile surrounded by double quotes, converting
* special characters to escape sequences, as necessary. If the optional
* parameter forceQuotes is explicitly set to false, quotes are included in
* the output only if necessary.
*/
void writeQuotedString(std::ostream & os, const std::string & str,
bool forceQuotes = true);
/*
* Friend function: readQuotedString
* Usage: readQuotedString(infile, str);
* -------------------------------------
* Reads the next string from infile into the reference parameter str. If
* the first character (other than whitespace) is either a single or a
* double quote, this function reads characters up to the matching quote,
* processing standard escape sequences as it goes. If not, readString
* reads characters up to any of the characters in the string
* STRING_DELIMITERS in the implementation file.
*/
void readQuotedString(std::istream & is, std::string & str);
/*
* Friend function: stringNeedsQuoting
* Usage: if (stringNeedsQuoting(str)) ...
* ---------------------------------------
* Checks whether the string needs quoting in order to be read correctly.
*/
bool stringNeedsQuoting(const std::string & str);
/*
* Friend function: writeGenericValue
* Usage: writeGenericValue(os, value, forceQuotes);
* -------------------------------------------------
* Writes a generic value to the output stream. If that value is a string,
* this function uses writeQuotedString to write the value.
*/
template <typename ValueType>
void writeGenericValue(std::ostream & os, const ValueType & value,
bool forceQuotes) {
os << value;
}
template <>
inline void writeGenericValue(std::ostream & os, const std::string & value,
bool forceQuotes) {
writeQuotedString(os, value, forceQuotes);
}
/*
* Friend function: readGenericValue
* Usage: readGenericValue(is, value);
* -----------------------------------
* Reads a generic value from the input stream. If that value is a string,
* this function uses readQuotedString to read the value.
*/
template <typename ValueType>
void readGenericValue(std::istream & is, ValueType & value) {
is >> value;
}
template <>
inline void readGenericValue(std::istream & is, std::string & value) {
readQuotedString(is, value);
}
#endif
#endif /* STRLIB_H_ */