From 6cd51d52e177314a68c6c2f6c598798136ba1b52 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Fri, 3 Nov 2023 18:13:05 +0100 Subject: add steccpy --- Makefile | 2 +- README | 10 +++++++++- libste.3 | 22 +++++++++++++++++++++- ste.h | 1 + tests.c | 23 ++++++++++++++++++++++- 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 7773c49..71f87bd 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CFLAGS=-g -O2 -Wall -Wno-switch -Wextra -Wwrite-strings all: libste.a example -libste.a: stechr.o stecpe.o stecpy.o steprn.o +libste.a: stechr.o stecpe.o stecpy.o steccpy.o steprn.o $(AR) $(ARFLAGS) $@ $^ example: example.o libste.a diff --git a/README b/README index 333a6b0..5776228 100644 --- a/README +++ b/README @@ -9,6 +9,9 @@ SYNOPSIS char * stecpy(char *dst, char *end, const char *src); + char * + steccpy(char *dst, char *end, const char *src, int c); + char * stecpe(char *dst, char *end, const char *src, const char *srcend); @@ -19,13 +22,18 @@ SYNOPSIS steprn(char *dst, char *end, const char *fmt, ...); DESCRIPTION - libste provides four useful functions for dealing with strings. + libste provides five useful functions for dealing with strings. stecpy copies the NUL-terminated string src to dst, but writes no characters beyond end. If any characters are copied, dst will be NUL- terminated and the return value is a pointer to the NUL byte. On truncation, end is returned. + steccpy copies the NUL-terminated string src to dst, stopping when the + character c is found. It writes no characters beyond end. If any + characters are copied, dst will be NUL-terminated and the return value is + a pointer to the NUL byte. On truncation, end is returned. + stecpe copies the string between src and srcend to dst, but writes no characters beyond end. If any characters are copied, dst will be NUL- terminated and the return value is a pointer to the NUL byte. On diff --git a/libste.3 b/libste.3 index c739a07..994d12a 100644 --- a/libste.3 +++ b/libste.3 @@ -12,6 +12,8 @@ .Ft "char *" .Fn stecpy "char *dst" "char *end" "const char *src" .Ft "char *" +.Fn steccpy "char *dst" "char *end" "const char *src" "int c" +.Ft "char *" .Fn stecpe "char *dst" "char *end" "const char *src" "const char *srcend" .Ft "char *" .Fn stechr "const char *src" "const char *end" "int c" @@ -19,7 +21,7 @@ .Fn steprn "char *dst" "char *end" "const char *fmt" "..." .Sh DESCRIPTION .Nm libste -provides four useful functions for dealing with strings. +provides five useful functions for dealing with strings. .Pp .Nm stecpy copies the NUL-terminated string @@ -36,6 +38,24 @@ On truncation, .Fa end is returned. .Pp +.Nm steccpy +copies the NUL-terminated string +.Fa src +to +.Fa dst , +stopping when the character +.Fa c +is found. +It writes no characters beyond +.Fa end . +If any characters are copied, +.Fa dst +will be NUL-terminated +and the return value is a pointer to the NUL byte. +On truncation, +.Fa end +is returned. +.Pp .Nm stecpe copies the string between .Fa src diff --git a/ste.h b/ste.h index b2ff64e..ed8fc53 100644 --- a/ste.h +++ b/ste.h @@ -1,4 +1,5 @@ char *stecpy(char *dst, char *end, const char *src); +char *steccpy(char *dst, char *end, const char *src, int c); char *stecpe(char *dst, char *end, const char *src, const char *srcend); char *stechr(const char *src, const char *end, int c); char *steprn(char *dst, char *end, const char *fmt, ...); diff --git a/tests.c b/tests.c index fa2e0ff..14829a8 100644 --- a/tests.c +++ b/tests.c @@ -16,7 +16,7 @@ is(const char *desc, int ok) int main() { - printf("1..40\n"); + printf("1..49\n"); printf("# stecpy\n"); @@ -125,5 +125,26 @@ main() is("y not found in first 6 chars", y == buf2 + 6); + printf("# steccpy\n"); + pos = buf; + pos = steccpy(pos, end, "abc,def", ','); + is("1x3 = 3", strlen(buf) == 3); + pos = steccpy(pos, end, "def:ghijkl", ':'); + is("2x3 = 6", strlen(buf) == 6); + pos = steccpy(pos, end, ":ghi", ':'); + pos = steccpy(pos, end, "ghi", ':'); + is("3x3 = 9", strlen(buf) == 9); + pos = steccpy(pos, end, "jkl", '\0'); + is("4x3 = 12", strlen(buf) == 12); + pos = steccpy(pos, end, "mnopqst", '!'); + is("5x3 = 15", strlen(buf) == 15); + pos = steccpy(pos, end, "full", '!'); + is("buffer is full", strlen(buf) == 15); + is("return value is end", pos == end); + pos = steccpy(pos, end, "fuller", 'r'); + is("buffer doesn't get fuller", strlen(buf) == 15); + is("return value is end", pos == end); + + return status; } -- cgit 1.4.1