Skip to content

Commit

Permalink
refactor utils.do_space_split_lines
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Jun 24, 2024
1 parent 2ae5fee commit 1ab4d69
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions src/glint/internal/utils.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -78,38 +78,21 @@ fn do_space_split_lines(
// base case
[], _, _ -> acc
// the start of a chain of newlines
// set the flag to true
["\n", "\n", ..rest], _, False -> do_space_split_lines(rest, #(acc.0, True))
// a continued chain of new lines, but no values have been accumulated yet
// this shouldnt be possible but we handle it anyway
["\n", "\n", ..rest], [], True -> do_space_split_lines(rest, #([], True))
// a continued chain of new lines
// add a newline to the most recent accumulated value
["\n", "\n", ..rest], [s, ..accs], True ->
do_space_split_lines(["\n", ..rest], #([s <> "\n", ..accs], True))
// a single newline in a chain of newlines, but no values have been accumulated yet
// this shouldnt be possible but we handle it anyway
["\n", ..rest], [], True -> do_space_split_lines(rest, #([], True))
// a single newline in a chain
// add a newline to the most recent accumulated value
// a single newline in a chain, add a newline to the accumulator
["\n", ..rest], [s, ..accs], True ->
do_space_split_lines(rest, #([s <> "\n", ..accs], True))
// a single newline on its own but with no accumulated values yet
// skip it
["\n", ..rest], [], False -> do_space_split_lines(rest, #([], False))
// a single newline on its own
// add a space to the most recent accumulated value
// a single newline on its own, add a space to the most recent accumulated value
["\n", ..rest], [s, ..accs], False ->
do_space_split_lines(rest, #([s <> " ", ..accs], False))
// a non-newline character after a chain of newlines
// start a new accumulated value
[c, ..rest], _, True -> do_space_split_lines(rest, #([c, ..acc.0], False))
// a non-newline character not after a chain of newlines, with no accumulated values
// add it as a new accumulated value
[c, ..rest], [], False -> do_space_split_lines(rest, #([c], False))
// a single new line part of a chain with no accumulated values yet
// skip it
["\n", ..rest], [], chain -> do_space_split_lines(rest, #([], chain))
// a non-newline character not after a chain of newlines
// add it to the end of the most recent accumulated value
[c, ..rest], [s, ..accs], False ->
do_space_split_lines(rest, #([s <> c, ..accs], False))
// a non-newline character, create a new accumulated value
[c, ..rest], _, _ -> do_space_split_lines(rest, #([c, ..acc.0], False))
}
}

0 comments on commit 1ab4d69

Please sign in to comment.