Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active January 29, 2017 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/6374ac0b08a316f8c4ace417c41d605d to your computer and use it in GitHub Desktop.
Save stknohg/6374ac0b08a316f8c4ace417c41d605d to your computer and use it in GitHub Desktop.
#
# Rot13を愚直に実装
#
filter Rot13([switch]$Decript) {
if ($_ -isnot [string]) {
$_ = $_.ToString()
}
$radix = 13
$output = ""
foreach ($c in [char[]]$_) {
switch ([int]$c) {
# A-Z
{$_ -ge 65 -and $_ -le 90} {
if ($Decript) {
$output += [char]((($_-90-$radix)%26)+90)
} else {
$output += [char]((($_-65+$radix)%26)+65)
}
}
# a-z
{$_ -ge 97 -and $_ -le 122} {
if ($Decript) {
$output += [char]((($_-122-$radix)%26)+122)
} else {
$output += [char]((($_-97+$radix)%26)+97)
}
}
Default {
$output += $c
}
}
}
Write-Output $output
}
# てすと
Import-Module Pester
Describe "Rot13のテスト" {
It "Rot13関数の基本形" {
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | Rot13 | Should Be "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"
"NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm" | Rot13 -Decrpt | Should Be "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
}
It "アルファベット以外の文字がある場合" {
"Hello World!" | Rot13 | Should Be "Uryyb Jbeyq!"
"Uryyb Jbeyq!" | Rot13 -Decrpt | Should Be "Hello World!"
"こんにちは世界" | Rot13 | Should Be "こんにちは世界"
"こんにちは世界" | Rot13 -Decrpt | Should Be "こんにちは世界"
}
}
#
# ワンライナー版
# ASCIIコードの変換テーブル $t で一発変換する富豪的実装です。
#
"Hello"-as[char[]]|%{$t=,$null*65+78..90+65..77+,$null*6+110..122+97..109;$o=""}{if($w=[char]$t[[int]$_]){$o+=$w}else{$o+=$_}}{$o}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment