Bryan O'Sullivan avatar Bryan O'Sullivan committed 3ae60fb

Add iter and iter_ functions to ease iteration.

Comments (0)

Files changed (1)

 head t = S.head (stream t)
 {-# INLINE head #-}
 
+-- | Iterate one step through a UTF-16 array, returning the current
+-- character and the step to add to give the next offset to iterate
+-- at.
+iter :: A.Array Word16 -> Int -> (Char,Int)
+iter arr i | m < 0xD800 || m > 0xDBFF = (unsafeChr m,  1)
+           | otherwise                = (U16.chr2 m n, 2)
+  where m = A.unsafeIndex arr i
+        n = A.unsafeIndex arr j
+        j = i + 1
+{-# INLINE iter #-}
+
+-- | Iterate one step through a UTF-16 array, returning the next
+-- offset to iterate at.
+iter_ :: A.Array Word16 -> Int -> Int
+iter_ arr i | m < 0xD800 || m > 0xDBFF = 1
+            | otherwise                = 2
+  where m = A.unsafeIndex arr i
+{-# INLINE iter_ #-}
+
 -- | /O(1)/ Returns the first character and rest of a 'Text', or
 -- 'Nothing' if empty. Subject to array fusion.
 uncons :: Text -> Maybe (Char, Text)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.