View Issue Details

IDProjectCategoryView StatusLast Update
0000434LDMud 3.5Efunspublic2019-09-24 10:25
Reporteriago3Assigned ToGnomi  
PrioritynormalSeverityfeatureReproducibilityN/A
Status closedResolutionwon't fix 
Summary0000434: New efun: wcswidth()
Description==Edits to func_spec=========================================================

int wcswidth(string|int*);

==Edits to efuns.c===========================================================

#include <wchar.h>
#include <wctype.h>

/*-------------------------------------------------------------------------*/
svalue_t *
f_wcswidth (svalue_t * sp)

/* EFUN wcswidth()
 *
 * int wcswidth(string str|int *)
 *
 * Returns the number of screen columns the given string or array of wide characters will take up
 */

{
    size_t charwidth, wcdest_width, i;
    wcdest_width=0;
    if (sp->type == T_STRING) {
        size_t wcdest_len, orig_len, len;
        wchar_t *wcdest, *current_wchar;
        char *orig_txt, *tmp_txt, **orig_txt_ptr;
        orig_len=mstrsize(sp->u.str);
        orig_txt=get_txt(sp->u.str);
        orig_txt_ptr=xalloc(sizeof(char *));
        wcdest=xalloc((orig_len+1)*sizeof(wchar_t));
        memset(wcdest,0,(orig_len+1)*sizeof(wchar_t));
        wcdest_len=0;
        len=0;
        tmp_txt=orig_txt;
        *orig_txt_ptr=orig_txt;
        while(len<orig_len) {
            wcdest_len+=mbsrtowcs(wcdest+wcdest_len, (const char **)orig_txt_ptr, orig_len-len, (mbstate_t *)NULL);
            len+=strlen(tmp_txt);
            if(len<orig_len) {
                len++;
                wcdest_len++;
                tmp_txt=orig_txt+len;
                *orig_txt_ptr=tmp_txt;
            }
        }
        current_wchar=wcdest;
        for(i=0;i<wcdest_len;i++) {
            if(*current_wchar=='\t') charwidth=8; /* tabs count as eight columns wide -- why? more accurate than zero! */
            else charwidth=wcwidth(*current_wchar);
            if(charwidth>0) wcdest_width+=charwidth;
            current_wchar++;
        }
        xfree(wcdest);
        xfree(orig_txt_ptr);
        free_string_svalue(sp);
        put_number(sp, (p_int)wcdest_width);
    } else if(sp->type == T_POINTER) {
        vector_t *vec;
        svalue_t *svp;
        vec=sp->u.vec;
        for(i = 0, svp = vec->item; ++i <= VEC_SIZE(vec); svp++) {
            if(svp->type == T_NUMBER) {
                if(svp->u.number=='\t') charwidth=8; /* tabs count as eight columns wide -- why? more accurate than zero! */
                else charwidth=wcwidth(svp->u.number);
                if(charwidth>0) wcdest_width+=charwidth;
            }
        }
        free_svalue(sp);
        put_number(sp,wcdest_width);
    }
    return sp;
} /* f_wcswidth() */
TagsNo tags attached.

Relationships

child of 0000432 resolvedGnomi LDMud 3.6 Add string efuns with multibyte character support 

Activities

iago3

2006-01-06 20:48

reporter   ~0000465

Explainer: This bug is desinged to add support for multibyte character sets to string efuns, as designed in bug#432

Issue History

Date Modified Username Field Change
2006-01-06 20:38 iago3 New Issue
2006-01-06 20:48 iago3 Note Added: 0000465
2008-07-02 03:15 Gnomi Project LDMud => LDMud 3.5
2008-07-02 03:17 Gnomi Relationship added child of 0000432
2019-09-24 10:25 Gnomi Assigned To => Gnomi
2019-09-24 10:25 Gnomi Status new => closed
2019-09-24 10:25 Gnomi Resolution open => won't fix