Skip to content

Instantly share code, notes, and snippets.

@alecjacobson
Created April 12, 2021 17:24
Show Gist options
  • Save alecjacobson/b9ace772006a923b680d0d0095c8090a to your computer and use it in GitHub Desktop.
Save alecjacobson/b9ace772006a923b680d0d0095c8090a to your computer and use it in GitHub Desktop.
Convert ascii text from standard input into fixed-width unicode characters.
#!/usr/bin/env ruby
#
# Convert ascii text from standard input into fixed-width unicode characters.
#
# http://xahlee.info/comp/unicode_full-width_chars.html
fixedWidth = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.:;!?"'`^~ ̄_&@#%+-*=<>()[]{}⦅⦆|¦/\¬$£¢₩¥ '
ascii = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.:;!?"\'`^~‾_&@#%+-*=<>()[]{}«»|¦/\\¬$£¢₩¥ '
input = STDIN.read
output = input.split('').map do |c|
i = ascii.index(c)
i.nil? ? c : fixedWidth[i]
end
puts output.join('')
@yig
Copy link

yig commented Apr 12, 2021

#!/usr/bin/env python3

import sys

fixedWidth = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.:;!?"'`^~ ̄_&@#%+-*=<>()[]{}⦅⦆|¦/\¬$£¢₩¥ \n\r'
ascii = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.:;!?"\'`^~‾_&@#%+-*=<>()[]{}«»|¦/\\¬$£¢₩¥ \n\r'
ascii2fixedWidth = dict( zip( ascii, fixedWidth ) )

for line in sys.stdin:
    fixed = ''.join( [ ascii2fixedWidth.get( c, '?' ) for c in line ] )
    print( fixed, file = sys.stdout, end = '' )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment