From f7bfd92051c004a37e7b6e38c2b2001764eeef74 Mon Sep 17 00:00:00 2001 From: Richard Walters Date: Sun, 1 Jul 2018 23:49:53 -0700 Subject: Refactoring Extract ShiftInHexDigit method from duplicated code in PercentEncodedCharacterDecoder. --- src/PercentEncodedCharacterDecoder.cpp | 47 ++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/PercentEncodedCharacterDecoder.cpp b/src/PercentEncodedCharacterDecoder.cpp index 3dd302f..c34b817 100644 --- a/src/PercentEncodedCharacterDecoder.cpp +++ b/src/PercentEncodedCharacterDecoder.cpp @@ -34,6 +34,8 @@ namespace { namespace Uri { struct PercentEncodedCharacterDecoder::Impl { + // Properties + /** * This is the decoded character. */ @@ -46,6 +48,33 @@ namespace Uri { * - 2: we received both hex digits */ size_t decoderState = 0; + + // Methods + + /** + * This method shifts in the given hex digit as part of + * building the decoded character. + * + * @param[in] c + * This is the hex digit to shift into the decoded character. + * + * @return + * An indication of whether or not the given hex digit + * was valid is returned. + */ + bool ShiftInHexDigit(char c) { + decodedCharacter <<= 4; + if (DIGIT.Contains(c)) { + decodedCharacter += (int)(c - '0'); + } else if (HEX_UPPER.Contains(c)) { + decodedCharacter += (int)(c - 'A') + 10; + } else if (HEX_LOWER.Contains(c)) { + decodedCharacter += (int)(c - 'a') + 10; + } else { + return false; + } + return true; + } }; PercentEncodedCharacterDecoder::~PercentEncodedCharacterDecoder() = default; @@ -61,28 +90,14 @@ namespace Uri { switch(impl_->decoderState) { case 0: { // % ... impl_->decoderState = 1; - impl_->decodedCharacter <<= 4; - if (DIGIT.Contains(c)) { - impl_->decodedCharacter += (int)(c - '0'); - } else if (HEX_UPPER.Contains(c)) { - impl_->decodedCharacter += (int)(c - 'A') + 10; - } else if (HEX_LOWER.Contains(c)) { - impl_->decodedCharacter += (int)(c - 'a') + 10; - } else { + if (!impl_->ShiftInHexDigit(c)) { return false; } } break; case 1: { // %[0-9A-F] ... impl_->decoderState = 2; - impl_->decodedCharacter <<= 4; - if (DIGIT.Contains(c)) { - impl_->decodedCharacter += (int)(c - '0'); - } else if (HEX_UPPER.Contains(c)) { - impl_->decodedCharacter += (int)(c - 'A') + 10; - } else if (HEX_LOWER.Contains(c)) { - impl_->decodedCharacter += (int)(c - 'a') + 10; - } else { + if (!impl_->ShiftInHexDigit(c)) { return false; } } break; -- cgit v1.2.3