Skip to content

Commit 203c660

Browse files
committed
Performance improvements and namespace.
1 parent de86718 commit 203c660

File tree

7 files changed

+81
-70
lines changed

7 files changed

+81
-70
lines changed

CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PROJECT(Base64URL VERSION 1.0.0 LANGUAGES CXX)
44
SET(CMAKE_CXX_STANDARD 23)
55
SET(CMAKE_CXX_STANDARD_REQUIRED True)
66

7-
OPTION(BUILD_TESTS "Build tests" OFF)
7+
OPTION(BUILD_TESTS "Build tests" ON)
88

99
INCLUDE(FetchContent)
1010

@@ -32,7 +32,7 @@ INSTALL(TARGETS base64url
3232

3333
INSTALL(EXPORT Base64URLTargets
3434
FILE Base64URLTargets.cmake
35-
NAMESPACE base64url::
35+
NAMESPACE zen_algorithms::base64url::
3636
DESTINATION lib/cmake/Base64URL
3737
)
3838

@@ -50,6 +50,14 @@ INSTALL(FILES
5050

5151
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
5252

53+
if(NOT CMAKE_BUILD_TYPE)
54+
set(CMAKE_BUILD_TYPE Release)
55+
endif()
56+
57+
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
58+
set(CMAKE_CXX_FLAGS_DEBUG "-g")
59+
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
60+
5361
if (BUILD_TESTS)
5462
FETCHCONTENT_DECLARE(
5563
googletest

about.png

-11 KB
Loading

lib/headers/base64url/container.hpp

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <map>
5+
6+
namespace zen_algorithms::base64url {
7+
static const std::string chars =
8+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9+
"abcdefghijklmnopqrstuvwxyz"
10+
"0123456789-_";
11+
12+
static std::map<char, int> chars_map = {
13+
{'A', 0}, {'B', 1}, {'C', 2}, {'D', 3}, {'E', 4}, {'F', 5}, {'G', 6}, {'H', 7}, {'I', 8}, {'J', 9},
14+
{'K', 10}, {'L', 11}, {'M', 12}, {'N', 13}, {'O', 14}, {'P', 15}, {'Q', 16}, {'R', 17}, {'S', 18}, {'T', 19},
15+
{'U', 20}, {'V', 21}, {'W', 22}, {'X', 23}, {'Y', 24}, {'Z', 25},
16+
{'a', 26}, {'b', 27}, {'c', 28}, {'d', 29}, {'e', 30}, {'f', 31}, {'g', 32}, {'h', 33}, {'i', 34}, {'j', 35},
17+
{'k', 36}, {'l', 37}, {'m', 38}, {'n', 39}, {'o', 40}, {'p', 41}, {'q', 42}, {'r', 43}, {'s', 44}, {'t', 45},
18+
{'u', 46}, {'v', 47}, {'w', 48}, {'x', 49}, {'y', 50}, {'z', 51},
19+
{'0', 52}, {'1', 53}, {'2', 54}, {'3', 55}, {'4', 56}, {'5', 57}, {'6', 58}, {'7', 59}, {'8', 60}, {'9', 61},
20+
{'-', 62}, {'_', 63}
21+
};
22+
23+
static std::string encode(const std::string & input, bool padding = true) {
24+
std::string output;
25+
int value = 0;
26+
int value_b = -6;
27+
28+
for (const unsigned char character: input) {
29+
value = (value << 8) + character;
30+
value_b += 8;
31+
while (value_b >= 0) {
32+
output.push_back(chars[(value >> value_b) & 0x3F]);
33+
value_b -= 6;
34+
}
35+
}
36+
if (value_b > -6)
37+
output.push_back(chars[((value << 8) >> (value_b + 8)) & 0x3F]);
38+
39+
if (padding)
40+
while (output.size() % 4 != 0)
41+
output.push_back('=');
42+
43+
return output;
44+
}
45+
46+
static std::string decode(const std::string & input) {
47+
std::string output;
48+
int value = 0;
49+
int value_b = -8;
50+
for (const char character: input) {
51+
if (character == '=') break;
52+
value = (value << 6) + chars_map[character];
53+
value_b += 6;
54+
if (value_b >= 0) {
55+
output.push_back(static_cast<char>((value >> value_b) & 0xFF));
56+
value_b -= 8;
57+
}
58+
}
59+
return output;
60+
}
61+
}

lib/sources/base64url.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <zen_algorithms/base64url.hpp>
2+
3+
namespace zen_algorithms::base64url { }

lib/sources/container.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

tests/implementation_test.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
#include <gtest/gtest.h>
2-
#include <base64url/container.hpp>
2+
#include <zen_algorithms/base64url.hpp>
33

44
TEST(Base64URL, Encode) {
5-
using namespace base64url;
5+
using namespace zen_algorithms;
66
const std::string input = "hello world";
7-
const std::string output = container::encode(input);
7+
const std::string output = base64url::encode(input);
88
ASSERT_EQ(output, "aGVsbG8gd29ybGQ=");
99

10-
const std::string output_no_padding = container::encode(input, false);
10+
const std::string output_no_padding = base64url::encode(input, false);
1111
ASSERT_EQ(output_no_padding, "aGVsbG8gd29ybGQ");
1212
}
1313

1414
TEST(Base64URL, Decode) {
15-
using namespace base64url;
15+
using namespace zen_algorithms;
1616
const std::string input = "aGVsbG8gd29ybGQ=";
17-
const std::string output = container::decode(input);
17+
const std::string output = base64url::decode(input);
1818
ASSERT_EQ(output, "hello world");
1919

2020
const std::string input_no_padding = "aGVsbG8gd29ybGQ";
21-
const std::string output_no_padding = container::decode(input_no_padding);
21+
const std::string output_no_padding = base64url::decode(input_no_padding);
2222
ASSERT_EQ(output_no_padding, "hello world");
2323
}

0 commit comments

Comments
 (0)