From 67d41ce2237e39ea3be7932659fe04f263c82b24 Mon Sep 17 00:00:00 2001 From: Zufu Liu Date: Sat, 3 Jul 2021 14:47:54 +1000 Subject: Feature [feature-requests:#1408] Avoid sprintf for hexadecimal character blobs. --- src/EditView.cxx | 9 ++++++++- src/EditView.h | 1 + src/Editor.cxx | 8 ++++---- 3 files changed, 13 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/EditView.cxx b/src/EditView.cxx index a71f385e7..e4b0fa5d9 100644 --- a/src/EditView.cxx +++ b/src/EditView.cxx @@ -170,6 +170,13 @@ void DrawStyledText(Surface *surface, const ViewStyle &vs, int styleOffset, PRec } } +void Hexits(char *hexits, int ch) noexcept { + hexits[0] = 'x'; + hexits[1] = "0123456789ABCDEF"[ch / 0x10]; + hexits[2] = "0123456789ABCDEF"[ch % 0x10]; + hexits[3] = 0; +} + } EditView::EditView() { @@ -1040,7 +1047,7 @@ void EditView::DrawEOL(Surface *surface, const EditModel &model, const ViewStyle if (UTF8IsAscii(chEOL)) { ctrlChar = ControlCharacterString(chEOL); } else { - sprintf(hexits, "x%2X", chEOL); + Hexits(hexits, chEOL); ctrlChar = hexits; } } diff --git a/src/EditView.h b/src/EditView.h index 148d6e52d..bac59ed31 100644 --- a/src/EditView.h +++ b/src/EditView.h @@ -40,6 +40,7 @@ void DrawTextNoClipPhase(Surface *surface, PRectangle rc, const Style &style, XY std::string_view text, DrawPhase phase); void DrawStyledText(Surface *surface, const ViewStyle &vs, int styleOffset, PRectangle rcText, const StyledText &st, size_t start, size_t length, DrawPhase phase); +void Hexits(char *hexits, int ch) noexcept; typedef void (*DrawTabArrowFn)(Surface *surface, PRectangle rcTab, int ymid, const ViewStyle &vsDraw, Stroke stroke); diff --git a/src/Editor.cxx b/src/Editor.cxx index 71aaeae53..d01eb0cdf 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -247,8 +247,8 @@ void Editor::SetRepresentations() { if (IsUnicodeMode()) { for (int k=0x80; k < 0x100; k++) { const char hiByte[2] = { static_cast(k), 0 }; - char hexits[5]; // Really only needs 4 but that causes warning from gcc 7.1 - sprintf(hexits, "x%2X", k); + char hexits[4]; + Hexits(hexits, k); reprs.SetRepresentation(hiByte, hexits); } } else if (pdoc->dbcsCodePage) { @@ -257,8 +257,8 @@ void Editor::SetRepresentations() { const char ch = static_cast(k); if (pdoc->IsDBCSLeadByteNoExcept(ch) || pdoc->IsDBCSLeadByteInvalid(ch)) { const char hiByte[2] = { ch, 0 }; - char hexits[5]; // Really only needs 4 but that causes warning from gcc 7.1 - sprintf(hexits, "x%2X", k); + char hexits[4]; + Hexits(hexits, k); reprs.SetRepresentation(hiByte, hexits); } } -- cgit v1.2.3