Created
May 29, 2011 01:07
-
-
Save aprescott/997376 to your computer and use it in GitHub Desktop.
Convert strings to their fullwidth versions
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
# 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