diff --git a/doc/Makefile b/doc/Makefile index 3d0da27f..4ff94263 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -1,6 +1,9 @@ HTML=$(patsubst %.txt,%.html,$(wildcard *.txt)) all: $(HTML) + +clean: + rm $(HTML) # when each target of a multi-target rule has its own prereq # we use a static pattern rule. diff --git a/doc/utstring.txt b/doc/utstring.txt index 16104999..75085ee6 100644 --- a/doc/utstring.txt +++ b/doc/utstring.txt @@ -164,6 +164,7 @@ Operations | utstring_body(s) | get `char*` to body of s (buffer is always null-terminated) | utstring_find(s,pos,str,len) | forward search from pos for a substring | utstring_findR(s,pos,str,len) | reverse search from pos a substring +| utstring_truncate(s,len) | change utstring length, extending with `NULL`s if needed |=============================================================================== New/free vs. init/done diff --git a/src/utstring.h b/src/utstring.h index 867442c8..0908039e 100644 --- a/src/utstring.h +++ b/src/utstring.h @@ -150,6 +150,17 @@ _UNUSED_ static void utstring_printf(UT_string *s, const char *fmt, ...) { va_end(ap); } +#define utstring_truncate(s,amt) \ +do { \ + size_t ext = (size_t) (amt) + 1 - (s)->n; \ + if (ext < (size_t) (amt)) { \ + utstring_reserve((s), ext); \ + memset((s)->d + (s)->n - ext, 0, ext); \ + } \ + (s)->i = amt; \ + (s)->d[amt] = '\0'; \ +} while(0) + /******************************************************************************* * begin substring search functions * ******************************************************************************/ diff --git a/tests/Makefile b/tests/Makefile index 977f858c..23fcf0cd 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -13,7 +13,7 @@ PROGS = test1 test2 test3 test4 test5 test6 test7 test8 test9 \ test58 test59 test60 test61 test62 test63 test64 test65 \ test66 test67 test68 test69 test70 test71 test72 test73 \ test74 test75 test76 test77 test78 test79 test80 test81 \ - test82 test83 test84 + test82 test83 test84 test85 CFLAGS = -I$(HASHDIR) #CFLAGS += -DHASH_BLOOM=16 #CFLAGS += -O2 diff --git a/tests/test85.ans b/tests/test85.ans new file mode 100644 index 00000000..e07d493e Binary files /dev/null and b/tests/test85.ans differ diff --git a/tests/test85.c b/tests/test85.c new file mode 100644 index 00000000..002b2835 --- /dev/null +++ b/tests/test85.c @@ -0,0 +1,24 @@ +#include /* write */ +#include "utstring.h" + +int main() { + UT_string *s; + char V_TestStr[] = "The quick brown dog, &c."; + utstring_new(s); + + utstring_bincpy(s, V_TestStr, sizeof(V_TestStr)-1); + write(STDOUT_FILENO, utstring_body(s), utstring_len(s)); + write(STDOUT_FILENO, "\n", 1); + + utstring_truncate(s, 10); + write(STDOUT_FILENO, utstring_body(s), utstring_len(s)); + write(STDOUT_FILENO, "\n", 1); + + utstring_truncate(s, 10000); + write(STDOUT_FILENO, utstring_body(s), utstring_len(s)); + write(STDOUT_FILENO, "\n", 1); + + utstring_free(s); + + return 0; +}