plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

commit 0e64de8ac00b13a58fd449a628ef521565f0fd90
parent 66bf45d4e933e7d6d91f612cc4bb560ca24a8692
Author: ssnf <ssnf@ssnf.xyz>
Date:   Sun, 31 Aug 2025 21:00:13 +0000

use file descriptor instead of Biobuf in Strgets

Diffstat:
Minclude/str.h | 2+-
Mman/man3/str.3 | 56+++++++++++++++++++++++---------------------------------
Msrc/libstr/Strgets.c | 23+++++++++++++++--------
Msrc/libstr/std.h | 1-
4 files changed, 39 insertions(+), 43 deletions(-)

diff --git a/include/str.h b/include/str.h @@ -17,7 +17,7 @@ void Strdelc(String*); void Strdelete(String*, Posn, Posn); void Strdup(String*, String); void Strgetf(String*, int); -int Strgets(String*, Biobuf*); +int Strgets(String*, int); void Strinit(String*); void Strinsert(String*, String, Posn); void Strinsure(String*, ulong); diff --git a/man/man3/str.3 b/man/man3/str.3 @@ -47,7 +47,7 @@ void Strdup(String *p, String s) void Strgetf(String *s, int fd) .PP .B -int Strgets(String *s, Biobuf *b) +int Strgets(String *s, int fd) .PP .B void Strinit(String *s) @@ -70,28 +70,33 @@ void Strzero(String *s) This library provides a dynamic string implementation that is mutually exclusive with Plan 9's standard libString. It offers a simpler, more lightweight alternative focused on ease of use. +.PP A .B String contains a pointer to the string data, its current length, and its allocated size. +.PP +When +.I size +is 0, the string cannot be shrunk or grown. .SS "Comparison with libString" This library differs from Plan 9's standard libString in several key ways: -.IP \(bu +.IP - .B No locking: libString includes locking mechanisms for thread safety, while this library omits them for simplicity. -.IP \(bu +.IP - .B Stack allocation: .I Str and .I Strn create stack-allocated strings that reference existing data, while libString requires heap allocation for all strings. -.IP \(bu +.IP - .B Direct formatting: .I Strprint provides direct formatted output using .IR vsmprint , while libString requires separate formatting steps. -.IP \(bu +.IP - .B Simpler structure: Uses a 3-field structure (s, n, size) compared to libString's 6-field structure with reference counting and locking. @@ -101,22 +106,21 @@ Both libraries provide similar Biobuf integration for reading operations. .I Str creates a .B String -from a null-terminated C string. -The resulting string references the original data and should not be modified. +that references a null-terminated C string, with size set to 0. .PP .I Strn creates a .B String -from the first +that references .I n -characters of a C string. -The resulting string references the original data and should not be modified. +characters of a C string, with size set to 0. +It does not modify the data to guarantee a valid C string, but libstr +functions will respect the length it claims to have. .PP .I Strinit initializes a .B String for use as a dynamic string. -The string is initially empty but has space allocated for growth. .PP .I Strdup copies string @@ -138,14 +142,12 @@ appends character .I c to string .IR p . -The string grows automatically if necessary. .PP .I Stradds appends string .I s to string .IR p . -The string grows automatically if necessary. .PP .I Strinsert inserts string @@ -154,9 +156,6 @@ into string .I s at position .IR p . -Characters after position -.I p -are shifted to make room. The behavior is undefined if .I s and @@ -183,7 +182,6 @@ ensures that string has space for at least .I n characters. -This can be used to preallocate space for better performance. .PP .I Strprint appends formatted text to string @@ -200,14 +198,13 @@ into string Any existing content is replaced. .PP .I Strgets -reads a line from -.I Biobuf -.I b +reads a line from an open file descriptor +.IR fd into string .IR s . The newline character is removed if present. -It returns the number of characters read (excluding any newline), -or 0 on end of file. +It returns the number of characters read, or 0 on end of file. +Any existing content is replaced. .PP .I Strtok tokenizes string @@ -293,14 +290,7 @@ Strclose(&s); Functions call .I sysfatal if memory allocation fails. -.SH NOTES -Strings created with -.I Str -and -.I Strn -reference the original data and should not be modified -or passed to -.IR Strclose . -Use -.I Strdup -to create a modifiable copy. +.PP +Strinsure calls +.I sysfatal +when size is 0. diff --git a/src/libstr/Strgets.c b/src/libstr/Strgets.c @@ -1,18 +1,25 @@ #include "std.h" +#include <bio.h> + int -Strgets(String *p, Biobuf *b) +Strgets(String *p, int fd) { - char *s; + Biobuf b; ulong n; + char *s; Strzero(p); - s = Brdstr(b, '\n', '\n'); - if (!s) - return 0; - n = Blinelen(b); - Stradds(p, Strn(s, n)); - free(s); + Binit(&b, fd, OREAD); + n = 0; + s = Brdstr(&b, '\n', 1); + if (s) { + free(p->s); + n = Blinelen(&b) + 1; + *p = Strn(s, n - 1); + p->size = n; + } + Bterm(&b); return n; } diff --git a/src/libstr/std.h b/src/libstr/std.h @@ -1,4 +1,3 @@ #include <u.h> #include <libc.h> -#include <bio.h> #include <str.h>