commit 98f8e538bf0499faf31a3c7368205a3c024a4949 from: Omar Polo date: Fri Mar 22 22:24:59 2024 UTC bufio: add buf_getdelim will help simplifying a bit in net.c, and it's generally useful. commit - 7bdb6d522add94db48fb6cf85f8217e512b9ff79 commit + 98f8e538bf0499faf31a3c7368205a3c024a4949 blob - b4f8f9cf1648947784cae99295879d0682cec0e7 blob + 3aa80c64cbb3134680538411c0e8903db15b8830 --- bufio.c +++ bufio.c @@ -68,6 +68,22 @@ int buf_has_line(struct buf *buf, const char *nl) { return (memmem(buf->buf, buf->len, nl, strlen(nl)) != NULL); +} + +char * +buf_getdelim(struct buf *buf, const char *nl, size_t *len) +{ + uint8_t *endl; + size_t nlen; + + *len = 0; + + nlen = strlen(nl); + if ((endl = memmem(buf->buf, buf->len, nl, nlen)) == NULL) + return (NULL); + *len = endl + nlen - buf->buf; + *endl = '\0'; + return (buf->buf); } void blob - ed6c460e96d4f70b3c4f0a72e086a041b1fca944 blob + 55fd3c75183051840222265fb44032e0b9265f97 --- bufio.h +++ bufio.h @@ -46,6 +46,7 @@ struct bufio { int buf_init(struct buf *); int buf_has_line(struct buf *, const char *); +char *buf_getdelim(struct buf *, const char *, size_t *); void buf_drain(struct buf *, size_t); void buf_drain_line(struct buf *, const char *); void buf_free(struct buf *);