From 8248c56400998eb0263b1aa9f29df1f3a7f6b500 Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Sat, 9 Jun 2007 17:52:47 +0000 Subject: [PATCH] Faster hex encoding git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_8_BRANCH@46387 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/richtext/richtextbuffer.cpp | 45 +++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp index 088bea9e26..039147138f 100644 --- a/src/richtext/richtextbuffer.cpp +++ b/src/richtext/richtextbuffer.cpp @@ -8168,19 +8168,48 @@ bool wxRichTextImageBlock::Load(wxImage& image) return success; } +// Array used in DecToHex conversion routine. +static char hexArray[] = "0123456789ABCDEF"; + +// Convert decimal integer to 2-character hex string +inline void wxRichTextDecToHex(int dec, char* buf) +{ + int firstDigit = (int)(dec/16.0); + int secondDigit = (int)(dec - (firstDigit*16.0)); + buf[0] = hexArray[firstDigit]; + buf[1] = hexArray[secondDigit]; +} + // Write data in hex to a stream bool wxRichTextImageBlock::WriteHex(wxOutputStream& stream) { - wxString hex; - int i; - for (i = 0; i < (int) m_dataSize; i++) + const int bufSize = 512; + char buf[bufSize+1]; + + int left = m_dataSize; + int n, i, j; + j = 0; + while (left > 0) { - hex = wxDecToHex(m_data[i]); - wxCharBuffer buf = hex.ToAscii(); + if (left*2 > bufSize) + { + n = bufSize; left -= (bufSize/2); + } + else + { + n = left*2; left = 0; + } - stream.Write((const char*) buf, hex.length()); + char* b = buf; + for (i = 0; i < (n/2); i++) + { + wxRichTextDecToHex(m_data[j], b); + b += 2; j ++; + } + + buf[n] = 0; + stream.Write((const char*) buf, n); } - return true; } @@ -8192,7 +8221,7 @@ bool wxRichTextImageBlock::ReadHex(wxInputStream& stream, int length, int imageT if (m_data) delete[] m_data; - wxString str(wxT(" ")); + wxChar str[2]; m_data = new unsigned char[dataSize]; int i; for (i = 0; i < dataSize; i ++)