diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-02-08 21:39:42 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-02-08 21:39:42 +0100 |
| commit | 764f8d22b250e09c1dea98fb8073e71a720892f7 (patch) | |
| tree | 8357f9f673846125ac49ff0f12288d242eb2fa49 /lib/cstr | |
| parent | b97ec93748902c90f25524116d3d189f1b263474 (diff) | |
add pager
Diffstat (limited to 'lib/cstr')
| -rw-r--r-- | lib/cstr/cstr.c | 50 | ||||
| -rw-r--r-- | lib/cstr/cstr.h | 9 |
2 files changed, 59 insertions, 0 deletions
diff --git a/lib/cstr/cstr.c b/lib/cstr/cstr.c index f464cf3..9f4ad6f 100644 --- a/lib/cstr/cstr.c +++ b/lib/cstr/cstr.c @@ -175,6 +175,50 @@ u64 strip_cstr(char *cstr, char strip) return strip_front(cstr, strip) + strip_back(cstr, strip); } + +u64 number_of_lines(const char *cstr) +{ + u64 lines = 0; + + while (*cstr) { + if (*cstr == '\n') ++lines; + ++cstr; + } + + return lines; +} + + +substr_t getline(const char *cstr, u64 n) +{ + substr_t line; + line.begin = (char *)cstr; + line.end = line.begin; + + while (*line.end && *line.end != '\n') ++line.end; + + for (int i = 0; i < n; ++i) + line = nextline(line); + + return line; +} + + +substr_t nextline(substr_t line) +{ + if (!(*line.end || *(line.end + 1))) { + line.begin = 0; + line.end = 0; + return line; + } + + line.begin = ++line.end; + + while (*line.end && *line.end != '\n') ++line.end; + + return line; +} + /* DOC * @type function * @name string_utf8_length @@ -272,6 +316,12 @@ void u64_to_cstr(u64 n, char *cstr, u64 length) n /= 10; } + if (p == cstr + length - 1) { + *(p--) = '0'; + } + + *p = 0; + d = p - cstr + 1; for (i = 0; i < length - d; ++i) diff --git a/lib/cstr/cstr.h b/lib/cstr/cstr.h index 80dc359..ce27eb0 100644 --- a/lib/cstr/cstr.h +++ b/lib/cstr/cstr.h @@ -3,6 +3,11 @@ #include "../sys/sizes.h" +typedef struct { + char *begin; + char *end; +} substr_t; + u64 cstr_length(const char *); i8 cstr_compare(const char *a, const char *b); u64 cstr_split(char *cstr, char split); @@ -10,6 +15,10 @@ const char *next_split(const char *previous); u64 strip_front(char *cstr, char strip); u64 strip_back(char *cstr, char strip); u64 strip_cstr(char *cstr, char strip); +u64 number_of_lines(const char *cstr); + +substr_t getline(const char *cstr, u64 n); +substr_t nextline(substr_t line); u64 cstr_utf8_length(const char *); u8 next_utf8(const char **); |