AceCommon  1.4.5
Arduino library for low-level common functions and features with no external dependencies
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 <stdint.h> // uintptr_t
29 #include <string.h> // strcmp()
30 #include <Print.h> // Print
31 #include "FCString.h"
32 
33 namespace ace_common {
34 
35 size_t FCString::printTo(Print& printer) const {
36  if (mString.cstring == nullptr) {
37  return printer.print((uintptr_t) this);
38  }
39 
40  if (mStringType == kCStringType) {
41  return printer.print(getCString());
42  } else {
43  return printer.print(getFString());
44  }
45 }
46 
47 namespace internal {
48 
49 int compareString(const char* a, const char* b) {
50  if (a == b) { return 0; }
51  if (a == nullptr) { return -1; }
52  if (b == nullptr) { return 1; }
53  return strcmp(a, b);
54 }
55 
56 int compareString(const char* a, const __FlashStringHelper* b) {
57  if (a == (const char*) b) { return 0; }
58  if (a == nullptr) { return -1; }
59  if (b == nullptr) { return 1; }
60  return strcmp_P(a, (const char*) b);
61 }
62 
63 int compareString(const __FlashStringHelper* a, const char* b) {
64  return -compareString(b, a);
65 }
66 
67 int compareString(const __FlashStringHelper* a, const __FlashStringHelper* b) {
68  if (a == b) { return 0; }
69  if (a == nullptr) { return -1; }
70  if (b == nullptr) { return 1; }
71  const char* aa = reinterpret_cast<const char*>(a);
72  const char* bb = reinterpret_cast<const char*>(b);
73 
74  while (true) {
75  uint8_t ca = pgm_read_byte(aa);
76  uint8_t cb = pgm_read_byte(bb);
77  if (ca != cb) return (int) ca - (int) cb;
78  if (ca == '\0') return 0;
79  aa++;
80  bb++;
81  }
82 }
83 
84 }
85 
86 int FCString::compareTo(const FCString& that) const {
88  if (that.getType() == FCString::kCStringType) {
89  return internal::compareString(getCString(), that.getCString());
90  } else {
91  return internal::compareString(getCString(), that.getFString());
92  }
93  } else {
94  if (that.getType() == FCString::kCStringType) {
95  return internal::compareString(getFString(), that.getCString());
96  } else {
97  return internal::compareString(getFString(), that.getFString());
98  }
99  }
100 }
101 
102 }
ace_common::FCString::getCString
const char * getCString() const
Get the c-string pointer.
Definition: FCString.h:77
ace_common::FCString::printTo
size_t printTo(Print &printer) const
Convenience method for printing an FCString to printer.
Definition: FCString.cpp:35
ace_common::FCString
A union of (const char*) and (const __FlashStringHelper*) with a discriminator.
Definition: FCString.h:47
ace_common::FCString::compareTo
int compareTo(const FCString &that) const
Compare to another FCString.
Definition: FCString.cpp:86
ace_common::FCString::getFString
const __FlashStringHelper * getFString() const
Get the flash string pointer.
Definition: FCString.h:80
ace_common::FCString::getType
uint8_t getType() const
Get the internal type of string.
Definition: FCString.h:74
ace_common::FCString::kCStringType
static const uint8_t kCStringType
Identifies the object as holding a c-string.
Definition: FCString.h:50