Last active
August 26, 2020 03:20
-
-
Save HumanEquivalentUnit/b7289b5ffb3a7e4b1ece8041d4dd72cc to your computer and use it in GitHub Desktop.
Import APLCart table.tsv and decode the tio.run links to check the code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# TIO Link verify helper for APLCart. | |
# Open this file in PowerShell ISE on Windows (or other Unicode-aware console, e.g. VS Code + PowerShell extension, new Windows Terminal; not basic PowerShell window) | |
# Update $tablePath to point to the table. | |
# Update the string of codes to find. | |
# Run. | |
$tablePath = "c:\sc\aplcart\table.tsv" | |
$codesToFind = @' | |
0∘×N | |
3.×N | |
0∘⌈N | |
'@ | |
function Get-CodeFromTio { | |
[CmdletBinding()] | |
param($TIOLink) | |
$fieldSeparator = [char]255 | |
# remove domain parts from URL | |
$hash = ($TIOLink -as [uri]).Fragment.Substring(1) | |
# port of some of the state decoding code from TIO.run | |
$hashArray = $hash.split("#") | |
$stateString = if ($hashArray[0]) { $hashArray[0] + $fieldSeparator } else { '' } | |
if ($hashArray[1]) { | |
# convert hash to base64, pad length to multipe of 4 with =, decode to bytes | |
$b64 = [Net.WebUtility]::UrlDecode($hashArray[1]).Replace('@', '+').Replace('-', '+').Replace('_', '/') | |
$deflatedBytes = [Convert]::FromBase64String($b64 + ("=" * ((4-($b64.Length % 4)) % 4))) | |
# inflate() (decompress) bytes | |
$inflatedStream = [IO.MemoryStream]::new() | |
[IO.Compression.DeflateStream]::new([IO.MemoryStream]::new($deflatedBytes), [IO.Compression.CompressionMode]::Decompress).CopyTo($inflatedStream) | |
# convert byte array to byte string | |
$byteString = -join [char[]]$inflatedStream.ToArray() | |
$stateString += $byteString | |
} | |
# extract fields from state string | |
$rFieldString = [regex]::new("^[\x00-\xf3\xff]+", [System.Text.RegularExpressions.RegexOptions]::Compiled) | |
$languageId, $fields = $rFieldString.Matches($stateString).Groups[0].Value.Split($fieldSeparator).foreach{ | |
[System.Text.Encoding]::UTF8.GetString([char[]]$_) | |
} | |
# generate output string | |
[String]::Join("`n", @( | |
if ($fields[0]) { "HEADER:`n" + ($fields[0] -replace '(?m)^', ' ') } | |
if ($fields[1]) { "CODE:`n" + ($fields[1] -replace '(?m)^', ' ') } | |
if ($fields[2]) { "FOOTER:`n" + ($fields[2] -replace '(?m)^', ' ') } | |
if ($fields[3]) { "INPUT:`n" + ($fields[3] -replace '(?m)^', ' ') } | |
)) | |
} | |
[System.Collections.ArrayList]$codesArray = $codesToFind -split "`r?`n" | foreach-object {$_.Trim()} | Where-Object {"" -ne $_} | |
Import-Csv -Path $tablePath -Delimiter "`t" | | |
ForEach-Object { | |
if ($_.Syntax -in $codesArray) { | |
$codesArray.Remove($_.Syntax) | |
$_ | |
} | |
} | | |
Select-Object -Property Syntax, TIO, @{Label="TIOCode"; Expression={Get-CodeFromTio $_.TIO}} | | |
#Out-GridView -OutputMode Multiple | | |
Format-List -Property * | |
$codesArray | ForEach-Object { | |
Write-Warning -Verbose -Message "$_ Not found" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment