Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FlashString abstract class #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 9 additions & 33 deletions api/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,6 @@ size_t Print::write(const uint8_t *buffer, size_t size)
return n;
}

size_t Print::print(const __FlashStringHelper *ifsh)
{
#if defined(__AVR__)
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
size_t n = 0;
while (1) {
unsigned char c = pgm_read_byte(p++);
if (c == 0) break;
if (write(c)) n++;
else break;
}
return n;
#else
return print(reinterpret_cast<const char *>(ifsh));
#endif
}

size_t Print::print(const String &s)
{
return write(s.c_str(), s.length());
Expand Down Expand Up @@ -132,13 +115,6 @@ size_t Print::print(double n, int digits)
return printFloat(n, digits);
}

size_t Print::println(const __FlashStringHelper *ifsh)
{
size_t n = print(ifsh);
n += println();
return n;
}

size_t Print::print(const Printable& x)
{
return x.printTo(*this);
Expand Down Expand Up @@ -330,15 +306,15 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
return bytes;
}

size_t Print::printFloat(double number, uint8_t digits)
{
size_t Print::printFloat(double number, uint8_t digits)
{
size_t n = 0;

if (isnan(number)) return print("nan");
if (isinf(number)) return print("inf");
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically

// Handle negative numbers
if (number < 0.0)
{
Expand All @@ -350,7 +326,7 @@ size_t Print::printFloat(double number, uint8_t digits)
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;

number += rounding;

// Extract the integer part of the number and print it
Expand All @@ -360,7 +336,7 @@ size_t Print::printFloat(double number, uint8_t digits)

// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
n += print(".");
n += print('.');
}

// Extract digits from the remainder one at a time
Expand All @@ -369,8 +345,8 @@ size_t Print::printFloat(double number, uint8_t digits)
remainder *= 10.0;
unsigned int toPrint = (unsigned int)remainder;
n += print(toPrint);
remainder -= toPrint;
}

remainder -= toPrint;
}
return n;
}
4 changes: 1 addition & 3 deletions api/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ class Print
size_t write(const char *buffer, size_t size) {
return write((const uint8_t *)buffer, size);
}

size_t print(const __FlashStringHelper *);

size_t print(const String &);
size_t print(const char[]);
size_t print(char);
Expand All @@ -68,7 +67,6 @@ class Print
size_t print(double, int = 2);
size_t print(const Printable&);

size_t println(const __FlashStringHelper *);
size_t println(const String &s);
size_t println(const char[]);
size_t println(char);
Expand Down
38 changes: 9 additions & 29 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ String::String(const String &value)
*this = value;
}

String::String(const __FlashStringHelper *pstr)
String::String(const FlashString &fstr)
{
init();
*this = pstr;
init();
*this = fstr;
}

#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
Expand Down Expand Up @@ -180,17 +180,6 @@ String & String::copy(const char *cstr, unsigned int length)
return *this;
}

String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
{
if (!reserve(length)) {
invalidate();
return *this;
}
len = length;
strcpy_P(buffer, (PGM_P)pstr);
return *this;
}

#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
void String::move(String &rhs)
{
Expand Down Expand Up @@ -245,11 +234,9 @@ String & String::operator = (const char *cstr)
return *this;
}

String & String::operator = (const __FlashStringHelper *pstr)
String & String::operator = (const FlashString &fstr)
{
if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
else invalidate();

*this = fstr.toString();
return *this;
}

Expand Down Expand Up @@ -336,16 +323,9 @@ unsigned char String::concat(double num)
return concat(string, strlen(string));
}

unsigned char String::concat(const __FlashStringHelper * str)
unsigned char String::concat(const FlashString &fstr)
{
if (!str) return 0;
int length = strlen_P((const char *) str);
if (length == 0) return 1;
unsigned int newlen = len + length;
if (!reserve(newlen)) return 0;
strcpy_P(buffer + len, (const char *) str);
len = newlen;
return 1;
return concat(fstr.toString());
}

/*********************************************/
Expand Down Expand Up @@ -422,7 +402,7 @@ StringSumHelper & operator + (const StringSumHelper &lhs, double num)
return a;
}

StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
StringSumHelper & operator + (const StringSumHelper &lhs, const FlashString &rhs)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(rhs)) a.invalidate();
Expand Down Expand Up @@ -592,7 +572,7 @@ int String::lastIndexOf(const String &s2) const

int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
{
if (s2.len == 0 || len == 0 || s2.len > len) return -1;
if (s2.len == 0 || len == 0 || s2.len > len) return -1;
if (fromIndex >= len) fromIndex = len - 1;
int found = -1;
for (char *p = buffer; p <= buffer + fromIndex; p++) {
Expand Down
39 changes: 26 additions & 13 deletions api/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,37 @@

#ifdef __cplusplus

#include "Printable.h"

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#if defined(__AVR__)
#include "avr/pgmspace.h"
#else
#include "deprecated-avr-comp/avr/pgmspace.h"
#endif

// When compiling programs with this class, the following gcc parameters
// dramatically increase performance and memory (RAM) efficiency, typically
// with little or no increase in code size.
// -felide-constructors
// -std=c++0x

class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
class String;

// Default implemtation of 'F' macro for cores than can use
// string literals directly as (const char *).
// For cores that require special interface to program memory,
// the default 'F' macro can be undefined at core
// implementation level and redefined as needed.
#define F(string_literal) (string_literal)

// Base Class for strings stored in program memory.
// For cores that require special interface to program memory,
// this class can be extended at the core implementation
// level in order to support String and Print operations from
// program memory.
class FlashString : public Printable
{
public:
virtual String toString() const = 0;
};

// An inherited class for holding the result of a concatenation. These
// result objects are assumed to be writable by subsequent concatenations.
Expand All @@ -62,7 +76,7 @@ class String
// be false).
String(const char *cstr = "");
String(const String &str);
String(const __FlashStringHelper *str);
String(const FlashString &fstr);
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
String(String &&rval);
String(StringSumHelper &&rval);
Expand All @@ -89,7 +103,7 @@ class String
// marked as invalid ("if (s)" will be false).
String & operator = (const String &rhs);
String & operator = (const char *cstr);
String & operator = (const __FlashStringHelper *str);
String & operator = (const FlashString &fstr);
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
String & operator = (String &&rval);
String & operator = (StringSumHelper &&rval);
Expand All @@ -110,7 +124,7 @@ class String
unsigned char concat(unsigned long num);
unsigned char concat(float num);
unsigned char concat(double num);
unsigned char concat(const __FlashStringHelper * str);
unsigned char concat(const FlashString &fstr);

// if there's not enough memory for the concatenated value, the string
// will be left unchanged (but this isn't signalled in any way)
Expand All @@ -124,7 +138,7 @@ class String
String & operator += (unsigned long num) {concat(num); return (*this);}
String & operator += (float num) {concat(num); return (*this);}
String & operator += (double num) {concat(num); return (*this);}
String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
String & operator += (const FlashString &fstr){concat(fstr); return (*this);}

friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
Expand All @@ -136,7 +150,7 @@ class String
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
friend StringSumHelper & operator + (const StringSumHelper &lhs, const FlashString &rhs);

// comparison (only works w/ Strings and "strings")
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
Expand Down Expand Up @@ -222,7 +236,6 @@ class String

// copy and move
String & copy(const char *cstr, unsigned int length);
String & copy(const __FlashStringHelper *pstr, unsigned int length);
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
void move(String &rhs);
#endif
Expand Down
Loading