word-wrap 0.3.3 → 0.4
raw patch · 5 files changed
+78/−14 lines, 5 filesdep +criteriondep ~base
Dependencies added: criterion
Dependency ranges changed: base
Files
- CHANGELOG.md +11/−0
- benchmarks/Main.hs +44/−0
- src/Text/Wrap.hs +9/−11
- tests/Main.hs +2/−2
- word-wrap.cabal +12/−1
CHANGELOG.md view
@@ -1,4 +1,15 @@ +0.4+===++Bug fixes:+ * Fixed a bug where each line was being wrapped after every word+ because only one case in breakTokens was reached (thanks Callum+ Oakley)++Package changes:+ * Added a simple benchmark suite+ 0.3.3 =====
+ benchmarks/Main.hs view
@@ -0,0 +1,44 @@+{-# LANGUAGE OverloadedStrings #-}+module Main where++import Criterion.Main+import qualified Data.Text as T++import Text.Wrap++smallDocument :: T.Text+smallDocument =+ T.unlines $+ replicate 10 $+ T.concat $+ replicate 1000 "foobar stuff things x "++mediumDocument :: T.Text+mediumDocument =+ T.unlines $+ replicate 100 $+ T.concat $+ replicate 1000 "foobar stuff things x "++largeDocument :: T.Text+largeDocument =+ T.unlines $+ replicate 1000 $+ T.concat $+ replicate 1000 "foobar stuff things x "++breakSettings :: WrapSettings+breakSettings = defaultWrapSettings { breakLongWords = True }++cases :: [Benchmark]+cases =+ [ bench "small_default" $ nf (wrapTextToLines defaultWrapSettings 10) smallDocument+ , bench "medium_default" $ nf (wrapTextToLines defaultWrapSettings 10) mediumDocument+ , bench "large_deafult" $ nf (wrapTextToLines defaultWrapSettings 10) largeDocument+ , bench "small_break" $ nf (wrapTextToLines breakSettings 5) smallDocument+ , bench "medium_break" $ nf (wrapTextToLines breakSettings 5) mediumDocument+ , bench "large_break" $ nf (wrapTextToLines breakSettings 5) largeDocument+ ]++main :: IO ()+main = defaultMain cases
src/Text/Wrap.hs view
@@ -17,8 +17,8 @@ -- ^ Whether to indent new lines created by wrapping -- when their original line was indented. , breakLongWords :: Bool- -- ^ Whether to break in the middle of individual words- -- when not doing so would result in a long line.+ -- ^ Whether to break in the middle of the first word+ -- on a line when that word exceeds the wrapping width. } deriving (Eq, Show, Read) @@ -99,13 +99,9 @@ -- Take enough tokens until we reach the point where taking more -- would exceed the line length. let go _ [] = ([], [])- go 0 (t@(NonWS _):toks) =- if tokenLength t <= limit- then ([t], toks)- else if not $ breakLongWords settings- then ([t], toks)- else let (h, tl) = T.splitAt limit (tokenContent t)- in ([NonWS h], NonWS tl : toks)+ -- Check to see whether the next token exceeds the limit. If so, bump+ -- it to the next line and terminate. Otherwise keep it and continue to+ -- the next token. go acc (tok:toks) = if tokenLength tok + acc <= limit then let (nextAllowed, nextDisallowed) = go (acc + tokenLength tok) toks@@ -113,8 +109,10 @@ else case tok of WS _ -> ([], toks) NonWS _ ->- if acc == 0- then ([tok], toks)+ if acc == 0 && breakLongWords settings+ then let (h, tl) = T.splitAt limit (tokenContent tok)+ in ([NonWS h], NonWS tl : toks)+ else if acc == 0 then ([tok], toks) else ([], tok:toks) -- Allowed tokens are the ones we keep on this line. The rest go
tests/Main.hs view
@@ -8,8 +8,8 @@ main :: IO () main = hspec $ do it "leaves short lines untouched" $ do- wrapTextToLines defaultWrapSettings 5 "foo"- `shouldBe` ["foo"]+ wrapTextToLines defaultWrapSettings 7 "foo bar"+ `shouldBe` ["foo bar"] it "wraps long lines" $ do wrapTextToLines defaultWrapSettings 7 "Hello, World!"
word-wrap.cabal view
@@ -1,5 +1,5 @@ name: word-wrap-version: 0.3.3+version: 0.4 synopsis: A library for word-wrapping description: A library for wrapping long lines of text. license: BSD3@@ -29,6 +29,17 @@ default-language: Haskell2010 ghc-options: -Wall build-depends: base < 5,+ text++benchmark word-wrap-benchmarks+ type: exitcode-stdio-1.0+ default-language: Haskell2010+ hs-source-dirs: benchmarks+ main-is: Main.hs+ ghc-options: -Wall+ build-depends: base < 5,+ word-wrap,+ criterion, text test-suite word-wrap-tests