From 9aa11e115079c8adf36660813a7c4966a254b310 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Fri, 24 May 2024 01:00:57 +0800 Subject: [PATCH] Fix length function to measure code units Fixes #17 --- System/OsString/Common.hs | 11 +++++++++++ tests/bytestring-tests/Properties/Common.hs | 4 ++++ 2 files changed, 15 insertions(+) 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" ,