Skip to content

Commit

Permalink
fixup! Fix a bug in string_view_utf8::copyToRAM() and implement check…
Browse files Browse the repository at this point in the history
… for truncated multibyte characters
  • Loading branch information
michalrudolf authored and danopernis committed Jun 20, 2024
1 parent 387dea5 commit 5c84a7a
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 29 deletions.
26 changes: 0 additions & 26 deletions src/lang/string_view_utf8.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,32 +230,6 @@ class string_view_utf8 {
return dst - dst_start;
}

/// Copy the string byte-by-byte into some RAM buffer for later processing, without multibyte cutting check
/// typically used to obtain a translated version of a format string for s(n)printf
/// @param dst target buffer to copy the bytes to
/// @param buffer_size size of dst in bytes
/// @returns number of bytes (not utf8 characters) copied not counting the terminating '\0'
/// Using sprintf to format some string is possible with translations, but it requires one more step than usually -
/// one must first fetch the translated format string into a RAM buffer and then feed the format string into standard sprintf
size_t copyBytesToRAM(char *dst, size_t buffer_size) {
if (buffer_size == 0) {
return 0;
}
char *dst_start = dst;
for (size_t i = 0; i < buffer_size; ++i) {
*dst = getbyte(attrs);
if (*dst == 0) {
return dst - dst_start;
}
++dst;
}

// Beware - no multibyte character check!
// dst pointer is decremented to point at the last character of the buffer
*(--dst) = 0; // safety termination in case of reaching the end of the buffer
return dst - dst_start;
}

/// Construct string_view_utf8 to provide data from CPU FLASH
static string_view_utf8 MakeCPUFLASH(const uint8_t *utf8raw) {
string_view_utf8 s;
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/lang/string_view_utf8/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,4 @@ TEST_CASE("string_view_utf8::CopyToRAM dst buffer too small + multibyte chars",
size_t copied_bytes = orig.copyToRAM(dst, sizeof(dst));
REQUIRE_THAT(dst, Equals(ref));
REQUIRE(copied_bytes == sizeof(ref) - 1); // -1 because we don't count copying null at the end

copied_bytes = orig.copyBytesToRAM(dst, sizeof(dst));
REQUIRE(copied_bytes == sizeof(dst) - 1);
}

0 comments on commit 5c84a7a

Please sign in to comment.