Skip to content

Commit

Permalink
Performance improvements and namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zen0x7 committed Oct 26, 2024
1 parent de86718 commit 203c660
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 70 deletions.
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PROJECT(Base64URL VERSION 1.0.0 LANGUAGES CXX)
SET(CMAKE_CXX_STANDARD 23)
SET(CMAKE_CXX_STANDARD_REQUIRED True)

OPTION(BUILD_TESTS "Build tests" OFF)
OPTION(BUILD_TESTS "Build tests" ON)

INCLUDE(FetchContent)

Expand Down Expand Up @@ -32,7 +32,7 @@ INSTALL(TARGETS base64url

INSTALL(EXPORT Base64URLTargets
FILE Base64URLTargets.cmake
NAMESPACE base64url::
NAMESPACE zen_algorithms::base64url::
DESTINATION lib/cmake/Base64URL
)

Expand All @@ -50,6 +50,14 @@ INSTALL(FILES

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")

if (BUILD_TESTS)
FETCHCONTENT_DECLARE(
googletest
Expand Down
Binary file modified about.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 0 additions & 18 deletions lib/headers/base64url/container.hpp

This file was deleted.

61 changes: 61 additions & 0 deletions lib/headers/zen_algorithms/base64url.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include <string>
#include <map>

namespace zen_algorithms::base64url {
static const std::string chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789-_";

static std::map<char, int> chars_map = {
{'A', 0}, {'B', 1}, {'C', 2}, {'D', 3}, {'E', 4}, {'F', 5}, {'G', 6}, {'H', 7}, {'I', 8}, {'J', 9},
{'K', 10}, {'L', 11}, {'M', 12}, {'N', 13}, {'O', 14}, {'P', 15}, {'Q', 16}, {'R', 17}, {'S', 18}, {'T', 19},
{'U', 20}, {'V', 21}, {'W', 22}, {'X', 23}, {'Y', 24}, {'Z', 25},
{'a', 26}, {'b', 27}, {'c', 28}, {'d', 29}, {'e', 30}, {'f', 31}, {'g', 32}, {'h', 33}, {'i', 34}, {'j', 35},
{'k', 36}, {'l', 37}, {'m', 38}, {'n', 39}, {'o', 40}, {'p', 41}, {'q', 42}, {'r', 43}, {'s', 44}, {'t', 45},
{'u', 46}, {'v', 47}, {'w', 48}, {'x', 49}, {'y', 50}, {'z', 51},
{'0', 52}, {'1', 53}, {'2', 54}, {'3', 55}, {'4', 56}, {'5', 57}, {'6', 58}, {'7', 59}, {'8', 60}, {'9', 61},
{'-', 62}, {'_', 63}
};

static std::string encode(const std::string & input, bool padding = true) {
std::string output;
int value = 0;
int value_b = -6;

for (const unsigned char character: input) {
value = (value << 8) + character;
value_b += 8;
while (value_b >= 0) {
output.push_back(chars[(value >> value_b) & 0x3F]);
value_b -= 6;
}
}
if (value_b > -6)
output.push_back(chars[((value << 8) >> (value_b + 8)) & 0x3F]);

if (padding)
while (output.size() % 4 != 0)
output.push_back('=');

return output;
}

static std::string decode(const std::string & input) {
std::string output;
int value = 0;
int value_b = -8;
for (const char character: input) {
if (character == '=') break;
value = (value << 6) + chars_map[character];
value_b += 6;
if (value_b >= 0) {
output.push_back(static_cast<char>((value >> value_b) & 0xFF));
value_b -= 8;
}
}
return output;
}
}
3 changes: 3 additions & 0 deletions lib/sources/base64url.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <zen_algorithms/base64url.hpp>

namespace zen_algorithms::base64url { }
43 changes: 0 additions & 43 deletions lib/sources/container.cpp

This file was deleted.

14 changes: 7 additions & 7 deletions tests/implementation_test.cc
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include <gtest/gtest.h>
#include <base64url/container.hpp>
#include <zen_algorithms/base64url.hpp>

TEST(Base64URL, Encode) {
using namespace base64url;
using namespace zen_algorithms;
const std::string input = "hello world";
const std::string output = container::encode(input);
const std::string output = base64url::encode(input);
ASSERT_EQ(output, "aGVsbG8gd29ybGQ=");

const std::string output_no_padding = container::encode(input, false);
const std::string output_no_padding = base64url::encode(input, false);
ASSERT_EQ(output_no_padding, "aGVsbG8gd29ybGQ");
}

TEST(Base64URL, Decode) {
using namespace base64url;
using namespace zen_algorithms;
const std::string input = "aGVsbG8gd29ybGQ=";
const std::string output = container::decode(input);
const std::string output = base64url::decode(input);
ASSERT_EQ(output, "hello world");

const std::string input_no_padding = "aGVsbG8gd29ybGQ";
const std::string output_no_padding = container::decode(input_no_padding);
const std::string output_no_padding = base64url::decode(input_no_padding);
ASSERT_EQ(output_no_padding, "hello world");
}

0 comments on commit 203c660

Please sign in to comment.