Changeset 815
- Timestamp:
- 03/28/08 22:32:22 (4 years ago)
- Location:
- trunk/superstring
- Files:
-
- 5 edited
-
include/superstring/superstring.h (modified) (4 diffs)
-
include/superstring_private.h (modified) (2 diffs)
-
source/SConscript (modified) (1 diff)
-
source/allocator.c (modified) (3 diffs)
-
source/test.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/superstring/include/superstring/superstring.h
r812 r815 114 114 }; 115 115 116 typedef struct _superstring_jumbo_ * superstring_jumbo;117 116 typedef struct _superstring_small_ * superstring; 118 117 … … 198 197 /* Get the C representation of a superstring value. */ 199 198 static inline const unsigned char *ss_cstring(superstring str) { 200 if (str == NULL) 199 if (str == NULL) { 200 ss_errno = SS_ERROR_INVALID_PARAMETER; 201 201 return NULL; 202 202 } 203 204 ss_errno = SS_ERROR_SUCCESS; 203 205 return (str->flags & SUPERSTRING_JUMBO ? 204 206 ((const unsigned char *)str) + sizeof(struct _superstring_jumbo_) : … … 208 210 /* Retrieve the length or size of a string */ 209 211 static inline size_t ss_length(superstring str) { 210 if (str == NULL) 212 if (str == NULL) { 213 ss_errno = SS_ERROR_INVALID_PARAMETER; 211 214 return 0; /* bad answer.. */ 212 215 } 216 217 ss_errno = SS_ERROR_SUCCESS; 213 218 return (size_t)(str->flags & SUPERSTRING_JUMBO ? 214 ((s uperstring_jumbo)str)->len :219 ((struct _superstring_jumbo_ *)str)->len : 215 220 str->len); 216 221 } 217 222 218 223 static inline size_t ss_size(superstring str) { 219 if (str == NULL) 224 if (str == NULL) { 225 ss_errno = SS_ERROR_INVALID_PARAMETER; 220 226 return 0; /* bad answer.. */ 221 227 } 228 229 ss_errno = SS_ERROR_SUCCESS; 222 230 return (size_t)(str->flags & SUPERSTRING_JUMBO ? 223 ((s uperstring_jumbo)str)->size :231 ((struct _superstring_jumbo_ *)str)->size : 224 232 str->size); 233 } 234 235 /* Retrieve or set the character at 'offset' in the string */ 236 static 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 } 245 static 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); 225 255 } 226 256 … … 249 279 250 280 /*****************************************************************************/ 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. */ 284 int ss_trim_ex(superstring str, size_t length, char fill); 285 static 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) 293 int ss_append_ex(superstring *lstr, superstring rstr, int flags, size_t length); 294 295 /* Append a string with no concern for length */ 296 static inline int ss_append(superstring *lstr, superstring rstr) { 297 return ss_append_ex(lstr, rstr, 0, 0); 298 } 299 static 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' */ 304 static inline int ss_nappend(superstring *lstr, superstring rstr, 305 size_t length) { 306 return ss_append_ex(lstr, rstr, 0, length); 307 } 308 static 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 /*****************************************************************************/ 251 314 /* Comparison function, with various wrappers: 252 315 - ss_compare_ex: Compare with flags and length. If length is 0 it will be -
trunk/superstring/include/superstring_private.h
r812 r815 60 60 str->flags = istr->flags; 61 61 if (istr->flags & SUPERSTRING_JUMBO) { 62 s uperstring_jumbo jstr = (superstring_jumbo)str;62 struct _superstring_jumbo_ *jstr = (struct _superstring_jumbo_ *)str; 63 63 jstr->len = istr->len; 64 64 jstr->size = istr->size; … … 88 88 89 89 /* Function to grow strings */ 90 int ss_grow(ss_internal *istr );90 int ss_grow(ss_internal *istr, superstring *outstr); 91 91 92 92 /* Handy stuff for handling printf-like functions and warnings in gcc (thanks -
trunk/superstring/source/SConscript
r812 r815 17 17 sourcelist = Split(''' 18 18 allocator.c 19 append.c 19 20 chartables.c 20 21 compare.c -
trunk/superstring/source/allocator.c
r812 r815 49 49 50 50 /* 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) */ 53 int ss_grow(ss_internal *istr, superstring *outstr) { 53 54 size_t grow; 54 55 size_t memsize; … … 115 116 } 116 117 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. */ 118 121 ssi_to_ss(istr, istr->str); 122 istr->data = ss_cstring(str); 123 if (outstr != NULL) 124 *outstr = istr->str; 125 119 126 ss_returnerror(SS_ERROR_SUCCESS); 120 127 } … … 151 158 istr.size = size; 152 159 153 if (ss_grow(&istr ) != SS_ERROR_SUCCESS)160 if (ss_grow(&istr, NULL) != SS_ERROR_SUCCESS) 154 161 return NULL; 155 162 156 163 /* Copy our data in */ 157 cstr = ss_cstring(istr.str);164 cstr = (unsigned char *)ss_cstring(istr.str); 158 165 memcpy((void *)cstr, value, istr.len); 159 166 cstr[istr.len] = '\0'; -
trunk/superstring/source/test.c
r812 r815 29 29 void test_anonymous(void); 30 30 void test_compare(void); 31 void test_setchar(void); 32 void test_append(void); 31 33 32 34 #define TEST_INIT_VALUE "Hello, world!" … … 156 158 } 157 159 160 void 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 171 void 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 158 194 int main(void) { 159 195 … … 162 198 runtest(anonymous); 163 199 runtest(compare); 200 runtest(setchar); 201 runtest(append); 164 202 165 203 /* 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.
