- Place the rot13 code into its own method
This commit is contained in:
Tristan B. Velloza Kildaire 2023-06-17 22:48:42 +02:00
parent cd84a7e38c
commit 737833dfd7
1 changed files with 18 additions and 6 deletions

View File

@ -28,20 +28,32 @@ public final class Rot13 : Mod
public override void react(Message fullMessage, string channel, string messageBody)
{
import std.string : indexOf, strip;
import std.algorithm : map;
import std.conv : to;
long idx = indexOf(messageBody, commandStr);
string toBeSecretified = messageBody[idx+commandStr.length..$];
getBot().channelMessage(
to!(string)(toBeSecretified.map!((ch) {
getBot().channelMessage(doRot(toBeSecretified), channel);
}
/**
* Computes the Rot13 of the given input text
*
* Params:
* text = the input text to transform
* Returns: the transformed text
*/
private string doRot(string text)
{
import std.algorithm : map;
import std.conv : to;
return to!(string)(toBeSecretified.map!((ch) {
if (ch >= 'a' && ch <= 'z')
ch = (ch - 'a' + 13) % 26 + 'a';
if (ch >= 'A' && ch <= 'Z')
ch = (ch - 'A' + 13) % 26 + 'A';
return ch.to!char;
})), channel
);
}));
}
}