2
PString.h - Lightweight printable string class
3
Copyright (c) 2009-2012 Mikal Hart. All right reserved.
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
15
You should have received a copy of the GNU Lesser General Public
16
License along with this library; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29
#define PSTRING_LIBRARY_VERSION 3
31
class PString : public Print
37
#if defined(ARDUINO) && ARDUINO >= 100
38
virtual size_t write(uint8_t);
40
virtual void write(uint8_t);
45
// Basic constructor requires a preallocated buffer
46
PString(char *buf, size_t size) : _buf(buf), _size(size)
49
// Constructor that doesn't clear the supplied buffer
50
PString(char *buf, size_t size, bool)
51
{ begin( buf, size ); }
53
// templated constructors allow inline renderings of this type: PString(buf, size, myfloat[, modifier]);
54
template<class T> PString(char *buf, size_t size, T arg) : _buf(buf), _size(size)
55
{ begin(); print(arg); }
57
template<class T> PString(char *buf, size_t size, T arg, int modifier) : _buf(buf), _size(size)
58
{ begin(); print(arg, modifier); }
60
// returns the length of the current string, not counting the 0 terminator
61
inline const size_t length()
62
{ return _cur - _buf; }
64
// returns the capacity of the string
65
inline const size_t capacity()
68
// gives access to the internal string
69
inline operator const char *()
72
// compare to another string
73
bool operator==(const char *str)
74
{ return _size > 0 && !strcmp(_buf, str); }
76
// call this to re-use an existing string
79
// call this to re-use an existing string and change where the buffer lives
80
void begin(char *, size_t size);
82
// This function allows assignment to an arbitrary scalar value like str = myfloat;
83
template<class T> inline PString &operator =(T arg)
84
{ begin(); print(arg); return *this; }
86
// Concatenation str += myfloat;
87
template<class T> inline PString &operator +=(T arg)
88
{ print(arg); return *this; }
90
// Safe access to sprintf-like formatting, e.g. str.format("Hi, my name is %s and I'm %d years old", name, age);
91
int format(char *str, ...);