json-html-serializer: fix clang-tidy warning
```
../include/json_html_serializer.hpp:426:42: error: 'end' declared with a const-qualified typedef; results in the type being 'char *const' instead of 'const char *' [misc-misplaced-const,-warnings-as-errors]
const std::array<char, 64>::iterator end =
^
/usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/array:106:44: note: typedef declared here
typedef value_type* iterator;
../include/json_html_serializer.hpp:165:60: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
std::snprintf(stringBuffer.data() + bytes, 7,
../include/json_html_serializer.hpp:327:11: error: 'bufferPtr' is an unsafe pointer used for buffer access [-Werror,-Wunsafe-buffer-usage]
auto* bufferPtr = begin(numberbuffer);
../include/json_html_serializer.hpp:393:11: error: 'begin' is an unsafe pointer used for buffer access [-Werror,-Wunsafe-buffer-usage]
char* begin = numberbuffer.data();
../include/json_html_serializer.hpp:425:56: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
std::remove(numberbuffer.begin(), numberbuffer.begin() + len, ',');
```
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: If6aaf038c939ad76da73e68e746a56b0905b2804
diff --git a/include/json_html_serializer.hpp b/include/json_html_serializer.hpp
index 320b5fd1..f23150e 100644
--- a/include/json_html_serializer.hpp
+++ b/include/json_html_serializer.hpp
@@ -162,7 +162,7 @@
if (codePoint <= 0xFFFF)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
- std::snprintf(stringBuffer.data() + bytes, 7,
+ std::snprintf(&stringBuffer[bytes], 7,
"\\u%04x",
static_cast<uint16_t>(codePoint));
bytes += 6;
@@ -171,8 +171,7 @@
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
std::snprintf(
- stringBuffer.data() + bytes, 13,
- "\\u%04x\\u%04x",
+ &stringBuffer[bytes], 13, "\\u%04x\\u%04x",
static_cast<uint16_t>(0xD7C0 +
(codePoint >> 10)),
static_cast<uint16_t>(0xDC00 +
@@ -325,7 +324,7 @@
}
// use a pointer to fill the buffer
- auto* bufferPtr = begin(numberbuffer);
+ auto bufferPtr = numberbuffer.begin();
const bool isNegative = std::is_same<NumberType, int64_t>::value &&
!(number >= 0); // see issue #755
@@ -355,8 +354,7 @@
// jump to the end to generate the string from backward
// so we later avoid reversing the result
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
- bufferPtr += nChars;
+ std::advance(bufferPtr, nChars);
// Fast int2ascii implementation inspired by "Fastware" talk by Andrei
// Alexandrescu See: https://www.youtube.com/watch?v=o4-CwDo2zpg
@@ -364,24 +362,24 @@
{
const auto digitsIndex = static_cast<unsigned>((absValue % 100));
absValue /= 100;
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
- *(--bufferPtr) = digitsTo99[digitsIndex][1];
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
- *(--bufferPtr) = digitsTo99[digitsIndex][0];
+ *bufferPtr = digitsTo99[digitsIndex][1];
+ bufferPtr = std::prev(bufferPtr);
+ *bufferPtr = digitsTo99[digitsIndex][0];
+ bufferPtr = std::prev(bufferPtr);
}
if (absValue >= 10)
{
const auto digitsIndex = static_cast<unsigned>(absValue);
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
- *(--bufferPtr) = digitsTo99[digitsIndex][1];
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
- *(--bufferPtr) = digitsTo99[digitsIndex][0];
+ *bufferPtr = digitsTo99[digitsIndex][1];
+ bufferPtr = std::prev(bufferPtr);
+ *bufferPtr = digitsTo99[digitsIndex][0];
+ // assignment never used: bufferPtr = std::prev(bufferPtr);
}
else
{
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
- *(--bufferPtr) = static_cast<char>('0' + absValue);
+ *bufferPtr = static_cast<char>('0' + absValue);
+ // assignment never used: bufferPtr = std::prev(bufferPtr);
}
out.append(numberbuffer.data(), nChars);
@@ -391,12 +389,11 @@
std::true_type /*isIeeeSingleOrDouble*/)
{
std::array<char, 64> numberbuffer{{}};
- char* begin = numberbuffer.data();
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
- ::nlohmann::detail::to_chars(begin, begin + numberbuffer.size(), number);
+ ::nlohmann::detail::to_chars(numberbuffer.begin(), numberbuffer.end(),
+ number);
- out += begin;
+ out += numberbuffer.data();
}
inline void dumpfloat(std::string& out, double number,
@@ -423,21 +420,25 @@
return;
}
- const std::array<char, 64>::iterator end =
- std::remove(numberbuffer.begin(), numberbuffer.begin() + len, ',');
+ auto end = numberbuffer.begin();
+ std::advance(end, len);
+ end = std::remove(numberbuffer.begin(), end, ',');
std::fill(end, numberbuffer.end(), '\0');
- if ((end - numberbuffer.begin()) > len)
+ if (std::distance(numberbuffer.begin(), end) > len)
{
return;
}
- len = (end - numberbuffer.begin());
+ len = std::distance(numberbuffer.begin(), end);
out.append(numberbuffer.data(), static_cast<std::size_t>(len));
// determine if need to append ".0"
+ auto newEnd = numberbuffer.begin();
+ std::advance(newEnd, len + 1);
+
const bool valueIsIntLike =
- std::none_of(numberbuffer.begin(), numberbuffer.begin() + len + 1,
+ std::none_of(numberbuffer.begin(), newEnd,
[](char c) { return (c == '.' or c == 'e'); });
if (valueIsIntLike)