Skip to content

Commit

Permalink
Fix length function to measure code units
Browse files Browse the repository at this point in the history
Fixes #17
  • Loading branch information
hasufell committed May 23, 2024
1 parent 570444d commit 9aa11e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions System/OsString/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,20 @@ null = coerce BSP.null

-- | /O(1)/ The length of a 'OsString'.
--
-- This returns the number of code units
-- (@Word8@ on unix and @Word16@ on windows), not
-- bytes.
--
-- >>> length "abc"
-- 3
--
-- @since 1.4.200.0
length :: PLATFORM_STRING -> Int
#ifdef WINDOWS
length = coerce BSP.numWord16
#else
length = coerce BSP.length
#endif

-- | /O(n)/ 'map' @f xs@ is the OsString obtained by applying @f@ to each
-- element of @xs@.
Expand Down
4 changes: 4 additions & 0 deletions tests/bytestring-tests/Properties/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ tests =

, ("length" ,
property $ \x -> numWord x === fromIntegral (length (B.unpack x)))
#if defined(OSWORD) || defined(WIN) || defined(POSIX)
, ("length abc" ,
once $ B.length (B.pack [0xbb, 0x03]) == 2)
#endif
, ("count" ,
property $ \(toElem -> c) x -> B.count c x === fromIntegral (length (elemIndices c (B.unpack x))))
, ("filter" ,
Expand Down

0 comments on commit 9aa11e1

Please sign in to comment.