Skip to content

Commit

Permalink
Rework/rename CharToBin to be HexCharToBin using a simpler (much fast…
Browse files Browse the repository at this point in the history
…er - no looping) method.

HexChars is now only used by ConvertStringToHexPresentation.
  • Loading branch information
David Lowndes authored and David Lowndes committed Dec 26, 2023
1 parent 612cd6a commit 7f46b5e
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions UTF8ConvDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
#include "UTF8ConvDlg.h"
#include <vector>
#include <string_view>
using namespace std;
#include <optional>
using std::optional;
using std::nullopt;
using std::wstring_view;
using std::vector;

#ifdef _DEBUG
#define new DEBUG_NEW
Expand Down Expand Up @@ -204,10 +208,9 @@ HCURSOR CUTF8ConvDlg::OnQueryDragIcon()
return (HCURSOR) m_hIcon;
}

static const char HexChars[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
static_assert( _countof( HexChars ) == 16, "HexChars must be 16 characters long" );

// Note: Although this is passed a wide character representation, it's only ever single byte characters, so the upper byte is always 0
// Note: Although this is passed a wide character representation, it's only
// ever single byte characters, so the upper byte is always 0 (and ignored in the
// code).
static CString ConvertStringToHexPresentation( wstring_view sUtf )
{
const size_t NumChars = sUtf.length();
Expand All @@ -218,6 +221,9 @@ static CString ConvertStringToHexPresentation( wstring_view sUtf )

for ( size_t indx = 0; indx < NumChars; ++indx )
{
static const char HexChars[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
static_assert(_countof( HexChars ) == 16, "HexChars must be 16 characters long");

const BYTE ch = static_cast<BYTE>( sUtf[indx] );
pszResults[indx*3] = HexChars[( ch >> 4 ) & 0x0f ];
pszResults[indx*3 + 1] = HexChars[ ch & 0x0f ];
Expand All @@ -229,27 +235,29 @@ static CString ConvertStringToHexPresentation( wstring_view sUtf )
return strResults;
}

constexpr static BYTE CharToBin( TCHAR ch )
constexpr static optional<BYTE> HexCharToBin( TCHAR ch )
{
BYTE Value = 0x0FF;
#if 1 // Use the modern STL approach
auto it = std::ranges::find( HexChars, ch );
if ( it != end( HexChars ) )
BYTE Val;

if ( (ch >= _T( '0' )) && (ch <= _T( '9' )) )
{
Value = static_cast<BYTE>( std::distance( begin( HexChars ), it ) );
Val = ch & 0x0F;
}
#else // Use the old fashioned loop (though much less code generated)
for ( BYTE indx = 0; indx < _countof( HexChars ); ++indx )
else if ( (ch >= _T( 'A' )) && (ch <= _T( 'F' )) )
{
if ( ch == HexChars[indx] )
{
Value = indx;
break;
}
Val = static_cast<BYTE>( ch - _T( 'A' ) + 10 );
}
#endif
_ASSERT( Value != 0x0FF ); // Should never happen, not called with non-hex characters
return Value;
else if ( (ch >= _T( 'a' )) && (ch <= _T( 'f' )) )
{
Val = static_cast<BYTE>( ch - _T( 'a' ) + 10 );
}
else
{
// Invalid character
return nullopt;
}

return Val;
}

// Bit of a mouthful, but this is what it does:
Expand Down Expand Up @@ -339,13 +347,13 @@ void CUTF8ConvDlg::OnToascii()
if ( bSecondOfPair )
{
Value <<= 4;
Value += CharToBin( ch );
Value += HexCharToBin( ch ).value();

vBin.push_back( Value );
}
else
{
Value = CharToBin( ch );
Value = HexCharToBin( ch ).value();
}

/* Next time it's the other way round */
Expand Down

0 comments on commit 7f46b5e

Please sign in to comment.