Skip to content

Commit

Permalink
Print.toStringWithIndentAndLinebreakIndentAsString optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
lue-bird committed Nov 12, 2024
1 parent 0c96667 commit 5a1a19f
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions src/Print.elm
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ toStringWithIndent indent print =


toStringWithIndentAndLinebreakIndentAsString : Int -> String -> Print -> String
toStringWithIndentAndLinebreakIndentAsString indent linebreakIndentAsString print =
toStringWithIndentAndLinebreakIndentAsString indentIgnoringMultiplesOfBy4 linebreakIndentAsString print =
-- IGNORE TCO
case print of
Exact string () ->
string

FollowedBy b a ->
toStringWithIndentAndLinebreakIndentAsString indent linebreakIndentAsString a
++ toStringWithIndentAndLinebreakIndentAsString indent linebreakIndentAsString b
toStringWithIndentAndLinebreakIndentAsString indentIgnoringMultiplesOfBy4 linebreakIndentAsString a
++ toStringWithIndentAndLinebreakIndentAsString indentIgnoringMultiplesOfBy4 linebreakIndentAsString b
++ ""

Linebreak () () ->
Expand All @@ -82,29 +82,27 @@ toStringWithIndentAndLinebreakIndentAsString indent linebreakIndentAsString prin

WithIndentIncreasedBy increase innerPrint ->
toStringWithIndentAndLinebreakIndentAsString
(indent + increase)
(linebreakIndentAsString ++ stringIndentFastAtMost4 increase)
(indentIgnoringMultiplesOfBy4 + increase + 0)
(linebreakIndentAsString
++ indentAtMost4 increase
++ ""
)
innerPrint

WithIndentAtNextMultipleOf4 innerPrint () ->
let
increase : Int
increase =
newIndent - indent

newIndent : Int
newIndent =
indent // 4 * 4 + 4
in
toStringWithIndentAndLinebreakIndentAsString
newIndent
(linebreakIndentAsString ++ stringIndentFastAtMost4 increase)
0
(linebreakIndentAsString
++ indentInverseRemainderBy4
(indentIgnoringMultiplesOfBy4 - indentIgnoringMultiplesOfBy4 // 4 * 4)
++ ""
)
innerPrint


stringIndentFastAtMost4 : Int -> String
stringIndentFastAtMost4 n =
case n of
indentAtMost4 : Int -> String
indentAtMost4 atMost4 =
case atMost4 of
1 ->
" "

Expand All @@ -119,6 +117,23 @@ stringIndentFastAtMost4 n =
" "


indentInverseRemainderBy4 : Int -> String
indentInverseRemainderBy4 inverseRemainderBy4 =
case inverseRemainderBy4 of
0 ->
" "

1 ->
" "

2 ->
" "

-- 3
_ ->
" "


{-| [How many lines](#LineSpread) the given [`Print`](#Print)
take up if turned into a string?
-}
Expand Down

0 comments on commit 5a1a19f

Please sign in to comment.