diff options
Diffstat (limited to 'lib/cstr/cstr.c')
| -rw-r--r-- | lib/cstr/cstr.c | 50 |
1 files changed, 50 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) |