newstr * newstr_new( void ) { newstr *s = (newstr *) malloc( sizeof( *s ) ); if ( s ) newstr_initalloc( s, newstr_initlen ); return s; }
static inline void newstr_strcpy_ensurespace( newstr *s, unsigned long n ) { unsigned long m = n + 1; if ( !s->data || !s->dim ) newstr_initalloc( s, m ); else if ( n+1 > s->dim ) newstr_realloc( s, m ); }
void newstr_addchar( newstr *s, char newchar ) { assert( s ); if ( !s->data || s->dim==0 ) newstr_initalloc( s, newstr_initlen ); if ( s->len + 2 > s->dim ) newstr_realloc( s, s->len+2 ); s->data[s->len++] = newchar; s->data[s->len] = '\0'; }
void newstr_strcpy( newstr *s, char *addstr ) { unsigned long n; assert( s && addstr ); n = strlen( addstr ); if ( !s->data || !s->dim ) newstr_initalloc( s, n+1 ); else if ( n+1 > s->dim ) newstr_realloc( s, n+1 ); strncpy( s->data, addstr, n ); s->data[n] = '\0'; s->len = n; }
void newstr_strcat( newstr *s, char *addstr ) { unsigned long lenaddstr; assert( s && addstr ); lenaddstr = strlen( addstr ); if ( !s->data || !s->dim ) newstr_initalloc( s, lenaddstr+1 ); else { if ( s->len + lenaddstr + 1 > s->dim ) newstr_realloc( s, s->len + lenaddstr + 1 ); } strncpy( &(s->data[s->len]), addstr, lenaddstr ); s->len += lenaddstr; s->data[s->len]='\0'; }
void newstr_prepend( newstr *s, char *addstr ) { unsigned long lenaddstr, i; assert( s && addstr ); lenaddstr = strlen( addstr ); if ( !s->data || !s->dim ) newstr_initalloc( s, lenaddstr+1 ); else { if ( s->len + lenaddstr + 1 > s->dim ) newstr_realloc( s, s->len + lenaddstr + 1 ); for ( i=s->len+lenaddstr-1; i>=lenaddstr; i-- ) s->data[i] = s->data[i-lenaddstr]; } strncpy( s->data, addstr, lenaddstr ); s->len += lenaddstr; s->data[ s->len ] = '\0'; }
void newstr_segcat( newstr *s, char *startat, char *endat ) { size_t seglength; char *p, *q; assert( s && startat && endat ); assert( (size_t) startat < (size_t) endat ); seglength=(size_t) endat - (size_t) startat; if ( !s->data || !s->dim ) newstr_initalloc( s, seglength+1 ); else { if ( s->len + seglength + 1 > s->dim ) newstr_realloc( s, s->len + seglength+1 ); } q = &(s->data[s->len]); p = startat; while ( *p && p!=endat ) *q++ = *p++; *q = '\0'; s->len += seglength; }
/* newstr_segcpy( s, start, end ); * * copies [start,end) into s */ void newstr_segcpy( newstr *s, char *startat, char *endat ) { size_t seglength; char *p, *q; assert( s && startat && endat ); assert( ((size_t) startat) <= ((size_t) endat) ); seglength=(size_t) endat - (size_t) startat; if ( seglength==0 ) return; if (s->data==NULL || s->dim==0) newstr_initalloc( s, seglength+1 ); else { if ( seglength+1 > s->dim ) newstr_realloc( s, seglength+1 ); } q = s->data; p = startat; while ( *p && p!=endat ) *q++ = *p++; *q = '\0'; s->len = seglength; }