mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-15 18:32:22 -07:00
Merge remote-tracking branch 'offical/master'
This commit is contained in:
@@ -423,7 +423,8 @@ _append_elem :: #force_inline proc(array: ^$T/[dynamic]$E, arg: E, should_zero:
|
||||
return 1, nil
|
||||
} else {
|
||||
if cap(array) < len(array)+1 {
|
||||
cap := 2 * cap(array) + max(8, 1)
|
||||
// Same behavior as _append_elems but there's only one arg, so we always just add 8.
|
||||
cap := 2 * cap(array) + 8
|
||||
|
||||
// do not 'or_return' here as it could be a partial success
|
||||
if should_zero {
|
||||
|
||||
@@ -97,14 +97,14 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||
}
|
||||
|
||||
|
||||
heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
|
||||
heap_alloc :: proc "contextless" (size: int, zero_memory := true) -> rawptr {
|
||||
return _heap_alloc(size, zero_memory)
|
||||
}
|
||||
|
||||
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
||||
heap_resize :: proc "contextless" (ptr: rawptr, new_size: int) -> rawptr {
|
||||
return _heap_resize(ptr, new_size)
|
||||
}
|
||||
|
||||
heap_free :: proc(ptr: rawptr) {
|
||||
heap_free :: proc "contextless" (ptr: rawptr) {
|
||||
_heap_free(ptr)
|
||||
}
|
||||
@@ -9,7 +9,7 @@ foreign {
|
||||
@(link_name="realloc") _orca_realloc :: proc "c" (ptr: rawptr, size: int) -> rawptr ---
|
||||
}
|
||||
|
||||
_heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
|
||||
_heap_alloc :: proc "contextless" (size: int, zero_memory := true) -> rawptr {
|
||||
if size <= 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -20,10 +20,10 @@ _heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
|
||||
}
|
||||
}
|
||||
|
||||
_heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
||||
_heap_resize :: proc "contextless" (ptr: rawptr, new_size: int) -> rawptr {
|
||||
return _orca_realloc(ptr, new_size)
|
||||
}
|
||||
|
||||
_heap_free :: proc(ptr: rawptr) {
|
||||
_heap_free :: proc "contextless" (ptr: rawptr) {
|
||||
_orca_free(ptr)
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
//+private
|
||||
package runtime
|
||||
|
||||
_heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
|
||||
_heap_alloc :: proc "contextless" (size: int, zero_memory := true) -> rawptr {
|
||||
unimplemented("base:runtime 'heap_alloc' procedure is not supported on this platform")
|
||||
}
|
||||
|
||||
_heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
||||
_heap_resize :: proc "contextless" (ptr: rawptr, new_size: int) -> rawptr {
|
||||
unimplemented("base:runtime 'heap_resize' procedure is not supported on this platform")
|
||||
}
|
||||
|
||||
_heap_free :: proc(ptr: rawptr) {
|
||||
_heap_free :: proc "contextless" (ptr: rawptr) {
|
||||
unimplemented("base:runtime 'heap_free' procedure is not supported on this platform")
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ foreign libc {
|
||||
@(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---
|
||||
}
|
||||
|
||||
_heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
|
||||
_heap_alloc :: proc "contextless" (size: int, zero_memory := true) -> rawptr {
|
||||
if size <= 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -27,12 +27,12 @@ _heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
|
||||
}
|
||||
}
|
||||
|
||||
_heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
||||
_heap_resize :: proc "contextless" (ptr: rawptr, new_size: int) -> rawptr {
|
||||
// NOTE: _unix_realloc doesn't guarantee new memory will be zeroed on
|
||||
// POSIX platforms. Ensure your caller takes this into account.
|
||||
return _unix_realloc(ptr, new_size)
|
||||
}
|
||||
|
||||
_heap_free :: proc(ptr: rawptr) {
|
||||
_heap_free :: proc "contextless" (ptr: rawptr) {
|
||||
_unix_free(ptr)
|
||||
}
|
||||
|
||||
@@ -14,11 +14,11 @@ foreign kernel32 {
|
||||
HeapFree :: proc(hHeap: rawptr, dwFlags: u32, lpMem: rawptr) -> b32 ---
|
||||
}
|
||||
|
||||
_heap_alloc :: proc(size: int, zero_memory := true) -> rawptr {
|
||||
_heap_alloc :: proc "contextless" (size: int, zero_memory := true) -> rawptr {
|
||||
HEAP_ZERO_MEMORY :: 0x00000008
|
||||
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY if zero_memory else 0, uint(size))
|
||||
}
|
||||
_heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
||||
_heap_resize :: proc "contextless" (ptr: rawptr, new_size: int) -> rawptr {
|
||||
if new_size == 0 {
|
||||
_heap_free(ptr)
|
||||
return nil
|
||||
@@ -30,7 +30,7 @@ _heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
||||
HEAP_ZERO_MEMORY :: 0x00000008
|
||||
return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ptr, uint(new_size))
|
||||
}
|
||||
_heap_free :: proc(ptr: rawptr) {
|
||||
_heap_free :: proc "contextless" (ptr: rawptr) {
|
||||
if ptr == nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -86,16 +86,23 @@ default_random_generator_proc :: proc(data: rawptr, mode: Random_Generator_Mode,
|
||||
init(r, 0)
|
||||
}
|
||||
|
||||
pos := i8(0)
|
||||
val := u64(0)
|
||||
for &v in p {
|
||||
if pos == 0 {
|
||||
val = read_u64(r)
|
||||
pos = 7
|
||||
switch len(p) {
|
||||
case size_of(u64):
|
||||
// Fast path for a 64-bit destination.
|
||||
intrinsics.unaligned_store(transmute(^u64)raw_data(p), read_u64(r))
|
||||
case:
|
||||
// All other cases.
|
||||
pos := i8(0)
|
||||
val := u64(0)
|
||||
for &v in p {
|
||||
if pos == 0 {
|
||||
val = read_u64(r)
|
||||
pos = 7
|
||||
}
|
||||
v = byte(val)
|
||||
val >>= 8
|
||||
pos -= 1
|
||||
}
|
||||
v = byte(val)
|
||||
val >>= 8
|
||||
pos -= 1
|
||||
}
|
||||
|
||||
case .Reset:
|
||||
|
||||
@@ -138,7 +138,9 @@ iterator_next :: proc(r: ^Reader) -> (record: []string, idx: int, err: Error, mo
|
||||
return record, r.line_count - 1, r.last_iterator_error, r.last_iterator_error == nil
|
||||
}
|
||||
|
||||
// Get last error if we the iterator
|
||||
// Get last CSV parse error if we ignored it in the iterator loop
|
||||
//
|
||||
// for record, row_idx in csv.iterator_next(&r) { ... }
|
||||
iterator_last_error :: proc(r: Reader) -> (err: Error) {
|
||||
return r.last_iterator_error
|
||||
}
|
||||
@@ -169,7 +171,7 @@ is_io_error :: proc(err: Error, io_err: io.Error) -> bool {
|
||||
|
||||
// read_all reads all the remaining records from r.
|
||||
// Each record is a slice of fields.
|
||||
// read_all is defined to read until an EOF, and does not treat, and does not treat EOF as an error
|
||||
// read_all is defined to read until an EOF, and does not treat EOF as an error
|
||||
@(require_results)
|
||||
read_all :: proc(r: ^Reader, allocator := context.allocator) -> ([][]string, Error) {
|
||||
context.allocator = allocator
|
||||
|
||||
@@ -368,6 +368,25 @@ caprintf :: proc(format: string, args: ..any, newline := false) -> cstring {
|
||||
caprintfln :: proc(format: string, args: ..any) -> cstring {
|
||||
return caprintf(format, ..args, newline=true)
|
||||
}
|
||||
// Creates a formatted C string
|
||||
//
|
||||
// *Allocates Using Context's Temporary Allocator*
|
||||
//
|
||||
// Inputs:
|
||||
// - args: A variadic list of arguments to be formatted.
|
||||
// - sep: An optional separator string (default is a single space).
|
||||
//
|
||||
// Returns: A formatted C string.
|
||||
//
|
||||
@(require_results)
|
||||
ctprint :: proc(args: ..any, sep := " ") -> cstring {
|
||||
str: strings.Builder
|
||||
strings.builder_init(&str, context.temp_allocator)
|
||||
sbprint(&str, ..args, sep=sep)
|
||||
strings.write_byte(&str, 0)
|
||||
s := strings.to_string(str)
|
||||
return cstring(raw_data(s))
|
||||
}
|
||||
// Creates a formatted C string
|
||||
//
|
||||
// *Allocates Using Context's Temporary Allocator*
|
||||
|
||||
@@ -41,7 +41,7 @@ init_from_f64 :: proc(x: ^$T/Fixed($Backing, $Fraction_Width), val: f64) {
|
||||
init_from_parts :: proc(x: ^$T/Fixed($Backing, $Fraction_Width), integer, fraction: Backing) {
|
||||
x.i = fraction
|
||||
x.i &= 1<<Fraction_Width - 1
|
||||
x.i |= integer
|
||||
x.i |= (integer << Fraction_Width)
|
||||
}
|
||||
|
||||
to_f64 :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> f64 {
|
||||
|
||||
@@ -121,6 +121,9 @@ _read :: proc(f: ^File, p: []byte) -> (i64, Error) {
|
||||
if n < 0 {
|
||||
return -1, _get_platform_error(n)
|
||||
}
|
||||
if n == 0 {
|
||||
return 0, .EOF
|
||||
}
|
||||
return i64(n), nil
|
||||
}
|
||||
|
||||
@@ -135,6 +138,9 @@ _read_at :: proc(f: ^File, p: []byte, offset: i64) -> (n: i64, err: Error) {
|
||||
if m < 0 {
|
||||
return -1, _get_platform_error(m)
|
||||
}
|
||||
if m == 0 {
|
||||
return 0, .EOF
|
||||
}
|
||||
n += i64(m)
|
||||
b = b[m:]
|
||||
offset += i64(m)
|
||||
|
||||
@@ -48,6 +48,9 @@ foreign kernel32 {
|
||||
nLength: DWORD,
|
||||
lpNumberOfEventsRead: LPDWORD) -> BOOL ---
|
||||
|
||||
// https://learn.microsoft.com/en-us/windows/console/getnumberofconsoleinputevents
|
||||
GetNumberOfConsoleInputEvents :: proc(hConsoleInput: HANDLE, lpcNumberOfEvents: LPDWORD) -> BOOL ---
|
||||
|
||||
GetConsoleMode :: proc(hConsoleHandle: HANDLE,
|
||||
lpMode: LPDWORD) -> BOOL ---
|
||||
SetConsoleMode :: proc(hConsoleHandle: HANDLE,
|
||||
@@ -63,6 +66,8 @@ foreign kernel32 {
|
||||
FlushConsoleInputBuffer :: proc(hConsoleInput: HANDLE) -> BOOL ---
|
||||
|
||||
GetFileInformationByHandle :: proc(hFile: HANDLE, lpFileInformation: LPBY_HANDLE_FILE_INFORMATION) -> BOOL ---
|
||||
|
||||
|
||||
SetHandleInformation :: proc(hObject: HANDLE,
|
||||
dwMask: DWORD,
|
||||
dwFlags: DWORD) -> BOOL ---
|
||||
|
||||
@@ -5,7 +5,7 @@ foreign import shell32 "system:Shell32.lib"
|
||||
|
||||
@(default_calling_convention="system")
|
||||
foreign shell32 {
|
||||
CommandLineToArgvW :: proc(cmd_list: wstring, num_args: ^c_int) -> ^wstring ---
|
||||
CommandLineToArgvW :: proc(cmd_list: wstring, num_args: ^c_int) -> [^]wstring ---
|
||||
ShellExecuteW :: proc(
|
||||
hwnd: HWND,
|
||||
lpOperation: LPCWSTR,
|
||||
|
||||
+274
-41
@@ -1,18 +1,27 @@
|
||||
/*
|
||||
The package `table` implements ASCII/markdown/HTML/custom rendering of tables.
|
||||
The package `table` implements plain-text/markdown/HTML/custom rendering of tables.
|
||||
|
||||
**Custom rendering example:**
|
||||
|
||||
tbl := init(&Table{})
|
||||
padding(tbl, 0, 1)
|
||||
row(tbl, "A_LONG_ENUM", "= 54,", "// A comment about A_LONG_ENUM")
|
||||
row(tbl, "AN_EVEN_LONGER_ENUM", "= 1,", "// A comment about AN_EVEN_LONGER_ENUM")
|
||||
build(tbl)
|
||||
for row in 0..<tbl.nr_rows {
|
||||
for col in 0..<tbl.nr_cols {
|
||||
write_table_cell(stdio_writer(), tbl, row, col)
|
||||
package main
|
||||
|
||||
import "core:io"
|
||||
import "core:text/table"
|
||||
|
||||
main :: proc() {
|
||||
stdout := table.stdio_writer()
|
||||
|
||||
tbl := table.init(&table.Table{})
|
||||
table.padding(tbl, 0, 1)
|
||||
table.row(tbl, "A_LONG_ENUM", "= 54,", "// A comment about A_LONG_ENUM")
|
||||
table.row(tbl, "AN_EVEN_LONGER_ENUM", "= 1,", "// A comment about AN_EVEN_LONGER_ENUM")
|
||||
table.build(tbl, table.unicode_width_proc)
|
||||
for row in 0..<tbl.nr_rows {
|
||||
for col in 0..<tbl.nr_cols {
|
||||
table.write_table_cell(stdout, tbl, row, col)
|
||||
}
|
||||
io.write_byte(stdout, '\n')
|
||||
}
|
||||
io.write_byte(stdio_writer(), '\n')
|
||||
}
|
||||
|
||||
This outputs:
|
||||
@@ -20,47 +29,57 @@ This outputs:
|
||||
A_LONG_ENUM = 54, // A comment about A_LONG_ENUM
|
||||
AN_EVEN_LONGER_ENUM = 1, // A comment about AN_EVEN_LONGER_ENUM
|
||||
|
||||
**ASCII rendering example:**
|
||||
**Plain-text rendering example:**
|
||||
|
||||
tbl := init(&Table{})
|
||||
defer destroy(tbl)
|
||||
package main
|
||||
|
||||
caption(tbl, "This is a table caption and it is very long")
|
||||
import "core:fmt"
|
||||
import "core:io"
|
||||
import "core:text/table"
|
||||
|
||||
padding(tbl, 1, 1) // Left/right padding of cells
|
||||
main :: proc() {
|
||||
stdout := table.stdio_writer()
|
||||
|
||||
header(tbl, "AAAAAAAAA", "B")
|
||||
header(tbl, "C") // Appends to previous header row. Same as if done header("AAAAAAAAA", "B", "C") from start.
|
||||
tbl := table.init(&table.Table{})
|
||||
defer table.destroy(tbl)
|
||||
|
||||
// Create a row with two values. Since there are three columns the third
|
||||
// value will become the empty string.
|
||||
//
|
||||
// NOTE: header() is not allowed anymore after this.
|
||||
row(tbl, 123, "foo")
|
||||
table.caption(tbl, "This is a table caption and it is very long")
|
||||
|
||||
// Use `format()` if you need custom formatting. This will allocate into
|
||||
// the arena specified at init.
|
||||
row(tbl,
|
||||
format(tbl, "%09d", 5),
|
||||
format(tbl, "%.6f", 6.28318530717958647692528676655900576))
|
||||
table.padding(tbl, 1, 1) // Left/right padding of cells
|
||||
|
||||
// A row with zero values is allowed as long as a previous row or header
|
||||
// exist. The value and alignment of each cell can then be set
|
||||
// individually.
|
||||
row(tbl)
|
||||
set_cell_value_and_alignment(tbl, last_row(tbl), 0, "a", .Center)
|
||||
set_cell_value(tbl, last_row(tbl), 1, "bbb")
|
||||
set_cell_value(tbl, last_row(tbl), 2, "c")
|
||||
table.header(tbl, "AAAAAAAAA", "B")
|
||||
table.header(tbl, "C") // Appends to previous header row. Same as if done header("AAAAAAAAA", "B", "C") from start.
|
||||
|
||||
// Headers are regular cells, too. Use header_row() as row index to modify
|
||||
// header cells.
|
||||
set_cell_alignment(tbl, header_row(tbl), 1, .Center) // Sets alignment of 'B' column to Center.
|
||||
set_cell_alignment(tbl, header_row(tbl), 2, .Right) // Sets alignment of 'C' column to Right.
|
||||
// Create a row with two values. Since there are three columns the third
|
||||
// value will become the empty string.
|
||||
//
|
||||
// NOTE: table.header() is not allowed anymore after this.
|
||||
table.row(tbl, 123, "foo")
|
||||
|
||||
build(tbl)
|
||||
// Use `format()` if you need custom formatting. This will allocate into
|
||||
// the arena specified at init.
|
||||
table.row(tbl,
|
||||
table.format(tbl, "%09d", 5),
|
||||
table.format(tbl, "%.6f", 6.28318530717958647692528676655900576))
|
||||
|
||||
write_ascii_table(stdio_writer(), tbl)
|
||||
write_markdown_table(stdio_writer(), tbl)
|
||||
// A row with zero values is allowed as long as a previous row or header
|
||||
// exist. The value and alignment of each cell can then be set
|
||||
// individually.
|
||||
table.row(tbl)
|
||||
|
||||
table.set_cell_value_and_alignment(tbl, table.last_row(tbl), 0, "a", .Center)
|
||||
table.set_cell_value(tbl, table.last_row(tbl), 1, "bbb")
|
||||
table.set_cell_value(tbl, table.last_row(tbl), 2, "c")
|
||||
|
||||
// Headers are regular cells, too. Use header_row() as row index to modify
|
||||
// header cells.
|
||||
table.set_cell_alignment(tbl, table.header_row(tbl), 1, .Center) // Sets alignment of 'B' column to Center.
|
||||
table.set_cell_alignment(tbl, table.header_row(tbl), 2, .Right) // Sets alignment of 'C' column to Right.
|
||||
|
||||
table.write_plain_table(stdout, tbl)
|
||||
fmt.println()
|
||||
table.write_markdown_table(stdout, tbl)
|
||||
}
|
||||
|
||||
This outputs:
|
||||
|
||||
@@ -83,5 +102,219 @@ and
|
||||
| a | bbb | c |
|
||||
|
||||
respectively.
|
||||
|
||||
|
||||
Additionally, if you want to set the alignment and values in-line while
|
||||
constructing a table, you can use `aligned_row_of_values` or
|
||||
`row_of_aligned_values` like so:
|
||||
|
||||
table.aligned_row_of_values(tbl, .Center, "Foo", "Bar")
|
||||
table.row_of_aligned_values(tbl, {{.Center, "Foo"}, {.Right, "Bar"}})
|
||||
|
||||
**Caching Results:**
|
||||
|
||||
If you only need to build a table once but display it potentially many times,
|
||||
it may be more efficient to cache the results of your write into a string.
|
||||
|
||||
Here's an example of how you can do that:
|
||||
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strings"
|
||||
import "core:text/table"
|
||||
|
||||
main :: proc() {
|
||||
string_buffer := strings.builder_make()
|
||||
defer strings.builder_destroy(&string_buffer)
|
||||
|
||||
{
|
||||
tbl: table.Table
|
||||
table.init(&tbl)
|
||||
defer table.destroy(&tbl)
|
||||
table.caption(&tbl, "Hellope!")
|
||||
table.row(&tbl, "Hellope", "World")
|
||||
|
||||
builder_writer := strings.to_writer(&string_buffer)
|
||||
|
||||
// The written table will be cached into the string builder after this call.
|
||||
table.write_plain_table(builder_writer, &tbl)
|
||||
}
|
||||
// The table is inaccessible, now that we're back in the first-level scope.
|
||||
|
||||
// But now the results are stored in the string builder, which can be converted to a string.
|
||||
my_table_string := strings.to_string(string_buffer)
|
||||
|
||||
// Remember that the string's allocated backing data lives in the
|
||||
// builder and must still be freed.
|
||||
//
|
||||
// The deferred call to `builder_destroy` will take care of that for us
|
||||
// in this simple example.
|
||||
fmt.println(my_table_string)
|
||||
}
|
||||
|
||||
**Regarding `Width_Procs`:**
|
||||
|
||||
If you know ahead of time that all the text you're parsing is ASCII, instead of
|
||||
Unicode, it is more efficient to use `table.ascii_width_proc` instead of the
|
||||
default `unicode_width_proc`, as that procedure has to perform in-depth lookups
|
||||
to determine multiple Unicode characteristics of the codepoints parsed in order
|
||||
to get the proper alignment for a variety of different scripts.
|
||||
|
||||
For example, you may do this instead:
|
||||
|
||||
table.write_plain_table(stdout, tbl, table.ascii_width_proc)
|
||||
table.write_markdown_table(stdout, tbl, table.ascii_width_proc)
|
||||
|
||||
The output will still be the same, but the preprocessing is much faster.
|
||||
|
||||
|
||||
You may also supply your own `Width_Proc`s, if you know more about how the text
|
||||
is structured than what we can assume.
|
||||
|
||||
simple_cjk_width_proc :: proc(str: string) -> (result: int) {
|
||||
for r in str {
|
||||
result += 2
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
table.write_plain_table(stdout, tbl, simple_cjk_width_proc)
|
||||
|
||||
This procedure will output 2 times the number of UTF-8 runes in a string, a
|
||||
simple heuristic for CJK-only wide text.
|
||||
|
||||
**Unicode Support:**
|
||||
|
||||
This package makes use of the `grapheme_count` procedure from the
|
||||
`core:unicode/utf8` package. It is a complete, standards-compliant
|
||||
implementation for counting graphemes and calculating visual width of a Unicode
|
||||
grapheme cluster in monospace cells.
|
||||
|
||||
Here is a full example of how well-supported Unicode is with this package:
|
||||
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:io"
|
||||
import "core:os"
|
||||
import "core:text/table"
|
||||
|
||||
scripts :: proc(w: io.Writer) {
|
||||
t: table.Table
|
||||
table.init(&t)
|
||||
table.caption(&t, "Tést Suite")
|
||||
table.padding(&t, 1, 3)
|
||||
table.header_of_aligned_values(&t, {{.Left, "Script"}, {.Center, "Sample"}})
|
||||
|
||||
table.row(&t, "Latin", "At vero eos et accusamus et iusto odio dignissimos ducimus,")
|
||||
table.row(&t, "Cyrillic", "Ру́сский язы́к — язык восточнославянской группы славянской")
|
||||
table.row(&t, "Greek", "Η ελληνική γλώσσα ανήκει στην ινδοευρωπαϊκή οικογένεια")
|
||||
table.row(&t, "Younger Futhark", "ᚴᚢᚱᛘᛦ ᚴᚢᚾᚢᚴᛦ ᚴᛅᚱᚦᛁ ᚴᚢᛒᛚ ᚦᚢᛋᛁ ᛅᚠᛏ ᚦᚢᚱᚢᛁ ᚴᚢᚾᚢ ᛋᛁᚾᛅ ᛏᛅᚾᛘᛅᚱᚴᛅᛦ ᛒᚢᛏ")
|
||||
table.row(&t, "Chinese hanzi", "官話為汉语的一支,主體分布在中国北部和西南部的大部分地区。")
|
||||
table.row(&t, "Japanese kana", "いろはにほへとちりぬるをわかよたれそつねならむ")
|
||||
table.row(&t, "Korean hangul", "한글, 조선글은 한국어의 공식문자로서, 세종이 한국어를")
|
||||
table.row(&t, "Thai", "ภาษาไทย หรือ ภาษาไทยกลาง เป็นภาษาในกลุ่มภาษาไท ซึ่งเป็นกลุ่มย่อยของตระกูลภาษาขร้า-ไท")
|
||||
table.row(&t, "Georgian", "ქართული ენა — ქართველურ ენათა ოჯახის ენა. ქართველების მშობლიური ენა,")
|
||||
table.row(&t, "Armenian", "Իր շուրջ հինգհազարամյա գոյության ընթացքում հայերենը շփվել է տարբեր")
|
||||
table.row(&t)
|
||||
table.row_of_aligned_values(&t, {{.Left, "Arabic"}, {.Right, "ٱللُّغَةُ ٱلْعَرَبِيَّة هي أكثر اللغات السامية تحدثًا، وإحدى أكثر"}})
|
||||
table.row_of_aligned_values(&t, {{.Left, "Hebrew"}, {.Right, "עִבְרִית היא שפה שמית, ממשפחת השפות האפרו-אסייתיות, הידועה"}})
|
||||
table.row(&t)
|
||||
table.row(&t, "Swedish", "Växjö [ˈvɛkːˌɧøː] är en tätort i södra Smålands inland samt centralort i Växjö kommun")
|
||||
table.row(&t, "Saxon", "Hwæt! We Gardena in geardagum, þeodcyninga, þrym gefrunon, hu ða æþelingas ellen fremedon.")
|
||||
table.row(&t)
|
||||
table.aligned_row_of_values(&t, .Center, "Emoji (Single codepoints)", "\U0001f4ae \U0001F600 \U0001F201 \U0001F21A")
|
||||
table.row(&t, "Excessive Diacritics", "H̷e̶l̵l̸o̴p̵e̷ ̸w̶o̸r̵l̶d̵!̴")
|
||||
|
||||
table.write_plain_table(w, &t)
|
||||
fmt.println()
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
stdout := os.stream_from_handle(os.stdout)
|
||||
|
||||
scripts(stdout)
|
||||
}
|
||||
|
||||
This will print out:
|
||||
|
||||
+----------------------------------------------------------------------------------------------------------------------------+
|
||||
| Tést Suite |
|
||||
+-----------------------------+----------------------------------------------------------------------------------------------+
|
||||
| Script | Sample |
|
||||
+-----------------------------+----------------------------------------------------------------------------------------------+
|
||||
| Latin | At vero eos et accusamus et iusto odio dignissimos ducimus, |
|
||||
| Cyrillic | Ру́сский язы́к — язык восточнославянской группы славянской |
|
||||
| Greek | Η ελληνική γλώσσα ανήκει στην ινδοευρωπαϊκή οικογένεια |
|
||||
| Younger Futhark | ᚴᚢᚱᛘᛦ ᚴᚢᚾᚢᚴᛦ ᚴᛅᚱᚦᛁ ᚴᚢᛒᛚ ᚦᚢᛋᛁ ᛅᚠᛏ ᚦᚢᚱᚢᛁ ᚴᚢᚾᚢ ᛋᛁᚾᛅ ᛏᛅᚾᛘᛅᚱᚴᛅᛦ ᛒᚢᛏ |
|
||||
| Chinese hanzi | 官話為汉语的一支,主體分布在中国北部和西南部的大部分地区。 |
|
||||
| Japanese kana | いろはにほへとちりぬるをわかよたれそつねならむ |
|
||||
| Korean hangul | 한글, 조선글은 한국어의 공식문자로서, 세종이 한국어를 |
|
||||
| Thai | ภาษาไทย หรือ ภาษาไทยกลาง เป็นภาษาในกลุ่มภาษาไท ซึ่งเป็นกลุ่มย่อยของตระกูลภาษาขร้า-ไท |
|
||||
| Georgian | ქართული ენა — ქართველურ ენათა ოჯახის ენა. ქართველების მშობლიური ენა, |
|
||||
| Armenian | Իր շուրջ հինգհազարամյա գոյության ընթացքում հայերենը շփվել է տարբեր |
|
||||
| | |
|
||||
| Arabic | ٱللُّغَةُ ٱلْعَرَبِيَّة هي أكثر اللغات السامية تحدثًا، وإحدى أكثر |
|
||||
| Hebrew | עִבְרִית היא שפה שמית, ממשפחת השפות האפרו-אסייתיות, הידועה |
|
||||
| | |
|
||||
| Swedish | Växjö [ˈvɛkːˌɧøː] är en tätort i södra Smålands inland samt centralort i Växjö kommun |
|
||||
| Saxon | Hwæt! We Gardena in geardagum, þeodcyninga, þrym gefrunon, hu ða æþelingas ellen fremedon. |
|
||||
| | |
|
||||
| Emoji (Single codepoints) | 💮 😀 🈁 🈚 |
|
||||
| Excessive Diacritics | H̷e̶l̵l̸o̴p̵e̷ ̸w̶o̸r̵l̶d̵!̴ |
|
||||
+-----------------------------+----------------------------------------------------------------------------------------------+
|
||||
|
||||
**Decorated Tables:**
|
||||
|
||||
If you'd prefer to change the borders used by the plain-text table printing,
|
||||
there is the `write_decorated_table` procedure that allows you to change the
|
||||
corners and dividers.
|
||||
|
||||
Here is a complete example:
|
||||
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:io"
|
||||
import "core:os"
|
||||
import "core:text/table"
|
||||
|
||||
box_drawing :: proc(w: io.Writer) {
|
||||
t: table.Table
|
||||
table.init(&t)
|
||||
table.caption(&t, "Box Drawing Example")
|
||||
table.padding(&t, 2, 2)
|
||||
table.header_of_aligned_values(&t, {{.Left, "Operating System"}, {.Center, "Year Introduced"}})
|
||||
|
||||
table.row(&t, "UNIX", "1973")
|
||||
table.row(&t, "MS-DOS", "1981")
|
||||
table.row(&t, "Commodore 64 KERNAL", "1982")
|
||||
table.row(&t, "Mac OS", "1984")
|
||||
table.row(&t, "Amiga", "1985")
|
||||
table.row(&t, "Windows 1.0", "1985")
|
||||
table.row(&t, "Linux", "1991")
|
||||
table.row(&t, "Windows 3.1", "1992")
|
||||
|
||||
decorations := table.Decorations {
|
||||
"┌", "┬", "┐",
|
||||
"├", "┼", "┤",
|
||||
"└", "┴", "┘",
|
||||
"│", "─",
|
||||
}
|
||||
|
||||
table.write_decorated_table(w, &t, decorations)
|
||||
fmt.println()
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
stdout := os.stream_from_handle(os.stdout)
|
||||
|
||||
box_drawing(stdout)
|
||||
}
|
||||
|
||||
While the decorations support multi-codepoint Unicode graphemes, do note that
|
||||
each border character should not be larger than one monospace cell.
|
||||
|
||||
*/
|
||||
package text_table
|
||||
|
||||
+279
-102
@@ -4,18 +4,22 @@
|
||||
|
||||
List of contributors:
|
||||
oskarnp: Initial implementation.
|
||||
Feoramund: Unicode support.
|
||||
*/
|
||||
|
||||
package text_table
|
||||
|
||||
import "core:io"
|
||||
import "core:fmt"
|
||||
import "core:log"
|
||||
import "core:mem"
|
||||
import "core:mem/virtual"
|
||||
import "core:unicode/utf8"
|
||||
import "base:runtime"
|
||||
|
||||
Cell :: struct {
|
||||
text: string,
|
||||
width: int,
|
||||
alignment: Cell_Alignment,
|
||||
}
|
||||
|
||||
@@ -25,6 +29,14 @@ Cell_Alignment :: enum {
|
||||
Right,
|
||||
}
|
||||
|
||||
Aligned_Value :: struct {
|
||||
alignment: Cell_Alignment,
|
||||
value: any,
|
||||
}
|
||||
|
||||
// Determines the width of a string used in the table for alignment purposes.
|
||||
Width_Proc :: #type proc(str: string) -> int
|
||||
|
||||
Table :: struct {
|
||||
lpad, rpad: int, // Cell padding (left/right)
|
||||
cells: [dynamic]Cell,
|
||||
@@ -34,13 +46,33 @@ Table :: struct {
|
||||
table_allocator: runtime.Allocator, // Used for allocating cells/colw
|
||||
format_allocator: runtime.Allocator, // Used for allocating Cell.text when applicable
|
||||
|
||||
dirty: bool, // True if build() needs to be called before rendering
|
||||
|
||||
// The following are computed on build()
|
||||
colw: [dynamic]int, // Width of each column (including padding, excluding borders)
|
||||
colw: [dynamic]int, // Width of each column (excluding padding and borders)
|
||||
tblw: int, // Width of entire table (including padding, excluding borders)
|
||||
}
|
||||
|
||||
Decorations :: struct {
|
||||
// These are strings, because of multi-codepoint Unicode graphemes.
|
||||
|
||||
// Connecting decorations:
|
||||
nw, n, ne,
|
||||
w, x, e,
|
||||
sw, s, se: string,
|
||||
|
||||
// Straight lines:
|
||||
vert: string,
|
||||
horz: string,
|
||||
}
|
||||
|
||||
ascii_width_proc :: proc(str: string) -> int {
|
||||
return len(str)
|
||||
}
|
||||
|
||||
unicode_width_proc :: proc(str: string) -> (width: int) {
|
||||
_, _, width = #force_inline utf8.grapheme_count(str)
|
||||
return
|
||||
}
|
||||
|
||||
init :: proc{init_with_allocator, init_with_virtual_arena, init_with_mem_arena}
|
||||
|
||||
init_with_allocator :: proc(tbl: ^Table, format_allocator := context.temp_allocator, table_allocator := context.allocator) -> ^Table {
|
||||
@@ -65,13 +97,11 @@ destroy :: proc(tbl: ^Table) {
|
||||
|
||||
caption :: proc(tbl: ^Table, value: string) {
|
||||
tbl.caption = value
|
||||
tbl.dirty = true
|
||||
}
|
||||
|
||||
padding :: proc(tbl: ^Table, lpad, rpad: int) {
|
||||
tbl.lpad = lpad
|
||||
tbl.rpad = rpad
|
||||
tbl.dirty = true
|
||||
}
|
||||
|
||||
get_cell :: proc(tbl: ^Table, row, col: int, loc := #caller_location) -> ^Cell {
|
||||
@@ -81,43 +111,47 @@ get_cell :: proc(tbl: ^Table, row, col: int, loc := #caller_location) -> ^Cell {
|
||||
return &tbl.cells[row*tbl.nr_cols + col]
|
||||
}
|
||||
|
||||
set_cell_value_and_alignment :: proc(tbl: ^Table, row, col: int, value: string, alignment: Cell_Alignment) {
|
||||
cell := get_cell(tbl, row, col)
|
||||
cell.text = format(tbl, "%v", value)
|
||||
cell.alignment = alignment
|
||||
tbl.dirty = true
|
||||
@private
|
||||
to_string :: #force_inline proc(tbl: ^Table, value: any, loc := #caller_location) -> (result: string) {
|
||||
switch val in value {
|
||||
case nil:
|
||||
result = ""
|
||||
case string:
|
||||
result = val
|
||||
case cstring:
|
||||
result = cast(string)val
|
||||
case:
|
||||
result = format(tbl, "%v", val)
|
||||
if result == "" {
|
||||
log.error("text/table.format() resulted in empty string (arena out of memory?)", location = loc)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
set_cell_value :: proc(tbl: ^Table, row, col: int, value: any, loc := #caller_location) {
|
||||
cell := get_cell(tbl, row, col, loc)
|
||||
switch val in value {
|
||||
case nil:
|
||||
cell.text = ""
|
||||
case string:
|
||||
cell.text = string(val)
|
||||
case cstring:
|
||||
cell.text = string(val)
|
||||
case:
|
||||
cell.text = format(tbl, "%v", val)
|
||||
if cell.text == "" {
|
||||
fmt.eprintf("{} text/table: format() resulted in empty string (arena out of memory?)\n", loc)
|
||||
}
|
||||
}
|
||||
tbl.dirty = true
|
||||
cell.text = to_string(tbl, value, loc)
|
||||
}
|
||||
|
||||
set_cell_alignment :: proc(tbl: ^Table, row, col: int, alignment: Cell_Alignment, loc := #caller_location) {
|
||||
cell := get_cell(tbl, row, col, loc)
|
||||
cell.alignment = alignment
|
||||
tbl.dirty = true
|
||||
}
|
||||
|
||||
format :: proc(tbl: ^Table, _fmt: string, args: ..any, loc := #caller_location) -> string {
|
||||
set_cell_value_and_alignment :: proc(tbl: ^Table, row, col: int, value: any, alignment: Cell_Alignment, loc := #caller_location) {
|
||||
cell := get_cell(tbl, row, col, loc)
|
||||
cell.text = to_string(tbl, value, loc)
|
||||
cell.alignment = alignment
|
||||
}
|
||||
|
||||
format :: proc(tbl: ^Table, _fmt: string, args: ..any) -> string {
|
||||
context.allocator = tbl.format_allocator
|
||||
return fmt.aprintf(_fmt, ..args)
|
||||
}
|
||||
|
||||
header :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
|
||||
header :: header_of_values
|
||||
header_of_values :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
|
||||
if (tbl.has_header_row && tbl.nr_rows != 1) || (!tbl.has_header_row && tbl.nr_rows != 0) {
|
||||
panic("Cannot add headers after rows have been added", loc)
|
||||
}
|
||||
@@ -133,26 +167,108 @@ header :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
|
||||
set_cell_value(tbl, header_row(tbl), col, val, loc)
|
||||
col += 1
|
||||
}
|
||||
|
||||
tbl.dirty = true
|
||||
}
|
||||
|
||||
row :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
|
||||
aligned_header_of_values :: proc(tbl: ^Table, alignment: Cell_Alignment, values: ..any, loc := #caller_location) {
|
||||
if (tbl.has_header_row && tbl.nr_rows != 1) || (!tbl.has_header_row && tbl.nr_rows != 0) {
|
||||
panic("Cannot add headers after rows have been added", loc)
|
||||
}
|
||||
|
||||
if tbl.nr_rows == 0 {
|
||||
tbl.nr_rows += 1
|
||||
tbl.has_header_row = true
|
||||
}
|
||||
|
||||
col := tbl.nr_cols
|
||||
tbl.nr_cols += len(values)
|
||||
for val in values {
|
||||
set_cell_value_and_alignment(tbl, header_row(tbl), col, val, alignment, loc)
|
||||
col += 1
|
||||
}
|
||||
}
|
||||
|
||||
header_of_aligned_values :: proc(tbl: ^Table, aligned_values: []Aligned_Value, loc := #caller_location) {
|
||||
if (tbl.has_header_row && tbl.nr_rows != 1) || (!tbl.has_header_row && tbl.nr_rows != 0) {
|
||||
panic("Cannot add headers after rows have been added", loc)
|
||||
}
|
||||
|
||||
if tbl.nr_rows == 0 {
|
||||
tbl.nr_rows += 1
|
||||
tbl.has_header_row = true
|
||||
}
|
||||
|
||||
col := tbl.nr_cols
|
||||
tbl.nr_cols += len(aligned_values)
|
||||
for av in aligned_values {
|
||||
set_cell_value_and_alignment(tbl, header_row(tbl), col, av.value, av.alignment, loc)
|
||||
col += 1
|
||||
}
|
||||
}
|
||||
|
||||
row :: row_of_values
|
||||
row_of_values :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
|
||||
if tbl.nr_cols == 0 {
|
||||
if len(values) == 0 {
|
||||
panic("Cannot create row without values unless knowing amount of columns in advance")
|
||||
panic("Cannot create empty row unless the number of columns is known in advance")
|
||||
} else {
|
||||
tbl.nr_cols = len(values)
|
||||
}
|
||||
}
|
||||
|
||||
tbl.nr_rows += 1
|
||||
|
||||
for col in 0..<tbl.nr_cols {
|
||||
val := values[col] if col < len(values) else nil
|
||||
set_cell_value(tbl, last_row(tbl), col, val)
|
||||
set_cell_value(tbl, last_row(tbl), col, val, loc)
|
||||
}
|
||||
tbl.dirty = true
|
||||
}
|
||||
|
||||
aligned_row_of_values :: proc(tbl: ^Table, alignment: Cell_Alignment, values: ..any, loc := #caller_location) {
|
||||
if tbl.nr_cols == 0 {
|
||||
if len(values) == 0 {
|
||||
panic("Cannot create empty row unless the number of columns is known in advance")
|
||||
} else {
|
||||
tbl.nr_cols = len(values)
|
||||
}
|
||||
}
|
||||
|
||||
tbl.nr_rows += 1
|
||||
|
||||
for col in 0..<tbl.nr_cols {
|
||||
val := values[col] if col < len(values) else nil
|
||||
set_cell_value_and_alignment(tbl, last_row(tbl), col, val, alignment, loc)
|
||||
}
|
||||
}
|
||||
|
||||
row_of_aligned_values :: proc(tbl: ^Table, aligned_values: []Aligned_Value, loc := #caller_location) {
|
||||
if tbl.nr_cols == 0 {
|
||||
if len(aligned_values) == 0 {
|
||||
panic("Cannot create empty row unless the number of columns is known in advance")
|
||||
} else {
|
||||
tbl.nr_cols = len(aligned_values)
|
||||
}
|
||||
}
|
||||
|
||||
tbl.nr_rows += 1
|
||||
|
||||
for col in 0..<tbl.nr_cols {
|
||||
if col < len(aligned_values) {
|
||||
val := aligned_values[col].value
|
||||
alignment := aligned_values[col].alignment
|
||||
set_cell_value_and_alignment(tbl, last_row(tbl), col, val, alignment, loc)
|
||||
} else {
|
||||
set_cell_value_and_alignment(tbl, last_row(tbl), col, "", .Left, loc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: This should work correctly when #3262 is fixed.
|
||||
// row :: proc {
|
||||
// row_of_values,
|
||||
// aligned_row_of_values,
|
||||
// row_of_aligned_values,
|
||||
// }
|
||||
|
||||
last_row :: proc(tbl: ^Table) -> int {
|
||||
return tbl.nr_rows - 1
|
||||
}
|
||||
@@ -165,27 +281,24 @@ first_row :: proc(tbl: ^Table) -> int {
|
||||
return header_row(tbl)+1 if tbl.has_header_row else 0
|
||||
}
|
||||
|
||||
build :: proc(tbl: ^Table) {
|
||||
tbl.dirty = false
|
||||
|
||||
build :: proc(tbl: ^Table, width_proc: Width_Proc) {
|
||||
resize(&tbl.colw, tbl.nr_cols)
|
||||
mem.zero_slice(tbl.colw[:])
|
||||
|
||||
for row in 0..<tbl.nr_rows {
|
||||
for col in 0..<tbl.nr_cols {
|
||||
cell := get_cell(tbl, row, col)
|
||||
if w := len(cell.text) + tbl.lpad + tbl.rpad; w > tbl.colw[col] {
|
||||
tbl.colw[col] = w
|
||||
}
|
||||
cell.width = width_proc(cell.text)
|
||||
tbl.colw[col] = max(tbl.colw[col], cell.width)
|
||||
}
|
||||
}
|
||||
|
||||
colw_sum := 0
|
||||
for v in tbl.colw {
|
||||
colw_sum += v
|
||||
colw_sum += v + tbl.lpad + tbl.rpad
|
||||
}
|
||||
|
||||
tbl.tblw = max(colw_sum, len(tbl.caption) + tbl.lpad + tbl.rpad)
|
||||
tbl.tblw = max(colw_sum, width_proc(tbl.caption) + tbl.lpad + tbl.rpad)
|
||||
|
||||
// Resize columns to match total width of table
|
||||
remain := tbl.tblw-colw_sum
|
||||
@@ -198,13 +311,9 @@ build :: proc(tbl: ^Table) {
|
||||
}
|
||||
|
||||
write_html_table :: proc(w: io.Writer, tbl: ^Table) {
|
||||
if tbl.dirty {
|
||||
build(tbl)
|
||||
}
|
||||
|
||||
io.write_string(w, "<table>\n")
|
||||
if tbl.caption != "" {
|
||||
io.write_string(w, "<caption>")
|
||||
io.write_string(w, "\t<caption>")
|
||||
io.write_string(w, tbl.caption)
|
||||
io.write_string(w, "</caption>\n")
|
||||
}
|
||||
@@ -219,45 +328,43 @@ write_html_table :: proc(w: io.Writer, tbl: ^Table) {
|
||||
}
|
||||
|
||||
if tbl.has_header_row {
|
||||
io.write_string(w, "<thead>\n")
|
||||
io.write_string(w, " <tr>\n")
|
||||
io.write_string(w, "\t<thead>\n")
|
||||
io.write_string(w, "\t\t<tr>\n")
|
||||
for col in 0..<tbl.nr_cols {
|
||||
cell := get_cell(tbl, header_row(tbl), col)
|
||||
io.write_string(w, " <th")
|
||||
io.write_string(w, "\t\t\t<th")
|
||||
io.write_string(w, align_attribute(cell))
|
||||
io.write_string(w, ">")
|
||||
io.write_string(w, cell.text)
|
||||
io.write_string(w, "</th>\n")
|
||||
}
|
||||
io.write_string(w, " </tr>\n")
|
||||
io.write_string(w, "</thead>\n")
|
||||
io.write_string(w, "\t\t</tr>\n")
|
||||
io.write_string(w, "\t</thead>\n")
|
||||
}
|
||||
|
||||
io.write_string(w, "<tbody>\n")
|
||||
io.write_string(w, "\t<tbody>\n")
|
||||
for row in 0..<tbl.nr_rows {
|
||||
if tbl.has_header_row && row == header_row(tbl) {
|
||||
continue
|
||||
}
|
||||
io.write_string(w, " <tr>\n")
|
||||
io.write_string(w, "\t\t<tr>\n")
|
||||
for col in 0..<tbl.nr_cols {
|
||||
cell := get_cell(tbl, row, col)
|
||||
io.write_string(w, " <td")
|
||||
io.write_string(w, "\t\t\t<td")
|
||||
io.write_string(w, align_attribute(cell))
|
||||
io.write_string(w, ">")
|
||||
io.write_string(w, cell.text)
|
||||
io.write_string(w, "</td>\n")
|
||||
}
|
||||
io.write_string(w, " </tr>\n")
|
||||
io.write_string(w, "\t\t</tr>\n")
|
||||
}
|
||||
io.write_string(w, " </tbody>\n")
|
||||
io.write_string(w, "\t</tbody>\n")
|
||||
|
||||
io.write_string(w, "</table>\n")
|
||||
}
|
||||
|
||||
write_ascii_table :: proc(w: io.Writer, tbl: ^Table) {
|
||||
if tbl.dirty {
|
||||
build(tbl)
|
||||
}
|
||||
write_plain_table :: proc(w: io.Writer, tbl: ^Table, width_proc: Width_Proc = unicode_width_proc) {
|
||||
build(tbl, width_proc)
|
||||
|
||||
write_caption_separator :: proc(w: io.Writer, tbl: ^Table) {
|
||||
io.write_byte(w, '+')
|
||||
@@ -271,7 +378,7 @@ write_ascii_table :: proc(w: io.Writer, tbl: ^Table) {
|
||||
if col == 0 {
|
||||
io.write_byte(w, '+')
|
||||
}
|
||||
write_byte_repeat(w, tbl.colw[col], '-')
|
||||
write_byte_repeat(w, tbl.colw[col] + tbl.lpad + tbl.rpad, '-')
|
||||
io.write_byte(w, '+')
|
||||
}
|
||||
io.write_byte(w, '\n')
|
||||
@@ -280,8 +387,8 @@ write_ascii_table :: proc(w: io.Writer, tbl: ^Table) {
|
||||
if tbl.caption != "" {
|
||||
write_caption_separator(w, tbl)
|
||||
io.write_byte(w, '|')
|
||||
write_text_align(w, tbl.tblw - tbl.lpad - tbl.rpad + tbl.nr_cols - 1,
|
||||
tbl.lpad, tbl.rpad, tbl.caption, .Center)
|
||||
write_text_align(w, tbl.caption, .Center,
|
||||
tbl.lpad, tbl.rpad, tbl.tblw + tbl.nr_cols - 1 - width_proc(tbl.caption) - tbl.lpad - tbl.rpad)
|
||||
io.write_byte(w, '|')
|
||||
io.write_byte(w, '\n')
|
||||
}
|
||||
@@ -303,48 +410,123 @@ write_ascii_table :: proc(w: io.Writer, tbl: ^Table) {
|
||||
write_table_separator(w, tbl)
|
||||
}
|
||||
|
||||
// Renders table according to GitHub Flavored Markdown (GFM) specification
|
||||
write_markdown_table :: proc(w: io.Writer, tbl: ^Table) {
|
||||
// NOTE(oskar): Captions or colspans are not supported by GFM as far as I can tell.
|
||||
|
||||
if tbl.dirty {
|
||||
build(tbl)
|
||||
write_decorated_table :: proc(w: io.Writer, tbl: ^Table, decorations: Decorations, width_proc: Width_Proc = unicode_width_proc) {
|
||||
draw_dividing_row :: proc(w: io.Writer, tbl: ^Table, left, horz, divider, right: string) {
|
||||
io.write_string(w, left)
|
||||
for col in 0..<tbl.nr_cols {
|
||||
for _ in 0..<tbl.colw[col] + tbl.lpad + tbl.rpad {
|
||||
io.write_string(w, horz)
|
||||
}
|
||||
if col < tbl.nr_cols-1 {
|
||||
io.write_string(w, divider)
|
||||
}
|
||||
}
|
||||
io.write_string(w, right)
|
||||
io.write_byte(w, '\n')
|
||||
}
|
||||
|
||||
for row in 0..<tbl.nr_rows {
|
||||
build(tbl, width_proc)
|
||||
|
||||
// This determines whether or not to divide the top border.
|
||||
top_divider := decorations.n if len(tbl.caption) == 0 else decorations.horz
|
||||
|
||||
// Draw the north border.
|
||||
draw_dividing_row(w, tbl, decorations.nw, decorations.horz, top_divider, decorations.ne)
|
||||
|
||||
if len(tbl.caption) != 0 {
|
||||
// Draw the caption.
|
||||
io.write_string(w, decorations.vert)
|
||||
write_text_align(w, tbl.caption, .Center,
|
||||
tbl.lpad, tbl.rpad, tbl.tblw + tbl.nr_cols - 1 - width_proc(tbl.caption) - tbl.lpad - tbl.rpad)
|
||||
io.write_string(w, decorations.vert)
|
||||
io.write_byte(w, '\n')
|
||||
|
||||
// Draw the divider between the caption and the table rows.
|
||||
draw_dividing_row(w, tbl, decorations.w, decorations.horz, decorations.n, decorations.e)
|
||||
}
|
||||
|
||||
// Draw the header.
|
||||
start := 0
|
||||
if tbl.has_header_row {
|
||||
io.write_string(w, decorations.vert)
|
||||
row := header_row(tbl)
|
||||
for col in 0..<tbl.nr_cols {
|
||||
write_table_cell(w, tbl, row, col)
|
||||
io.write_string(w, decorations.vert)
|
||||
}
|
||||
io.write_byte(w, '\n')
|
||||
start += row + 1
|
||||
|
||||
draw_dividing_row(w, tbl, decorations.w, decorations.horz, decorations.x, decorations.e)
|
||||
}
|
||||
|
||||
// Draw the cells.
|
||||
for row in start..<tbl.nr_rows {
|
||||
for col in 0..<tbl.nr_cols {
|
||||
if col == 0 {
|
||||
io.write_string(w, decorations.vert)
|
||||
}
|
||||
write_table_cell(w, tbl, row, col)
|
||||
io.write_string(w, decorations.vert)
|
||||
}
|
||||
io.write_byte(w, '\n')
|
||||
}
|
||||
|
||||
// Draw the south border.
|
||||
draw_dividing_row(w, tbl, decorations.sw, decorations.horz, decorations.s, decorations.se)
|
||||
}
|
||||
|
||||
// Renders table according to GitHub Flavored Markdown (GFM) specification
|
||||
write_markdown_table :: proc(w: io.Writer, tbl: ^Table, width_proc: Width_Proc = unicode_width_proc) {
|
||||
// NOTE(oskar): Captions or colspans are not supported by GFM as far as I can tell.
|
||||
build(tbl, width_proc)
|
||||
|
||||
write_row :: proc(w: io.Writer, tbl: ^Table, row: int, alignment: Cell_Alignment = .Left) {
|
||||
for col in 0..<tbl.nr_cols {
|
||||
cell := get_cell(tbl, row, col)
|
||||
if col == 0 {
|
||||
io.write_byte(w, '|')
|
||||
}
|
||||
write_text_align(w, tbl.colw[col] - tbl.lpad - tbl.rpad, tbl.lpad, tbl.rpad, cell.text,
|
||||
.Center if tbl.has_header_row && row == header_row(tbl) else .Left)
|
||||
write_text_align(w, cell.text, alignment, tbl.lpad, tbl.rpad, tbl.colw[col] - cell.width)
|
||||
io.write_string(w, "|")
|
||||
}
|
||||
io.write_byte(w, '\n')
|
||||
}
|
||||
|
||||
if tbl.has_header_row && row == header_row(tbl) {
|
||||
for col in 0..<tbl.nr_cols {
|
||||
cell := get_cell(tbl, row, col)
|
||||
if col == 0 {
|
||||
io.write_byte(w, '|')
|
||||
}
|
||||
switch cell.alignment {
|
||||
case .Left:
|
||||
io.write_byte(w, ':')
|
||||
write_byte_repeat(w, max(1, tbl.colw[col]-1), '-')
|
||||
case .Center:
|
||||
io.write_byte(w, ':')
|
||||
write_byte_repeat(w, max(1, tbl.colw[col]-2), '-')
|
||||
io.write_byte(w, ':')
|
||||
case .Right:
|
||||
write_byte_repeat(w, max(1, tbl.colw[col]-1), '-')
|
||||
io.write_byte(w, ':')
|
||||
}
|
||||
start := 0
|
||||
|
||||
if tbl.has_header_row {
|
||||
row := header_row(tbl)
|
||||
|
||||
write_row(w, tbl, row, .Center)
|
||||
|
||||
for col in 0..<tbl.nr_cols {
|
||||
cell := get_cell(tbl, row, col)
|
||||
if col == 0 {
|
||||
io.write_byte(w, '|')
|
||||
}
|
||||
io.write_byte(w, '\n')
|
||||
divider_width := tbl.colw[col] + tbl.lpad + tbl.rpad - 1
|
||||
switch cell.alignment {
|
||||
case .Left:
|
||||
io.write_byte(w, ':')
|
||||
write_byte_repeat(w, max(1, divider_width), '-')
|
||||
case .Center:
|
||||
io.write_byte(w, ':')
|
||||
write_byte_repeat(w, max(1, divider_width - 1), '-')
|
||||
io.write_byte(w, ':')
|
||||
case .Right:
|
||||
write_byte_repeat(w, max(1, divider_width), '-')
|
||||
io.write_byte(w, ':')
|
||||
}
|
||||
io.write_byte(w, '|')
|
||||
}
|
||||
io.write_byte(w, '\n')
|
||||
|
||||
start += row + 1
|
||||
}
|
||||
|
||||
for row in start..<tbl.nr_rows {
|
||||
write_row(w, tbl, row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -355,27 +537,22 @@ write_byte_repeat :: proc(w: io.Writer, n: int, b: byte) {
|
||||
}
|
||||
|
||||
write_table_cell :: proc(w: io.Writer, tbl: ^Table, row, col: int) {
|
||||
if tbl.dirty {
|
||||
build(tbl)
|
||||
}
|
||||
cell := get_cell(tbl, row, col)
|
||||
write_text_align(w, tbl.colw[col]-tbl.lpad-tbl.rpad, tbl.lpad, tbl.rpad, cell.text, cell.alignment)
|
||||
write_text_align(w, cell.text, cell.alignment, tbl.lpad, tbl.rpad, tbl.colw[col] - cell.width)
|
||||
}
|
||||
|
||||
write_text_align :: proc(w: io.Writer, colw, lpad, rpad: int, text: string, alignment: Cell_Alignment) {
|
||||
write_text_align :: proc(w: io.Writer, text: string, alignment: Cell_Alignment, lpad, rpad, space: int) {
|
||||
write_byte_repeat(w, lpad, ' ')
|
||||
switch alignment {
|
||||
case .Left:
|
||||
io.write_string(w, text)
|
||||
write_byte_repeat(w, colw - len(text), ' ')
|
||||
write_byte_repeat(w, space, ' ')
|
||||
case .Center:
|
||||
pad := colw - len(text)
|
||||
odd := pad & 1 != 0
|
||||
write_byte_repeat(w, pad/2, ' ')
|
||||
write_byte_repeat(w, space/2, ' ')
|
||||
io.write_string(w, text)
|
||||
write_byte_repeat(w, pad/2 + 1 if odd else pad/2, ' ')
|
||||
write_byte_repeat(w, space/2 + space & 1, ' ')
|
||||
case .Right:
|
||||
write_byte_repeat(w, colw - len(text), ' ')
|
||||
write_byte_repeat(w, space, ' ')
|
||||
io.write_string(w, text)
|
||||
}
|
||||
write_byte_repeat(w, rpad, ' ')
|
||||
|
||||
@@ -220,6 +220,10 @@ unix :: proc "contextless" (sec: i64, nsec: i64) -> Time {
|
||||
return Time{(sec*1e9 + nsec)}
|
||||
}
|
||||
|
||||
from_nanoseconds :: #force_inline proc "contextless" (nsec: i64) -> Time {
|
||||
return Time{nsec}
|
||||
}
|
||||
|
||||
to_unix_seconds :: time_to_unix
|
||||
time_to_unix :: proc "contextless" (t: Time) -> i64 {
|
||||
return t._nsec/1e9
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
# Odin Roadmap
|
||||
|
||||
Not in any particular order
|
||||
|
||||
* Custom backend to replace LLVM
|
||||
- Improve SSA design to accommodate for lowering to a "bytecode"
|
||||
- SSA optimizations
|
||||
- COFF generation
|
||||
- linker
|
||||
* Type safe "macros"
|
||||
* Documentation generator for "Entities"
|
||||
* Multiple architecture support
|
||||
* Inline assembly
|
||||
* Linking options
|
||||
- Executable
|
||||
- Static/Dynamic Library
|
||||
* Debug information
|
||||
- pdb format too
|
||||
* Command line tooling
|
||||
* Compiler internals:
|
||||
- Big numbers library
|
||||
+1
-1
@@ -8729,7 +8729,7 @@ gb_internal ExprKind check_or_branch_expr(CheckerContext *c, Operand *o, Ast *no
|
||||
// okay
|
||||
} else {
|
||||
gbString s = type_to_string(right_type);
|
||||
error(node, "'%.*s' requires a boolean or nil-able type, got %s", s);
|
||||
error(node, "'%.*s' requires a boolean or nil-able type, got %s", LIT(name), s);
|
||||
gb_string_free(s);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3493,20 +3493,6 @@ gb_internal DECL_ATTRIBUTE_PROC(proc_decl_attribute) {
|
||||
error(elem, "Expected a string value for '%.*s'", LIT(name));
|
||||
}
|
||||
return true;
|
||||
} else if (name == "warning") {
|
||||
ExactValue ev = check_decl_attribute_value(c, value);
|
||||
|
||||
if (ev.kind == ExactValue_String) {
|
||||
String msg = ev.value_string;
|
||||
if (msg.len == 0) {
|
||||
error(elem, "Warning message cannot be an empty string");
|
||||
} else {
|
||||
ac->warning_message = msg;
|
||||
}
|
||||
} else {
|
||||
error(elem, "Expected a string value for '%.*s'", LIT(name));
|
||||
}
|
||||
return true;
|
||||
} else if (name == "require_results") {
|
||||
if (value != nullptr) {
|
||||
error(elem, "Expected no value for '%.*s'", LIT(name));
|
||||
|
||||
@@ -1082,6 +1082,9 @@ gb_internal bool parse_build_flags(Array<String> args) {
|
||||
build_context.build_mode = BuildMode_Assembly;
|
||||
} else if (str == "llvm" || str == "llvm-ir") {
|
||||
build_context.build_mode = BuildMode_LLVM_IR;
|
||||
} else if (str == "test") {
|
||||
build_context.build_mode = BuildMode_Executable;
|
||||
build_context.command_kind = Command_test;
|
||||
} else {
|
||||
gb_printf_err("Unknown build mode '%.*s'\n", LIT(str));
|
||||
gb_printf_err("Valid build modes:\n");
|
||||
@@ -1091,6 +1094,7 @@ gb_internal bool parse_build_flags(Array<String> args) {
|
||||
gb_printf_err("\texe\n");
|
||||
gb_printf_err("\tasm, assembly, assembler\n");
|
||||
gb_printf_err("\tllvm, llvm-ir\n");
|
||||
gb_printf_err("\ttest\n");
|
||||
bad_flags = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package test_core_math_fixed
|
||||
|
||||
import "core:math/fixed"
|
||||
import "core:testing"
|
||||
|
||||
@test
|
||||
test_fixed_4_4_unsigned :: proc(t: ^testing.T) {
|
||||
I_SHIFT :: 4
|
||||
F_MASK :: 15
|
||||
F_ULP :: 0.0625
|
||||
Fixed :: fixed.Fixed(u8, 4)
|
||||
|
||||
for c in 0..<256 {
|
||||
raw := u8(c)
|
||||
fv := transmute(Fixed)raw
|
||||
|
||||
i := raw >> I_SHIFT
|
||||
f := raw & F_MASK
|
||||
expected := f64(i) + F_ULP * f64(f)
|
||||
|
||||
testing.expectf(t, fixed.to_f64(fv) == expected, "Expected Fixed(u8, 4)(%v) to equal %.5f, got %.5f", raw, expected, fixed.to_f64(fv))
|
||||
}
|
||||
}
|
||||
|
||||
@test
|
||||
test_fixed_4_4_signed :: proc(t: ^testing.T) {
|
||||
I_SHIFT :: 4
|
||||
F_MASK :: 15
|
||||
F_ULP :: 0.0625
|
||||
Fixed :: fixed.Fixed(i8, 4)
|
||||
|
||||
for c in 0..<256 {
|
||||
raw := i8(c)
|
||||
fv := transmute(Fixed)raw
|
||||
|
||||
f := raw & F_MASK
|
||||
expected: f64
|
||||
if c < 128 {
|
||||
i := raw >> I_SHIFT
|
||||
expected = f64(i) + F_ULP * f64(f)
|
||||
} else if c == 128 {
|
||||
expected = 8.0
|
||||
|
||||
} else if c > 128 {
|
||||
i := i8(-8)
|
||||
i += (raw & 0b0111_0000) >> I_SHIFT
|
||||
expected = f64(i) + F_ULP * f64(f)
|
||||
}
|
||||
testing.expectf(t, fixed.to_f64(fv) == expected, "Expected Fixed(i8, 4)(%v, %v) to equal %.5f, got %.5f", c, raw, expected, fixed.to_f64(fv))
|
||||
}
|
||||
}
|
||||
Vendored
+3
-1
@@ -25,6 +25,8 @@ when ODIN_OS == .Windows {
|
||||
"system:ntdll.lib",
|
||||
"system:opengl32.lib",
|
||||
"system:advapi32.lib",
|
||||
"system:user32.lib",
|
||||
"system:gdi32.lib",
|
||||
}
|
||||
} else when ODIN_OS == .Darwin {
|
||||
@(private) ARCH :: "x86_64" when ODIN_ARCH == .amd64 else "aarch64" when ODIN_ARCH == .arm64 else #panic("unsupported WGPU Native architecture")
|
||||
@@ -52,7 +54,7 @@ when ODIN_OS == .Windows {
|
||||
|
||||
foreign import libwgpu {
|
||||
LIB,
|
||||
"system:ld",
|
||||
"system:dl",
|
||||
"system:m",
|
||||
}
|
||||
} else when ODIN_OS == .JS {
|
||||
|
||||
Reference in New Issue
Block a user