-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.cpp
More file actions
83 lines (75 loc) · 2.23 KB
/
replace.cpp
File metadata and controls
83 lines (75 loc) · 2.23 KB
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
#include "replace.h"
template <typename T, typename T2>
std::string pyfunc::replace(std::string source, T search, T2 replacement) {
std::string search_ = (std::string)"" + search;
std::string replacement_ = (std::string)"" + replacement;
std::string replaced_string = "";
int search_len = search_.length();
int end_index = 0;
while ((end_index = source.find(search_)) != -1) {
replaced_string += source.substr(0, end_index) + replacement_;
source = source.substr(end_index + search_len);
}
replaced_string += source;
return replaced_string;
}
template <typename T>
std::vector<T> pyfunc::replace(std::vector<T> source, T search, T replacement) {
for (int i = 0; i < source.size(); i++) {
if (source[i] == search) {
source[i] = replacement;
}
}
return source;
}
template <typename T>
T* pyfunc::replace(T* source, size_t size, T search, T replacement) {
T* replaced_array;
if (typeid(T) == typeid(char)) {
replaced_array = new T[size + 1];
replaced_array[size] = '\0';
}
else {
replaced_array = new T[size];
}
for (size_t index = 0; index < size; index++) {
if (*source == search) {
replaced_array[index] = replacement;
}
else {
replaced_array[index] = *source;
}
source++;
}
return replaced_array;
}
template <typename T>
T* pyfunc::replace(const T* source, size_t size, T search, T replacement) {
T* replaced_array;
if (typeid(T) == typeid(char)) {
replaced_array = new T[size + 1];
replaced_array[size] = '\0';
}
else {
replaced_array = new T[size];
}
for (size_t index = 0; index < size; index++) {
if (*source == search) {
replaced_array[index] = replacement;
}
else {
replaced_array[index] = *source;
}
source++;
}
return replaced_array;
}
template <typename T, size_t size>
std::array<T, size> pyfunc::replace(std::array<T, size> source, T search, T replacement) {
for (size_t index = 0; index < size; index++) {
if (source[index] == search) {
source[index] = replacement;
}
}
return source;
}