Skip to content

Commit af23fad

Browse files
author
Jason Duncan
committed
Now building a list of strings and joining at the very end rather than concatenating larger and larger strings. More efficient and faster.
1 parent 66f854f commit af23fad

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

format-table.el

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Arguments are the list of values ROW, the list of MAX-COL-WIDTHS, and delimiter
172172
information in OUTPUT-MODE. Optionally use PAD-FN to pad each column value,
173173
otherwise values will be padded to the right with spaces."
174174
(let ((pad-fn (or pad-fn #'format-table-pad-right)))
175-
(concat
175+
(list
176176
(plist-get output-mode :begin-row)
177177
(string-join
178178
(-zip-with pad-fn row max-col-widths)
@@ -186,12 +186,13 @@ otherwise values will be padded to the right with spaces."
186186

187187
(defun format-table-render-separator-row (max-col-widths output-mode)
188188
"Given the list of MAX-COL-WIDTHS and delimiter information in OUTPUT-MODE, render a row which separates the header row from the rest of the rows."
189-
(concat (plist-get output-mode :separator-begin-row)
190-
(string-join
191-
(-map #'format-table-generate-dash-string max-col-widths)
192-
(plist-get output-mode :separator-col-separator))
193-
(plist-get output-mode :separator-end-row)
194-
hard-newline))
189+
(list
190+
(plist-get output-mode :separator-begin-row)
191+
(string-join
192+
(-map #'format-table-generate-dash-string max-col-widths)
193+
(plist-get output-mode :separator-col-separator))
194+
(plist-get output-mode :separator-end-row)
195+
hard-newline))
195196

196197
(defun format-table-render-json (table)
197198
"Render the TABLE of values as a json string."
@@ -204,17 +205,16 @@ otherwise values will be padded to the right with spaces."
204205
(defun format-table-render-table (table output-mode)
205206
"Given the TABLE of values and delimiter information in OUTPUT-MODE, re-render the table as a string."
206207
(if (equal output-mode 'json)
207-
(format-table-render-json table)
208+
(list (format-table-render-json table))
208209
(let ((top-border-fn (plist-get output-mode :top-border-fn))
209210
(bottom-border-fn (plist-get output-mode :bottom-border-fn))
210211
(header-pad-fn (plist-get output-mode :header-pad-fn))
211212
(max-col-widths (plist-get table :max-col-widths)))
212-
(concat
213+
(append
213214
(if top-border-fn (funcall top-border-fn max-col-widths output-mode))
214215
(format-table-render-row (plist-get table :header) max-col-widths output-mode header-pad-fn)
215216
(format-table-render-separator-row max-col-widths output-mode)
216-
(string-join
217-
(--map (format-table-render-row it max-col-widths output-mode) (plist-get table :body)))
217+
(apply #'append (--map (format-table-render-row it max-col-widths output-mode) (plist-get table :body)))
218218
(if bottom-border-fn (funcall bottom-border-fn max-col-widths output-mode))))))
219219

220220
(defun format-table-render-row-count (count output-mode)
@@ -266,9 +266,11 @@ otherwise values will be padded to the right with spaces."
266266
(table (format-table-cleanup-and-parse str input-mode)))
267267
(if (not table)
268268
str
269-
(concat
270-
(format-table-render-table table output-mode)
271-
(format-table-render-row-count (plist-get table :row-count) output-mode)))))
269+
(string-join
270+
(append
271+
(format-table-render-table table output-mode)
272+
(list
273+
(format-table-render-row-count (plist-get table :row-count) output-mode)))))))
272274

273275
(provide 'format-table)
274276
;;; format-table.el ends here

test/format-table-test.el

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
;; -*- lexical-binding: t -*-
22

33
(require 'ert)
4+
(require 'format-table)
45
(require 'format-table-test-helper)
56

67
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -105,39 +106,52 @@
105106
(ert-deftest format-table-render-row-should-render-row-for-ms ()
106107
(let ((expected "foo bar
107108
")
108-
(actual (format-table-render-row '("foo" "bar") '(3 3) (alist-get 'ms format-table-format-alist))))
109+
(actual
110+
(string-join
111+
(format-table-render-row
112+
'("foo" "bar") '(3 3) (alist-get 'ms format-table-format-alist)))))
109113
(should (string-equal expected actual))))
110114

111115
(ert-deftest format-table-render-row-should-render-row-for-org ()
112116
(let ((expected "| foo | bar |
113117
")
114-
(actual (format-table-render-row '("foo" "bar") '(3 3) (alist-get 'org format-table-format-alist))))
118+
(actual
119+
(string-join
120+
(format-table-render-row '("foo" "bar") '(3 3) (alist-get 'org format-table-format-alist)))))
115121
(should (string-equal expected actual))))
116122

117123
(ert-deftest format-table-render-row-should-render-row-for-mysql ()
118124
(let ((expected "| foo | bar |
119125
")
120-
(actual (format-table-render-row '("foo" "bar") '(3 3) (alist-get 'mysql format-table-format-alist))))
126+
(actual
127+
(string-join
128+
(format-table-render-row '("foo" "bar") '(3 3) (alist-get 'mysql format-table-format-alist)))))
121129
(should (string-equal expected actual))))
122130

123131
;;;;;;;;format-table-render-separator-row;;;;;;;;;
124132

125133
(ert-deftest format-table-render-separator-row-should-render-separator-row-for-ms ()
126134
(let ((expected "--- ---
127135
")
128-
(actual (format-table-render-separator-row '(3 3) (alist-get 'ms format-table-format-alist))))
136+
(actual
137+
(string-join
138+
(format-table-render-separator-row '(3 3) (alist-get 'ms format-table-format-alist)))))
129139
(should (string-equal expected actual))))
130140

131141
(ert-deftest format-table-render-separator-row-should-render-separator-row-for-org ()
132142
(let ((expected "|-----+-----|
133143
")
134-
(actual (format-table-render-separator-row '(3 3) (alist-get 'org format-table-format-alist))))
144+
(actual
145+
(string-join
146+
(format-table-render-separator-row '(3 3) (alist-get 'org format-table-format-alist)))))
135147
(should (string-equal expected actual))))
136148

137149
(ert-deftest format-table-render-separator-row-should-render-separator-row-for-mysql ()
138150
(let ((expected "+-----+-----+
139151
")
140-
(actual (format-table-render-separator-row '(3 3) (alist-get 'mysql format-table-format-alist))))
152+
(actual
153+
(string-join
154+
(format-table-render-separator-row '(3 3) (alist-get 'mysql format-table-format-alist)))))
141155
(should (string-equal expected actual))))
142156

143157
;;;;;;;;;;;;;;format-table-split-row;;;;;;;;;;;;;;

0 commit comments

Comments
 (0)