AceCommon  1.6.0
Arduino library for low-level common functions and features with no external dependencies
KString.h
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 #ifndef ACE_COMMON_KSTRING_H
26 #define ACE_COMMON_KSTRING_H
27 
28 #include <stdint.h> // int8_t
29 class __FlashStringHelper;
30 class Print;
31 
32 namespace ace_common {
33 
34 class KStringIterator;
35 
53 class KString {
54  friend class KStringIterator;
55  friend class KStringKeywords;
56 
57  public:
67  explicit KString(
68  const char* s,
69  const char* const* keywords,
70  uint8_t numKeywords
71  ):
72  string_(s),
73  keywords_((const void* const*) keywords),
74  type_(kTypeCstring),
75  numKeywords_(numKeywords > 0x20 ? 0x20 : numKeywords)
76  {}
77 
86  explicit KString(
87  const __FlashStringHelper* fs,
88  const __FlashStringHelper* const* keywords,
89  uint8_t numKeywords
90  ):
91  string_(fs),
92  keywords_((const void* const*) keywords),
93  type_(kTypeFstring),
94  numKeywords_(numKeywords > 0x20 ? 0x20 : numKeywords)
95  {}
96 
103  int compareTo(const char* s);
104 
110  int compareTo(const KString& s);
111 
113  void printTo(Print& printer);
114 
115  private:
116  static const uint8_t kTypeCstring = 0;
117  static const uint8_t kTypeFstring = 1;
118 
119  // The order of the following fields is deliberate to reduce the memory
120  // size of this class on 32-bit processors.
121  const void* const string_;
122  const void* const* const keywords_;
123  uint8_t const type_;
124  uint8_t const numKeywords_;
125 };
126 
136  public:
139  ks_(ks),
140  firstPtr_((const char*) ks.string_),
141  secondPtr_(nullptr)
142  {}
143 
153  char get();
154 
156  void next() { firstPtr_++; }
157 
158  private:
159  static char getInternal(uint8_t type, const char* p) {
160  return (type == KString::kTypeCstring) ? *p : pgm_read_byte(p);
161  }
162 
163  private:
164  // ordering of fields optimized to reduce gaps on 32-bit processors
165  const KString& ks_;
166  const char* firstPtr_;
167  const char* secondPtr_;
168 };
169 
176  public:
183  KStringKeywords(uint8_t type, const void* const* keywords) :
184  type_(type),
185  keywords_(keywords)
186  {}
187 
193  const char* get(uint8_t i) const {
194  if (type_ == KString::kTypeCstring) {
195  auto words = (const char* const*) keywords_;
196  return words[i];
197  } else {
198  auto words = (const __FlashStringHelper* const*) keywords_;
199  return (const char*) pgm_read_ptr(words + i);
200  }
201  }
202 
203  private:
204  uint8_t type_;
205  const void* const* keywords_;
206 };
207 
208 } // ace_common
209 
210 #endif
An interator that points to a character inside a KString.
Definition: KString.h:135
KStringIterator(const KString &ks)
Constructor.
Definition: KString.h:138
void next()
Advance the iterator one character,.
Definition: KString.h:156
char get()
Return the current character referenced by the iterator.
Definition: KString.cpp:109
A thin helper object around an array of const char* in regular memory, or an array of const __FlashSt...
Definition: KString.h:175
KStringKeywords(uint8_t type, const void *const *keywords)
Definition: KString.h:183
const char * get(uint8_t i) const
Return the string pointer of index i.
Definition: KString.h:193
A wrapper class around a normal c-string or Arduino f-string which is encoded and compressed using ke...
Definition: KString.h:53
void printTo(Print &printer)
Expand and print the current string to the given printer.
Definition: KString.cpp:83
int compareTo(const char *s)
Compare this string against a c-string s and return <0, 0 or >0 if this string is <,...
Definition: KString.cpp:7
KString(const char *s, const char *const *keywords, uint8_t numKeywords)
Constructor around a simple c-string.
Definition: KString.h:67
KString(const __FlashStringHelper *fs, const __FlashStringHelper *const *keywords, uint8_t numKeywords)
Constructor around an Arduino Flash string.
Definition: KString.h:86