AceCommon  1.4.5
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 
51 class KString {
52  public:
61  explicit KString(
62  const char* s,
63  const char* const* keywords,
64  uint8_t numKeywords
65  ):
66  string_(s),
67  keywords_(keywords),
68  type_(kTypeCstring),
69  numKeywords_(numKeywords > 0x20 ? 0x20 : numKeywords)
70  {}
71 
73  explicit KString(
74  const __FlashStringHelper* fs,
75  const char* const* keywords,
76  uint8_t numKeywords
77  ):
78  string_(fs),
79  keywords_(keywords),
80  type_(kTypeFstring),
81  numKeywords_(numKeywords > 0x20 ? 0x20 : numKeywords)
82  {}
83 
88  int compareTo(const char* s);
89 
91  void printTo(Print& printer);
92 
93  private:
94  static const uint8_t kTypeCstring = 0;
95  static const uint8_t kTypeFstring = 1;
96 
97  // The order of the following fields is deliberate to reduce the memory
98  // size of this class on 32-bit processors.
99  const void* const string_;
100  const char* const* const keywords_;
101  uint8_t const type_;
102  uint8_t const numKeywords_;
103 };
104 
105 } // ace_common
106 
107 #endif
ace_common::KString::printTo
void printTo(Print &printer)
Expand and print the current string to the given printer.
Definition: KString.cpp:74
ace_common::KString::KString
KString(const char *s, const char *const *keywords, uint8_t numKeywords)
Constructor around a simple c-string.
Definition: KString.h:61
ace_common::KString
A wrapper class around a normal c-string or Arduino f-string which is encoded and compressed using ke...
Definition: KString.h:51
ace_common::KString::KString
KString(const __FlashStringHelper *fs, const char *const *keywords, uint8_t numKeywords)
Constructor around an Arduino Flash string.
Definition: KString.h:73
ace_common::KString::compareTo
int compareTo(const char *s)
Compare this string against s and return <0, 0 or >0 if this string is less than, equal to,...
Definition: KString.cpp:11