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

Data.Text.Rope

Description

 
Synopsis

Documentation

data Rope #

Rope of Text chunks with logarithmic concatenation. This rope offers an interface, based on code points. Use Data.Text.Utf16.Rope, if you need UTF-16 code units, or Data.Text.Utf16.Rope.Mixed, if you need both interfaces.

Instances

Instances details
IsString Rope # 
Instance details

Defined in Data.Text.Rope

Methods

fromString :: String -> Rope

Monoid Rope # 
Instance details

Defined in Data.Text.Rope

Methods

mempty :: Rope

mappend :: Rope -> Rope -> Rope

mconcat :: [Rope] -> Rope

Semigroup Rope # 
Instance details

Defined in Data.Text.Rope

Methods

(<>) :: Rope -> Rope -> Rope

sconcat :: NonEmpty Rope -> Rope

stimes :: Integral b => b -> Rope -> Rope

Show Rope # 
Instance details

Defined in Data.Text.Rope

Methods

showsPrec :: Int -> Rope -> ShowS

show :: Rope -> String

showList :: [Rope] -> ShowS

NFData Rope # 
Instance details

Defined in Data.Text.Rope

Methods

rnf :: Rope -> ()

Eq Rope # 
Instance details

Defined in Data.Text.Rope

Methods

(==) :: Rope -> Rope -> Bool

(/=) :: Rope -> Rope -> Bool

Ord Rope # 
Instance details

Defined in Data.Text.Rope

Methods

compare :: Rope -> Rope -> Ordering

(<) :: Rope -> Rope -> Bool

(<=) :: Rope -> Rope -> Bool

(>) :: Rope -> Rope -> Bool

(>=) :: Rope -> Rope -> Bool

max :: Rope -> Rope -> Rope

min :: Rope -> Rope -> Rope

fromText :: Text -> Rope #

Create from Text, linear time.

fromTextLines :: TextLines -> Rope #

Create from TextLines, linear time.

toText :: Rope -> Text #

Glue chunks into Text, linear time.

toTextLines :: Rope -> TextLines #

Glue chunks into TextLines, linear time.

null :: Rope -> Bool #

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

Lines

lines :: Rope -> [Text] #

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 :: Rope -> Word #

Equivalent to length . lines, but in logarithmic time.

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

If you do not care about ignoring the last newline character, you can use posLine . lengthAsPosition instead, which works in O(1).

splitAtLine :: Word -> Rope -> (Rope, Rope) #

Split at given line, logarithmic time.

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

Code points

length :: Rope -> Word #

Length in code points, similar to Data.Text.length, O(1).

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

splitAt :: Word -> Rope -> (Rope, Rope) #

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 #

Represent a position in a text.

Constructors

Position 

Fields

Instances

Instances details
Monoid Position # 
Instance details

Defined in Data.Text.Lines.Internal

Semigroup Position #

Associativity does not hold when posLine overflows.

Instance details

Defined in Data.Text.Lines.Internal

Methods

(<>) :: Position -> Position -> Position

sconcat :: NonEmpty Position -> Position

stimes :: Integral b => b -> Position -> Position

Show Position # 
Instance details

Defined in Data.Text.Lines.Internal

Methods

showsPrec :: Int -> Position -> ShowS

show :: Position -> String

showList :: [Position] -> ShowS

NFData Position # 
Instance details

Defined in Data.Text.Lines.Internal

Methods

rnf :: Position -> ()

Eq Position # 
Instance details

Defined in Data.Text.Lines.Internal

Methods

(==) :: Position -> Position -> Bool

(/=) :: Position -> Position -> Bool

Ord Position # 
Instance details

Defined in Data.Text.Lines.Internal

Methods

compare :: Position -> Position -> Ordering

(<) :: Position -> Position -> Bool

(<=) :: Position -> Position -> Bool

(>) :: Position -> Position -> Bool

(>=) :: Position -> Position -> Bool

max :: Position -> Position -> Position

min :: Position -> Position -> Position

lengthAsPosition :: Rope -> Position #

Measure text length as an amount of lines and columns, O(1).

>>> :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 -> Rope -> (Rope, Rope) #

Combination of splitAtLine and subsequent splitAt. Time is linear in posColumn and logarithmic in 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𐀀я","")