text-rope-0.2: Text lines and ropes
Copyright(c) 2021-2022 Andrew Lelechenko
LicenseBSD3
MaintainerAndrew Lelechenko <andrew.lelechenko@gmail.com>
Safe HaskellNone
LanguageHaskell2010

Data.Text.Lines

Description

 
Synopsis

Documentation

data TextLines Source #

A wrapper around Text for fast line/column navigation. Concatenation takes linear time.

This is a building block for Rope, which provides logarithmic concatenation.

fromText :: Text -> TextLines Source #

Create from Text, linear time.

toText :: TextLines -> Text Source #

Extract Text, O(1).

null :: TextLines -> Bool Source #

Check whether a text is empty, O(1).

Lines

lines :: TextLines -> [Text] Source #

Split into lines by \n, similar to Data.Text.lines. Each line is produced in O(1).

>>> :set -XOverloadedStrings
>>> lines ""
[]
>>> lines "foo"
["foo"]
>>> lines "foo\n"
["foo"]
>>> lines "foo\n\n"
["foo",""]
>>> lines "foo\nbar"
["foo","bar"]

lengthInLines :: TextLines -> Word Source #

Equivalent to length . lines, but in O(1).

>>> :set -XOverloadedStrings
>>> lengthInLines ""
0
>>> lengthInLines "foo"
1
>>> lengthInLines "foo\n"
1
>>> lengthInLines "foo\n\n"
2
>>> lengthInLines "foo\nbar"
2

splitAtLine :: Word -> TextLines -> (TextLines, TextLines) Source #

Split at given line, O(1).

>>> :set -XOverloadedStrings
>>> map (\l -> splitAtLine l "foo\nbar") [0..3]
[("","foo\nbar"),("foo\n","bar"),("foo\nbar",""),("foo\nbar","")]

Code points

length :: TextLines -> Word Source #

Length in code points, similar to Data.Text.length. Takes linear time.

>>> :set -XOverloadedStrings
>>> length "fя𐀀"
3
>>> Data.Text.Utf16.Lines.length "fя𐀀"
4

span :: (Char -> Bool) -> TextLines -> (TextLines, TextLines) Source #

Span by a predicate, similar to Data.Text.span. Takes linear (by length of the prefix satisfying the predicate) time.

splitAt :: Word -> TextLines -> (TextLines, TextLines) Source #

Split at given code point, similar to Data.Text.splitAt. Takes linear time.

>>> :set -XOverloadedStrings
>>> map (\c -> splitAt c "fя𐀀") [0..4]
[("","fя𐀀"),("f","я𐀀"),("fя","𐀀"),("fя𐀀",""),("fя𐀀","")]

data Position Source #

Represent a position in a text.

Constructors

Position 

Fields

Instances

Instances details
Monoid Position Source # 
Instance details

Defined in Data.Text.Lines.Internal

Semigroup Position Source #

Associativity does not hold when posLine overflows.

Instance details

Defined in Data.Text.Lines.Internal

Show Position Source # 
Instance details

Defined in Data.Text.Lines.Internal

NFData Position Source # 
Instance details

Defined in Data.Text.Lines.Internal

Methods

rnf :: Position -> () #

Eq Position Source # 
Instance details

Defined in Data.Text.Lines.Internal

Ord Position Source # 
Instance details

Defined in Data.Text.Lines.Internal

lengthAsPosition :: TextLines -> Position Source #

Measure text length as an amount of lines and columns. Time is proportional to the length of the last line.

>>> :set -XOverloadedStrings
>>> lengthAsPosition "f𐀀"
Position {posLine = 0, posColumn = 2}
>>> lengthAsPosition "f\n𐀀"
Position {posLine = 1, posColumn = 1}
>>> lengthAsPosition "f\n𐀀\n"
Position {posLine = 2, posColumn = 0}

splitAtPosition :: Position -> TextLines -> (TextLines, TextLines) Source #

Combination of splitAtLine and subsequent splitAt. Time is linear in posColumn, but does not depend on posLine.

>>> :set -XOverloadedStrings
>>> splitAtPosition (Position 1 0) "f\n𐀀я"
("f\n","𐀀я")
>>> splitAtPosition (Position 1 1) "f\n𐀀я"
("f\n𐀀","я")
>>> splitAtPosition (Position 1 2) "f\n𐀀я"
("f\n𐀀я","")
>>> splitAtPosition (Position 0 2) "f\n𐀀я"
("f\n","𐀀я")
>>> splitAtPosition (Position 0 3) "f\n𐀀я"
("f\n𐀀","я")
>>> splitAtPosition (Position 0 4) "f\n𐀀я"
("f\n𐀀я","")