This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Strings.cpp
82 lines (66 loc) · 1.59 KB
/
Strings.cpp
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
75
76
77
78
79
80
81
82
// some common operations on strings that are designed to work
// whether the strings are stored in AVR RAM or program memory
#include <Arduino.h>
#include "Strings.h"
char Strings::get_char(const char *p)
{
#if defined(pgm_read_byte)
if ((unsigned int)p >= PROGMEM_BOUNDARY) {
return (char)pgm_read_byte(p - PROGMEM_BOUNDARY);
} else
return *p;
#else
return *p;
#endif
}
unsigned int Strings::strlen(const char *s)
{
unsigned int len = 0;
while (get_char(s++))
++len;
return len;
}
void Strings::print(const char *s, unsigned int len)
{
Serial.print(len);
Serial.print(F(" \""));
while (len--)
Serial.print(get_char(s++));
Serial.print(F("\""));
}
// for null terminated strings
int Strings::strcmp(const char *s1, const char *s2)
{
char c1, c2;
while ((c1 = get_char(s1)) == (c2 = get_char(s2)))
{
if (!c1)
return 0;
s1++;
s2++;
}
return ((unsigned)c1 - (unsigned)c2);
}
// for strings with known length
int Strings::strcmp(const char *s1, unsigned int len1,
const char *s2, unsigned int len2)
{
if (len1 && len2)
{
while(--len1 && --len2 && (get_char(s1)==get_char(s2)))
{
s1++;
s2++;
}
return ((unsigned)get_char(s1)-(unsigned)get_char(s2));
}
if (!(len1 | len2))
return 0;
return (len1 ? 1 : -1);
}
char *Strings::strcpy(char *dst, const char *src)
{
while ((*dst = get_char(src++)))
++dst;
return dst;
}