Skip to content

Instantly share code, notes, and snippets.

@polynomialspace
Last active March 4, 2020 21:21
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 polynomialspace/ab1a45bf592f9ab3661eea9c0eb7f789 to your computer and use it in GitHub Desktop.
Save polynomialspace/ab1a45bf592f9ab3661eea9c0eb7f789 to your computer and use it in GitHub Desktop.
"ascii to fullwidth" convert plaintext standard chars to fullwidth
package main
import (
"fmt"
"os"
"bufio"
"strings"
)
func atofw(in string) (out string) {
tofw := func(r rune) rune {
switch {
case r >= '!' && r <= 'z':
return r - '!' + '!'
case r == ' ': /* fullwidth space is noncontiguous in utf8 */
return ' ';
}
return r
}
return strings.Map(tofw, in)
}
func main() {
words := strings.Join(os.Args[1:], " ")
/* Oneshot mode if supplied by cmdline args */
if words != "" {
fmt.Println(atofw(words))
return
}
/* Otherwise loop Stdin */
scanner := bufio.NewScanner(os.Stdin)
for fmt.Printf("> "); scanner.Scan(); fmt.Printf("> ") {
words := scanner.Text()
fmt.Println(atofw(words))
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment