diff --git a/System/OsString/Common.hs b/System/OsString/Common.hs index f8d0387..d225854 100644 --- a/System/OsString/Common.hs +++ b/System/OsString/Common.hs @@ -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@. diff --git a/tests/bytestring-tests/Properties/Common.hs b/tests/bytestring-tests/Properties/Common.hs index bb4dcac..8959dda 100644 --- a/tests/bytestring-tests/Properties/Common.hs +++ b/tests/bytestring-tests/Properties/Common.hs @@ -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" ,