-
Notifications
You must be signed in to change notification settings - Fork 0
/
blStringsManipulation.hpp
163 lines (116 loc) · 5.73 KB
/
blStringsManipulation.hpp
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
#ifndef BL_STRINGSMANIPULATION_HPP
#define BL_STRINGSMANIPULATION_HPP
//-------------------------------------------------------------------
// FILE: blStringManipulation.hpp
//
//
//
// PURPOSE: Functions useful in manipulating strings
//
// -- All functions/algorithms are defined within
// the "blAlgorithmsLIB" namespace
//
//
//
// AUTHOR: Vincenzo Barbato
// http://www.barbatolabs.com
//
//
//
// LISENSE: MIT-LICENCE
// http://www.opensource.org/licenses/mit-license.php
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes needed for these functions
//-------------------------------------------------------------------
#include <string>
#include <algorithm>
#include "blCyclicStlAlgorithms.hpp"
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// NOTE: This class is defined within the blAlgorithmsLIB namespace
//-------------------------------------------------------------------
namespace blAlgorithmsLIB
{
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to search for substrings in a user
// specified string
//-------------------------------------------------------------------
template<typename blContainerType,
typename blStringType>
inline bool is_in_container(const blContainerType& container,
const blStringType& stringToSearchFor)
{
return ( blAlgorithmsLIB::search(container.begin(),container.end(),stringToSearchFor.begin(),stringToSearchFor.end()) != container.end());
}
template<typename blContainerType,
typename blStringType>
inline auto search_in_container(const blContainerType& container,
const blStringType& stringToSearchFor)
{
return blAlgorithmsLIB::search(container.begin(),container.end(),stringToSearchFor.begin(),stringToSearchFor.end());
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to know whether a container starts/ends
// with a specified string
//-------------------------------------------------------------------
template<typename blContainerType,
typename blStringType>
inline bool starts_with(const blContainerType& container,
const blStringType& stringToSearchFor)
{
return blAlgorithmsLIB::is_partially_equal(container.begin(),
container.end(),
stringToSearchFor.begin(),
stringToSearchFor.end());
}
template<typename blContainerType,
typename blStringType>
inline bool ends_with(const blContainerType& container,
const blStringType& stringToSearchFor)
{
if(container.size() < stringToSearchFor.size())
return false;
auto containerBeginIter = container.begin();
std::advance(containerBeginIter,container.size() - stringToSearchFor.size());
return blAlgorithmsLIB::is_partially_equal(containerBeginIter,
container.end(),
stringToSearchFor.begin(),
stringToSearchFor.end());
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to replace all substrings matching a user
// specified "old substring" with a user specified "new substring"
//-------------------------------------------------------------------
inline void findAndReplaceAllMatchingSubstrings(std::string& sourceString,
const std::string& oldSubstring,
const std::string& newSubstring)
{
auto positionWhereOldSubstringWasFound = std::distance(sourceString.begin(),
std::search(sourceString.begin(),
sourceString.end(),
oldSubstring.begin(),
oldSubstring.end()));
auto endPosition = std::distance(sourceString.begin(),sourceString.end());
while(positionWhereOldSubstringWasFound < endPosition)
{
sourceString.replace(positionWhereOldSubstringWasFound,oldSubstring.length(),newSubstring);
endPosition = std::distance(sourceString.begin(),sourceString.end());
positionWhereOldSubstringWasFound += newSubstring.size();
positionWhereOldSubstringWasFound = std::distance(sourceString.begin(),
std::search(sourceString.begin() + positionWhereOldSubstringWasFound,
sourceString.end(),
oldSubstring.begin(),
oldSubstring.end()));
}
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// End of namespace
}
//-------------------------------------------------------------------
#endif // BL_STRINGSMANIPULATION_HPP