module Windows::Unicode

Constants

ANSI_CHARSET
ARABIC_CHARSET
CHINESEBIG5_CHARSET
CP_ACP
CP_MACCP
CP_OEMCP
CP_SYMBOL
CP_THREAD_ACP
CP_UTF7
CP_UTF8
DEFAULT_CHARSET
EASTEUROPE_CHARSET
GB2312_CHARSET
GREEK_CHARSET
HANGEUL_CHARSET
HANGUL_CHARSET
HEBREW_CHARSET
IS_TEXT_UNICODE_ASCII16
IS_TEXT_UNICODE_CONTROLS
IS_TEXT_UNICODE_DBCS_LEADBYTE
IS_TEXT_UNICODE_ILLEGAL_CHARS
IS_TEXT_UNICODE_NOT_ASCII_MASK
IS_TEXT_UNICODE_NOT_UNICODE_MASK
IS_TEXT_UNICODE_NULL_BYTES
IS_TEXT_UNICODE_ODD_LENGTH
IS_TEXT_UNICODE_REVERSE_ASCII16
IS_TEXT_UNICODE_REVERSE_CONTROLS
IS_TEXT_UNICODE_REVERSE_MASK
IS_TEXT_UNICODE_REVERSE_SIGNATURE
IS_TEXT_UNICODE_REVERSE_STATISTICS
IS_TEXT_UNICODE_SIGNATURE
IS_TEXT_UNICODE_STATISTICS
IS_TEXT_UNICODE_UNICODE_MASK
JOHAB_CHARSET
MB_COMPOSITE
MB_ERR_INVALID_CHARS
MB_PRECOMPOSED
MB_USEGLYPHCHARS
OEM_CHARSET
RUSSIAN_CHARSET
SHIFTJIS_CHARSET
SYMBOL_CHARSET
TCI_SRCCHARSET
TCI_SRCCODEPAGE
TCI_SRCFONTSIG
TCI_SRCLOCALE
THAI_CHARSET
TURKISH_CHARSET
VIETNAMESE_CHARSET
WC_COMPOSITECHECK
WC_DEFAULTCHAR
WC_DISCARDNS
WC_NO_BEST_FIT_CHARS
WC_SEPCHARS

Private Instance Methods

multi_to_wide(string, encoding=nil) click to toggle source

Maps a character string to a UTF-16 (wide) character string using the specified encoding. If no encoding is specified, then CP_UTF8 is used if $KCODE (or the encoding name in Ruby 1.9.x) is set to UTF8. Otherwise, CP_ACP is used.

If the function fails it simply returns the string as-is.

# File lib/windows/unicode.rb, line 96
def multi_to_wide(string, encoding=nil)
  return nil unless string
  raise TypeError unless string.is_a?(String)
  return string if IsTextUnicode(string, string.size, nil)

  unless encoding
    if RUBY_VERSION.to_f >= 1.9
      encoding = (string.encoding.name == 'UTF-8') ? CP_UTF8 : CP_ACP
    else
      encoding = ($KCODE == 'UTF8') ? CP_UTF8 : CP_ACP
    end
  end

  int = MultiByteToWideChar(encoding, 0, string, -1, nil, 0)

  # Trailing nulls are retained
  if int > 0
    buf = ' ' * int * 2
    MultiByteToWideChar(encoding, 0, string, -1, buf, int)
    buf
  else
    raise ArgumentError, get_last_error
  end
end
wide_to_multi(wstring, encoding=nil) click to toggle source

Maps a wide character string to a new character string using the specified encoding. If no encoding is specified, then CP_UTF8 is used if $KCODE (or the encoding name in Ruby 1.9.x) is set to UTF8. Otherwise, CP_ACP is used.

If the function fails it simply returns the string as-is.

# File lib/windows/unicode.rb, line 128
def wide_to_multi(wstring, encoding=nil)
  return nil unless wstring
  raise TypeError unless wstring.is_a?(String)

  unless encoding
    if RUBY_VERSION.to_f >= 1.9
      encoding = (wstring.encoding.name == 'UTF-8') ? CP_UTF8 : CP_ACP
    else
      encoding = ($KCODE == 'UTF8') ? CP_UTF8 : CP_ACP
    end
  end

  # Add a trailing double null if necessary
  wstring << "\000\000" if wstring[-1].chr != "\000"

  int = WideCharToMultiByte(encoding, 0, wstring, -1, 0, 0, nil, nil)

  # Trailing nulls are stripped
  if int > 0
    buf = ' ' * int
    WideCharToMultiByte(encoding, 0, wstring, -1, buf, strlen(buf), nil, nil)
    buf[ /^[^\0]*/ ]
  else
    raise ArgumentError, get_last_error
  end
end