HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulebasement-0.0.16Haskell2010

Basement.String

A String type backed by a UTF8 encoded byte array and all the necessary functions to manipulate the string.

You can think of String as a specialization of a byte array that have element of type Char.

The String data must contain UTF8 valid data.

  • 4 types
  • 66 values
  • Packagebasement-0.0.16
  • Exports70
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceString.hs
newtypenewtype String
#

Opaque packed array of characters in the UTF8 encoding

Constructors

Instances13IsList, Eq, Data, Ord, Show, IsString, …
newtypenewtype MutableString st
#

Mutable String Buffer.

Use as an *append* buffer, as UTF8 variable encoding doesn't really allow to change previously written character without potentially shifting bytes.

Constructors

valuecreate
  1. :: PrimMonad prim
  2. => CountOf Word8
  3. -> MutableString (PrimState prim) -> prim (Offset Word8)
  4. -> prim String
#

Unsafely create a string of up to sz bytes.

The callback f needs to return the number of bytes filled in the underlaying bytes buffer. No check is made on the callback return values, and if it's not contained without the bounds, bad things will happen.

Binary conversion

60 declarations
datadata Encoding
#

Various String Encoding that can be use to convert to and from bytes

Instances6Bounded, Enum, Eq, Data, Ord, Show

Convert a ByteArray to a string assuming a specific encoding.

It returns a 3-tuple of:

  • The string that has been succesfully converted without any error

  • An optional validation error

  • The remaining buffer that hasn't been processed (either as a result of an error, or because the encoded sequence is not fully available)

Considering a stream of data that is fetched chunk by chunk, it's valid to assume that some sequence might fall in a chunk boundary. When converting chunks, if the error is Nothing and the remaining buffer is not empty, then this buffer need to be prepended to the next chunk

valuefromChunkBytes :: [UArray Word8] -> [String]
#

Decode a stream of binary chunks containing UTF8 encoding in a list of valid String

Chunk not necessarily contains a valid string, as a UTF8 sequence could be split over 2 chunks.

Convert a Byte Array representing UTF8 data directly to a string without checking for UTF8 validity

If the input contains invalid sequences, it will trigger runtime async errors when processing data.

In doubt, use fromBytes

Convert a UTF8 array of bytes to a String.

If there's any error in the stream, it will automatically insert replacement bytes to replace invalid sequences.

In the case of sequence that fall in the middle of 2 chunks, the remaining buffer is supposed to be preprended to the next chunk, and resume the parsing.

valuetoBytes :: Encoding -> String -> UArray Word8
#

Convert a String to a bytearray in a specific encoding

if the encoding is UTF8, the underlying buffer is returned without extra allocation or any processing

In any other encoding, some allocation and processing are done to convert.

valuecopy :: String -> String
#

Copy the String

The slice of memory is copied to a new slice, making the new string independent from the original string..

valueindex :: String -> Offset Char -> Maybe Char
#

Return the nth character in a String

Compared to an array, the string need to be scanned from the beginning since the UTF8 encoding is variable.

valuetake :: CountOf Char -> String -> String
#

Create a string composed of a number @n of Chars (Unicode code points).

if the input @s contains less characters than required, then the input string is returned.

valuesplitOn :: (Char -> Bool) -> String -> [String]
#

Split on the input string using the predicate as separator

e.g.

splitOn (== ',') ","          == ["",""]
splitOn (== ',') ",abc,"      == ["","abc",""]
splitOn (== ':') "abc"        == ["abc"]
splitOn (== ':') "abc::def"   == ["abc","","def"]
splitOn (== ':') "::abc::def" == ["","","abc","","def"]
valuesub :: String -> Offset8 -> Offset8 -> String
#

Internal call to make a substring given offset in bytes.

This is unsafe considering that one can create a substring starting and/or ending on the middle of a UTF8 sequence.

valueelem :: Char -> String -> Bool
#

Return whereas the string contains a specific character or not

valueindices :: String -> String -> [Offset8]
#

Finds where are the insertion points when we search for a needle within an haystack.

valueintersperse :: Char -> String -> String
#

Intersperse the character sep between each character in the string

intersperse ' ' "Hello Foundation"

"H e l l o F o u n d a t i o n"

valuespan :: (Char -> Bool) -> String -> (String, String)
#

Apply a predicate to the string to return the longest prefix that satisfy the predicate and the remaining

valuespanEnd :: (Char -> Bool) -> String -> (String, String)
#

Apply a predicate to the string to return the longest suffix that satisfy the predicate and the remaining

Same as break but cut on a line feed with an optional carriage return.

This is the same operation as 'breakElem LF' dropping the last character of the string if it's a CR.

Also for efficiency reason (streaming), it returns if the last character was a CR character.

valuesnoc :: String -> Char -> String
#

Append a Char to the end of the String and return this new String

valuecons :: Char -> String -> String
#

Prepend a Char to the beginning of the String and return this new String

valueunsnoc :: String -> Maybe (String, Char)
#

Extract the String stripped of the last character and the last character if not empty

If empty, Nothing is returned

valueuncons :: String -> Maybe (Char, String)
#

Extract the First character of a string, and the String stripped of the first character.

If empty, Nothing is returned

Try to read a floating number as a Rational

Note that for safety reason, only exponent between -10000 and 10000 is allowed as otherwise DoS/OOM is very likely. if you don't want this behavior, switching to a scientific type (not provided yet) that represent the exponent separately is the advised solution.

valuereadFloatingExact :: String -> ReadFloatingCallback a -> Maybe a
#

Read an Floating like number of the form:

-

numbers

[

.

numbers

] [ (

e

|

E

) [

-

]

number

]

Call a function with:

  • A boolean representing if the number is negative

  • The digits part represented as a single natural number (123.456 is represented as 123456)

  • The number of digits in the fractional part (e.g. 123.456 => 3)

  • The exponent if any

The code is structured as a simple state machine that:

  • Optionally Consume a - sign

  • Consume number for the integral part

  • Optionally

  • Consume .

  • Consume remaining digits if not already end of string

  • Optionally Consume a e or E follow by an optional - and a number

valuecaseFold :: String -> String
#

Convert a String to the unicode case fold equivalent.

Case folding is mostly used for caseless comparison of strings.

valueisInfixOf :: String -> String -> Bool
#

Check whether the first string is contains within the second string.

TODO: implemented the naive way and thus terribly inefficient, reimplement properly

Try to strip a prefix from the start of a String.

If the prefix is not starting the string, then Nothing is returned, otherwise the striped string is returned

Try to strip a suffix from the end of a String.

If the suffix is not ending the string, then Nothing is returned, otherwise the striped string is returned

Legacy utility

5 declarations
valuelines :: String -> [String]
#

Split lines in a string using newline as separation.

Note that carriage return preceding a newline are also strip for maximum compatibility between Windows and Unix system.

valuewords :: String -> [String]
#

Split words in a string using spaces as separation

words "Hello Foundation"
Hello, Foundation
valuetoBase64URL :: Bool -> String -> String
#

Transform string src to URL-safe base64 binary representation. The result will be either padded or unpadded, depending on the boolean padded argument.