Skip to content

Instantly share code, notes, and snippets.

@wmorgan
Created May 7, 2010 21:25
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 wmorgan/729d17559ac523fabf39 to your computer and use it in GitHub Desktop.
Save wmorgan/729d17559ac523fabf39 to your computer and use it in GitHub Desktop.
#define _XOPEN_SOURCE
#include <wchar.h>
#include <stdlib.h>
#include <ruby.h>
static VALUE width(VALUE self, VALUE string) {
wchar_t* dest;
int ret, len;
Check_Type(string, T_STRING);
len = RSTRING_LEN(string);
dest = (wchar_t*)calloc(len, sizeof(wchar_t));
ret = mbstowcs(dest, RSTRING_PTR(string), len);
if(ret == -1) return Qnil;
ret = wcswidth(dest, RSTRING_LEN(string));
free(dest);
return INT2NUM(ret);
}
void Init_WcsWidth() {
VALUE cWcsWidth;
cWcsWidth = rb_define_class("WcsWidth", rb_cObject);
rb_define_module_function(cWcsWidth, "width", width, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment