AceCommon  1.5.2
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 
56  public:
66  explicit KString(
67  const char* s,
68  const char* const* keywords,
69  uint8_t numKeywords
70  ):
71  string_(s),
72  keywords_(keywords),
73  type_(KString::kTypeCstring),
74  numKeywords_(numKeywords > 0x20 ? 0x20 : numKeywords)
75  {}
76 
78  explicit KString(
79  const __FlashStringHelper* fs,
80  const char* const* keywords,
81  uint8_t numKeywords
82  ):
83  string_(fs),
84  keywords_(keywords),
85  type_(kTypeFstring),
86  numKeywords_(numKeywords > 0x20 ? 0x20 : numKeywords)
87  {}
88 
95  int compareTo(const char* s);
96 
102  int compareTo(const KString& s);
103 
105  void printTo(Print& printer);
106 
107  private:
108  static const uint8_t kTypeCstring = 0;
109  static const uint8_t kTypeFstring = 1;
110 
111  // The order of the following fields is deliberate to reduce the memory
112  // size of this class on 32-bit processors.
113  const void* const string_;
114  const char* const* const keywords_;
115  uint8_t const type_;
116  uint8_t const numKeywords_;
117 };
118 
128  public:
131  ks_(ks),
132  firstType_(ks.type_),
133  secondType_(KString::kTypeCstring),
134  firstPtr_((const char*) ks.string_),
135  secondPtr_(nullptr)
136  {}
137 
147  char get() {
148  // We don't support recursive compression fragments (i.e. compress tokens
149  // within fragments) so this does NOT need to be a loop.
150  char c = getInternal(firstType_, firstPtr_);
151  if (c == '\0') {
152  if (secondPtr_ != nullptr) {
153  // pop the stack
154  firstType_ = secondType_;
155  firstPtr_ = secondPtr_;
156  secondPtr_ = nullptr;
157 
158  // advance one character
159  firstPtr_++;
160  c = getInternal(firstType_, firstPtr_);
161  }
162  }
163 
164  if (c != '\0' && c < 0x20) { // fragment keyword string
165  // push the stack
166  secondType_ = firstType_;
167  secondPtr_ = firstPtr_;
168  firstType_ = KString::kTypeCstring;
169  firstPtr_ = ks_.keywords_[(uint8_t) c];
170  c = getInternal(firstType_, firstPtr_);
171  }
172  return c;
173  }
174 
176  void next() { firstPtr_++; }
177 
178  private:
179  static char getInternal(uint8_t type, const char* p) {
180  return (type == KString::kTypeCstring) ? *p : pgm_read_byte(p);
181  }
182 
183  private:
184  // ordering of fields optimized to reduce gaps on 32-bit processors
185  const KString& ks_;
186  uint8_t firstType_;
187  uint8_t secondType_;
188  const char* firstPtr_;
189  const char* secondPtr_;
190 };
191 
192 } // ace_common
193 
194 #endif
An interator that points to a character inside a KString.
Definition: KString.h:127
KStringIterator(const KString &ks)
Constructor.
Definition: KString.h:130
void next()
Advance the iterator one character,.
Definition: KString.h:176
char get()
Return the current character referenced by the iterator.
Definition: KString.h:147
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:66
KString(const __FlashStringHelper *fs, const char *const *keywords, uint8_t numKeywords)
Constructor around an Arduino Flash string.
Definition: KString.h:78