Sha256: 706f872523ca1923fad99ebb20cc92bb97adb243d278a9bd99d3b17c67472326
Contents?: true
Size: 1.62 KB
Versions: 139
Compression:
Stored size: 1.62 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 = 0; 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 s{normalized_cipher_text()}; if (!s.empty()) { s.erase(remove(s.begin(), s.end(), ' '), s.end()); } return s; } string cipher::normalized_cipher_text() const { const auto num_rows = size(); const auto plain = plain_text_segments(); string result; for (size_t i = 0; i < num_rows; ++i) { for (string const& s : plain) { if (s.length() > i) { result += s[i]; } else { result += ' '; } } result += ' '; } if (!result.empty()) { result.pop_back(); } return result; } }
Version data entries
139 entries across 139 versions & 1 rubygems