Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utstring_truncate, analogous to man (2) truncate #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/Makefile
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions doc/utstring.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/utstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)) { \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this condition is correct? It seems like you want something like
if ((size_t)(amt)+1 > (s)->n)

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 *
******************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added tests/test85.ans
Binary file not shown.
24 changes: 24 additions & 0 deletions tests/test85.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <unistd.h> /* 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;
}