Skip to content

Commit 64b0613

Browse files
committed
Correct the implementation of padTo
1 parent 08d06a8 commit 64b0613

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

Cabal/src/Distribution/GetOpt.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,14 @@ usageInfo header (map flattenNames -> options) = unlines (header : table)
117117
rowOption name = indent : padTo (nameWidth - 2) name
118118

119119
-- | Pad a string to a given length with spaces.
120+
-- >>> padTo 5 "123"
121+
-- "123 "
120122
--
121-
-- If the string is longer than the given length, it is truncated.
123+
-- If the string is longer than the given length, it is not truncated.
122124
-- >>> padTo 3 "12345"
123-
-- "123"
125+
-- "12345"
124126
padTo :: Int -> String -> String
125-
padTo n x = take n (x ++ repeat ' ')
127+
padTo = flip $ foldr (\ch rest n -> ch : rest (n - 1)) (`replicate` ' ')
126128

127129
-- | Flatten the short and long option names into a single string for display.
128130
flattenNames :: OptDescr a -> OptHelp

0 commit comments

Comments
 (0)