-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeSnap-Strings.h.html
214 lines (202 loc) · 6.77 KB
/
CodeSnap-Strings.h.html
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
<!----------------------------------------------------------------------------
CodeSnap-Strings.cpp.htm
Published 19 Mar 2017
Jim Fawcett, CSE687 : Object Oriented Design, Summer 2017
Note:
- Markup characters in the text part, enclosed in <pre>...</pre> have to be
replaced with escape sequences, e.g., < becomes < and > becomes >
- Be careful that you don't replace genuine markup characters with escape
sequences, e.g., everything outside of the <pre>...</pre> section.
----------------------------------------------------------------------------->
<html>
<head>
<script src="js/ScriptsUtilities.js"></script>
<script src="js/ScriptsTemplate.js"></script>
<script src="js/ScriptsKeyboard.js"></script>
<script src="js/ScriptsMenuCpp.js"></script>
<link rel="stylesheet" href="css/StylesTemplate.css" />
<link rel="stylesheet" href="css/StylesMenu.css" />
<style>
h3 {
font-weight: normal;
}
</style>
</head>
<body id="github" onload="initializeMenu()">
<nav>
<div id="navbar"></div>
</nav>
<a id="Next" href="CodeSnap-Strings.cpp.html">N</a>
<a id="Prev" href="CodeSnap-Strings.txt.html">P</a>
<navKeys-Container>
<nav-Key id="sKey" onclick="toggleSwipeEvents()">S</nav-Key>
<nav-Key id="rKey" onclick="location.reload()">R</nav-Key>
<nav-Key id="tKey" onclick="scrollPageTop()">T</nav-Key>
<nav-Key id="bKey" onclick="scrollPageBottom()">B</nav-Key>
<nav-Key id="hKey" onclick="helpWin()">H</nav-Key>
<nav-Key id="pKey" onclick="loadPrev()">P</nav-Key>
<nav-Key id="nKey" onclick="loadNext()">N</nav-Key>
</navKeys-Container>
<h3>
<a href="CodeSnap-Strings.h.html">Strings.h</a>,
<a href="CodeSnap-Strings.cpp.html">Strings.cpp</a>,
<a href="CodeSnap-Strings.txt.html">Strings.txt</a>,
<a class="disable" href="#">Code folder</a>
</h3>
<div class="indent">
This demo illustrates basic std::string operations by creating
several useful utility functions.
</div>
<hr />
<pre class="codeSnap">
#ifndef STRINGUTILITIES_H
#define STRINGUTILITIES_H
///////////////////////////////////////////////////////////////////////
// StringUtilities.h - small, generally useful, helper classes //
// ver 1.0 //
// Language: C++, Visual Studio 2017 //
// Application: Most Projects, CSE687 - Object Oriented Design //
// Author: Jim Fawcett, Syracuse University, CST 4-187 //
// [email protected] //
///////////////////////////////////////////////////////////////////////
/*
* Package Operations:
* -------------------
* This package provides functions:
* - Title(text) display title
* - title(text) display subtitle
* - putline(n) display n newlines
* - trim(str) remove leading and trailing whitespace
* - split(str, 'delim') break string into vector of strings separated by delim char
* - showSplit(vector) display splits
*
* Required Files:
* ---------------
* StringUtilities.h
*
* Maintenance History:
* --------------------
* ver 1.0 : 12 Jan 2018
* - first release
* - refactored from earlier Utilities.h
*
* Notes:
* ------
* - Designed to provide all functionality in header file.
* - Implementation file only needed for test and demo.
*
* Planned Additions and Changes:
* ------------------------------
* - none yet
*/
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <functional>
#include <locale>
namespace Utilities
{
/////////////////////////////////////////////////////////////////////
// String Helper functions
//----< display underlined title >-----------------------------------
inline void Title(const std::string& text, std::ostream& out = std::cout, char underline = '=')
{
out << "\n " << text;
out << "\n " << std::string(text.size() + 2, underline);
}
//----< display underlined subtitle >--------------------------------
inline void title(const std::string& text, std::ostream& out = std::cout, char underline = '-')
{
out << "\n " << text;
out << "\n " << std::string(text.size() + 2, underline);
}
//----< display j newlines >-----------------------------------------
inline void putline(size_t j = 1, std::ostream& out = std::cout)
{
for (size_t i = 0; i < j; ++i)
out << "\n";
}
/*--- remove whitespace from front and back of string argument ---*/
/*
* - does not remove newlines
*/
template <typename T>
inline std::basic_string<T> trim(const std::basic_string<T>& toTrim)
{
if (toTrim.size() == 0)
return toTrim;
std::basic_string<T> temp;
std::locale loc;
typename std::basic_string<T>::const_iterator iter = toTrim.begin();
while (isspace(*iter, loc) && *iter != '\n')
{
if (++iter == toTrim.end())
{
break;
}
}
for (; iter != toTrim.end(); ++iter)
{
temp += *iter;
}
typename std::basic_string<T>::reverse_iterator riter;
size_t pos = temp.size();
for (riter = temp.rbegin(); riter != temp.rend(); ++riter)
{
--pos;
if (!isspace(*riter, loc) || *riter == '\n')
{
break;
}
}
if (0 <= pos && pos < temp.size())
temp.erase(++pos);
return temp;
}
/*--- split sentinel separated strings into a vector of trimmed strings ---*/
template <typename T>
inline std::vector<std::basic_string<T>> split(const std::basic_string<T>& toSplit, T splitOn = ',')
{
std::vector<std::basic_string<T>> splits;
std::basic_string<T> temp;
typename std::basic_string<T>::const_iterator iter;
for (iter = toSplit.begin(); iter != toSplit.end(); ++iter)
{
if (*iter != splitOn)
{
temp += *iter;
}
else
{
splits.push_back(trim(temp));
temp.clear();
}
}
if (temp.length() > 0)
splits.push_back(trim(temp));
return splits;
}
/*--- show collection of string splits ------------------------------------*/
template <typename T>
inline void showSplits(const std::vector<std::basic_string<T>>& splits, std::ostream& out = std::cout)
{
out << "\n";
for (auto item : splits)
{
if (item == "\n")
out << "\n--" << "newline";
else
out << "\n--" << item;
}
out << "\n";
}
}
#endif
</pre>
<!--<div class="photo">
<img src="pictures/CSTstrip.jpg" width="100%" />
</div>-->
<info-bar></info-bar>
</body>
</html>