1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include <wchar.h>
MODULE = Text::CharWidth PACKAGE = Text::CharWidth
int
mbwidth(str)
const char * str
CODE:
wchar_t wstr[2];
int r;
r = mbstowcs(wstr, str, 1);
if (r == -1) RETVAL = -1;
else if (r == 0) RETVAL = 0;
else RETVAL = wcwidth(wstr[0]);
OUTPUT:
RETVAL
int
mbswidth(str)
const char *str
CODE:
int r = 0, len, len2;
wchar_t wstr[2];
len = strlen(str);
RETVAL = 0;
while (*str != 0) {
r = mbstowcs(wstr, str, 1);
if (r == 0 || r == -1) {RETVAL = -1; break;}
RETVAL += wcwidth(wstr[0]);
len2 = mblen(str, len+1);
if (len2 <= 0) {RETVAL = -1; break;}
str += len2; len -= len2;
}
OUTPUT:
RETVAL
int
mblen(str)
const char *str
CODE:
RETVAL = mblen(str, strlen(str));
OUTPUT:
RETVAL
|