AceRoutine  0.1
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
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 // Much of this copied from AUnit/src/aunit/FCString.h and
26 // AUnit/src/aunit/Compare.h.
27 
28 #include <string.h> // strcmp()
29 #include <Print.h> // Print
30 #include "Flash.h"
31 #include "FCString.h"
32 
33 namespace ace_routine {
34 
35 void FCString::print(Print& printer) const {
36  if (mString.cstring == nullptr) {
37  printer.print((unsigned long) this);
38  return;
39  }
40 
41  if (mStringType == kCStringType) {
42  printer.print(getCString());
43  } else {
44  printer.print(getFString());
45  }
46 }
47 
48 void FCString::println(Print& printer) const {
49  if (mString.cstring == nullptr) {
50  printer.println((unsigned long) this);
51  return;
52  }
53 
54  if (mStringType == kCStringType) {
55  printer.println(getCString());
56  } else {
57  printer.println(getFString());
58  }
59 }
60 
61 namespace internal {
62 
63 int compareString(const char* a, const char* b) {
64  if (a == b) { return 0; }
65  if (a == nullptr) { return -1; }
66  if (b == nullptr) { return 1; }
67  return strcmp(a, b);
68 }
69 
70 int compareString(const char* a, const __FlashStringHelper* b) {
71  if (a == (const char*) b) { return 0; }
72  if (a == nullptr) { return -1; }
73  if (b == nullptr) { return 1; }
74  return strcmp_P(a, (const char*) b);
75 }
76 
77 int compareString(const __FlashStringHelper* a, const char* b) {
78  return -compareString(b, a);
79 }
80 
81 int compareString(const __FlashStringHelper* a, const __FlashStringHelper* b) {
82  if (a == b) { return 0; }
83  if (a == nullptr) { return -1; }
84  if (b == nullptr) { return 1; }
85  const char* aa = reinterpret_cast<const char*>(a);
86  const char* bb = reinterpret_cast<const char*>(b);
87 
88  while (true) {
89  uint8_t ca = pgm_read_byte(aa);
90  uint8_t cb = pgm_read_byte(bb);
91  if (ca != cb) return (int) ca - (int) cb;
92  if (ca == '\0') return 0;
93  aa++;
94  bb++;
95  }
96 }
97 
98 }
99 
100 int FCString::compareTo(const FCString& that) const {
101  if (getType() == FCString::kCStringType) {
102  if (that.getType() == FCString::kCStringType) {
103  return internal::compareString(getCString(), that.getCString());
104  } else {
105  return internal::compareString(getCString(), that.getFString());
106  }
107  } else {
108  if (that.getType() == FCString::kCStringType) {
109  return internal::compareString(getFString(), that.getCString());
110  } else {
111  return internal::compareString(getFString(), that.getFString());
112  }
113  }
114 }
115 
116 }
const __FlashStringHelper * getFString() const
Get the flash string pointer.
Definition: FCString.h:83
uint8_t getType() const
Get the internal type of string.
Definition: FCString.h:77
void print(Print &printer) const
Convenience method for printing an FCString.
Definition: FCString.cpp:35
A union of (const char*) and (const __FlashStringHelper*) with a discriminator.
Definition: FCString.h:56
void println(Print &printer) const
Convenience method for printing an FCString.
Definition: FCString.cpp:48
const char * getCString() const
Get the c-string pointer.
Definition: FCString.h:80
int compareTo(const FCString &that) const
Compare to another FCString.
Definition: FCString.cpp:100
Various macros to smooth over the differences among the various platforms with regards to their suppo...