1. Bryan O'Sullivan
  2. attoparsec

Commits

Bryan O'Sullivan  committed 8a62781

pappend for ByteString

  • Participants
  • Parent commits e8e7be9
  • Branches default

Comments (0)

Files changed (2)

File Data/Attoparsec/ByteString/Buffer.hs

View file
     , buffer
     , unbuffer
     , length
+    , pappend
     , unsafeIndex
     , substring
     , unsafeDrop
     mempty = unsafePerformIO $ Buf nullForeignPtr 0 0 0 0 <$> newIORef 0
     {-# NOINLINE mempty #-}
 
-    mappend (Buf _ _ _ 0 _ _) b = b
-    mappend a (Buf _ _ _ 0 _ _) = a
-    mappend (Buf fp0 off0 len0 cap0 gen ref) (Buf fp1 off1 len1 _ _ _) =
-      inlinePerformIO . withForeignPtr fp0 $ \ptr0 ->
-        withForeignPtr fp1 $ \ptr1 -> do
-          gen' <- atomicModifyIORef ref (\a -> let a' = a+1 in (a',a'))
-          let newlen = len0 + len1
-          if gen' == gen+1 && newlen <= cap0
-            then do
-              memcpy (ptr0 `plusPtr` (off0+len0))
-                     (ptr1 `plusPtr` off1)
-                     len1
-              return (Buf fp0 off0 newlen cap0 gen' ref)
-            else do
-              let newcap = newlen * 2
-              fp <- mallocPlainForeignPtrBytes newcap
-              withForeignPtr fp $ \ptr -> do
-                memcpy ptr (ptr0 `plusPtr` off0) len0
-                memcpy (ptr `plusPtr` len0) (ptr1 `plusPtr` off1) len1
-              return (Buf fp 0 newlen newcap gen' ref)
+    mappend     (Buf _ _ _ 0 _ _) b    = b
+    mappend a   (Buf _ _ _ 0 _ _)      = a
+    mappend buf (Buf fp off len _ _ _) = append buf fp off len
 
     mconcat [] = mempty
     mconcat xs = foldl1' mappend xs
 
+pappend :: Buffer -> ByteString -> Buffer
+pappend buf (PS fp off len) = append buf fp off len
+
+append :: Buffer -> ForeignPtr a -> Int -> Int -> Buffer
+append (Buf fp0 off0 len0 cap0 gen ref) fp1 off1 len1 =
+  inlinePerformIO . withForeignPtr fp0 $ \ptr0 ->
+    withForeignPtr fp1 $ \ptr1 -> do
+      gen' <- atomicModifyIORef ref (\a -> let a' = a+1 in (a',a'))
+      let newlen = len0 + len1
+      if gen' == gen+1 && newlen <= cap0
+        then do
+          memcpy (ptr0 `plusPtr` (off0+len0))
+                 (ptr1 `plusPtr` off1)
+                 len1
+          return (Buf fp0 off0 newlen cap0 gen' ref)
+        else do
+          let newcap = newlen * 2
+          fp <- mallocPlainForeignPtrBytes newcap
+          withForeignPtr fp $ \ptr -> do
+            memcpy ptr (ptr0 `plusPtr` off0) len0
+            memcpy (ptr `plusPtr` len0) (ptr1 `plusPtr` off1) len1
+          return (Buf fp 0 newlen newcap gen' ref)
+
 length :: Buffer -> Int
 length (Buf _ _ len _ _ _) = len
 {-# INLINE length #-}

File Data/Attoparsec/ByteString/Internal.hs

View file
 prompt t pos _more lose succ = Partial $ \s ->
   if B.null s
   then lose t pos Complete
-  else succ (t <> buffer s) pos Incomplete
+  else succ (Buf.pappend t s) pos Incomplete
 
 -- | Immediately demand more input via a 'Partial' continuation
 -- result.