AUnit  1.7.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
FCString.cpp
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 #include <Print.h>
26 #include "Compare.h"
27 #include "FCString.h"
28 
29 namespace aunit {
30 namespace internal {
31 
32 void FCString::print(Print* printer) const {
33  if (mString.cstring == nullptr) return;
34 
35  if (mStringType == kCStringType) {
36  printer->print(getCString());
37  } else {
38  printer->print(getFString());
39  }
40 }
41 
42 void FCString::println(Print* printer) const {
43  if (mString.cstring == nullptr) {
44  printer->println();
45  return;
46  }
47 
48  if (mStringType == kCStringType) {
49  printer->println(getCString());
50  } else {
51  printer->println(getFString());
52  }
53 }
54 
55 int FCString::compareTo(const FCString& that) const {
56  if (getType() == FCString::kCStringType) {
57  if (that.getType() == FCString::kCStringType) {
58  return compareString(getCString(), that.getCString());
59  } else {
60  return compareString(getCString(), that.getFString());
61  }
62  } else {
63  if (that.getType() == FCString::kCStringType) {
64  return compareString(getFString(), that.getCString());
65  } else {
66  return compareString(getFString(), that.getFString());
67  }
68  }
69 }
70 
71 int FCString::compareToN(const char* that, size_t n) const {
72  if (getType() == FCString::kCStringType) {
73  return compareStringN(getCString(), that, n);
74  } else {
75  return compareStringN(getFString(), that, n);
76  }
77 }
78 
79 int FCString::compareToN(const __FlashStringHelper* that, size_t n) const {
80  if (getType() == FCString::kCStringType) {
81  return compareStringN(getCString(), that, n);
82  } else {
83  return compareStringN(getFString(), that, n);
84  }
85 }
86 
87 bool FCString::hasSubstring(const char* substring) const {
88  if (getType() == FCString::kCStringType) {
89  return compareSubstring(getCString(), substring);
90  } else {
91  return compareSubstring(getFString(), substring);
92  }
93 }
94 
95 }
96 }
This file provides overloaded compareXxx(a, b) functions which are used by the various assertXxx(a,...
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
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
void println(Print *printer) const
Convenience method for printing an FCString.
Definition: FCString.cpp:42