From 663adb4492db65ec206a7b459e8e50003f3ac559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 20 May 2015 18:59:48 +0200 Subject: [PATCH] Apply chriseth's idea about improving find_base64_char_index helper function. --- libdevcore/Base64.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/libdevcore/Base64.cpp b/libdevcore/Base64.cpp index cb6e873df..e36f8a18a 100644 --- a/libdevcore/Base64.cpp +++ b/libdevcore/Base64.cpp @@ -29,21 +29,25 @@ #include "Base64.h" using namespace dev; -static const char base64_chars[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; - static inline bool is_base64(byte c) { return (isalnum(c) || (c == '+') || (c == '/')); } static inline byte find_base64_char_index(byte c) { - auto it = std::find(base64_chars, base64_chars + sizeof(base64_chars), c); - return static_cast(it - base64_chars); + if ('A' <= c && c <= 'Z') return c - 'A'; + else if ('a' <= c && c <= 'z') return c - 'a' + 1 + find_base64_char_index('Z'); + else if ('0' <= c && c <= '9') return c - '0' + 1 + find_base64_char_index('z'); + else if (c == '+') return 1 + find_base64_char_index('9'); + else if (c == '/') return 1 + find_base64_char_index('+'); + else return 1 + find_base64_char_index('/'); } std::string dev::toBase64(bytesConstRef _in) { + static const char base64_chars[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + std::string ret; int i = 0; int j = 0;