ArrayPrinter.hpp
Go to the documentation of this file.
1 /****
2  * ArrayPrinter.cpp - Print support for arrays
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the FlashString Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "Print.hpp"
23 #include <stringutil.h>
24 
25 namespace FSTR
26 {
27 template <typename T> typename std::enable_if<!std::is_same<T, char>::value, size_t>::type printElement(Print& p, T e)
28 {
29  return print(p, e);
30 }
31 
32 template <typename T> typename std::enable_if<std::is_same<T, char>::value, size_t>::type printElement(Print& p, T c)
33 {
34  auto escape = [](char c) -> char {
35  switch(c) {
36  case '\0':
37  return '0';
38  case '\'':
39  return '\'';
40  case '\"':
41  return '"';
42  case '\?':
43  return '?';
44  case '\\':
45  return '\\';
46  case '\a':
47  return 'a';
48  case '\b':
49  return 'b';
50  case '\f':
51  return 'f';
52  case '\n':
53  return 'n';
54  case '\r':
55  return 'r';
56  case '\t':
57  return 't';
58  case '\v':
59  return 'v';
60  default:
61  return '\0';
62  }
63  };
64 
65  char buf[8];
66  char* o = buf;
67  *o++ = '\'';
68  char esc = escape(c);
69  if(esc) {
70  *o++ = '\\';
71  *o++ = esc;
72  } else if(isprint(c)) {
73  *o++ = c;
74  } else {
75  *o++ = '\\';
76  *o++ = 'x';
77  *o++ = hexchar(uint8_t(c) >> 4);
78  *o++ = hexchar(uint8_t(c) & 0x0f);
79  }
80  *o++ = '\'';
81  return p.write(buf, o - buf);
82 }
83 
88 template <class ArrayType> class ArrayPrinter
89 {
90 public:
91  ArrayPrinter(const ArrayType& array) : array(array)
92  {
93  }
94 
95  size_t printTo(Print& p) const
96  {
97  size_t count = 0;
98 
99  count += p.print('{');
100  for(unsigned i = 0; i < array.length(); ++i) {
101  if(i > 0) {
102  count += p.print(", ");
103  }
104  count += printElement(p, array[i]);
105  }
106  count += p.print('}');
107 
108  return count;
109  }
110 
111 private:
112  const ArrayType& array;
113 };
114 
115 } // namespace FSTR
Class template to provide a simple way to print the contents of an array.
Definition: ArrayPrinter.hpp:89
size_t printTo(Print &p) const
Definition: ArrayPrinter.hpp:95
ArrayPrinter(const ArrayType &array)
Definition: ArrayPrinter.hpp:91
Provides formatted output to stream.
Definition: Print.h:37
virtual size_t write(uint8_t c)=0
Writes a single character to output stream.
size_t print(char c)
Prints a single character to output stream.
Definition: Print.h:97
Definition: Array.hpp:108
std::enable_if<!std::is_same< T, char >::value, size_t >::type printElement(Print &p, T e)
Definition: ArrayPrinter.hpp:27
std::enable_if< std::is_class< ObjectType >::value, size_t >::type print(Print &p, const ObjectType &object)
Print an object.
Definition: Print.hpp:40
static char hexchar(unsigned char c)
Definition: stringutil.h:53