From 9ec5bed6a7d715ffa69851569485a685dd69db2e Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sun, 17 May 2020 16:19:17 -0700 Subject: prototype of front end cli --- sys/libn/string.c | 74 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 17 deletions(-) (limited to 'sys/libn/string.c') diff --git a/sys/libn/string.c b/sys/libn/string.c index 694cdea..fb92a04 100644 --- a/sys/libn/string.c +++ b/sys/libn/string.c @@ -150,6 +150,34 @@ utf8·findrune(byte* s, long c) return nil; } +byte* +utf8·findrrune(byte* s, long c) +{ + long c1; + rune r; + byte *l; + + if (c < RuneSync) + return strrchr(s, c); + + l = nil; + for (;;) { + c1 = *(ubyte*)s; + if (c1 < RuneSelf) { + if (c1 == 0) return l; + if (c1 == c) l = s; + s++; + continue; + } + c1 = utf8·chartorune(&r, s); + if (r == c) + l = s; + s += c1; + } + + return nil; +} + #undef Bit #undef Tbyte #undef RuneX @@ -319,7 +347,7 @@ str·fit(string *s) // string to our buffer. The result is reallocated if not enough room is present // in the buffer. void -str·appendcount(string *s, vlong n, const byte* b) +str·appendlen(string *s, vlong n, const byte* b) { vlong bl = strlen(b); if (n > bl) panicf("attempted to make a substring longer than string"); @@ -339,7 +367,7 @@ str·appendcount(string *s, vlong n, const byte* b) void str·append(string *s, const byte* b) { - return str·appendcount(s, strlen(b), b); + return str·appendlen(s, strlen(b), b); } // AppendByte will append the given byte to our string. @@ -358,24 +386,11 @@ str·appendbyte(string *s, const byte b) *s[h->len] = '\0'; // NOTE: I don't think an explicit zero is required..? } -// Equals returns true if string s and t are equivalent. -bool -str·equals(const string s, const string t) -{ - vlong sL = str·len(s); - vlong tL = str·len(t); - if (sL != tL) return false; - - return memcmp(s, t, sL) == 0; -} - -//------------------------------------------------------------------------ -// Utility Methods - /* * Appendf will append the given formatted string to our buffer. * Returns the newly minted string */ + void str·appendf(string *s, const byte* fmt, ...) { @@ -399,6 +414,31 @@ str·appendf(string *s, const byte* fmt, ...) h->len += n; } +// Equals returns true if string s and t are equivalent. +bool +str·equals(const string s, const string t) +{ + vlong sL = str·len(s); + vlong tL = str·len(t); + if (sL != tL) return false; + + return memcmp(s, t, sL) == 0; +} + +//------------------------------------------------------------------------ +// Utility Methods + +int +str·read(string s, int size, int n, void *buf) +{ + int len; + + len = MIN(n * size, str·len(s)); + memcpy(buf, s, len); + + return len; +} + // Find will find the first occurence of // substr in the string Returns -1 if nothing was found. int @@ -502,7 +542,7 @@ str·join(vlong len, byte** fields, const byte* sep) for (j = 0; j < len; j++) { str·append(&s, fields[j]); if (j < len - 1) - str·appendcount(&s, 1, sep); + str·appendlen(&s, 1, sep); } return s; -- cgit v1.2.1