ghc-8.2.2: The GHC API

Safe Haskell | None
Language | Haskell2010

StringBuffer

Contents

- Creation/destruction
- Inspection
- Moving and comparison
- Conversion
- Parsing integers

Synopsis

- data StringBuffer = StringBuffer {
  - buf :: !(ForeignPtr Word8)
  - len :: !Int
  - cur :: !Int
  }
- hGetStringBuffer :: FilePath -> IO StringBuffer
- hGetStringBufferBlock :: Handle -> Int -> IO StringBuffer
- appendStringBuffers :: StringBuffer -> StringBuffer -> IO StringBuffer
- stringToStringBuffer :: String -> StringBuffer
- nextChar :: StringBuffer -> (Char, StringBuffer)
- currentChar :: StringBuffer -> Char
- prevChar :: StringBuffer -> Char -> Char
- atEnd :: StringBuffer -> Bool
- stepOn :: StringBuffer -> StringBuffer
- offsetBytes :: Int -> StringBuffer -> StringBuffer
- byteDiff :: StringBuffer -> StringBuffer -> Int
- atLine :: Int -> StringBuffer -> Maybe StringBuffer
- lexemeToString :: StringBuffer -> Int -> String
- lexemeToFastString :: StringBuffer -> Int -> FastString
- parseUnsignedInteger :: StringBuffer -> Int -> Integer -> (Char -> Int) -> Integer

# Documentation

data StringBuffer #

A StringBuffer is an internal pointer to a sized chunk of bytes. The bytes are intended to be *immutable*. There are pure operations to read the contents of a StringBuffer.

A StringBuffer may have a finalizer, depending on how it was obtained.

Constructors

StringBuffer |

Fields

- buf :: !(ForeignPtr Word8)
- len :: !Int
- cur :: !Int

Instances

Show StringBuffer # |
Methods showsPrec :: Int -> StringBuffer -> ShowS Source # show :: StringBuffer -> String Source # showList :: [StringBuffer] -> ShowS Source #

# Creation/destruction

hGetStringBuffer :: FilePath -> IO StringBuffer #

Read a file into a `StringBuffer`. The resulting buffer is automatically managed by the garbage collector.

hGetStringBufferBlock :: Handle -> Int -> IO StringBuffer #

appendStringBuffers :: StringBuffer -> StringBuffer -> IO StringBuffer #

stringToStringBuffer :: String -> StringBuffer #

Encode a `String` into a `StringBuffer` as UTF-8. The resulting buffer is automatically managed by the garbage collector.

# Inspection

nextChar :: StringBuffer -> (Char, StringBuffer) #

Return the first UTF-8 character of a nonempty `StringBuffer` and as well the remaining portion (analogous to `uncons`). Warning: The behavior is undefined if the `StringBuffer` is empty. The result shares the same buffer as the original. Similar to `utf8DecodeChar`, if the character cannot be decoded as UTF-8, '\0' is returned.

currentChar :: StringBuffer -> Char #

Return the first UTF-8 character of a nonempty `StringBuffer` (analogous to `head`). Warning: The behavior is undefined if the `StringBuffer` is empty. Similar to `utf8DecodeChar`, if the character cannot be decoded as UTF-8, '\0' is returned.

prevChar :: StringBuffer -> Char -> Char #

atEnd :: StringBuffer -> Bool #

Check whether a `StringBuffer` is empty (analogous to `null`).

# Moving and comparison

stepOn :: StringBuffer -> StringBuffer #

Return a `StringBuffer` with the first UTF-8 character removed (analogous to `tail`). Warning: The behavior is undefined if the `StringBuffer` is empty. The result shares the same buffer as the original.

offsetBytes #

Arguments

:: Int | `n`, the number of bytes
-> StringBuffer |
-> StringBuffer |

Return a `StringBuffer` with the first `n` bytes removed. Warning: If there aren't enough characters, the returned `StringBuffer` will be invalid and any use of it may lead to undefined behavior. The result shares the same buffer as the original.

byteDiff :: StringBuffer -> StringBuffer -> Int #

Compute the difference in offset between two `StringBuffer`s that share the same buffer. Warning: The behavior is undefined if the `StringBuffer`s use separate buffers.

atLine :: Int -> StringBuffer -> Maybe StringBuffer #

Computes a `StringBuffer` which points to the first character of the wanted line. Lines begin at 1.

# Conversion

lexemeToString #

Arguments

:: StringBuffer |
-> Int | `n`, the number of bytes
-> String |

Decode the first `n` bytes of a `StringBuffer` as UTF-8 into a `String`. Similar to `utf8DecodeChar`, if the character cannot be decoded as UTF-8, they will be replaced with '\0'.

lexemeToFastString #

Arguments

:: StringBuffer |
-> Int | `n`, the number of bytes
-> FastString |

# Parsing integers

parseUnsignedInteger :: StringBuffer -> Int -> Integer -> (Char -> Int) -> Integer #