aboutsummaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-05-17 11:48:51 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-05-17 11:48:51 -0700
commit11743fd21a99b32ae358f56b7cabdd8368bf78aa (patch)
tree239aa0a8b58bec4f4f1ad70d6b356e9196f16e0f /sys
parentec3ad4722a5cd53491d6cf031c94d394f17e5c5e (diff)
change signature for string functions
Diffstat (limited to 'sys')
-rw-r--r--sys/libn/bufio.c5
-rw-r--r--sys/libn/flate.c4
-rw-r--r--sys/libn/io.c6
-rw-r--r--sys/libn/string.c136
-rw-r--r--sys/libn/test.c2
5 files changed, 77 insertions, 76 deletions
diff --git a/sys/libn/bufio.c b/sys/libn/bufio.c
index 38714a5..ac8e175 100644
--- a/sys/libn/bufio.c
+++ b/sys/libn/bufio.c
@@ -91,11 +91,6 @@ bufio·ungetbyte(io·Buffer *buf, byte c)
return bufio·err;
}
- if (c != *buf->pos) {
- errorf("unget char does not match");
- return bufio·err;
- }
-
buf->pos--;
return 0;
}
diff --git a/sys/libn/flate.c b/sys/libn/flate.c
index c3e363f..ff68d9c 100644
--- a/sys/libn/flate.c
+++ b/sys/libn/flate.c
@@ -35,7 +35,7 @@ flate·openreader(io·Reader rdr, void* r, mem·Allocator mem, void* m)
zrdr = mem.alloc(m, 1, sizeof(*zrdr));
- zrdr->zalloc = mem.alloc;
+ zrdr->zalloc = (void *(*)(void *, unsigned int, unsigned int))mem.alloc;
zrdr->zfree = mem.free;
zrdr->opaque = m;
zrdr->avail_in = rdr.read(r, 1, arrlen(zrdr->buf), zrdr->buf);
@@ -146,7 +146,7 @@ flate·openwriter(io·Writer wtr, void* w, mem·Allocator mem, void* m)
flate·Writer *zwtr;
zwtr = mem.alloc(m, 1, sizeof(*zwtr));
- zwtr->zalloc = mem.alloc;
+ zwtr->zalloc = (void *(*)(void *, unsigned int, unsigned int))mem.alloc;
zwtr->zfree = mem.free;
zwtr->opaque = m;
zwtr->avail_in = 0;
diff --git a/sys/libn/io.c b/sys/libn/io.c
index 3827a82..2852f42 100644
--- a/sys/libn/io.c
+++ b/sys/libn/io.c
@@ -22,6 +22,12 @@ io·stat(Stream *s, io·Stat *buf)
return fstat(fileno(s), buf);
}
+int
+io·exists(byte *path, int flag)
+{
+ return access(path, flag);
+}
+
error
io·close(Stream *s)
{
diff --git a/sys/libn/string.c b/sys/libn/string.c
index 100c1fe..694cdea 100644
--- a/sys/libn/string.c
+++ b/sys/libn/string.c
@@ -3,6 +3,13 @@
#define MAX_STRING_ALLOC 1024 * 1024
+typedef struct Hdr
+{
+ vlong len;
+ vlong cap;
+ byte buf[];
+} Hdr;
+
// -------------------------------------------------------------------------
// UTF-8 functions
@@ -156,9 +163,9 @@ utf8·findrune(byte* s, long c)
// len defines the length of the C substring that we will copy into our buffer.
// The backing buffer will have capacity cap.
string
-str·makecap(const byte* s, vlong len, vlong cap)
+str·makecap(const byte *s, vlong len, vlong cap)
{
- struct str·Hdr* h;
+ struct Hdr* h;
h = malloc(sizeof(*h) + cap + 1);
if (s == nil) memset(h, 0, sizeof(*h));
@@ -186,7 +193,7 @@ cleanup:
// New returns a new dynamic string object, initialized from the given C string.
// The backing buffer capacity is equivalent to the string length.
string
-str·makelen(const byte* s, vlong len)
+str·makelen(const byte *s, vlong len)
{
vlong sl = (!s) ? 0 : strlen(s);
if (sl < len) panicf("attempted to take a bigger substring than string length");
@@ -198,7 +205,7 @@ str·makelen(const byte* s, vlong len)
// New returns a new dynamic string object, initialized from the given C string.
// The backing buffer capacity is equivalent to the string length.
string
-str·make(const byte* s)
+str·make(const byte *s)
{
vlong len = (!s) ? 0 : strlen(s);
return str·makelen(s, len);
@@ -206,7 +213,7 @@ str·make(const byte* s)
// Newf returns a new dynamic string object
string
-str·makef(const byte* fmt, ...)
+str·makef(const byte *fmt, ...)
{
va_list args;
va_start(args, fmt);
@@ -219,7 +226,7 @@ str·makef(const byte* fmt, ...)
vsnprintf(s, n + 1, fmt, args);
va_end(args);
- str·Hdr* h = (str·Hdr*)(s - sizeof(str·Hdr));
+ Hdr* h = (Hdr*)(s - sizeof(Hdr));
h->len = n;
return s;
@@ -229,14 +236,14 @@ str·makef(const byte* fmt, ...)
void
str·free(string s)
{
- free(s - sizeof(str·Hdr));
+ free(s - sizeof(Hdr));
}
// Len returns the length of the string.
int
str·len(const string s)
{
- str·Hdr* h = (str·Hdr*)(s - sizeof(str·Hdr));
+ Hdr* h = (Hdr*)(s - sizeof(Hdr));
return h->len;
}
@@ -244,97 +251,93 @@ str·len(const string s)
int
str·cap(const string s)
{
- str·Hdr* h = (str·Hdr*)(s - sizeof(str·Hdr));
+ Hdr* h = (Hdr*)(s - sizeof(Hdr));
return h->cap;
}
-string
-str·clear(string s)
+void
+str·clear(string *s)
{
- str·Hdr* h = (str·Hdr*)(s - sizeof(str·Hdr));
+ Hdr* h = (Hdr*)(*s - sizeof(Hdr));
h->len = 0;
- *s = 0;
-
- return s;
+ *s[0] = '\0';
}
// Grow ensures that the string can encompass AT LEAST delta bytes.
// If it already can, this is a NO OP.
// If it can't, the string will be reallocated.
-string
-str·grow(string s, vlong delta)
+void
+str·grow(string *s, vlong delta)
{
- str·Hdr *h, *newh;
- vlong cap = str·cap(s);
- vlong len = str·len(s);
- Assert(cap >= len); // To prevent unsigned behavior
+ Hdr *h, *newh;
+ vlong cap = str·cap(*s);
+ vlong len = str·len(*s);
+ assert(cap >= len); // To prevent unsigned behavior
- if (cap - len >= delta) return s;
+ if (cap - len >= delta) return;
- h = (str·Hdr*)(s - sizeof(str·Hdr));
+ h = (Hdr*)(*s - sizeof(Hdr));
vlong newCap = cap + delta;
- Assert(newCap >= cap); // To prevent unsigned behavior
+ assert(newCap >= cap); // To prevent unsigned behavior
if (newCap < MAX_STRING_ALLOC) {
newCap *= 2;
} else
newCap += MAX_STRING_ALLOC;
- newh = (str·Hdr*)realloc(h, sizeof(*h) + newCap + 1);
- if (newh == nil) return nil;
+ newh = (Hdr*)realloc(h, sizeof(*h) + newCap + 1);
+ if (newh == nil) return;
memset(newh->buf + len, '\0', newCap - len);
newh->cap = newCap;
newh->len = len;
- return newh->buf;
+ *s = newh->buf;
}
// Fit reallocates the string such that the buffer is exactly sized for the
// buffer. If the capacity equals the length, then the function is a NOOP. The
// byte array is unchanged.
-string
-str·fit(string s)
+void
+str·fit(string *s)
{
- str·Hdr* h;
- vlong cap = str·cap(s);
- vlong len = str·len(s);
+ Hdr* h;
+ vlong cap = str·cap(*s);
+ vlong len = str·len(*s);
- if (cap == len) return s;
+ if (cap == len) return;
- h = (str·Hdr*)(s - sizeof(str·Hdr));
+ h = (Hdr*)(s - sizeof(Hdr));
h = realloc(h, sizeof(*h) + len + 1);
h->cap = len;
- return h->buf;
+ *s = h->buf;
}
// Append will append the given null terminated C string to the string data
// structure. This variant can append a substring of length len of the given
// string to our buffer. The result is reallocated if not enough room is present
// in the buffer.
-string
-str·appendcount(string s, vlong n, const byte* b)
+void
+str·appendcount(string *s, vlong n, const byte* b)
{
vlong bl = strlen(b);
if (n > bl) panicf("attempted to make a substring longer than string");
- s = str·grow(s, n);
- if (s == nil) return nil;
+ str·grow(s, n);
+ if (*s == nil) return;
- str·Hdr* h = (str·Hdr*)(s - sizeof(str·Hdr));
+ Hdr* h = (Hdr*)(s - sizeof(Hdr));
- memcpy(s + str·len(s), b, n);
+ memcpy(*s + str·len(*s), b, n);
h->len += n;
- s[h->len] = '\0';
-
- return s;
+ *s[h->len] = '\0';
}
// Append will append the given null terminated C string to the string data
// structure. This variant will append the entire string.
-string
-str·append(string s, const byte* b)
+void
+str·append(string *s, const byte* b)
{
return str·appendcount(s, strlen(b), b);
}
@@ -342,19 +345,17 @@ str·append(string s, const byte* b)
// AppendByte will append the given byte to our string.
// NOTE: As the byte is on the stack, it is not null-terminated.
// Can not pass to the above functions.
-string
-str·appendbyte(string s, const byte b)
+void
+str·appendbyte(string *s, const byte b)
{
- s = str·grow(s, 1);
- if (s == nil) return nil;
+ str·grow(s, 1);
+ if (*s == nil) return;
- str·Hdr* h = (str·Hdr*)(s - sizeof(str·Hdr));
+ Hdr* h = (Hdr*)(s - sizeof(Hdr));
- *(s + str·len(s)) = b;
+ *(*s + str·len(*s)) = b;
h->len++;
- s[h->len] = '\0'; // NOTE: I don't think an explicit zero is required..?
-
- return s;
+ *s[h->len] = '\0'; // NOTE: I don't think an explicit zero is required..?
}
// Equals returns true if string s and t are equivalent.
@@ -375,29 +376,27 @@ str·equals(const string s, const string t)
* Appendf will append the given formatted string to our buffer.
* Returns the newly minted string
*/
-string
-str·appendf(string s, const byte* fmt, ...)
+void
+str·appendf(string *s, const byte* fmt, ...)
{
va_list args;
va_start(args, fmt);
- int remain = str·cap(s) - str·len(s);
- int n = vsnprintf(s + str·len(s), remain + 1, fmt, args);
+ int remain = str·cap(*s) - str·len(*s);
+ int n = vsnprintf(*s + str·len(*s), remain + 1, fmt, args);
va_end(args);
if (n > remain) {
// If the first write was incomplete, we overwite the data again.
- s = str·grow(s, n);
+ str·grow(s, n);
va_list args;
va_start(args, fmt);
- n = vsnprintf(s + str·len(s), n + 1, fmt, args);
- Assert(n - remain <= str·cap(s));
+ n = vsnprintf(*s + str·len(*s), n + 1, fmt, args);
+ assert(n - remain <= str·cap(*s));
va_end(args);
}
- str·Hdr* h = (str·Hdr*)(s - sizeof(str·Hdr));
+ Hdr* h = (Hdr*)(*s - sizeof(Hdr));
h->len += n;
-
- return s;
}
// Find will find the first occurence of
@@ -497,12 +496,13 @@ cleanup:
string
str·join(vlong len, byte** fields, const byte* sep)
{
- string s = str·makecap(nil, 0, 10);
+ string s = str·makecap("", 0, 10);
int j = 0;
for (j = 0; j < len; j++) {
- s = str·append(s, fields[j]);
- if (j < len - 1) { s = str·appendcount(s, 1, sep); }
+ str·append(&s, fields[j]);
+ if (j < len - 1)
+ str·appendcount(&s, 1, sep);
}
return s;
diff --git a/sys/libn/test.c b/sys/libn/test.c
index b52207d..66c3c7f 100644
--- a/sys/libn/test.c
+++ b/sys/libn/test.c
@@ -138,7 +138,7 @@ test·sort()
}
t = clock();
- qsort(test, arrlen(test), sizeof(int), &less);
+ qsort(test, arrlen(test), sizeof(int), (int (*)(const void *, const void *))less);
t = clock() - t;
printf("std qsort code took %f ms to execute\n", 1000.*t/CLOCKS_PER_SEC);