-
-
Save wmorgan/729d17559ac523fabf39 to your computer and use it in GitHub Desktop.
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
#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