AUnit  1.0.0
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 
55 class FCString {
56  public:
57  static const uint8_t kCStringType = 0;
58  static const uint8_t kFStringType = 1;
59 
61  FCString() {}
62 
64  explicit FCString(const char* s):
65  mStringType(kCStringType) {
66  mString.cstring = s;
67  }
68 
70  explicit FCString(const __FlashStringHelper* s):
71  mStringType(kFStringType) {
72  mString.fstring = s;
73  }
74 
76  uint8_t getType() const { return mStringType; }
77 
79  const char* getCString() const { return mString.cstring; }
80 
82  const __FlashStringHelper* getFString() const { return mString.fstring; }
83 
85  void print(Print* printer) const;
86 
88  void println(Print* printer) const;
89 
91  int compareTo(const FCString& that) const;
92 
98  int compareToN(const char* that, size_t n) const;
99 
105  int compareToN(const __FlashStringHelper* that, size_t n) const;
106 
107  private:
108  // NOTE: It might be possible just use a (void *) instead of a union.
109  union {
110  const char* cstring;
111  const __FlashStringHelper* fstring;
112  } mString = { nullptr };
113 
114  uint8_t mStringType = kCStringType;
115 };
116 
117 }
118 }
119 
120 #endif
void println(Print *printer) const
Convenience method for printing an FCString.
Definition: FCString.cpp:42
FCString(const char *s)
Construct with a c-string.
Definition: FCString.h:64
int compareToN(const char *that, size_t n) const
Compare to C-string using the first n characters.
Definition: FCString.cpp:71
A union of (const char*) and (const __FlashStringHelper*) with a discriminator.
Definition: FCString.h:55
const __FlashStringHelper * getFString() const
Get the flash string pointer.
Definition: FCString.h:82
const char * getCString() const
Get the c-string pointer.
Definition: FCString.h:79
int compareTo(const FCString &that) const
Compare to another FCString.
Definition: FCString.cpp:55
FCString(const __FlashStringHelper *s)
Construct with a flash string.
Definition: FCString.h:70
FCString()
Default constructor initializes to a nullptr of kCStringType.
Definition: FCString.h:61
uint8_t getType() const
Get the internal type of string.
Definition: FCString.h:76
void print(Print *printer) const
Convenience method for printing an FCString.
Definition: FCString.cpp:32