Skip to content

Commit 3377614

Browse files
authored
Merge pull request #5 from TcMenu/main-mbed-fix
#3 clean up and remove the use of min/max that conflict with std lib.…
2 parents fb851dd + a4eb0c1 commit 3377614

3 files changed

Lines changed: 49 additions & 20 deletions

File tree

src/TextUtilities.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,26 @@ char hexChar(uint8_t val) {
113113

114114

115115
void intToHexString(char* buffer, size_t bufferSize, unsigned int input, int digits, bool with0x) {
116+
buffer[0] = 0;
116117
if(with0x) {
118+
if(bufferSize < (digits + 3)) {
119+
return;
120+
}
117121
buffer[0] = '0';
118122
buffer[1] = 'x';
119123
bufferSize -= 2;
120124
buffer += 2;
125+
} else if(bufferSize < digits) {
126+
return;
121127
}
122128

123129
if(digits >= bufferSize) {
124130
digits = bufferSize - 1;
125131
}
126132

127-
int i = 0;
128-
while(bufferSize && i < digits) {
133+
size_t i = 0;
134+
bufferSize--;
135+
while(i < bufferSize && i < digits) {
129136
buffer[(digits-1) - i] = hexChar(input & 0x0f);
130137
input = input >> 4;
131138
i++;

src/TextUtilities.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,21 @@ void intToHexString(char* buffer, size_t bufferSize, unsigned int input, int dig
125125
inline float tcFltAbs(float f1) {
126126
return f1 > 0.0F ? f1 : -f1;
127127
}
128-
129-
#if defined(IOA_USE_MBED) || defined(BUILD_FOR_PICO_CMAKE)
128+
#if (defined(__MBED__) && !defined(ARDUINO_ARCH_MBED)) || defined(BUILD_FOR_PICO_CMAKE)
130129
#define strcmp_P(x,y) strcmp(x,y)
131130
#define strncpy_P(x,y,z) strncpy(x,y,z)
132131
#define strcpy_P(x,y) strcpy(x,y)
133132
#define strlen_P(x) strlen(x)
134133
#define highByte(x) ((x) >> 8)
135134
#define lowByte(x) ((x) & 0xff)
136-
#define ltoa(a,b,c) itoa(a,b,c)
137-
# ifndef min
138-
template<class T> void min(T x, T y) { return (((x) < (y))?(x):(y)); }
139-
template<class T> void max(T x, T y) { return (((x) > (y))?(x):(y)); }
140-
# endif //TCMENU_MBED_NO_MINMAX
141-
#endif // IOA_USE_MBED
142-
143-
#ifdef IOA_ARDUINO_MBED
144-
#define ltoa(a,b,c) itoa(a,b,c)
145-
#endif
135+
#endif // MBED OR PICO
136+
137+
#ifndef internal_min
138+
#define internal_min(a, b) ((a) > (b) ? (b) : (a));
139+
#endif // internal_min
140+
141+
#ifndef internal_max
142+
#define internal_max(a, b) ((a) < (b) ? (b) : (a));
143+
#endif // internal_max
146144

147145
#endif //IOABSTRACTION_TEXTUTILITIES_H

test/test_text_utils/test_text_utils.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ void testTcUtilHexCoversions() {
5454

5555
intToHexString(szBuffer, sizeof szBuffer, 0x0000, 4, true);
5656
TEST_ASSERT_EQUAL_STRING("0x0000", szBuffer);
57-
58-
intToHexString(szBuffer, 6, 0xFFFF, 4, true);
59-
TEST_ASSERT_EQUAL_STRING("0xFFF", szBuffer);
60-
61-
intToHexString(szBuffer, 3, 0xFFFF, 4, false);
62-
TEST_ASSERT_EQUAL_STRING("FF", szBuffer);
6357
}
6458

6559
void testTcUtilFloatConversions() {
@@ -73,11 +67,41 @@ void testTcUtilFloatConversions() {
7367
TEST_ASSERT_EQUAL_STRING("-10.3", szBuffer);
7468
}
7569

70+
void testTcUtilLimits() {
71+
char szBuffer[20];
72+
73+
// Test very large value
74+
szBuffer[0]=0;
75+
intToHexString(szBuffer, sizeof szBuffer, 0xFFF0FF0F, 8, true);
76+
TEST_ASSERT_EQUAL_STRING("0xFFF0FF0F", szBuffer);
77+
78+
// test very small value with large number of places
79+
intToHexString(szBuffer, sizeof szBuffer, 0x1, 8, true);
80+
TEST_ASSERT_EQUAL_STRING("0x00000001", szBuffer);
81+
82+
// test limits - method does nothing when buffer too small.
83+
intToHexString(szBuffer, 5, 0xFFF00000, 8, true);
84+
TEST_ASSERT_EQUAL_STRING("", szBuffer);
85+
86+
// test large integers
87+
ltoaClrBuff(szBuffer, 147483647, 9, NOT_PADDED, sizeof szBuffer);
88+
TEST_ASSERT_EQUAL_STRING("147483647", szBuffer);
89+
ltoaClrBuff(szBuffer, -147483647, 9, NOT_PADDED, sizeof szBuffer);
90+
TEST_ASSERT_EQUAL_STRING("-147483647", szBuffer);
91+
92+
93+
ltoaClrBuff(szBuffer, -2147483647, 9, NOT_PADDED, 5);
94+
TEST_ASSERT_EQUAL_STRING("-147", szBuffer);
95+
96+
}
97+
98+
7699
void setup() {
77100
UNITY_BEGIN();
78101
RUN_TEST(testTcUtilHexCoversions);
79102
RUN_TEST(testTcUtilIntegerConversions);
80103
RUN_TEST(testTcUtilFloatConversions);
104+
RUN_TEST(testTcUtilLimits);
81105
UNITY_END();
82106
}
83107

0 commit comments

Comments
 (0)