From 65e84b15a944c83862da736a427636f3e64d3fc2 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Thu, 4 Nov 2021 15:00:36 -0700 Subject: Feat: input readline now unicode aware Updated our assumptions of readline to handle valid unicode input. This required integrating with an explicit library to handle unicode knowledge. --- include/libunicode.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/libunicode.h b/include/libunicode.h index d6618eb..25d6dee 100644 --- a/include/libunicode.h +++ b/include/libunicode.h @@ -27,7 +27,7 @@ char *utf8·findlast(char* s, rune); // find last rune in char stream int utf8·canfit(char *, int); // XXX: odd function... -int utf8·isletter(rune r); +int utf8·isalpha(rune r); int utf8·isdigit(rune r); int utf8·isspace(rune r); int utf8·istitle(rune r); -- cgit v1.2.1 From 7ea1cdb7d31f00024f5a1d124b42cd19a03b959a Mon Sep 17 00:00:00 2001 From: Nicholas Date: Wed, 10 Nov 2021 20:12:45 -0800 Subject: chore: libunicode -> libutf --- include/base.h | 8 ++++---- include/libunicode.h | 38 -------------------------------------- include/libutf.h | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 42 deletions(-) delete mode 100644 include/libunicode.h create mode 100644 include/libutf.h (limited to 'include') diff --git a/include/base.h b/include/base.h index a7813e6..b841040 100644 --- a/include/base.h +++ b/include/base.h @@ -261,12 +261,12 @@ int bufio·read(io·Buffer *buf, int sz, int n, void *out); // ----------------------------------------------------------------------------- // memory mapped files -typedef struct mmap·Reader +typedef struct mmap·Reader { vlong len; - union { - byte *buf; - ubyte *ubuf; + union{ + char *b; + uchar *u; }; } mmap·Reader; diff --git a/include/libunicode.h b/include/libunicode.h deleted file mode 100644 index 25d6dee..0000000 --- a/include/libunicode.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -typedef uint32 rune; - -#define UTFmax 4 -#define RuneErr 0xFFFDu -#define RuneMax 0x10FFFFu - -#define utf8·onebyte(c) (((c)&0x80u)==0x00u) -#define utf8·twobyte(c) (((c)&0xE0u)==0xC0u) -#define utf8·threebyte(c) (((c)&0xF0u)==0xE0u) -#define utf8·fourbyte(c) (((c)&0xF8u)==0xF0u) - -/* - * UTF-8 functions. - */ -int utf8·len(char *s); // returns number of runes for s -int utf8·runelen(rune r); // returns number of bytes for rune -int utf8·runewidth(rune r); // returns number of printed columns for rune - -int utf8·decode(char *, rune *); // decode 1 rune from char stream, store into rune, return number of bytes -int utf8·encode(rune *, char *); // encode 1 rune from rune stream, store into char, return number of bytes -int utf8·decodeprev(char *s, rune *r); // decode 1 rune from char stream, reading backwards, store into rune, return number of bytes - -char *utf8·find(char *s, rune); // find rune in char stream -char *utf8·findlast(char* s, rune); // find last rune in char stream - -int utf8·canfit(char *, int); // XXX: odd function... - -int utf8·isalpha(rune r); -int utf8·isdigit(rune r); -int utf8·isspace(rune r); -int utf8·istitle(rune r); -int utf8·ispunct(rune r); - -rune utf8·toupper(rune r); -rune utf8·tolower(rune r); -rune utf8·totitle(rune r); diff --git a/include/libutf.h b/include/libutf.h new file mode 100644 index 0000000..25d6dee --- /dev/null +++ b/include/libutf.h @@ -0,0 +1,38 @@ +#pragma once + +typedef uint32 rune; + +#define UTFmax 4 +#define RuneErr 0xFFFDu +#define RuneMax 0x10FFFFu + +#define utf8·onebyte(c) (((c)&0x80u)==0x00u) +#define utf8·twobyte(c) (((c)&0xE0u)==0xC0u) +#define utf8·threebyte(c) (((c)&0xF0u)==0xE0u) +#define utf8·fourbyte(c) (((c)&0xF8u)==0xF0u) + +/* + * UTF-8 functions. + */ +int utf8·len(char *s); // returns number of runes for s +int utf8·runelen(rune r); // returns number of bytes for rune +int utf8·runewidth(rune r); // returns number of printed columns for rune + +int utf8·decode(char *, rune *); // decode 1 rune from char stream, store into rune, return number of bytes +int utf8·encode(rune *, char *); // encode 1 rune from rune stream, store into char, return number of bytes +int utf8·decodeprev(char *s, rune *r); // decode 1 rune from char stream, reading backwards, store into rune, return number of bytes + +char *utf8·find(char *s, rune); // find rune in char stream +char *utf8·findlast(char* s, rune); // find last rune in char stream + +int utf8·canfit(char *, int); // XXX: odd function... + +int utf8·isalpha(rune r); +int utf8·isdigit(rune r); +int utf8·isspace(rune r); +int utf8·istitle(rune r); +int utf8·ispunct(rune r); + +rune utf8·toupper(rune r); +rune utf8·tolower(rune r); +rune utf8·totitle(rune r); -- cgit v1.2.1 From 45f9449ae0b904917110d3f6937d0266daa84769 Mon Sep 17 00:00:00 2001 From: Nicholas Date: Thu, 11 Nov 2021 08:10:09 -0800 Subject: feat: added libfmt --- include/libfmt.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 include/libfmt.h (limited to 'include') diff --git a/include/libfmt.h b/include/libfmt.h new file mode 100644 index 0000000..17effc6 --- /dev/null +++ b/include/libfmt.h @@ -0,0 +1,62 @@ +#pragma once + +typedef struct fmt·State fmt·State; + +struct fmt·State +{ + struct{ + char *beg; + char *cur; + char *end; + } buffer; + int (*flush)(fmt·State *); + int n; + va_list args; + rune verb; + ulong flag; + int width; + int prec; +}; + +#define iota(x) (1 << (x)) +enum +{ + fmt·Width = iota(0), + fmt·Left = iota(1), + fmt·Prec = iota(2), + fmt·Sharp = iota(3), + fmt·Space = iota(4), + fmt·Sign = iota(5), + fmt·Apost = iota(6), + fmt·Zero = iota(7), + fmt·Unsigned = iota(8), + fmt·Short = iota(9), + fmt·Long = iota(10), + fmt·Vlong = iota(11), + fmt·Comma = iota(12), + fmt·Byte = iota(13), + fmt·Ldouble = iota(14), + fmt·Flag = iota(15), +}; +#undef iota + +/* normal printing interface */ +int fmt·print(char *fmt, ...); +int fmt·fprint(int fd, char *fmt, ...); + +int fmt·sprint(char *buf, char *fmt, ...); +int fmt·nsprint(int len, char *buf, char *fmt, ...); +int fmt·esprint(char *buf, char *end, char *fmt, ...); + +int fmt·vprint(char *fmt, va_list args); +int fmt·vfprint(int fd, char *fmt, va_list args); +int fmt·vwrite(char *buf, char *fmt, va_list args); +int fmt·vnwrite(int len, char *buf, char *fmt, va_list args); +int fmt·vewrite(char *buf, char *end, char *fmt, va_list args); + +/* low-level interface: custom printing verbs */ +int fmt·do(fmt·State *, char *fmt); +int fmt·put(fmt·State *, char *fmt, ...); +int fmt·vput(fmt·State *, char *fmt, va_list args); +int fmt·install(fmt·State *, int (*put)(fmt·State*)); +int fmt·flush(fmt·State *); -- cgit v1.2.1 From c65794b50b1bc729e7a4e940b76a973afa3030b9 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Thu, 11 Nov 2021 14:49:35 -0800 Subject: feat: libfmt prototype added from plan9 --- include/libfmt.h | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'include') diff --git a/include/libfmt.h b/include/libfmt.h index 17effc6..7ba40a0 100644 --- a/include/libfmt.h +++ b/include/libfmt.h @@ -9,13 +9,21 @@ struct fmt·State char *cur; char *end; } buffer; - int (*flush)(fmt·State *); int n; + va_list args; rune verb; ulong flag; int width; int prec; + char *thousands, *groups, *decimal; + + void *file; + int (*flush)(fmt·State *); + struct { + void *heap; + mem·Reallocator mem; + }; }; #define iota(x) (1 << (x)) @@ -41,22 +49,24 @@ enum #undef iota /* normal printing interface */ -int fmt·print(char *fmt, ...); -int fmt·fprint(int fd, char *fmt, ...); - -int fmt·sprint(char *buf, char *fmt, ...); -int fmt·nsprint(int len, char *buf, char *fmt, ...); -int fmt·esprint(char *buf, char *end, char *fmt, ...); - -int fmt·vprint(char *fmt, va_list args); -int fmt·vfprint(int fd, char *fmt, va_list args); -int fmt·vwrite(char *buf, char *fmt, va_list args); -int fmt·vnwrite(int len, char *buf, char *fmt, va_list args); -int fmt·vewrite(char *buf, char *end, char *fmt, va_list args); - -/* low-level interface: custom printing verbs */ -int fmt·do(fmt·State *, char *fmt); -int fmt·put(fmt·State *, char *fmt, ...); -int fmt·vput(fmt·State *, char *fmt, va_list args); -int fmt·install(fmt·State *, int (*put)(fmt·State*)); -int fmt·flush(fmt·State *); +int fmt·print(char *fmt, ...); +int fmt·fprint(int fd, char *fmt, ...); + +int fmt·sprint(char *buf, char *fmt, ...); +int fmt·nsprint(int len, char *buf, char *fmt, ...); +char *fmt·esprint(char *buf, char *end, char *fmt, ...); + +int fmt·vprint(char *fmt, va_list args); +int fmt·vfprint(int fd, char *fmt, va_list args); +int fmt·vnsprint(int len, char *buf, char *fmt, va_list args); +char *fmt·vesprint(char *buf, char *end, char *fmt, va_list args); + +/* low-level interface: used for custom printing verbs */ +int fmt·open(int fd, int len, char *buf, fmt·State *); // creates a buffer on a file +int fmt·make(mem·Reallocator mem, void *heap, fmt·State *); // creates an in-memory buffer +void fmt·free(fmt·State *); // releases an in-memory buffer + +int fmt·do(fmt·State *, char *fmt); +int fmt·write(fmt·State *, char *fmt, ...); +int fmt·vwrite(fmt·State *, char *fmt, va_list args); +int fmt·install(int c, int (*format)(fmt·State *)); -- cgit v1.2.1 From 9695ea005d4af93dcd60f74f10fd3c54499a182f Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Thu, 11 Nov 2021 16:31:58 -0800 Subject: chore: split up base library into individual files for smaller binaries --- include/base.h | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) (limited to 'include') diff --git a/include/base.h b/include/base.h index b841040..ca229b3 100644 --- a/include/base.h +++ b/include/base.h @@ -51,7 +51,6 @@ void* ·bufgrow(void*, vlong, vlong); // ----------------------------------------------------------------------------- // memory allocation -// TODO(nnoll): Allow for nil iterfaces? /* allocator interface */ typedef struct mem·Allocator { void *(*alloc)(void *heap, uint n, ulong size); @@ -66,7 +65,7 @@ typedef struct mem·Reallocator { void (*free)(void *iface, void *ptr); } mem·Reallocator; -extern mem·Reallocator sys·Relocator; +extern mem·Reallocator sys·FullMemory; /* simple memory arena */ typedef struct mem·Arena mem·Arena; @@ -94,23 +93,23 @@ error coro·free(Coro *c); typedef byte* string; -/* string helpers */ -string str·makecap(const byte *s, vlong len, vlong cap); -string str·makelen(const byte *s, vlong len); -string str·make(const byte *s); -string str·makef(const byte *fmt, ...); +/* augmented string functions */ +string str·makecap(const char *s, vlong len, vlong cap); +string str·makelen(const char *s, vlong len); +string str·make(const char *s); +string str·makef(const char *fmt, ...); void str·free(string s); int str·len(const string s); int str·cap(const string s); void str·clear(string *s); void str·grow(string *s, vlong delta); void str·fit(string *s); -int str·appendlen(string *s, vlong len, const byte *b); -int str·append(string *s, const byte* b); -int str·appendf(string *s, const byte* fmt, ...); -int str·appendbyte(string *s, const byte b); +int str·appendlen(string *s, vlong len, const char *b); +int str·append(string *s, const char *b); +int str·appendf(string *s, const char *fmt, ...); +int str·appendbyte(string *s, const char b); bool str·equals(const string s, const string t); -int str·find(string s, const byte* substr); +int str·find(string s, const char* substr); void str·lower(string s); void str·upper(string s); int str·read(string s, int size, int n, void *buf); @@ -118,6 +117,9 @@ void str·replace(string s, const byte* from, const byte* to); string* str·split(string s, const byte* tok); string str·join(vlong len, byte** fields, const byte* sep); +/* raw C string functions */ +char *str·copyn(char *dst, char *src, int n); + // ----------------------------------------------------------------------------- // i/o @@ -366,22 +368,22 @@ void vpanicf(byte *fmt, va_list args); // ----------------------------------------------------------------------------- // sorting -void sort·ints(uintptr n, int arr[]); -void sort·int8s(uintptr n, int8 arr[]); -void sort·int16s(uintptr n, int16 arr[]); -void sort·int32s(uintptr n, int32 arr[]); -void sort·int64s(uintptr n, int64 arr[]); +void sort·int(uintptr n, int arr[]); +void sort·int8(uintptr n, int8 arr[]); +void sort·int16(uintptr n, int16 arr[]); +void sort·int32(uintptr n, int32 arr[]); +void sort·int64(uintptr n, int64 arr[]); -void sort·uints(uintptr n, uint arr[]); -void sort·uint8s(uintptr n, uint8 arr[]); -void sort·uint16s(uintptr n, uint16 arr[]); -void sort·uint32s(uintptr n, uint32 arr[]); -void sort·uint64s(uintptr n, uint64 arr[]); +void sort·uint(uintptr n, uint arr[]); +void sort·uint8(uintptr n, uint8 arr[]); +void sort·uint16(uintptr n, uint16 arr[]); +void sort·uint32(uintptr n, uint32 arr[]); +void sort·uint64(uintptr n, uint64 arr[]); -void sort·floats(uintptr n, float arr[]); -void sort·doubles(uintptr n, double arr[]); +void sort·float(uintptr n, float arr[]); +void sort·double(uintptr n, double arr[]); -void sort·strings(uintptr n, byte* arr[]); +void sort·string(uintptr n, byte* arr[]); // ----------------------------------------------------------------------------- // fast random number generation @@ -390,6 +392,7 @@ error rng·init(uint64 seed); double rng·random(void); double rng·exponential(double lambda); bool rng·bernoulli(double f); +double rng·normal(void); uint64 rng·randi(int max); uint64 rng·poisson(double mean); -- cgit v1.2.1