Last active
March 4, 2020 21:21
-
-
Save polynomialspace/ab1a45bf592f9ab3661eea9c0eb7f789 to your computer and use it in GitHub Desktop.
"ascii to fullwidth" convert plaintext standard chars to fullwidth
This file contains 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
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