Changeset 815 for trunk/superstring


Ignore:
Timestamp:
03/28/08 22:32:22 (4 years ago)
Author:
wd
Message:

Interrim update for superstring bits.

Location:
trunk/superstring
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/superstring/include/superstring/superstring.h

    r812 r815  
    114114}; 
    115115 
    116 typedef struct _superstring_jumbo_ * superstring_jumbo; 
    117116typedef struct _superstring_small_ * superstring; 
    118117 
     
    198197/* Get the C representation of a superstring value. */ 
    199198static inline const unsigned char *ss_cstring(superstring str) { 
    200     if (str == NULL) 
     199    if (str == NULL) { 
     200        ss_errno = SS_ERROR_INVALID_PARAMETER; 
    201201        return NULL; 
    202  
     202    } 
     203 
     204    ss_errno = SS_ERROR_SUCCESS; 
    203205    return (str->flags & SUPERSTRING_JUMBO ? 
    204206            ((const unsigned char *)str) + sizeof(struct _superstring_jumbo_) : 
     
    208210/* Retrieve the length or size of a string */ 
    209211static inline size_t ss_length(superstring str) { 
    210     if (str == NULL) 
     212    if (str == NULL) { 
     213        ss_errno = SS_ERROR_INVALID_PARAMETER; 
    211214        return 0; /* bad answer.. */ 
    212  
     215    } 
     216 
     217    ss_errno = SS_ERROR_SUCCESS; 
    213218    return (size_t)(str->flags & SUPERSTRING_JUMBO ? 
    214             ((superstring_jumbo)str)->len : 
     219            ((struct _superstring_jumbo_ *)str)->len : 
    215220            str->len); 
    216221} 
    217222 
    218223static inline size_t ss_size(superstring str) { 
    219     if (str == NULL) 
     224    if (str == NULL) { 
     225        ss_errno = SS_ERROR_INVALID_PARAMETER; 
    220226        return 0; /* bad answer.. */ 
    221  
     227    } 
     228 
     229    ss_errno = SS_ERROR_SUCCESS; 
    222230    return (size_t)(str->flags & SUPERSTRING_JUMBO ? 
    223             ((superstring_jumbo)str)->size : 
     231            ((struct _superstring_jumbo_ *)str)->size : 
    224232            str->size); 
     233} 
     234 
     235/* Retrieve or set the character at 'offset' in the string */ 
     236static inline unsigned char ss_char(superstring str, size_t index) { 
     237    if (str == NULL || index >= str->len) { 
     238        ss_errno = SS_ERROR_INVALID_PARAMETER; 
     239        return 0; 
     240    } 
     241    return (str->flags & SUPERSTRING_JUMBO ? 
     242            *((unsigned char *)str + sizeof(struct _superstring_jumbo_) + index) : 
     243            *((unsigned char *)str + sizeof(struct _superstring_small_) + index)); 
     244} 
     245static inline int ss_setchar(superstring str, size_t index, char c) { 
     246    if (str == NULL || index >= str->len) 
     247        return (ss_errno = SS_ERROR_INVALID_PARAMETER); 
     248 
     249    if (str->flags & SUPERSTRING_JUMBO) 
     250        *((unsigned char *)str + sizeof(struct _superstring_jumbo_) + index) = c; 
     251    else 
     252        *((unsigned char *)str + sizeof(struct _superstring_small_) + index) = c; 
     253 
     254    return (ss_errno = SS_ERROR_SUCCESS); 
    225255} 
    226256 
     
    249279 
    250280/*****************************************************************************/ 
     281/* Shorten or lengthen a superstring by trimming or appending data.  For 
     282 * trim, if the length is greater than the current length spaces (or a 
     283 * specified character) are appended as necessary. */ 
     284int ss_trim_ex(superstring str, size_t length, char fill); 
     285static inline int ss_trim(superstring str, size_t length) { 
     286    return ss_trim_ex(str, length, ' '); 
     287} 
     288 
     289/*****************************************************************************/ 
     290/* Append data to a superstring.  There are several variants provided which 
     291 * allow for setting maximum string length, using C strings, etc */ 
     292#define SS_APPEND_CSTRING           (0x80000000) 
     293int ss_append_ex(superstring *lstr, superstring rstr, int flags, size_t length); 
     294 
     295/* Append a string with no concern for length */ 
     296static inline int ss_append(superstring *lstr, superstring rstr) { 
     297    return ss_append_ex(lstr, rstr, 0, 0); 
     298} 
     299static inline int ss_append_cs(superstring *lstr, const unsigned char *rstr) { 
     300    return ss_append_ex(lstr, (superstring)rstr, SS_APPEND_CSTRING, 0); 
     301} 
     302 
     303/* Append a string but do not allow lstr to exceed 'length' */ 
     304static inline int ss_nappend(superstring *lstr, superstring rstr, 
     305        size_t length) { 
     306    return ss_append_ex(lstr, rstr, 0, length); 
     307} 
     308static inline int ss_nappend_cs(superstring *lstr, const unsigned char *rstr, 
     309        size_t length) { 
     310    return ss_append_ex(lstr, (superstring)rstr, SS_APPEND_CSTRING, length); 
     311} 
     312 
     313/*****************************************************************************/ 
    251314/* Comparison function, with various wrappers: 
    252315   - ss_compare_ex: Compare with flags and length.  If length is 0 it will be 
  • trunk/superstring/include/superstring_private.h

    r812 r815  
    6060    str->flags = istr->flags; 
    6161    if (istr->flags & SUPERSTRING_JUMBO) { 
    62         superstring_jumbo jstr = (superstring_jumbo)str; 
     62        struct _superstring_jumbo_ *jstr = (struct _superstring_jumbo_ *)str; 
    6363        jstr->len = istr->len; 
    6464        jstr->size = istr->size; 
     
    8888 
    8989/* Function to grow strings */ 
    90 int ss_grow(ss_internal *istr); 
     90int ss_grow(ss_internal *istr, superstring *outstr); 
    9191 
    9292/* Handy stuff for handling printf-like functions and warnings in gcc (thanks 
  • trunk/superstring/source/SConscript

    r812 r815  
    1717sourcelist = Split(''' 
    1818allocator.c 
     19append.c 
    1920chartables.c 
    2021compare.c 
  • trunk/superstring/source/allocator.c

    r812 r815  
    4949 
    5050/* This function will grow the data pointed to by an ss_internal structure 
    51  * as need-be.  It will also correctly handle structure type transitioning.  */ 
    52 int ss_grow(ss_internal *istr) { 
     51 * as need-be.  It will also correctly handle structure type transitioning 
     52 * and assign the correct memory address to 'outstr' (if provided) */ 
     53int ss_grow(ss_internal *istr, superstring *outstr) { 
    5354    size_t grow; 
    5455    size_t memsize; 
     
    115116    } 
    116117 
    117     /* map in the updated values */ 
     118    /* map in the updated values.  Mostly we can do this by calling 
     119     * ssi_to_ss, but we also need to update the data pointer of istr to 
     120     * reflect the new address of the data. */ 
    118121    ssi_to_ss(istr, istr->str); 
     122    istr->data = ss_cstring(str); 
     123    if (outstr != NULL) 
     124        *outstr = istr->str; 
     125 
    119126    ss_returnerror(SS_ERROR_SUCCESS); 
    120127} 
     
    151158    istr.size = size; 
    152159 
    153     if (ss_grow(&istr) != SS_ERROR_SUCCESS) 
     160    if (ss_grow(&istr, NULL) != SS_ERROR_SUCCESS) 
    154161        return NULL; 
    155162 
    156163    /* Copy our data in */ 
    157     cstr = ss_cstring(istr.str); 
     164    cstr = (unsigned char *)ss_cstring(istr.str); 
    158165    memcpy((void *)cstr, value, istr.len); 
    159166    cstr[istr.len] = '\0'; 
  • trunk/superstring/source/test.c

    r812 r815  
    2929void test_anonymous(void); 
    3030void test_compare(void); 
     31void test_setchar(void); 
     32void test_append(void); 
    3133 
    3234#define TEST_INIT_VALUE "Hello, world!" 
     
    156158} 
    157159 
     160void test_setchar(void) { 
     161    superstring str = ss_create(TEST_INIT_VALUE); 
     162 
     163    tassert(ss_char(str, 0) == TEST_INIT_VALUE[0]); 
     164    tassert(ss_setchar(str, 0, 'y') == SS_ERROR_SUCCESS); 
     165    tassert(ss_char(str, 0) == 'y'); 
     166    tassert(ss_char(str, 0) != TEST_INIT_VALUE[0]); 
     167 
     168    tassert(ss_setchar(str, 1024, '!') == SS_ERROR_INVALID_PARAMETER); 
     169} 
     170 
     171void test_append(void) { 
     172    superstring str = ss_create("abc"); 
     173    superstring str2 = ss_create("123"); 
     174 
     175    tassert(ss_append(&str, NULL) == SS_ERROR_INVALID_PARAMETER); 
     176    tassert(ss_append(&str, str2) == SS_ERROR_SUCCESS); 
     177    tassert(ss_compare_cs(str, "abc123") == 0); 
     178    ss_destroy(&str); 
     179    ss_destroy(&str2); 
     180 
     181    /* test an allocation which forces growth */ 
     182    size_t len; 
     183    str = ss_create("abc"); 
     184    len = ss_length(str); 
     185    for (int i = 0;i < 100;i++) { 
     186        tassert(ss_append_cs(&str, "123") == SS_ERROR_SUCCESS); 
     187        len += 3; /* length should grow by 3 .. */ 
     188    } 
     189    tassert(ss_length(str) == len); 
     190 
     191    ss_destroy(&str); 
     192} 
     193 
    158194int main(void) { 
    159195 
     
    162198    runtest(anonymous); 
    163199    runtest(compare); 
     200    runtest(setchar); 
     201    runtest(append); 
    164202 
    165203    /* If we made it here we are at the end!  Let them know we succeeded. */ 
Note: See TracChangeset for help on using the changeset viewer.