| 1 | <h2>Comments</h2> |
| 2 | <pre><code>-- Single line comment |
| 3 | {- Multi-line |
| 4 | comment -}</code></pre> |
| 5 | |
| 6 | <h2>Strings and characters</h2> |
| 7 | <pre><code>'a' |
| 8 | '\n' |
| 9 | '\^A' |
| 10 | '\^]' |
| 11 | '\NUL' |
| 12 | '\23' |
| 13 | '\o75' |
| 14 | '\xFE' |
| 15 | "Here is a backslant \\ as well as \137, \ |
| 16 | \a numeric escape character, and \^X, a control character."</code></pre> |
| 17 | |
| 18 | <h2>Numbers</h2> |
| 19 | <pre><code>42 |
| 20 | 123.456 |
| 21 | 123.456e-789 |
| 22 | 1e+3 |
| 23 | 0o74 |
| 24 | 0XAF</code></pre> |
| 25 | |
| 26 | <h2>Full example</h2> |
| 27 | <pre><code>hGetLine h = |
| 28 | wantReadableHandle_ "Data.ByteString.hGetLine" h $ |
| 29 | \ h_@Handle__{haByteBuffer} -> do |
| 30 | flushCharReadBuffer h_ |
| 31 | buf <- readIORef haByteBuffer |
| 32 | if isEmptyBuffer buf |
| 33 | then fill h_ buf 0 [] |
| 34 | else haveBuf h_ buf 0 [] |
| 35 | where |
| 36 | |
| 37 | fill h_@Handle__{haByteBuffer,haDevice} buf len xss = |
| 38 | len `seq` do |
| 39 | (r,buf') <- Buffered.fillReadBuffer haDevice buf |
| 40 | if r == 0 |
| 41 | then do writeIORef haByteBuffer buf{ bufR=0, bufL=0 } |
| 42 | if len > 0 |
| 43 | then mkBigPS len xss |
| 44 | else ioe_EOF |
| 45 | else haveBuf h_ buf' len xss |
| 46 | |
| 47 | haveBuf h_@Handle__{haByteBuffer} |
| 48 | buf@Buffer{ bufRaw=raw, bufR=w, bufL=r } |
| 49 | len xss = |
| 50 | do |
| 51 | off <- findEOL r w raw |
| 52 | let new_len = len + off - r |
| 53 | xs <- mkPS raw r off |
| 54 | |
| 55 | -- if eol == True, then off is the offset of the '\n' |
| 56 | -- otherwise off == w and the buffer is now empty. |
| 57 | if off /= w |
| 58 | then do if (w == off + 1) |
| 59 | then writeIORef haByteBuffer buf{ bufL=0, bufR=0 } |
| 60 | else writeIORef haByteBuffer buf{ bufL = off + 1 } |
| 61 | mkBigPS new_len (xs:xss) |
| 62 | else do |
| 63 | fill h_ buf{ bufL=0, bufR=0 } new_len (xs:xss) |
| 64 | |
| 65 | -- find the end-of-line character, if there is one |
| 66 | findEOL r w raw |
| 67 | | r == w = return w |
| 68 | | otherwise = do |
| 69 | c <- readWord8Buf raw r |
| 70 | if c == fromIntegral (ord '\n') |
| 71 | then return r -- NB. not r+1: don't include the '\n' |
| 72 | else findEOL (r+1) w raw |
| 73 | |
| 74 | mkPS :: RawBuffer Word8 -> Int -> Int -> IO ByteString |
| 75 | mkPS buf start end = |
| 76 | create len $ \p -> |
| 77 | withRawBuffer buf $ \pbuf -> do |
| 78 | copyBytes p (pbuf `plusPtr` start) len |
| 79 | where |
| 80 | len = end - start</code></pre> |