Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewarren committed Jul 25, 2017
0 parents commit 603be3f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Punycoder

Converts punycode domains to unicode and vice-versa.

## Installation

```
go get github.com/jakewarren/punycoder
```

## Usage

Simply provide either a punycode (IDN) or Unicode domain and it will be converted automagically.

```
$ punycoder bücher.example.com
xn--bcher-kva.example.com
$ punycoder xn--bcher-kva.example.com
bücher.example.com
```
48 changes: 48 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"os"
"regexp"

"golang.org/x/net/idna"
)

var usage = `
██████╗ ██╗ ██╗███╗ ██╗██╗ ██╗ ██████╗ ██████╗ ██████╗ ███████╗██████╗
██╔══██╗██║ ██║████╗ ██║╚██╗ ██╔╝██╔════╝██╔═══██╗██╔══██╗██╔════╝██╔══██╗
██████╔╝██║ ██║██╔██╗ ██║ ╚████╔╝ ██║ ██║ ██║██║ ██║█████╗ ██████╔╝
██╔═══╝ ██║ ██║██║╚██╗██║ ╚██╔╝ ██║ ██║ ██║██║ ██║██╔══╝ ██╔══██╗
██║ ╚██████╔╝██║ ╚████║ ██║ ╚██████╗╚██████╔╝██████╔╝███████╗██║ ██║
╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝
Converts punycode to unicode and vice-versa.
Usage:
punycoder <domain>
`

func main() {

if len(os.Args) != 2 {
fmt.Println(usage)
os.Exit(1)
}

domain := os.Args[1]
var re = regexp.MustCompile(`^xn--`)

switch {
case domain == "-h", domain == "--help":
fmt.Println(usage)
case re.Match([]byte(domain)):
//punycode was provided, convert to Unicode
unidomain, _ := idna.ToUnicode(domain)
fmt.Printf("%s\n", unidomain)
default:
//convert to ASCII punycode
asciidomain, _ := idna.ToASCII(domain)
fmt.Printf("%s\n", asciidomain)
}

}

0 comments on commit 603be3f

Please sign in to comment.