about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2023-11-03 18:13:05 +0100
committerLeah Neukirchen <leah@vuxu.org>2024-07-14 14:50:01 +0200
commit10374ca23413c7e2f85a8b773f90250b5408195e (patch)
treeea07fd347ae9eb286923c8d64fb680aaa1aa374c
parentfce8d1ec3329a97fc8886cf71eb3d1a763d71ad7 (diff)
downloadlibste-10374ca23413c7e2f85a8b773f90250b5408195e.tar.gz
libste-10374ca23413c7e2f85a8b773f90250b5408195e.tar.xz
libste-10374ca23413c7e2f85a8b773f90250b5408195e.zip
add steccpy HEAD master
-rw-r--r--Makefile2
-rw-r--r--README10
-rw-r--r--libste.322
-rw-r--r--ste.h1
-rw-r--r--steccpy.c16
-rw-r--r--tests.c23
6 files changed, 70 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
@@ -10,6 +10,9 @@ SYNOPSIS
      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 *
@@ -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/steccpy.c b/steccpy.c
new file mode 100644
index 0000000..3bf8da1
--- /dev/null
+++ b/steccpy.c
@@ -0,0 +1,16 @@
+char *
+steccpy(char *dst, char *end, const char *src, int c)
+{
+	if (dst >= end)
+		return dst;
+
+	while (dst < end && *src != c && (*dst = *src))
+		src++, dst++;
+
+	if (dst == end)
+		dst[-1] = 0;
+	else if (*src == c)
+		dst[0] = 0;
+
+	return dst;
+}
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;
 }