AUnit  1.7.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
FCString.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 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 AUNIT_FSTRING_H
26 #define AUNIT_FSTRING_H
27 
28 #include <stddef.h> // size_t
29 
30 class Print;
31 class __FlashStringHelper;
32 
33 namespace aunit {
34 namespace internal {
35 
58 class FCString {
59  public:
60  static const uint8_t kCStringType = 0;
61  static const uint8_t kFStringType = 1;
62 
64  FCString() {}
65 
67  explicit FCString(const char* s):
68  mStringType(kCStringType) {
69  mString.cstring = s;
70  }
71 
73  explicit FCString(const __FlashStringHelper* s):
74  mStringType(kFStringType) {
75  mString.fstring = s;
76  }
77 
79  uint8_t getType() const { return mStringType; }
80 
82  const char* getCString() const { return mString.cstring; }
83 
85  const __FlashStringHelper* getFString() const { return mString.fstring; }
86 
88  void print(Print* printer) const;
89 
91  void println(Print* printer) const;
92 
94  int compareTo(const FCString& that) const;
95 
101  int compareToN(const char* that, size_t n) const;
102 
108  int compareToN(const __FlashStringHelper* that, size_t n) const;
109 
111  bool hasSubstring(const char* substring) const;
112 
113  private:
114  // NOTE: It might be possible just use a (void *) instead of a union.
115  union {
116  const char* cstring;
117  const __FlashStringHelper* fstring;
118  } mString = { nullptr };
119 
120  uint8_t mStringType = kCStringType;
121 };
122 
123 }
124 }
125 
126 #endif
A union of (const char*) and (const __FlashStringHelper*) with a discriminator.
Definition: FCString.h:58
const char * getCString() const
Get the c-string pointer.
Definition: FCString.h:82
FCString()
Default constructor initializes to a nullptr of kCStringType.
Definition: FCString.h:64
FCString(const __FlashStringHelper *s)
Construct with a flash string.
Definition: FCString.h:73
uint8_t getType() const
Get the internal type of string.
Definition: FCString.h:79
bool hasSubstring(const char *substring) const
Determine if given substring exists.
Definition: FCString.cpp:87
const __FlashStringHelper * getFString() const
Get the flash string pointer.
Definition: FCString.h:85
int compareToN(const char *that, size_t n) const
Compare to C-string using the first n characters.
Definition: FCString.cpp:71
int compareTo(const FCString &that) const
Compare to another FCString.
Definition: FCString.cpp:55
void print(Print *printer) const
Convenience method for printing an FCString.
Definition: FCString.cpp:32
FCString(const char *s)
Construct with a c-string.
Definition: FCString.h:67
void println(Print *printer) const
Convenience method for printing an FCString.
Definition: FCString.cpp:42