Skip to content

Instantly share code, notes, and snippets.

@ruxo
Last active February 9, 2023 23:12
Show Gist options
  • Select an option

  • Save ruxo/7f2cbd857742452bc74e8704e7906e55 to your computer and use it in GitHub Desktop.

Select an option

Save ruxo/7f2cbd857742452bc74e8704e7906e55 to your computer and use it in GitHub Desktop.
open System
open System.Text.RegularExpressions
let private encodingMatch (m: Match) =
match m.Value[0] with
| '+' -> "-"
| '/' -> "_"
| _ -> m.Value
let private decodingMatch (m: Match) =
match m.Value[0] with
| '-' -> "+"
| '_' -> "/"
| _ -> failwith "Impossible"
let s = "hello+world/na=="
let r = Regex.Replace(s.TrimEnd('='), "[+/]", encodingMatch) |> Dump // LINQPad's Dump
Regex.Replace(r, "[-_]", decodingMatch) + String('=', 4 - r.Length % 4) |> Dump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment