/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include <string_view>
#include <algorithm>
#include <cassert>
bool checkB64(const std::string_view input) {
if (input.length() % 4 == 0 && std::all_of(input.begin(), input.end(),
[](const char c) {
return ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
(c == '\\') ||
(c == '+') ||
(c == '='));}))
{
// filter by the location of '=' sign.
if (auto pos = input.find("==="); pos != std::string_view::npos)
if (pos < input.length() - 3) return false;
else if (auto pos = input.find("=="); pos != std::string_view::npos)
if (pos < input.length() - 2) return false;
else if (auto pos = input.find("="); pos != std::string_view::npos)
if (pos < input.length() - 1) return false;
return true;
}
return false;
}
int main()
{
// std::string input = ;
assert(checkB64("SGVsbG93b3JsZA==")); // Helloworld
assert(checkB64("SGVsbG93b3JsZHM=")); // Helloworlds
assert(checkB64("Q2hlY2tCNjQ=")); // CheckB64
assert(checkB64("Q2hlY2tCNjQt")); // CheckB64-
assert(checkB64("NQ==")); // 5
return 0;
}