headroom-0.4.2.0: License Header Manager
Copyright(c) 2019-2021 Vaclav Svejcar
LicenseBSD-3-Clause
Maintainervaclav.svejcar@gmail.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Headroom.Data.Text

Description

Module containing bunch of useful functions for working with text.

Synopsis

Documentation

read Source #

Arguments

:: Read a 
=> Text

input text to parse

-> Maybe a

parsed value

Same as readMaybe, but takes Text as input instead of String.

>>> read "123" :: Maybe Int
Just 123

commonLinesPrefix Source #

Arguments

:: Text

lines of text to find prefix for

-> Maybe Text

found longest common prefixs

Similar to commonPrefixes, but tries to find common prefix for all lines in given text.

>>> commonLinesPrefix "-- first\n-- second\n-- third"
Just "-- "

replaceFirst :: Text -> Text -> Text -> Text Source #

Similar to replace, but replaces only very first occurence of pattern.

>>> replaceFirst ":" "/" "a : b : c"
"a / b : c"

Working with text lines

mapLines Source #

Arguments

:: (Text -> Text)

function to map over individual lines

-> Text

input text

-> Text

resulting text

Maps given function over individual lines of the given text.

>>> mapLines ("T: " <>) "foo zz\nbar"
"T: foo zz\nT: bar"

mapLinesF Source #

Arguments

:: Foldable t 
=> (Text -> t Text)

function to map over inividual lines

-> Text

input text

-> Text

resulting text

Similar to mapLines, but the mapping function returns Foldable, which gives some more control over outcome. After mapping over all individual lines, results are folded and concatenated, which allows for example filtering out some lines.

>>> mapLinesF (\l -> if l == "bar" then Nothing else Just l) "foo\nbar"
"foo"

fromLines Source #

Arguments

:: [Text]

lines to join

-> Text

text joined from individual lines

Similar to unlines, but does not automatically adds n at the end of the text. Advantage is that when used together with toLines, it doesn't ocassionaly change the newlines ad the end of input text:

>>> fromLines . toLines $ "foo\nbar"
"foo\nbar"
>>> fromLines . toLines $ "foo\nbar\n"
"foo\nbar\n"

Other examples:

>>> fromLines []
""
>>> fromLines ["foo"]
"foo"
>>> fromLines ["first", "second"]
"first\nsecond"
>>> fromLines ["first", "second", ""]
"first\nsecond\n"

toLines Source #

Arguments

:: Text

text to break into lines

-> [Text]

lines of input text

Similar to lines, but does not drop trailing newlines from output. Advantage is that when used together with fromLines, it doesn't ocassionaly change the newlines ad the end of input text:

>>> fromLines . toLines $ "foo\nbar"
"foo\nbar"
>>> fromLines . toLines $ "foo\nbar\n"
"foo\nbar\n"

Other examples:

>>> toLines ""
[]
>>> toLines "first\nsecond"
["first","second"]
>>> toLines "first\nsecond\n"
["first","second",""]