Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
There are two principal string types used internally by GHC:
- A compact, hash-consed, representation of character strings.
- Generated by
fsLit
. - You can get a
Unique
from them. - Equality test is O(1) (it uses the Unique).
- Comparison is O(1) or O(n):
- O(n) but deterministic with lexical comparison (
lexicalCompareFS
) - O(1) but non-deterministic with Unique comparison (
uniqCompareFS
) - Turn into
SDoc
withftext
.
- Pointer and size of a Latin-1 encoded string.
- Practically no operations.
- Outputting them is fast.
- Generated by
mkPtrString#
. - Length of string literals (mkPtrString# "abc"#) is computed statically
- Turn into
SDoc
withptext
- Requires manual memory management. Improper use may lead to memory leaks or dangling pointers.
- It assumes Latin-1 as the encoding, therefore it cannot represent arbitrary Unicode strings.
Use PtrString
unless you want the facilities of FastString
.
Synopsis
- bytesFS :: FastString -> ByteString
- fastStringToByteString :: FastString -> ByteString
- mkFastStringByteString :: ByteString -> FastString
- fastZStringToByteString :: FastZString -> ByteString
- unsafeMkByteString :: String -> ByteString
- fastStringToShortByteString :: FastString -> ShortByteString
- mkFastStringShortByteString :: ShortByteString -> FastString
- data FastZString
- hPutFZS :: Handle -> FastZString -> IO ()
- zString :: FastZString -> String
- zStringTakeN :: Int -> FastZString -> String
- lengthFZS :: FastZString -> Int
- data FastString = FastString {
- uniq :: !Int
- n_chars :: !Int
- fs_sbs :: !ShortByteString
- fs_zenc :: FastZString
- newtype NonDetFastString = NonDetFastString FastString
- newtype LexicalFastString = LexicalFastString FastString
- fsLit :: String -> FastString
- mkFastString :: String -> FastString
- mkFastStringBytes :: Ptr Word8 -> Int -> FastString
- mkFastStringByteList :: [Word8] -> FastString
- mkFastString# :: Addr# -> FastString
- unpackFS :: FastString -> String
- unconsFS :: FastString -> Maybe (Char, FastString)
- zEncodeFS :: FastString -> FastZString
- uniqueOfFS :: FastString -> Int
- lengthFS :: FastString -> Int
- nullFS :: FastString -> Bool
- appendFS :: FastString -> FastString -> FastString
- concatFS :: [FastString] -> FastString
- consFS :: Char -> FastString -> FastString
- nilFS :: FastString
- lexicalCompareFS :: FastString -> FastString -> Ordering
- uniqCompareFS :: FastString -> FastString -> Ordering
- hPutFS :: Handle -> FastString -> IO ()
- getFastStringTable :: IO [[[FastString]]]
- getFastStringZEncCounter :: IO Int
- data PtrString = PtrString !(Ptr Word8) !Int
- mkPtrString# :: Addr# -> PtrString
- unpackPtrString :: PtrString -> String
- unpackPtrStringTakeN :: Int -> PtrString -> String
- lengthPS :: PtrString -> Int
ByteString
bytesFS :: FastString -> ByteString Source #
Gives the Modified UTF-8 encoded bytes corresponding to a FastString
fastStringToByteString :: FastString -> ByteString Source #
Deprecated: Use bytesFS
instead
Gives the Modified UTF-8 encoded bytes corresponding to a FastString
mkFastStringByteString :: ByteString -> FastString Source #
Create a FastString
by copying an existing ByteString
ShortByteString
mkFastStringShortByteString :: ShortByteString -> FastString Source #
Create a FastString
from an existing ShortByteString
without
copying.
FastZString
data FastZString Source #
Instances
NFData FastZString Source # | |
Defined in GHC.Data.FastString rnf :: FastZString -> () # |
zString :: FastZString -> String Source #
zStringTakeN :: Int -> FastZString -> String Source #
zStringTakeN n =
but is performed in \(O(\min(n,l))\) rather than \(O(l)\),
where \(l\) is the length of the take
n . zString
FastZString
.
lengthFZS :: FastZString -> Int Source #
FastStrings
data FastString Source #
A FastString
is a UTF-8 encoded string together with a unique ID. All
FastString
s are stored in a global hashtable to support fast O(1)
comparison.
It is also associated with a lazy reference to the Z-encoding of this string which is used by the compiler internally.
FastString | |
|
Instances
newtype NonDetFastString Source #
Non-deterministic FastString
This is a simple FastString wrapper with an Ord instance using
uniqCompareFS
(i.e. which compares FastStrings on their Uniques). Hence it
is not deterministic from one run to the other.
Instances
newtype LexicalFastString Source #
Lexical FastString
This is a simple FastString wrapper with an Ord instance using
lexicalCompareFS
(i.e. which compares FastStrings on their String
representation). Hence it is deterministic from one run to the other.
Instances
Construction
fsLit :: String -> FastString Source #
mkFastString :: String -> FastString Source #
Creates a UTF-8 encoded FastString
from a String
mkFastStringBytes :: Ptr Word8 -> Int -> FastString Source #
mkFastStringByteList :: [Word8] -> FastString Source #
Creates a FastString
from a UTF-8 encoded [Word8]
mkFastString# :: Addr# -> FastString Source #
Deconstruction
unpackFS :: FastString -> String Source #
Lazily unpacks and decodes the FastString
unconsFS :: FastString -> Maybe (Char, FastString) Source #
Encoding
zEncodeFS :: FastString -> FastZString Source #
Returns a Z-encoded version of a FastString
. This might be the
original, if it was already Z-encoded. The first time this
function is applied to a particular FastString
, the results are
memoized.
Operations
uniqueOfFS :: FastString -> Int Source #
lengthFS :: FastString -> Int Source #
Returns the length of the FastString
in characters
nullFS :: FastString -> Bool Source #
Returns True
if the FastString
is empty
appendFS :: FastString -> FastString -> FastString Source #
concatFS :: [FastString] -> FastString Source #
consFS :: Char -> FastString -> FastString Source #
nilFS :: FastString Source #
lexicalCompareFS :: FastString -> FastString -> Ordering Source #
Compare FastString lexically
If you don't care about the lexical ordering, use uniqCompareFS
instead.
uniqCompareFS :: FastString -> FastString -> Ordering Source #
Compare FastString by their Unique (not lexically).
Much cheaper than lexicalCompareFS
but non-deterministic!
Outputting
hPutFS :: Handle -> FastString -> IO () Source #
Outputs a FastString
with no decoding at all, that is, you
get the actual bytes in the FastString
written to the Handle
.
Internal
getFastStringTable :: IO [[[FastString]]] Source #
PtrStrings
A PtrString
is a pointer to some array of Latin-1 encoded chars.
Construction
Deconstruction
unpackPtrString :: PtrString -> String Source #
unpackPtrStringTakeN :: Int -> PtrString -> String Source #
unpackPtrStringTakeN n =
but is performed in \(O(\min(n,l))\) rather than \(O(l)\),
where \(l\) is the length of the take
n . unpackPtrString
PtrString
.