AceCommon  1.4.6
Arduino library for low-level common functions and features with no external dependencies
printReplaceTo.h
Go to the documentation of this file.
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 
32 #ifndef ACE_COMMON_PRINT_REPLACE_TO_H
33 #define ACE_COMMON_PRINT_REPLACE_TO_H
34 
35 #include <stddef.h> // size_t
36 #include <Print.h> // Print
37 #include "../fstrings/FlashString.h"
38 
39 class Print;
40 class __FlashStringHelper;
41 
42 namespace ace_common {
43 
50 template <typename T>
51 void printReplaceCharTo(Print& printer, T src, char oldChar, char newChar) {
52  char c;
53  while ((c = *src++) != '\0') {
54  if (c == oldChar) {
55  if (newChar == '\0') continue;
56  c = newChar;
57  }
58  printer.write(c);
59  }
60 }
61 
66 // 'inline' required, see https://stackoverflow.com/questions/4445654
67 template<>
68 inline void printReplaceCharTo<const __FlashStringHelper*>(
69  Print& printer, const __FlashStringHelper* src,
70  char oldChar, char newChar) {
71  printReplaceCharTo<FlashString>(printer, FlashString(src), oldChar, newChar);
72 }
73 
80 template <typename T>
82  Print& printer, T src, char oldChar, const char* newString) {
83  char c;
84  while ((c = *src++) != '\0') {
85  if (c == oldChar) {
86  printer.print(newString);
87  } else {
88  printer.write(c);
89  }
90  }
91 }
92 
97 // 'inline' required, see https://stackoverflow.com/questions/4445654
98 template<>
99 inline void printReplaceStringTo<const __FlashStringHelper*>(
100  Print& printer, const __FlashStringHelper* src,
101  char oldChar, const char* newString) {
102  printReplaceStringTo<FlashString>(
103  printer, FlashString(src), oldChar, newString);
104 }
105 
106 } // ace_common
107 
108 #endif
ace_common::printReplaceStringTo
void printReplaceStringTo(Print &printer, T src, char oldChar, const char *newString)
Print the src to print while replacing all occurrence of oldChar with newString.
Definition: printReplaceTo.h:81
ace_common::printReplaceCharTo
void printReplaceCharTo(Print &printer, T src, char oldChar, char newChar)
Print the src to printer while replacing all occurrences of oldChar with newChar.
Definition: printReplaceTo.h:51
ace_common::FlashString
A thin wrapper around a (const __FlashStringHelper*) so that it acts exactly like a (const char*) wit...
Definition: FlashString.h:49