Skip to content

Commit b76a1ab

Browse files
committed
Refactor JSON format helpers, update usage
Update json_format_helpers: bump copyright to 2025-2026, add <cassert>, introduce Append2Digits helper and rename/refactor float/offset helpers (FormatFloat->Float, FormatUtcOffset->UtcOffset). UtcOffset now handles negative hours, builds the buffer manually, and asserts buffer bounds. Adjusted caller in json_config_global.cpp to use format::UtcOffset and minor header cleanup.
1 parent ab47eaf commit b76a1ab

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

common/include/json/json_format_helpers.h

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file json_format_helpers.h
33
*
44
*/
5-
/* Copyright (C) 2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -28,30 +28,46 @@
2828

2929
#include <cstdio>
3030
#include <cstdint>
31+
#include <cassert>
3132

3233
namespace format
3334
{
3435
constexpr size_t kFloatBufferSize = 8; // For "%.2f", "%.1f"
3536
constexpr size_t kOffsetBufferSize = 12; // For timezone offsets e.g. "+01:00"
3637

37-
[[nodiscard]] inline const char* FormatFloat(float value, char (&buf)[kFloatBufferSize], const char* fmt = "%.2f")
38+
inline void Append2Digits(char*& p, uint32_t v)
39+
{
40+
*p++ = static_cast<char>('0' + (v / 10));
41+
*p++ = static_cast<char>('0' + (v % 10));
42+
}
43+
44+
inline const char* Float(float value, char (&buf)[kFloatBufferSize], const char* fmt = "%.2f")
3845
{
3946
snprintf(buf, sizeof(buf), fmt, value);
4047
return buf;
4148
}
4249

43-
[[nodiscard]] inline const char* FormatUtcOffset(int32_t hours, uint32_t minutes, char (&buf)[kOffsetBufferSize])
50+
inline const char* UtcOffset(int32_t hours, uint32_t minutes, char (&buf)[kOffsetBufferSize])
4451
{
45-
if (hours <= 0)
46-
{
47-
snprintf(buf, sizeof(buf), "%.2d:%.2u", hours, minutes);
48-
}
49-
else
52+
const auto kNegative = hours < 0;
53+
if (kNegative) hours = -hours;
54+
55+
auto* p = buf;
56+
57+
if (hours != 0)
5058
{
51-
snprintf(buf, sizeof(buf), "+%.2d:%.2u", hours, minutes);
59+
*p++ = kNegative ? '-' : '+';
5260
}
61+
62+
Append2Digits(p, static_cast<uint32_t>(hours));
63+
*p++ = ':';
64+
Append2Digits(p, minutes);
65+
*p = '\0';
66+
67+
assert(static_cast<size_t>((p - buf) + 1) <= kOffsetBufferSize);
68+
5369
return buf;
5470
}
5571
} // namespace format
5672

57-
#endif // JSON_JSON_FORMAT_HELPERS_H_
73+
#endif // JSON_JSON_FORMAT_HELPERS_H_

lib-hal/src/json/json_config_global.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ uint32_t GetGlobal(char* buffer, uint32_t length) {
3737

3838
return json::helpers::Serialize(buffer, length, [&](JsonDoc& doc) {
3939
char offset[format::kOffsetBufferSize];
40-
doc[GlobalParamsConst::kUtcOffset.name] = format::FormatUtcOffset(hours, minutes, offset);
40+
doc[GlobalParamsConst::kUtcOffset.name] = format::UtcOffset(hours, minutes, offset);
4141
});
4242
}
4343

0 commit comments

Comments
 (0)