Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/length'
Browse files Browse the repository at this point in the history
  • Loading branch information
hasufell committed May 24, 2024
2 parents 570444d + 9aa11e1 commit 76d8096
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 76d8096

Please sign in to comment.