From b888585f759dd4535724746b28592ffa4d4be5b9 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Fri, 27 May 2022 14:47:26 +0200 Subject: [PATCH] A bit of error handling and better comments --- GCryptCLI/src/ModuleDataFormatter.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/GCryptCLI/src/ModuleDataFormatter.cpp b/GCryptCLI/src/ModuleDataFormatter.cpp index a737618..bd809bc 100644 --- a/GCryptCLI/src/ModuleDataFormatter.cpp +++ b/GCryptCLI/src/ModuleDataFormatter.cpp @@ -189,7 +189,7 @@ std::vector ModuleDataFormatter::StringToBlocks( case Configuration::IOBASE_FORMAT::BASE_16: case Configuration::IOBASE_FORMAT::BASE_64: // Easy case: Each digit is exactly one char in size. - // We can just calculate how many bits we should take. + // We can just calculate how many bytes we should read. for (std::size_t i = 0; i < str.length(); i += blockWidth) { const std::string subs = str.substr(i, blockWidth); @@ -204,8 +204,8 @@ std::vector ModuleDataFormatter::StringToBlocks( break; case Configuration::IOBASE_FORMAT::BASE_UWU: - case Configuration::IOBASE_FORMAT::BASE_UGH: - // Hard case: Each digit n digits long. Digits may vary in length. + case Configuration::IOBASE_FORMAT::BASE_UGH: { + // Hard case: Each digit is n digits long. Digits may vary in length. // They are seperated by spaces. // We have to parse them... std::size_t digitsPassed = 0; @@ -217,8 +217,8 @@ std::vector ModuleDataFormatter::StringToBlocks( if (digitsPassed == blockWidth) { const std::string subs = str.substr( - blockStart, - i - blockStart + blockStart, + i - blockStart ); Block newBlock = StringToBlock( @@ -232,10 +232,15 @@ std::vector ModuleDataFormatter::StringToBlocks( blockStart = i+1; } } - - } + // Here should never be any digits left. A formatted block should ALWAYS be full length. break; + } + + default: + throw std::invalid_argument("ModuleDataFormatter::StringToBlocks() has been passed an unknown base! No switch-case matched!"); + break; + } return blocks;