| 1 |
-- | Text utility functions. |
| 2 |
module NLP.Minimorph.Util |
| 3 |
( (<+>), tshow ) |
| 4 |
where |
| 5 |
|
| 6 |
#if !(MIN_VERSION_base(4,11,0)) |
| 7 |
-- this is redundant starting with base-4.11 / GHC 8.4 |
| 8 |
import Data.Semigroup |
| 9 |
#endif |
| 10 |
|
| 11 |
import qualified Data.Char as Char |
| 12 |
import Data.Text (Text) |
| 13 |
import qualified Data.Text as T |
| 14 |
|
| 15 |
infixr 6 <+> -- matches Monoid.<> |
| 16 |
-- | Separated by space unless one of them is empty (in which case just |
| 17 |
-- the non-empty one) or the first ends or the last begins with whitespace. |
| 18 |
(<+>) :: Text -> Text -> Text |
| 19 |
t1 <+> t2 | T.null t1 = t2 |
| 20 |
| T.null t2 = t1 |
| 21 |
| Char.isSpace (T.last t1) || Char.isSpace (T.head t2) = t1 <> t2 |
| 22 |
| otherwise = t1 <> " " <> t2 |
| 23 |
|
| 24 |
-- | Show a value in `Text` format. |
| 25 |
tshow :: Show a => a -> Text |
| 26 |
tshow = T.pack . show |