about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2023-11-21 15:30:21 +0100
committerLeah Neukirchen <leah@vuxu.org>2024-07-14 14:50:34 +0200
commit5dabeb7e3c10f8b421ec19189b20bf19996194fa (patch)
tree7be5ddab503e22944b027fc0d1e44af714dc90da
parent10374ca23413c7e2f85a8b773f90250b5408195e (diff)
downloadlibste-5dabeb7e3c10f8b421ec19189b20bf19996194fa.tar.gz
libste-5dabeb7e3c10f8b421ec19189b20bf19996194fa.tar.xz
libste-5dabeb7e3c10f8b421ec19189b20bf19996194fa.zip
add steprl
-rw-r--r--Makefile2
-rw-r--r--README8
-rw-r--r--libste.319
-rw-r--r--ste.h1
-rw-r--r--steprl.c25
-rw-r--r--tests.c15
6 files changed, 68 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 71f87bd..458c6ea 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 steccpy.o steprn.o
+libste.a: stechr.o stecpe.o stecpy.o steccpy.o steprn.o steprl.o
 	$(AR) $(ARFLAGS) $@ $^
 
 example: example.o libste.a
diff --git a/README b/README
index 5776228..17f32b5 100644
--- a/README
+++ b/README
@@ -21,6 +21,9 @@ SYNOPSIS
      char *
      steprn(char *dst, char *end, const char *fmt, ...);
 
+     char *
+     steprl(char *dst, char *end, long n);
+
 DESCRIPTION
      libste provides five useful functions for dealing with strings.
 
@@ -49,6 +52,11 @@ DESCRIPTION
      terminated and the return value is a pointer to the NUL byte.  On
      truncation, end is returned.
 
+     steprl formats n as a decimal number (potentially with a leading minus)
+     and appends it to dst, but writes no characters beyond end.  If any
+     characters are written, dst will be NUL-terminated and the return value
+     is a pointer to the NUL byte.  On truncation, end is returned.
+
      Note that it is safe to pass the return value of all functions listed
      above as argument for dst when the same end is reused.  In this case, the
      function call does nothing but return dst again.  At any point,
diff --git a/libste.3 b/libste.3
index 994d12a..717cd85 100644
--- a/libste.3
+++ b/libste.3
@@ -19,6 +19,8 @@
 .Fn stechr "const char *src" "const char *end" "int c"
 .Ft "char *"
 .Fn steprn "char *dst" "char *end" "const char *fmt" "..."
+.Ft "char *"
+.Fn steprl "char *dst" "char *end" "long n"
 .Sh DESCRIPTION
 .Nm libste
 provides five useful functions for dealing with strings.
@@ -106,6 +108,23 @@ On truncation,
 .Fa end
 is returned.
 .Pp
+.Nm steprl
+formats
+.Fa n
+as a decimal number
+.Pq potentially with a leading minus
+and appends it to
+.Fa dst ,
+but writes no characters beyond
+.Fa end .
+If any characters are written,
+.Fa dst
+will be NUL-terminated
+and the return value is a pointer to the NUL byte.
+On truncation,
+.Fa end
+is returned.
+.Pp
 Note that it is safe to pass the return value of all functions listed above
 as argument for
 .Fa dst
diff --git a/ste.h b/ste.h
index ed8fc53..32f2f8f 100644
--- a/ste.h
+++ b/ste.h
@@ -3,3 +3,4 @@ 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, ...);
+char *steprl(char *dst, char *end, long n);
diff --git a/steprl.c b/steprl.c
new file mode 100644
index 0000000..5c14a23
--- /dev/null
+++ b/steprl.c
@@ -0,0 +1,25 @@
+#include "ste.h"
+
+char *
+steprl(char *dst, char *end, long n)
+{
+	if (dst >= end)
+		return end;
+
+	char buf[24];
+	char *bufend = buf + sizeof buf;
+	char *s = bufend;
+
+	int neg = n < 0;
+	if (neg)
+		n = -n;
+
+	if (!n)
+		*--s = '0';
+	for (; n > 0; n /= 10)
+		*--s = '0' + (n%10);
+	if (neg)
+		*--s = '-';
+
+	return stecpe(dst, end, s, bufend);
+}
diff --git a/tests.c b/tests.c
index 14829a8..7d6d9c2 100644
--- a/tests.c
+++ b/tests.c
@@ -16,7 +16,7 @@ is(const char *desc, int ok)
 int
 main()
 {
-	printf("1..49\n");
+	printf("1..55\n");
 
 	printf("# stecpy\n");
 
@@ -145,6 +145,19 @@ main()
 	is("buffer doesn't get fuller", strlen(buf) == 15);
 	is("return value is end", pos == end);
 
+	printf("# steprl\n");
+	pos = buf;
+	pos = steprl(pos, end, 12345);
+	is("12345 = 5", strlen(buf) == 5);
+	pos = steprl(pos, end, -9876);
+	is("-9876 = 10", strlen(buf) == 10);
+	pos = steprl(pos, end, 0);
+	is("0 = 11", strlen(buf) == 11);
+	pos = steprl(pos, end, 1);
+	is("1 = 12", strlen(buf) == 12);
+	pos = steprl(pos, end, 77777777);
+	is("77777777 = full", strlen(buf) == 15);
+	is("return value is end", pos == end);
 
 	return status;
 }