From: William Morgan Date: 2010-05-10T10:29:44+09:00 Subject: wcswidth, ruby 1.9, and string encodings Hello all, I need some advice about how to use wcswidth on Ruby 1.9 strings. Wcswidth returns the column width of a string, i.e. how much of the horizontal space on the screen a string takes up when printed to a terminal. Being able to calculate column width is crucial for any console-based program that deals with non-ASCII characters. For example, Chinese characters typically take up two columns. So, I'd like to write a gem that provides wcswidth to Ruby. I have a simple version that works: https://gist.github.com/729d17559ac523fabf39 Unfortunately, this code requires that the string's encoding match your LC_CTYPE, because mbstowcs relies on LC_CTYPE to produce wchar_t's. If those don't match, you'll either get nothing, or, possibly, a wrong answer. So, is there a mapping between Ruby string encodings and LC_CTYPE values? If so, I could at least check that the conversion is done correctly (or possibly even skip the mbstowcs step, if I know what wchar_t is and what encoding corresponds to it--of course what wchar_t means is system-dependent.) Or is there a better way to do this than using mbstowcs? Or heck, is there already a way of getting string column width in Ruby? It seems like there should be, but I don't see one. Thanks! -- William