Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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