Skip to content

Commit

Permalink
Correct hex case conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Sep 7, 2022
1 parent e3673c3 commit 5b17b39
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Sming/System/m_printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ static nputs_callback_t _puts_callback;
#define is_digit(c) ((c) >= '0' && (c) <= '9')
#define is_print(c) ((c) >= ' ' && (c) <= '~')

static char to_upper(char c)
{
return (c >= 'a' && c <= 'z') ? (c + 'A' - 'a') : c;
}

static void str_upper(char* s)
{
for(; *s != '\0'; ++s) {
*s = to_upper(*s);
}
}

static int skip_atoi(const char **s)
{
int i = 0;
Expand Down Expand Up @@ -126,6 +138,7 @@ int m_vsnprintf(char *buf, size_t maxLen, const char *fmt, va_list args)
int8_t width = 0;
char pad = ' ';
uint8_t length = 0;
bool upcase = false;

while (char f = *fmt) {
if (f == '-') minus = 1;
Expand Down Expand Up @@ -214,8 +227,12 @@ int m_vsnprintf(char *buf, size_t maxLen, const char *fmt, va_list args)
break;

case 'x':
ubase = 16;
break;

case 'X':
ubase = 16;
upcase = true;
break;

case 'u':
Expand All @@ -235,6 +252,9 @@ int m_vsnprintf(char *buf, size_t maxLen, const char *fmt, va_list args)
} else {
s = ultoa_wp(va_arg(args, uint32_t), tempNum, ubase, width, pad);
}
if (upcase) {
str_upper(tempNum);
}
}

// copy string to target
Expand Down
2 changes: 2 additions & 0 deletions tests/HostTests/modules/Libc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class LibcTest : public TestGroup
REQUIRE_EQ(String(buffer), "12345678");
m_snprintf(buffer, sizeof(buffer), "%llx", 0x123456789ABCDEFULL);
REQUIRE_EQ(String(buffer), "123456789abcdef");
m_snprintf(buffer, sizeof(buffer), "0x%016llX", 0x123456789ABCDEFULL);
REQUIRE_EQ(String(buffer), "0x0123456789ABCDEF");
m_snprintf(buffer, sizeof(buffer), "%llu", 123456789123456789ULL);
REQUIRE_EQ(String(buffer), "123456789123456789");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/HostTests/modules/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class StreamTest : public TestGroup
TEST_CASE("ChunkedStream / StreamTransformer")
{
DEFINE_FSTR_LOCAL(FS_INPUT, "Some test data");
DEFINE_FSTR_LOCAL(FS_OUTPUT, "e\r\nSome test data\r\n0\r\n\r\n");
DEFINE_FSTR_LOCAL(FS_OUTPUT, "E\r\nSome test data\r\n0\r\n\r\n");
ChunkedStream chunked(new FlashMemoryStream(FS_INPUT));
MemoryDataStream output;
output.copyFrom(&chunked);
Expand Down

0 comments on commit 5b17b39

Please sign in to comment.