countUTF8Chars :: Ptr Word8 -> Int -> IO Int
countUTF8Chars ptr len = go ptr 0
  where
        !end = ptr `plusPtr` len

        go p !n
           | p >= end = return n
           | otherwise  = do
                case utf8DecodeChar# (unPtr p) of
                  (# _, nBytes# #) -> go (p `plusPtr#` nBytes#) (n+1)
countUTF8Chars :: Ptr Word8 -> Int -> IO Int
countUTF8Chars ptr len = go ptr 0
  where
        !end = ptr `plusPtr` len

        go p !n
           | p >= end = return n
           | otherwise  = do
                case utf8DecodeChar# (unPtr p) of
                  (# _, nBytes# #) -> go (p `plusPtr#` nBytes#) (n+1)
countUTF8Chars ptr len = go ptr 0
  where
        !end = ptr `plusPtr` len

        go p !n
           | p >= end = return n
           | otherwise  = do
                case utf8DecodeChar# (unPtr p) of
                  (# _, nBytes# #) -> go (p `plusPtr#` nBytes#) (n+1)