From 4f143b56b5ec2bc686a999571c825a7e9fa0d139 Mon Sep 17 00:00:00 2001 From: Richard Walters Date: Sun, 1 Jul 2018 23:54:11 -0700 Subject: Refactoring We don't really need a formal state machine for decoding percent-encoded characters. We really just need to shift in two hex digits and we're done. --- src/PercentEncodedCharacterDecoder.cpp | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/src/PercentEncodedCharacterDecoder.cpp b/src/PercentEncodedCharacterDecoder.cpp index c34b817..4fb92c0 100644 --- a/src/PercentEncodedCharacterDecoder.cpp +++ b/src/PercentEncodedCharacterDecoder.cpp @@ -42,12 +42,10 @@ namespace Uri { int decodedCharacter = 0; /** - * This is the current state of the decoder's state machine. - * - 0: we haven't yet received the first hex digit. - * - 1: we received the first hex digit but not the second. - * - 2: we received both hex digits + * This is the number of digits that we still need to shift in + * to decode the character. */ - size_t decoderState = 0; + size_t digitsLeft = 2; // Methods @@ -87,28 +85,15 @@ namespace Uri { } bool PercentEncodedCharacterDecoder::NextEncodedCharacter(char c) { - switch(impl_->decoderState) { - case 0: { // % ... - impl_->decoderState = 1; - if (!impl_->ShiftInHexDigit(c)) { - return false; - } - } break; - - case 1: { // %[0-9A-F] ... - impl_->decoderState = 2; - if (!impl_->ShiftInHexDigit(c)) { - return false; - } - } break; - - default: break; + if (!impl_->ShiftInHexDigit(c)) { + return false; } + --impl_->digitsLeft; return true; } bool PercentEncodedCharacterDecoder::Done() const { - return (impl_->decoderState == 2); + return (impl_->digitsLeft == 0); } char PercentEncodedCharacterDecoder::GetDecodedCharacter() const { -- cgit v1.2.3