redis source notes (continually updated)

1. Reset string

void sdsclear (SDS S) { 

    // remove sdshdr 
    struct sdshdr SH * = ( void *) (S- ( the sizeof ( struct sdshdr))); 

    // recalculate properties 
    SH-> Free + = SH-> len; 
    SH - > len = 0 ; 

    // the character located at the front end (corresponding to an inert deleted in buf) 
    SH-> buf [ 0 ] = ' \ 0 ' ; 
}

2. Mobile String

/ * 
 * Sds left and right ends of the trim, which clears all characters specified cset 
 * 
 * such sdsstrim (xxyyabcyyxy, "xy") returns "ABC" 
 * 
 * Complexity: 
 * T = O (M * N), M SDS is a length, N being cset length. 
 * / 
SDS sdstrim (SDS S, const  char * CSet) {
     struct sdshdr SH * = ( void *) (S- ( the sizeof ( struct sdshdr)));
     char * Start, End *, * SP, * EP; 
    size_t len ; 

    // set the record pointer and 
    SP = Start = S; 
    EP = S + = End sdslen (S) - . 1 ; 

    // trim, T = O (N ^ 2) 
    the while(SP <= End && the strchr (CSet, SP *)) SP ++ ;
     the while (EP> Start the strchr && (CSet, EP *)) ep-- ; 

    // calculate the remaining length of the string after completion trim 
    len = (sp> EP)? 0 : ((EP-SP) + . 1 ); 
    
    // if necessary, the contents of the string forward
     // T = O (N) 
    IF ! (SH-> buf = SP) memmove (SH-> buf , SP, len); 

    // add terminator 
    SH-> buf [len] = ' \ 0 ' ; 

    // update attribute 
    SH-> Free = SH-> Free + (SH-> len len); 
    SH -> = len len; 

    // after the return pruning sds
    return s;
}

3. Intercept string

void sdsrange(sds s, int start, int end) {
    struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
    size_t newlen, len = sdslen(s);

    if (len == 0) return;
    if (start < 0) {
        start = len+start;
        if (start < 0) start = 0;
    }
    if (end < 0) {
        end = len+end;
        if (end < 0) end = 0;
    }
    newlen = (start > end) ? 0 : (end-start)+1;
    if (newlen != 0) {
        if (start >= (signed)len) {
            newlen = 0;
        } else if (end >= (signed)len) {
            end = len-1;
            newlen = (start > end) ? 0 : (end-start)+1;
        }
    } the else { 
        Start = 0 ; 
    } 

    // if necessary, moving the string
     // T = O (N) 
    IF (Start && newlen) memmove (SH-> buf, SH-> buf + Start, newlen); 

    // Add terminator 
    SH-> buf [newlen] = 0 ; 

    // update attribute 
    SH-> Free = SH-> Free + (SH-> len newlen); 
    SH -> len = newlen; 
}

 

Guess you like

Origin www.cnblogs.com/Jawen/p/11264829.html