Skip to content

Instantly share code, notes, and snippets.

@pascalduez
Last active April 9, 2018 16:52
Show Gist options
  • Save pascalduez/a5d718b311c452bd4f00 to your computer and use it in GitHub Desktop.
Save pascalduez/a5d718b311c452bd4f00 to your computer and use it in GitHub Desktop.
ROT13 implementation in Sass.
// ----
// Sass (v3.3.8)
// Compass (v1.0.0.alpha.18)
// ----
// ROT13 implementation in Sass.
// https://en.wikipedia.org/wiki/ROT13
@function rot13($str) {
$chars: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$result: '';
@for $i from 1 through str-length($str) {
$char: str-slice($str, $i, $i);
$index: str-index($chars, $char);
$pos: 0;
@if $index {
// $index > 26 == Lower case
// $index <= 26 == Upper case
$start: if($index > 26, 27, 1);
$pos: (($index - $start) + 13) % 26 + $start;
}
$result: $result + if($index, str-slice($chars, $pos, $pos), $char);
}
@return $result;
}
Test {
$decoded: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$encoded: 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm';
encode: rot13($decoded) == $encoded;
decode: rot13($encoded) == $decoded;
chars: rot13('ifzTf45:;Kù^é(àçerijAhZg')
}
@charset "UTF-8";
Test {
encode: true;
decode: true;
chars: "vsmGs45:;Xù^é(àçrevwNuMt";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment