From 1d188c4f816fce8728fdffaa7ad6ef205ca05abd Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Fri, 17 Apr 2020 18:50:29 -0700 Subject: chore: update naming & calling conventions --- src/coro.c | 20 +++++----- src/rules.mk | 5 ++- src/string.c | 125 ++++++++++++++++++++++++++++++----------------------------- 3 files changed, 79 insertions(+), 71 deletions(-) (limited to 'src') diff --git a/src/coro.c b/src/coro.c index 4e651d3..82ef4f4 100644 --- a/src/coro.c +++ b/src/coro.c @@ -6,7 +6,7 @@ extern void _newcoro(coro *co, uintptr (*func)(coro*, uintptr), void *stk); extern uintptr _coroyield(coro *co, uintptr arg); -struct coro +struct coro { void* sp; void* bp; @@ -15,9 +15,10 @@ struct coro }; coro* -coro·New(uintptr stk, uintptr (*func)(coro*, uintptr)) +coro·new(uintptr stk, uintptr (*func)(coro*, uintptr)) { - if (!func) return nil; + if (!func) + return nil; if (stk == 0) stk = 8192; @@ -31,26 +32,27 @@ coro·New(uintptr stk, uintptr (*func)(coro*, uintptr)) } error -coro·Free(coro *co) +coro·free(coro *co) { - enum + enum { - GOOD, NIL, + GOOD, EMPTY, LOST, }; - if (!co) return NIL; - if (!co->bp) return LOST; + if (!co) return NIL; + if (!co->bp) return LOST; if (co->size == 0) return EMPTY; free(co->bp); + return GOOD; } uintptr -coro·Yield(coro *c, uintptr arg) +coro·yield(coro *c, uintptr arg) { return _coroyield(c, arg); } diff --git a/src/rules.mk b/src/rules.mk index 2a13bff..ef9b36c 100644 --- a/src/rules.mk +++ b/src/rules.mk @@ -8,7 +8,10 @@ d := $(DIR) # Local variables SRCS_$(d) := $(wildcard $(d)/*.c) -OBJS_$(d) := $(patsubst $(SRC_DIR)/%, $(OBJ_DIR)/%, $(SRCS_$(d):.c=.o)) +ASMS_$(d) := $(wildcard $(d)/*.s) +OBJS_$(d) := $(SRCS_$(d):.c=.o) +OBJS_$(d) += $(ASMS_$(d):.s=.o) +OBJS_$(d) := $(patsubst $(SRC_DIR)/%, $(OBJ_DIR)/%, $(OBJS_$(d))) DEPS_$(d) := $(OBJS_$(d):.o=.d) OBJS := $(OBJS) $(OBJS_$(d)) diff --git a/src/string.c b/src/string.c index d46f0c0..5e66282 100644 --- a/src/string.c +++ b/src/string.c @@ -26,7 +26,7 @@ enum }; int -utf8·CharToRune(Rune* r, byte* s) +utf8·charToRune(Rune* r, byte* s) { int c[UTFmax], i; Rune l; @@ -64,7 +64,7 @@ bad: } int -utf8·RuneToChar(byte* s, Rune* r) +utf8·runeToChar(byte* s, Rune* r) { int i, j; Rune c; @@ -94,14 +94,14 @@ utf8·RuneToChar(byte* s, Rune* r) } int -utf8·RuneLen(Rune r) +utf8·runeLen(Rune r) { byte s[10]; - return utf8·RuneToChar(s, &r); + return utf8·runeToChar(s, &r); } int -utf8·FullRune(byte* s, int n) +utf8·fullRune(byte* s, int n) { int i; Rune c; @@ -118,7 +118,7 @@ utf8·FullRune(byte* s, int n) } byte* -utf8·FindRune(byte* s, long c) +utf8·findRune(byte* s, long c) { long c1; Rune r; @@ -134,7 +134,7 @@ utf8·FindRune(byte* s, long c) s++; continue; } - n = utf8·CharToRune(&r, s); + n = utf8·charToRune(&r, s); if (r == c) return s; s += n; } @@ -155,7 +155,7 @@ 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·NewCap(const byte* s, vlong len, vlong cap) +str·newCap(const byte* s, vlong len, vlong cap) { struct str·Hdr* h; @@ -184,34 +184,34 @@ 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·NewLen(const byte* s, vlong len) +str·newLen(const byte* s, vlong len) { vlong sl = (s == nil) ? 0 : strlen(s); if (sl < len) panicf("attempted to take a bigger substring than string length"); vlong cap = (len == 0) ? 1 : len; - return str·NewCap(s, len, cap); + return str·newCap(s, len, cap); } // 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·New(const byte* s) +str·new(const byte* s) { vlong len = (s == nil) ? 0 : strlen(s); - return str·NewLen(s, len); + return str·newLen(s, len); } // Newf returns a new dynamic string object string -str·Newf(const byte* fmt, ...) +str·newf(const byte* fmt, ...) { va_list args; va_start(args, fmt); vlong n = vsnprintf(nil, 0, fmt, args); va_end(args); - string s = str·NewCap(nil, 0, n); + string s = str·newCap(nil, 0, n); va_start(args, fmt); vsnprintf(s, n + 1, fmt, args); @@ -225,14 +225,14 @@ str·Newf(const byte* fmt, ...) // Free returns memory associated to the buffer. void -str·Free(string s) +str·free(string s) { free(s - sizeof(str·Hdr)); } // Len returns the length of the string. int -str·Len(const string s) +str·len(const string s) { str·Hdr* h = (str·Hdr*)(s - sizeof(str·Hdr)); return h->len; @@ -240,14 +240,14 @@ str·Len(const string s) // Cap returns the capacity of the string buffer. int -str·Cap(const string s) +str·cap(const string s) { str·Hdr* h = (str·Hdr*)(s - sizeof(str·Hdr)); return h->cap; } string -str·Clear(string s) +str·clear(string s) { str·Hdr* h = (str·Hdr*)(s - sizeof(str·Hdr)); h->len = 0; @@ -260,11 +260,11 @@ str·Clear(string s) // 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) +str·grow(string s, vlong delta) { str·Hdr *h, *newh; - vlong cap = str·Cap(s); - vlong len = str·Len(s); + vlong cap = str·cap(s); + vlong len = str·len(s); Assert(cap >= len); // To prevent unsigned behavior if (cap - len >= delta) return s; @@ -292,11 +292,11 @@ str·Grow(string s, vlong delta) // buffer. If the capacity equals the length, then the function is a NOOP. The // byte array is unchanged. string -str·Fit(string s) +str·fit(string s) { str·Hdr* h; - vlong cap = str·Cap(s); - vlong len = str·Len(s); + vlong cap = str·cap(s); + vlong len = str·len(s); if (cap == len) return s; @@ -312,17 +312,17 @@ str·Fit(string s) // string to our buffer. The result is reallocated if not enough room is present // in the buffer. string -str·AppendCount(string s, const byte* b, vlong n) +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); + s = str·grow(s, n); if (s == nil) return nil; str·Hdr* h = (str·Hdr*)(s - sizeof(str·Hdr)); - memcpy(s + str·Len(s), b, n); + memcpy(s + str·len(s), b, n); h->len += n; s[h->len] = '\0'; @@ -332,23 +332,23 @@ str·AppendCount(string s, const byte* b, vlong n) // 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) +str·append(string s, const byte* b) { - return str·AppendCount(s, b, strlen(b)); + return str·appendCount(s, strlen(b), 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) +str·appendByte(string s, const byte b) { - s = str·Grow(s, 1); + s = str·grow(s, 1); if (s == nil) return nil; str·Hdr* h = (str·Hdr*)(s - sizeof(str·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..? @@ -357,35 +357,38 @@ str·AppendByte(string s, const byte b) // Equals returns true if string s and t are equivalent. bool -str·Equals(const string s, const string t) +str·equals(const string s, const string t) { - vlong sL = str·Len(s); - vlong tL = str·Len(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. +//------------------------------------------------------------------------ +// Utility Methods + +/* + * Appendf will append the given formatted string to our buffer. + * Returns the newly minted string + */ string -str·Appendf(string s, const byte* fmt, ...) +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); + s = 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); } @@ -398,7 +401,7 @@ str·Appendf(string s, const byte* fmt, ...) // Find will find the first occurence of // substr in the string Returns -1 if nothing was found. int -str·Find(string s, const byte* substr) +str·find(string s, const byte* substr) { byte* loc = strstr(s, substr); if (loc == nil) return -1; @@ -412,18 +415,18 @@ str·Lower(string s) { byte *b, *e; b = s; - e = b + str·Len(s); + e = b + str·len(s); while (b++ != e) *b = tolower(*b); } // Upper will force all runes in the string to be uppercase. void -str·Upper(string s) +str·upper(string s) { byte *b, *e; b = s; - e = b + str·Len(s); + e = b + str·len(s); while (b++ != e) *b = toupper(*b); } @@ -432,13 +435,13 @@ str·Upper(string s) // Edits are done in place and modify the string. // NOTE: As of now strings from and to must be the same size. void -str·Replace(string s, const byte* from, const byte* to) +str·replace(string s, const byte* from, const byte* to) { vlong fromL = strlen(from); vlong toL = strlen(to); if (toL != fromL) { panicf("different sized replacement string not supported"); } - vlong l = str·Len(s); + vlong l = str·len(s); vlong i = l; vlong j = l; @@ -456,12 +459,12 @@ str·Replace(string s, const byte* from, const byte* to) // Returns a stretchy buffer of strings that result from the partition. // It is the caller's responsibility to clean the memory. string* -str·Split(string s, const byte* tok) +str·split(string s, const byte* tok) { string* fields = nil; vlong start = 0; - vlong sL = str·Len(s); + vlong sL = str·len(s); vlong tokL = strlen(tok); if (sL == 0 || tokL == 0) return nil; @@ -469,7 +472,7 @@ str·Split(string s, const byte* tok) for (vlong i = 0; i < sL - tokL; i++) { if ((tokL == 1 && s[i] == tokL) || !memcmp(s + i, tok, tokL)) { - bufpush(fields, str·NewLen(s + start, i - start)); + bufpush(fields, str·newLen(s + start, i - start)); if (fields[buflen(fields) - 1] == nil) goto cleanup; start = i + tokL; @@ -477,27 +480,27 @@ str·Split(string s, const byte* tok) } } - bufpush(fields, str·NewLen(s + start, sL - start)); + bufpush(fields, str·newLen(s + start, sL - start)); return fields; cleanup: for (vlong i = 0; i < buflen(fields); i++) { - str·Free(fields[i]); + str·free(fields[i]); } buffree(fields); return nil; } string -str·Join(byte** fields, vlong numFields, const byte* sep) +str·join(vlong len, byte** fields, const byte* sep) { - string s = str·NewCap(nil, 0, 10); + string s = str·newCap(nil, 0, 10); int j = 0; - for (j = 0; j < numFields; j++) { - s = str·Append(s, fields[j]); - if (j < numFields - 1) { s = str·AppendCount(s, sep, 1); } + for (j = 0; j < len; j++) { + s = str·append(s, fields[j]); + if (j < len - 1) { s = str·appendCount(s, 1, sep); } } return s; -- cgit v1.2.1