Sha256: fb316d28a70038b18e8066f943dfabe3eafb2298f0597cf34da7e2c2084a1736

Contents?: true

Size: 1.68 KB

Versions: 257

Compression:

Stored size: 1.68 KB

Contents

#include "crypto_square.h"
#include <boost/algorithm/string/case_conv.hpp>
#include <algorithm>
#include <cctype>
#include <iterator>

using namespace std;

namespace
{

string normalize(string const &text)
{
    string result;
    copy_if(text.begin(), text.end(), back_inserter(result),
        [](const char c) { return !isspace(c) && !ispunct(c); });
    return boost::to_lower_copy(result);
}

}

namespace crypto_square
{

cipher::cipher(string const& text)
    : text_(normalize(text))
{
}

string cipher::normalize_plain_text() const
{
    return text_;
}

size_t cipher::size() const
{
    size_t length = 1;
    while (length*length < text_.size()) {
        ++length;
    }
    return length;
}

vector<string> cipher::plain_text_segments() const
{
    vector<string> segments;
    const size_t segment_size{size()};
    for (size_t i = 0; i < text_.length(); i += segment_size) {
        segments.push_back(text_.substr(i, segment_size));
    }
    return segments;
}

string cipher::cipher_text() const
{
    string result;
    const vector<string> segments{plain_text_segments()};
    const size_t rows{size()};
    const size_t num_segments{segments.size()};
    for (size_t i = 0; i < rows; ++i) {
        for (size_t j = 0; j < num_segments; ++j) {
            if (i < segments[j].length()) {
                result += segments[j][i];
            }
        }
    }
    return result;
}

string cipher::normalized_cipher_text() const
{
    const string encoded{cipher_text()};
    const auto num_rows = size();
    string result{encoded.substr(0, num_rows)};
    for (auto i = num_rows; i < encoded.length(); i += num_rows) {
        result += ' ' + encoded.substr(i, num_rows);
    }
    return result;
}

}

Version data entries

257 entries across 257 versions & 1 rubygems

Version Path
trackler-2.2.1.39 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.38 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.37 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.36 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.35 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.34 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.33 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.32 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.31 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.30 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.29 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.28 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.27 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.26 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.25 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.24 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.23 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.22 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.21 tracks/cpp/exercises/crypto-square/example.cpp
trackler-2.2.1.20 tracks/cpp/exercises/crypto-square/example.cpp