Design Pattern Examples
Overview of object-oriented design patterns
replace.cpp
Go to the documentation of this file.
1
8
9#include <cstring>
10
11#include "replace.h"
12#include "strstri.h"
13
14namespace Helpers
15{
16
18 //
19 // Function : Replace
20 //
21 // Scope : Public
22 //
23 // Arguments : s = string to search
24 // str1 = pointer to string to search for
25 // str2 = pointer to string to replace with
26 // (can be an empty string)
27 // bCaseInsensitive = true if search is case insensitive
28 // false if search is case sensitive
29 // (default is false)
30 //
31 // Returns : new string containing the changes.
32 //
33 // Description: Replace all occurrences of string str1 with string str2 in s.
34 // If str1 is empty or either str1 or str2 are null, no
35 // replacement is made. If str2 is empty then all matches to
36 // str1 are effectively removed from the string.
37 //
38 // Notes : I'm using std::string throughout this function despite some
39 // of the obvious inefficiencies. A more efficient algorithm
40 // would work with a single buffer that increases as necessary
41 // to accommodate the changed string. However, that means
42 // managing the buffer myself. So I let std::string do all the
43 // work.
44 //
46
47 std::string Replace(
48 const std::string& s,
49 const char* str1,
50 const char* str2,
51 bool bCaseInsensitive/*=false*/)
52 {
53 std::string new_string(s);
54 size_t str1Len = (str1 != nullptr) ? strlen(str1) : 0;
55
56 // If there is something to do then
57 if (!s.empty() && str1Len > 0 && str2 != nullptr)
58 {
59 bool bReplaced = false;
60 std::string out; // build new string here first
61 const char* pStart = s.c_str();
62 const char* pEnd = pStart + s.length();
63
64 // Start at the beginning of the source string
65 // and continue to the end.
66 while (pStart < pEnd)
67 {
68 const char* pFound = nullptr;
69
70 // Look for next occurrence of str1
71 if (bCaseInsensitive)
72 {
73 pFound = strstri(pStart, str1);
74 }
75 else
76 {
77 pFound = strstr(pStart, str1);
78 }
79
80 // If str1 has been found, replace it
81 if (pFound != nullptr)
82 {
83 // First, append the stuff between the search start
84 // and the just found str1
85 out.append(pStart, pFound - pStart);
86 // "Insert" str2 into the string (this is the replacement)
87 out.append(str2);
88 // Update the search start to point to after the found str1
89 pStart = pFound + str1Len;
90 bReplaced = true;
91 }
92 else
93 {
94 // str1 not found, copy rest of source to out
95 // and terminate the loop.
96 if (pStart != nullptr)
97 {
98 out += pStart;
99 }
100 pStart = pEnd;
101 }
102 }
103
104 // If replacements made then set source string to new string.
105 if (bReplaced)
106 {
107 new_string = out;
108 }
109 }
110
111 return new_string;
112 }
113
114
116 //
117 // Function : Replace
118 //
119 // Scope : Public
120 //
121 // Arguments : s = string to search
122 // c1 = character to search for
123 // c2 = character to replace with
124 // bCaseInsensitive = true if search is case insensitive
125 // false if search is case sensitive
126 // (default is false)
127 //
128 // Returns : new string containing the changes.
129 //
130 // Description: Replace all occurrences of character c1 with character c2
131 // in s.
132 //
133 // Notes : I'm reusing the Replace() for strings instead of
134 // std::replace_if() because using the former is cleaner and
135 // easier to maintain than adding two functors to satisfy the
136 // requirements for std::replace_if().
137 //
139
140 std::string Replace(
141 const std::string& s,
142 char c1,
143 char c2,
144 bool bCaseInsensitive/*=false*/)
145 {
146 std::string str1(1, c1);
147 std::string str2(1, c2);
148 return (Replace(s, str1.c_str(), str2.c_str(), bCaseInsensitive));
149 }
150}; // end namespace
Declaration of the Replace() functions, for replacing characters and strings in a string.
Declaration of the strstri function, case-insensitive string search for narrow character strings.
The namespace containing all the "helper" functions in the C++ code.
std::string Replace(const std::string &s, const char *str1, const char *str2, bool bCaseInsensitive)
Replace all occurrences of narrow string str1 with narrow string str2 in s. If str2 is empty then all...
Definition: replace.cpp:47
char * strstri(const char *s1, const char *s2)
Do case-insensitive search for string 2 (s2) in string 1 (s1). Similar to the C library's strstr().
Definition: strstri.cpp:35