fixed up Text.stripMargin · JavaDerek/unison@56ce6a4 · GitHub
Skip to content

Commit 56ce6a4

Browse files
committed
fixed up Text.stripMargin
1 parent 757d4ef commit 56ce6a4

6 files changed

Lines changed: 158 additions & 170 deletions

File tree

‎codebase2/util/U/Util/String.hs‎

Lines changed: 3 additions & 8 deletions

‎codebase2/util/U/Util/Text.hs‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
{-# LANGUAGE BlockArguments #-}
2+
{-# LANGUAGE LambdaCase #-}
13
{-# LANGUAGE OverloadedStrings #-}
24
{-# LANGUAGE QuasiQuotes #-}
35

46
module U.Util.Text where
57

8+
import qualified Data.Char as Char
69
import Data.Maybe (fromMaybe)
710
import Data.Text (Text)
811
import qualified Data.Text as Text
912
import Safe.Foldable (minimumMay)
10-
import Debug.Trace (trace, traceShowId)
1113

1214
-- | remove however many spaces prefix all of the lines of the input
1315
-- e.g.
@@ -22,25 +24,15 @@ stripMargin :: Text -> Text
2224
stripMargin str =
2325
let stripLen =
2426
Data.Maybe.fromMaybe 0 . minimumMay
25-
. map (Text.length . fst)
26-
. filter (not . Text.null . snd)
27-
. map (Text.span (== ' '))
28-
. filter (not . Text.null)
27+
. map (Text.length . fst . Text.span (== ' '))
28+
. filter (not . Text.all (Char.isSpace))
2929
$ Text.lines str
30-
in Text.unlines . traceShowId. map (Text.drop $ traceShowId stripLen) $ traceShowId $ Text.lines str
31-
32-
test :: Bool
33-
test =
34-
stripMargin x
35-
== y
36-
37-
x' :: Text
38-
x' = stripMargin x
39-
x :: Text
40-
x = " def foo:" <> "\n" <>
41-
" blah blah" <> "\n" <>
42-
" "
43-
44-
y :: Text
45-
y = "def foo:" <> "\n" <>
46-
" blah blah" <> "\n"
30+
dropFirstIf f = \case
31+
h : t | f h -> t
32+
x -> x
33+
dropLastIf f = reverse . dropFirstIf f . reverse
34+
in Text.unlines
35+
. dropLastIf Text.null
36+
. dropFirstIf Text.null
37+
. map (Text.drop stripLen)
38+
$ Text.lines str

‎parser-typechecker/tests/Suite.hs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test rt = tests
6969
, Typechecker.test
7070
, UriParser.test
7171
, Context.test
72-
-- , Git.test
72+
, Git.test
7373
, Upgrade12.test
7474
, GitSimple.test
7575
, TestIO.test

‎parser-typechecker/tests/Unison/Test/Codebase/Upgrade12.hs‎

Lines changed: 89 additions & 89 deletions

0 commit comments

Comments
 (0)