1{-# LANGUAGE OverloadedStrings #-}
2
3-- Module      : Main
4-- Copyright   : (c) 2014-2020 Brendan Hay <brendan.g.hay@gmail.com>
5-- License     : This Source Code Form is subject to the terms of
6--               the Mozilla Public License, v. 2.0.
7--               A copy of the MPL can be found in the LICENSE file or
8--               you can obtain it at http://mozilla.org/MPL/2.0/.
9-- Maintainer  : Brendan Hay <brendan.g.hay@gmail.com>
10-- Stability   : experimental
11-- Portability : non-portable (GHC extensions)
12
13module Main (main) where
14
15import Criterion
16import Criterion.Main
17import Data.List (intersperse)
18import Data.Monoid
19import Data.Text (Text)
20import qualified Data.Text as Text
21import Data.Text.Manipulate
22
23main :: IO ()
24main =
25  defaultMain
26    [ bgroup
27        "Data.Text"
28        [ bench "takeWord" $
29            whnf (Text.takeWhile (not . isWordBoundary)) phrase,
30          bench "toCamel" $
31            whnf (lowerHead . mconcat . group Text.toTitle) phrase,
32          bench "toPascal" $
33            whnf (upperHead . mconcat . group Text.toTitle) phrase,
34          bench "toSnake" $
35            whnf (mconcat . intersperse "_" . group Text.toLower) phrase,
36          bench "toSpinal" $
37            whnf (mconcat . intersperse "-" . group Text.toLower) phrase,
38          bench "toTrain" $
39            whnf (mconcat . intersperse "-" . group Text.toTitle) phrase
40        ],
41      bgroup
42        "Data.Text.Case"
43        [ bench "takeWord" $ whnf takeWord phrase,
44          bench "toCamel" $ whnf toCamel phrase,
45          bench "toPacal" $ whnf toPascal phrase,
46          bench "toSnake" $ whnf toSnake phrase,
47          bench "toSpinal" $ whnf toSpinal phrase,
48          bench "toTrain" $ whnf toTrain phrase
49        ]
50    ]
51
52phrase :: Text
53phrase = "Supercalifragilistic, world! This-is  A multipleDelimiter_String"
54
55group :: (Text -> Text) -> Text -> [Text]
56group f =
57  map (f . Text.dropWhile isBoundary)
58    . Text.groupBy (const (not . isWordBoundary))
59