-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdtoken.h
74 lines (63 loc) · 1.8 KB
/
cmdtoken.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef __cmdtoken_H__
#define __cmdtoken_H__
#include <Arduino.h>
#include <stdint.h>
#include <string.h>
namespace Cmd
{
class Token
{
public:
Token( const char *start = nullptr, uint8_t length = 0 );
inline const char* start() const;
inline void setStart( const char *start );
inline const char* end() const;
inline uint8_t length() const;
inline void setLength( uint8_t length );
inline bool isValid() const;
inline void toString( char *string ) const;
inline bool operator== ( const char *string ) const;
inline bool operator== ( const __FlashStringHelper *string ) const;
inline bool operator!= ( const char *string ) const;
inline bool operator!= ( const __FlashStringHelper *string ) const;
private:
const char *m_start;
uint8_t m_length;
};
// ----- INLINE FUNCTIONS -----
const char* Token::start() const {
return m_start;
}
void Token::setStart( const char *start ) {
m_start = start;
}
const char* Token::end() const {
return m_start + m_length;
}
uint8_t Token::length() const {
return m_length;
}
void Token::setLength( uint8_t length ) {
m_length = length;
}
bool Token::isValid() const {
return 0 != m_length;
}
void Token::toString( char *string ) const {
strncpy( string, m_start, m_length );
string[ m_length ] = 0;
}
bool Token::operator== ( const char *string ) const {
return ( strlen( string ) == m_length ) && !strncmp( m_start, string, m_length );
}
bool Token::operator== ( const __FlashStringHelper *string ) const {
return ( strlen_P( reinterpret_cast< PGM_P >( string ) ) == m_length ) && !strncmp_P( m_start, reinterpret_cast< PGM_P >( string ), m_length );
}
bool Token::operator!= ( const char *string ) const {
return !operator==( string );
}
bool Token::operator!= ( const __FlashStringHelper *string ) const {
return !operator==( string );
}
}
#endif