-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstrings.cpp
More file actions
163 lines (137 loc) · 4.48 KB
/
strings.cpp
File metadata and controls
163 lines (137 loc) · 4.48 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*******************************************************************************
strings.cpp
(c) 2010 by Sophie Dexter
This C++ module provides functions for working with 'strings' of ascii 'char's
C++ should have functions like these, but it doesn't seem to so these are my
own ones. They are very simple and do not check for any errors e.g. StrCpy
does not check that s1 is big enough to store all of s2.
********************************************************************************
WARNING: Use at your own risk, sadly this software comes with no guarantees.
This software is provided 'free' and in good faith, but the author does not
accept liability for any damage arising from its use.
*******************************************************************************/
#include "strings.h"
// copies a string, s2, (array of chars) to another string, s1.
char *StrCpy(char *s1, char *s2) {
// while (*s1++ = *s2++);
while (*s2)
*s1++ = *s2++;
return s1;
}
// returns the number of chars in a string
int StrLen(char *s) {
int x = 0;
while (*s++)
x++;
return (x);
}
// checks s1 to see if it the same as s2
// returns TRUE if there is a match
// WARNING actually only checks that s1 starts with s2!
bool StrCmp(char *s1, char *s2) {
// while (*s2 != '\0') {
while (*s2) {
if (*s1++ != *s2++) {
return false;
}
}
return true;
}
// Converts lower case ascii letters a-z to upper case
// leaves other characters unchanged
char ToUpper(char c) {
if (c >= 'a' && c <= 'z')
c -= 32;
return c;
}
// Converts upper case ascii letters A-Z to lower case
// leaves other characters unchanged
char ToLower(char c) {
if (c >= 'A' && c <= 'Z')
c += 32;
return c;
}
// Converts ASCII numbers 0-9 and letters a-f (and A-F) to hex values 0x00-0x0F
// leaves other characters unchanged
// ASCII '0' is worth 0x30 so subtract 0x30 if ascii character is a number
// Lower case ASCII letter 'a' is worth 0x61, but we want this to be worth 0x0A
// Subtract 0x57 (0x61 + 0x0A = 0x57) from lower case letters
// Upper case ASCII letter 'A' is worth 0x41, but we want this to be worth 0x0A
// Subtract 0x37 (0x41 + 0x0A = 0x37) from upper case letters
char *aToh(char *s) {
while (*s) {
if ((*s >= '0') && (*s <='9'))
*s -= '0';
else if ((*s >= 'a') && (*s <='f'))
*s -= ('a' - 0x0A);
else if ((*s >= 'A') && (*s <='F'))
*s -= ('A' - 0x0A);
s++;
}
return s;
}
// StrAddc adds a single char to the end of a string
char *StrAddc (char *s, const char c) {
char *s1 = s;
// Find the end of the string 's'
while (*s)
(void) *s++;
// add the new character
*s++ = c;
// put the end of string character at its new position
*s = '\0';
return s1;
}
//-----------------------------------------------------------------------------
/**
Converts an ASCII character to low nibble of a byte.
@param rx_byte character to convert
@return resulting value
*/
uint8_t ascii2nibble(char str)
{
return str >= 'a' ? (str - 'a' + 10) & 0x0f :
(str >= 'A' ? (str - 'A' + 10) & 0x0f :
(str - '0') & 0x0f);
}
//-----------------------------------------------------------------------------
/**
Converts an ASCII string to integer (checks string contents beforehand).
@param val destination integer
@param str pointer to source string
@param length length of source string
@return succ / fail
*/
bool ascii2int(uint32_t* val, const char* str, uint8_t length)
{
// nothing to convert
if (!str || length < 1)
{
*val = 0;
return false;
}
// check string contents
uint8_t shift;
for (shift = 0; shift < length; ++shift)
{
if (!isxdigit(*(str + shift)))
{
// not a hex value
*val = 0;
return false;
}
}
// convert string
*val = ascii2nibble(*(str++));
for (shift = 1; shift < length; ++shift)
{
*val <<= 4;
*val += ascii2nibble(*(str++));
}
return true;
}
int isxdigit ( int ch )
{
return (unsigned int)( ch - '0') < 10u ||
(unsigned int)((ch | 0x20) - 'a') < 6u;
}