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