View Issue Details

IDProjectCategoryView StatusLast Update
0000437LDMud 3.5Efunspublic2019-09-24 08:26
Reporteriago3Assigned ToGnomi  
PrioritynormalSeverityfeatureReproducibilityN/A
Status closedResolutionfixed 
Summary0000437: New efun: substr()
Description/*-------------------------------------------------------------------------*/
svalue_t *
f_substr (svalue_t * sp)

/* EFUN substr()
 *
 * int substr(string str, int begin, int len)
 *
 * Returns the substring of the multibyte string from the letter at begin, for len characters
 */

{
    wchar_t *wcdest, *current_wchar, **wcdest_ptr;
    char *orig_txt, **orig_txt_ptr, *tmp_txt, *mbdest;
    size_t len, orig_len, wcdest_len, conv_len;
    string_t *result;
    long int start, chars;
    orig_len=mstrsize((sp-2)->u.str);
    orig_txt=get_txt((sp-2)->u.str);
    start=(sp-1)->u.number;
    chars=sp->u.number;
    orig_txt_ptr=xalloc(sizeof(char *));
    wcdest=xalloc((orig_len+1)*sizeof(wchar_t));
    wcdest_ptr=xalloc(sizeof(wchar_t *));
    memset(wcdest,0,(orig_len+1)*sizeof(wchar_t));
    *orig_txt_ptr=orig_txt;
    tmp_txt=orig_txt;
    len=0;
    wcdest_len=0;
    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;
        }
    }
    if(start<0) start=0;
    if(start>(long int)wcdest_len) start=(long int)wcdest_len;
    if(chars<0) chars=(long int)wcdest_len-start;
    if(start+chars>(long int)wcdest_len) chars=(long int)wcdest_len-start;
    memset(wcdest+start+chars,0,sizeof(wchar_t));
    result=mstring_new_string("");
    current_wchar=wcdest+start;
    *wcdest_ptr=current_wchar;
    conv_len=0;
    while(conv_len<chars) {
        len=wcsrtombs(NULL,(const wchar_t **)wcdest_ptr,0,(mbstate_t *)NULL);
        mbdest=xalloc(len+1);
        memset(mbdest,0,len+1);
        wcsrtombs(mbdest,(const wchar_t **)wcdest_ptr,len,(mbstate_t *)NULL);
        conv_len+=wcslen(current_wchar);
        result=mstring_append_txt(result,mbdest,len);
        if(conv_len<chars) {
            conv_len++;
            current_wchar=wcdest+start+conv_len;
            *wcdest_ptr=current_wchar;
            result=mstring_append_txt(result,"\0",1);
        }
        xfree(mbdest);
    }
    free_svalue(sp--);
    free_svalue(sp--);
    free_string_svalue(sp);
    put_string(sp, result);
    return sp;
} /* f_substr() */
TagsNo tags attached.

Relationships

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

Activities

iago3

2006-01-06 19:49

reporter   ~0000468

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

Gnomi

2019-09-24 08:26

manager   ~0002482

Implemented as [..] operator.

Issue History

Date Modified Username Field Change
2006-01-06 19:42 iago3 New Issue
2006-01-06 19:49 iago3 Note Added: 0000468
2008-07-02 01:15 Gnomi Project LDMud => LDMud 3.5
2008-07-02 01:16 Gnomi Relationship added parent of 0000432
2008-07-02 01:18 Gnomi Relationship deleted parent of 0000432
2008-07-02 01:18 Gnomi Relationship added child of 0000432
2019-09-24 08:26 Gnomi Assigned To => Gnomi
2019-09-24 08:26 Gnomi Status new => closed
2019-09-24 08:26 Gnomi Resolution open => fixed
2019-09-24 08:26 Gnomi Note Added: 0002482