From b2e38120bb02572cc395dcca6f2c7b10da100f78 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Tue, 21 Nov 2023 15:30:21 +0100 Subject: add steprl --- Makefile | 2 +- README | 8 ++++++++ libste.3 | 19 +++++++++++++++++++ ste.h | 1 + steprl.c | 25 +++++++++++++++++++++++++ tests.c | 15 ++++++++++++++- 6 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 steprl.c 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; } -- cgit 1.4.1