AceCommon  1.6.1
Arduino library for low-level common functions and features with no external dependencies
copyReplace.cpp
1 /*
2 MIT License
3 
4 Copyright (c) 2021 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #include <Arduino.h> // pgm_read_byte()
26 #include "copyReplace.h"
27 
28 namespace ace_common {
29 
30 void copyReplaceChar(char* dst, size_t dstSize, const char* src,
31  char oldChar, char newChar) {
32  char c;
33  while ((c = *src++) != '\0' && dstSize > 0) {
34  if (c == oldChar) {
35  if (newChar == '\0') continue;
36  c = newChar;
37  }
38  *dst++ = c;
39  dstSize--;
40  }
41 
42  if (dstSize == 0) {
43  --dst;
44  }
45  *dst = '\0';
46 }
47 
48 void copyReplaceChar(char* dst, size_t dstSize, const __FlashStringHelper* src,
49  char oldChar, char newChar) {
50  char c;
51  const char* s = (const char*) src;
52  while ((c = (char) pgm_read_byte(s++)) != '\0' && dstSize > 0) {
53  if (c == oldChar) {
54  if (newChar == '\0') continue;
55  c = newChar;
56  }
57  *dst++ = c;
58  dstSize--;
59  }
60 
61  if (dstSize == 0) {
62  --dst;
63  }
64  *dst = '\0';
65 }
66 
67 // 4 overloaded versions of copyReplaceString() below, from the 2 types of `src`
68 // and 2 types of `newString`.
69 
70 void copyReplaceString(char* dst, size_t dstSize, const char* src,
71  char oldChar, const char* newString) {
72  char c;
73  while ((c = *src++) != '\0' && dstSize > 0) {
74  if (c == oldChar) {
75  const char* s = newString;
76  while (*s != '\0' && dstSize > 0) {
77  *dst++ = *s++;
78  dstSize--;
79  }
80  } else {
81  *dst++ = c;
82  dstSize--;
83  }
84  }
85 
86  if (dstSize == 0) {
87  --dst;
88  }
89  *dst = '\0';
90 }
91 
92 void copyReplaceString(char* dst, size_t dstSize, const char* src,
93  char oldChar, const __FlashStringHelper* newString) {
94  char c;
95  while ((c = *src++) != '\0' && dstSize > 0) {
96  if (c == oldChar) {
97  const char* s = (const char*) newString;
98  while ((char) pgm_read_byte(s) != '\0' && dstSize > 0) {
99  *dst++ = (char) pgm_read_byte(s++);
100  dstSize--;
101  }
102  } else {
103  *dst++ = c;
104  dstSize--;
105  }
106  }
107 
108  if (dstSize == 0) {
109  --dst;
110  }
111  *dst = '\0';
112 }
113 
114 void copyReplaceString(char* dst, size_t dstSize,
115  const __FlashStringHelper* src, char oldChar, const char* newString) {
116  char c;
117  const char* ss = (const char*) src;
118  while ((c = (char) pgm_read_byte(ss++)) != '\0' && dstSize > 0) {
119  if (c == oldChar) {
120  const char* s = (const char*) newString;
121  while (*s != '\0' && dstSize > 0) {
122  *dst++ = *s++;
123  dstSize--;
124  }
125  } else {
126  *dst++ = c;
127  dstSize--;
128  }
129  }
130 
131  if (dstSize == 0) {
132  --dst;
133  }
134  *dst = '\0';
135 }
136 
137 void copyReplaceString(char* dst, size_t dstSize,
138  const __FlashStringHelper* src, char oldChar,
139  const __FlashStringHelper* newString) {
140  char c;
141  const char* ss = (const char*) src;
142  while ((c = (char) pgm_read_byte(ss++)) != '\0' && dstSize > 0) {
143  if (c == oldChar) {
144  const char* s = (const char*) newString;
145  while ((char) pgm_read_byte(s) != '\0' && dstSize > 0) {
146  *dst++ = (char) pgm_read_byte(s++);
147  dstSize--;
148  }
149  } else {
150  *dst++ = c;
151  dstSize--;
152  }
153  }
154 
155  if (dstSize == 0) {
156  --dst;
157  }
158  *dst = '\0';
159 }
160 
161 }
Functions that copy c-strings from src to dst while replacing a given character with another characte...