Optimize printing of markdown tables

Check only once if the table has a header row, instead of every row.
This commit is contained in:
Feoramund
2024-06-20 15:01:09 -04:00
parent dd099d9dd6
commit fd28199178
+14 -3
View File
@@ -324,8 +324,7 @@ write_markdown_table :: proc(w: io.Writer, tbl: ^Table) {
build(tbl, WIDTH_PROC) build(tbl, WIDTH_PROC)
} }
for row in 0..<tbl.nr_rows { write_row :: proc(w: io.Writer, tbl: ^Table, row: int, alignment: Cell_Alignment = .Left) {
alignment: Cell_Alignment = .Center if tbl.has_header_row && row == header_row(tbl) else .Left
for col in 0 ..< tbl.nr_cols { for col in 0 ..< tbl.nr_cols {
cell := get_cell(tbl, row, col) cell := get_cell(tbl, row, col)
if col == 0 { if col == 0 {
@@ -335,8 +334,15 @@ write_markdown_table :: proc(w: io.Writer, tbl: ^Table) {
io.write_string(w, "|") io.write_string(w, "|")
} }
io.write_byte(w, '\n') io.write_byte(w, '\n')
}
start := 0
if tbl.has_header_row {
row := header_row(tbl)
write_row(w, tbl, row, .Center)
if tbl.has_header_row && row == header_row(tbl) {
for col in 0 ..< tbl.nr_cols { for col in 0 ..< tbl.nr_cols {
cell := get_cell(tbl, row, col) cell := get_cell(tbl, row, col)
if col == 0 { if col == 0 {
@@ -358,7 +364,12 @@ write_markdown_table :: proc(w: io.Writer, tbl: ^Table) {
io.write_byte(w, '|') io.write_byte(w, '|')
} }
io.write_byte(w, '\n') io.write_byte(w, '\n')
start += row + 1
} }
for row in start ..< tbl.nr_rows {
write_row(w, tbl, row)
} }
} }