{-# LANGUAGE MagicHash, NoImplicitPrelude, BangPatterns, UnliftedFFITypes #-}
module GHC.CString (
unpackCString#, unpackAppendCString#, unpackFoldrCString#,
cstringLength#,
unpackCStringUtf8#, unpackAppendCStringUtf8#, unpackFoldrCStringUtf8#,
unpackNBytes#,
) where
import GHC.Types hiding (One)
import GHC.Prim
unpackCString# :: Addr# -> [Char]
{-# NOINLINE CONLIKE unpackCString# #-}
unpackCString# :: Addr# -> [Char]
unpackCString# Addr#
addr
| Int# -> Bool
isTrue# (Char#
ch Char# -> Char# -> Int#
`eqChar#` Char#
'\0'#) = []
| Bool
True = Char# -> Char
C# Char#
ch Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Addr# -> [Char]
unpackCString# (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#)
where
!ch :: Char#
ch = Addr# -> Int# -> Char#
indexCharOffAddr# Addr#
addr Int#
0#
unpackAppendCString# :: Addr# -> [Char] -> [Char]
{-# NOINLINE unpackAppendCString# #-}
unpackAppendCString# :: Addr# -> [Char] -> [Char]
unpackAppendCString# Addr#
addr [Char]
rest
| Int# -> Bool
isTrue# (Char#
ch Char# -> Char# -> Int#
`eqChar#` Char#
'\0'#) = [Char]
rest
| Bool
True = Char# -> Char
C# Char#
ch Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Addr# -> [Char] -> [Char]
unpackAppendCString# (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#) [Char]
rest
where
!ch :: Char#
ch = Addr# -> Int# -> Char#
indexCharOffAddr# Addr#
addr Int#
0#
{-# NOINLINE[0] unpackFoldrCString# #-}
unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
unpackFoldrCString# :: forall a. Addr# -> (Char -> a -> a) -> a -> a
unpackFoldrCString# Addr#
str Char -> a -> a
f a
z_init = Addr# -> a -> a
go Addr#
str a
z_init
where
go :: Addr# -> a -> a
go Addr#
addr a
z
| Int# -> Bool
isTrue# (Char#
ch Char# -> Char# -> Int#
`eqChar#` Char#
'\0'#) = a
z
| Bool
True = Char# -> Char
C# Char#
ch Char -> a -> a
`f` Addr# -> a -> a
go (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#) a
z
where
!ch :: Char#
ch = Addr# -> Int# -> Char#
indexCharOffAddr# Addr#
addr Int#
0#
unpackCStringUtf8# :: Addr# -> [Char]
{-# NOINLINE CONLIKE unpackCStringUtf8# #-}
unpackCStringUtf8# :: Addr# -> [Char]
unpackCStringUtf8# Addr#
addr
| Int# -> Bool
isTrue# (Char#
ch Char# -> Char# -> Int#
`eqChar#` Char#
'\0'# ) = []
| Bool
True =
let !byte_count :: Bytes
byte_count = Char# -> Bytes
getByteCount Char#
ch
!utf_ch :: Char#
utf_ch = Bytes -> Char# -> Addr# -> Char#
unpackUtf8Char# Bytes
byte_count Char#
ch Addr#
addr
!addr' :: Addr#
addr' = Addr#
addr Addr# -> Bytes -> Addr#
`plusBytes` Bytes
byte_count
in Char# -> Char
C# Char#
utf_ch Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Addr# -> [Char]
unpackCStringUtf8# Addr#
addr'
where
!ch :: Char#
ch = Addr# -> Int# -> Char#
indexCharOffAddr# Addr#
addr Int#
0#
unpackAppendCStringUtf8# :: Addr# -> [Char] -> [Char]
{-# NOINLINE unpackAppendCStringUtf8# #-}
unpackAppendCStringUtf8# :: Addr# -> [Char] -> [Char]
unpackAppendCStringUtf8# Addr#
addr [Char]
rest
| Int# -> Bool
isTrue# (Char#
ch Char# -> Char# -> Int#
`eqChar#` Char#
'\0'#) = [Char]
rest
| Bool
True =
let !byte_count :: Bytes
byte_count = Char# -> Bytes
getByteCount Char#
ch
!utf_ch :: Char#
utf_ch = Bytes -> Char# -> Addr# -> Char#
unpackUtf8Char# Bytes
byte_count Char#
ch Addr#
addr
!addr' :: Addr#
addr' = (Addr#
addr Addr# -> Bytes -> Addr#
`plusBytes` Bytes
byte_count)
in Char# -> Char
C# Char#
utf_ch Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Addr# -> [Char] -> [Char]
unpackAppendCStringUtf8# Addr#
addr' [Char]
rest
where
!ch :: Char#
ch = Addr# -> Int# -> Char#
indexCharOffAddr# Addr#
addr Int#
0#
{-# NOINLINE[0] unpackFoldrCStringUtf8# #-}
unpackFoldrCStringUtf8# :: Addr# -> (Char -> a -> a) -> a -> a
unpackFoldrCStringUtf8# :: forall a. Addr# -> (Char -> a -> a) -> a -> a
unpackFoldrCStringUtf8# Addr#
addr_init Char -> a -> a
f a
z_init
= Addr# -> a -> a
go Addr#
addr_init a
z_init
where
go :: Addr# -> a -> a
go Addr#
addr a
z
| Int# -> Bool
isTrue# (Char#
ch Char# -> Char# -> Int#
`eqChar#` Char#
'\0'#) = a
z
| Bool
True =
let !byte_count :: Bytes
byte_count = Char# -> Bytes
getByteCount Char#
ch
!utf_ch :: Char#
utf_ch = Bytes -> Char# -> Addr# -> Char#
unpackUtf8Char# Bytes
byte_count Char#
ch Addr#
addr
!addr' :: Addr#
addr' = (Addr#
addr Addr# -> Bytes -> Addr#
`plusBytes` Bytes
byte_count)
in Char# -> Char
C# Char#
utf_ch Char -> a -> a
`f` Addr# -> a -> a
go Addr#
addr' a
z
where
!ch :: Char#
ch = Addr# -> Int# -> Char#
indexCharOffAddr# Addr#
addr Int#
0#
unpackNBytes# :: Addr# -> Int# -> [Char]
{-# NOINLINE unpackNBytes# #-}
unpackNBytes# :: Addr# -> Int# -> [Char]
unpackNBytes# Addr#
_addr Int#
0# = []
unpackNBytes# Addr#
addr Int#
len# = [Char] -> Int# -> [Char]
unpack [] (Int#
len# Int# -> Int# -> Int#
-# Int#
1#)
where
unpack :: [Char] -> Int# -> [Char]
unpack :: [Char] -> Int# -> [Char]
unpack [Char]
acc Int#
i#
| Int# -> Bool
isTrue# (Int#
i# Int# -> Int# -> Int#
<# Int#
0#) = [Char]
acc
| Bool
True =
case Addr# -> Int# -> Char#
indexCharOffAddr# Addr#
addr Int#
i# of
Char#
ch -> [Char] -> Int# -> [Char]
unpack (Char# -> Char
C# Char#
ch Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char]
acc) (Int#
i# Int# -> Int# -> Int#
-# Int#
1#)
foreign import ccall unsafe "strlen" c_strlen :: Addr# -> Int#
cstringLength# :: Addr# -> Int#
{-# INLINE[0] cstringLength# #-}
cstringLength# :: Addr# -> Int#
cstringLength# = Addr# -> Int#
c_strlen
data Bytes = One | Two | Three | Four
{-# INLINE getByteCount #-}
getByteCount :: Char# -> Bytes
getByteCount :: Char# -> Bytes
getByteCount Char#
ch
| Int# -> Bool
isTrue# (Char#
ch Char# -> Char# -> Int#
`leChar#` Char#
'\x7F'#) = Bytes
One
| Int# -> Bool
isTrue# (Char#
ch Char# -> Char# -> Int#
`leChar#` Char#
'\xDF'#) = Bytes
Two
| Int# -> Bool
isTrue# (Char#
ch Char# -> Char# -> Int#
`leChar#` Char#
'\xEF'#) = Bytes
Three
| Bool
True = Bytes
Four
{-# INLINE plusBytes #-}
plusBytes :: Addr# -> Bytes -> Addr#
plusBytes :: Addr# -> Bytes -> Addr#
plusBytes Addr#
addr Bytes
bytes =
case Bytes
bytes of
Bytes
One -> Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#
Bytes
Two -> Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
2#
Bytes
Three -> Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
3#
Bytes
Four -> Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
4#
{-# INLINE unpackUtf8Char# #-}
unpackUtf8Char# :: Bytes -> Char# -> Addr# -> Char#
unpackUtf8Char# :: Bytes -> Char# -> Addr# -> Char#
unpackUtf8Char# Bytes
bytes Char#
ch Addr#
addr =
case Bytes
bytes of
Bytes
One -> Char#
ch
Bytes
Two -> (Int# -> Char#
chr# (((Char# -> Int#
ord# Char#
ch Int# -> Int# -> Int#
-# Int#
0xC0#) Int# -> Int# -> Int#
`uncheckedIShiftL#` Int#
6#) Int# -> Int# -> Int#
+#
(Char# -> Int#
ord# (Addr# -> Int# -> Char#
indexCharOffAddr# (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#) Int#
0#) Int# -> Int# -> Int#
-# Int#
0x80#)))
Bytes
Three -> (Int# -> Char#
chr# (((Char# -> Int#
ord# Char#
ch Int# -> Int# -> Int#
-# Int#
0xE0#) Int# -> Int# -> Int#
`uncheckedIShiftL#` Int#
12#) Int# -> Int# -> Int#
+#
((Char# -> Int#
ord# (Addr# -> Int# -> Char#
indexCharOffAddr# (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#) Int#
0#) Int# -> Int# -> Int#
-# Int#
0x80#) Int# -> Int# -> Int#
`uncheckedIShiftL#` Int#
6#) Int# -> Int# -> Int#
+#
(Char# -> Int#
ord# (Addr# -> Int# -> Char#
indexCharOffAddr# (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
2#) Int#
0#) Int# -> Int# -> Int#
-# Int#
0x80#)))
Bytes
Four -> (Int# -> Char#
chr# (((Char# -> Int#
ord# Char#
ch Int# -> Int# -> Int#
-# Int#
0xF0#) Int# -> Int# -> Int#
`uncheckedIShiftL#` Int#
18#) Int# -> Int# -> Int#
+#
((Char# -> Int#
ord# (Addr# -> Int# -> Char#
indexCharOffAddr# (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#) Int#
0#) Int# -> Int# -> Int#
-# Int#
0x80#) Int# -> Int# -> Int#
`uncheckedIShiftL#` Int#
12#) Int# -> Int# -> Int#
+#
((Char# -> Int#
ord# (Addr# -> Int# -> Char#
indexCharOffAddr# (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
2#) Int#
0#) Int# -> Int# -> Int#
-# Int#
0x80#) Int# -> Int# -> Int#
`uncheckedIShiftL#` Int#
6#) Int# -> Int# -> Int#
+#
(Char# -> Int#
ord# (Addr# -> Int# -> Char#
indexCharOffAddr# (Addr#
addr Addr# -> Int# -> Addr#
`plusAddr#` Int#
3#) Int#
0#) Int# -> Int# -> Int#
-# Int#
0x80#)))