void Ns_DStringTrunc( Ns_DString *dsPtr, int length );
The Ns_DStringTrunc function truncates an Ns_DString to the given length. Unlike Ns_DStringFree, which truncates the Ns_DString to length 0 and frees any memory that may have been allocated on the heap, Ns_DStringTrunc allows you to truncate the string to any length. It maintains any memory allocated on the heap. This function is useful in a loop where the Ns_DString is likely to overflow the static space each time through. Using Ns_DStringTrunc instead of Ns_DStringFree will avoid having the Ns_DString call malloc to obtain the addition space in each iteration. You will need to call Ns_DStringFree eventually to free any space that may have been allocated for the Ns_DString.
Ns_DString ds; int i; Ns_DStringInit(&ds); for (i=0; i < 50; i++) { Ns_DStringPrintf(&ds, "%s%d", "aBigString", i); /* do something with the dstring constructed above*/ Ns_DStringTrunc(&ds, 0); }
Ns_DStringFree(&ds); /* finished with dstring */