Skip to content

Instantly share code, notes, and snippets.

@aprescott
Created May 29, 2011 01:07
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 aprescott/997376 to your computer and use it in GitHub Desktop.
Save aprescott/997376 to your computer and use it in GitHub Desktop.
Convert strings to their fullwidth versions
# it turns out that "A".ord - "A".ord is constant when you replace the two characters
# with, say "B" and "B", so ("B".ord + DIFFERENCE).to_s(16) == "ff22" and "\uff22"
# is "B". (Thanks, Unicode consortium.)
#
# an exception is U+0020 space, which has IDEOGRAPHIC SPACE U+3000, but the two don't
# differ by DIFFERENCE == 65248 == 0xfee0, so you have to replace 20+65248 == 0xff00
# with 0x3000.
DIFFERENCE = 65248
def fullwidth(str)
str.unpack("U*").map { |e| e + DIFFERENCE }.pack("U*").gsub("\uFF00", "\u3000")
end
fullwidth "B" #=> "B"
fullwidth "Goodbye, World... (parenthetical remark~~~ ...)"
#=> "Goodbye, World... (parenthetical remark~~~ ...)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment