Skip to content

Commit 2438992

Browse files
gamachemichalmuskala
authored andcommitted
build formatter output in FIFO order
1 parent 7740ade commit 2438992

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

lib/formatter.ex

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ defmodule Jason.Formatter do
162162
defp pp_iodata(input, output_acc, depth, in_str, in_bs, empty, first, opts)
163163

164164
defp pp_iodata("", output_acc, depth, in_str, in_bs, empty, first, opts) do
165-
{:lists.reverse(output_acc), {depth, in_str, in_bs, empty, first, opts}}
165+
{output_acc, {depth, in_str, in_bs, empty, first, opts}}
166166
end
167167

168168
defp pp_iodata([], output_acc, depth, in_str, in_bs, empty, first, opts) do
169-
{:lists.reverse(output_acc), {depth, in_str, in_bs, empty, first, opts}}
169+
{output_acc, {depth, in_str, in_bs, empty, first, opts}}
170170
end
171171

172172
defp pp_iodata(<<byte::size(8), rest::binary>>, output_acc, depth, in_str, in_bs, empty, first, opts) do
@@ -182,9 +182,9 @@ defmodule Jason.Formatter do
182182
{reversed_output, end_state} = Enum.reduce list, {[], starting_state}, fn (item, {output_acc, state}) ->
183183
{depth, in_str, in_bs, empty, first, opts} = state
184184
{item_output, new_state} = pp_iodata(item, [], depth, in_str, in_bs, empty, first, opts)
185-
{[item_output | output_acc], new_state}
185+
{[output_acc, item_output], new_state}
186186
end
187-
{[:lists.reverse(reversed_output) | output_acc], end_state}
187+
{[output_acc, reversed_output], end_state}
188188
end
189189

190190

@@ -204,26 +204,26 @@ defmodule Jason.Formatter do
204204
## in string, following backslash
205205
defp pp_byte(byte, rest, output, depth, true=in_str, true=_in_bs, empty, first, opts) do
206206
in_bs = false
207-
pp_iodata(rest, [byte | output], depth, in_str, in_bs, empty, first, opts)
207+
pp_iodata(rest, [output, byte], depth, in_str, in_bs, empty, first, opts)
208208
end
209209

210210
## in string, backslash
211211
defp pp_byte(byte, rest, output, depth, true=in_str, _in_bs, empty, first, opts)
212212
when byte in '\\' do
213213
in_bs = true
214-
pp_iodata(rest, [byte | output], depth, in_str, in_bs, empty, first, opts)
214+
pp_iodata(rest, [output, byte], depth, in_str, in_bs, empty, first, opts)
215215
end
216216

217217
## in string, end quote
218218
defp pp_byte(byte, rest, output, depth, true=_in_str, in_bs, empty, first, opts)
219219
when byte in '"' do
220220
in_str = false
221-
pp_iodata(rest, [byte | output], depth, in_str, in_bs, empty, first, opts)
221+
pp_iodata(rest, [output, byte], depth, in_str, in_bs, empty, first, opts)
222222
end
223223

224224
## in string, other character
225225
defp pp_byte(byte, rest, output, depth, true=in_str, in_bs, empty, first, opts) do
226-
pp_iodata(rest, [byte | output], depth, in_str, in_bs, empty, first, opts)
226+
pp_iodata(rest, [output, byte], depth, in_str, in_bs, empty, first, opts)
227227
end
228228

229229
## out of string, whitespace
@@ -244,46 +244,46 @@ defmodule Jason.Formatter do
244244
first = false
245245
empty = true
246246
depth = depth + 1
247-
pp_iodata(rest, [out | output], depth, in_str, in_bs, empty, first, opts)
247+
pp_iodata(rest, [output, out], depth, in_str, in_bs, empty, first, opts)
248248
end
249249

250250
## out of string, end empty block
251251
defp pp_byte(byte, rest, output, depth, in_str, in_bs, true=_empty, first, opts)
252252
when byte in '}]' do
253253
empty = false
254254
depth = depth - 1
255-
pp_iodata(rest, [byte | output], depth, in_str, in_bs, empty, first, opts)
255+
pp_iodata(rest, [output, byte], depth, in_str, in_bs, empty, first, opts)
256256
end
257257

258258
## out of string, end non-empty block
259259
defp pp_byte(byte, rest, output, depth, in_str, in_bs, false=empty, first, opts)
260260
when byte in '}]' do
261261
depth = depth - 1
262262
out = [opts[:line_separator], tab(opts, depth), byte]
263-
pp_iodata(rest, [out | output], depth, in_str, in_bs, empty, first, opts)
263+
pp_iodata(rest, [output, out], depth, in_str, in_bs, empty, first, opts)
264264
end
265265

266266
## out of string, comma
267267
defp pp_byte(byte, rest, output, depth, in_str, in_bs, _empty, first, opts)
268268
when byte in ',' do
269269
empty = false
270270
out = [byte, opts[:line_separator], tab(opts, depth)]
271-
pp_iodata(rest, [out | output], depth, in_str, in_bs, empty, first, opts)
271+
pp_iodata(rest, [output, out], depth, in_str, in_bs, empty, first, opts)
272272
end
273273

274274
## out of string, colon
275275
defp pp_byte(byte, rest, output, depth, in_str, in_bs, empty, first, opts)
276276
when byte in ':' do
277277
out = [byte, opts[:after_colon]]
278-
pp_iodata(rest, [out | output], depth, in_str, in_bs, empty, first, opts)
278+
pp_iodata(rest, [output, out], depth, in_str, in_bs, empty, first, opts)
279279
end
280280

281281
## out of string, other character (maybe start quote)
282282
defp pp_byte(byte, rest, output, depth, _in_str, in_bs, empty, first, opts) do
283283
out = if empty, do: [opts[:line_separator], tab(opts, depth), byte], else: byte
284284
in_str = byte in '"'
285285
empty = false
286-
pp_iodata(rest, [out | output], depth, in_str, in_bs, empty, first, opts)
286+
pp_iodata(rest, [output, out], depth, in_str, in_bs, empty, first, opts)
287287
end
288288
end
289289

0 commit comments

Comments
 (0)