This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -1,20 +1,21 @@
import std.stdio, std.traits;
pure S rot(S)(in S s, in int key) if (isSomeString!S) {
auto res = s.dup;
pure S rot(S)(in S s, in int key) pure /*nothrow*/
if (isSomeString!S) {
auto res = s.dup; // Not nothrow.
foreach (i, ref c; res) {
foreach (immutable i, ref c; res) {
if ('a' <= c && c <= 'z')
c = ((c - 'a' + key) % 26 + 'a');
else if ('A' <= c && c <= 'Z')
c = ((c - 'A' + key) % 26 + 'A');
}
return cast(S) res;
return res;
}
void main() {
int key = 3;
auto txt = "The five boxing wizards jump quickly";
enum key = 3;
immutable txt = "The five boxing wizards jump quickly";
writeln("Original: ", txt);
writeln("Encrypted: ", txt.rot(key));
writeln("Decrypted: ", txt.rot(key).rot(26 - key));

View file

@ -0,0 +1,17 @@
import std.stdio, std.ascii, std.string, std.algorithm;
string rot(in string s, in int key) pure /*nothrow*/ {
auto uppr = uppercase.dup.representation;
bringToFront(uppr[0 .. key], uppr[key .. $]);
auto lowr = lowercase.dup.representation;
bringToFront(lowr[0 .. key], lowr[key .. $]);
return s.translate(makeTrans(letters, cast(char[])(uppr ~ lowr)));
}
void main() {
enum key = 3;
immutable txt = "The five boxing wizards jump quickly";
writeln("Original: ", txt);
writeln("Encrypted: ", txt.rot(key));
writeln("Decrypted: ", txt.rot(key).rot(26 - key));
}