-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathText.hs
More file actions
125 lines (109 loc) · 3.1 KB
/
Text.hs
File metadata and controls
125 lines (109 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-|
Module : HsLua.Module.Text
Copyright : © 2017–2021 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <tarleb+hslua@zeitkraut.de>
Stability : alpha
Portability : ForeignFunctionInterface
Provides a Lua module containing a selection of useful Text functions.
-}
module HsLua.Module.Text
( -- * Module
documentedModule
-- ** Functions
, len
, lower
, reverse
, sub
, upper
) where
import Prelude hiding (reverse)
import Data.Text (Text)
import Data.Maybe (fromMaybe)
import HsLua.Marshalling (peekIntegral, peekText, pushIntegral, pushText)
import HsLua.Packaging
import qualified Data.Text as T
-- | The @text@ module.
documentedModule :: Module e
documentedModule = Module
{ moduleName = "text"
, moduleOperations = []
, moduleFields = []
, moduleFunctions =
[ len
, lower
, reverse
, sub
, upper
]
, moduleDescription =
"UTF-8 aware text manipulation functions, implemented in Haskell."
}
--
-- Functions
--
-- | Wrapper for @'T.length'@.
len :: DocumentedFunction e
len = defun "len"
### liftPure T.length
<#> textParam "s"
=#> intResult "length"
#? "Determines the number of characters in a string."
-- | Wrapper for @'T.toLower'@.
lower :: DocumentedFunction e
lower = defun "lower"
### liftPure T.toLower
<#> textParam "s"
=#> textResult "Lowercase copy of `s`"
#? "Converts a string to lower case."
-- | Wrapper for @'T.reverse'@.
reverse :: DocumentedFunction e
reverse = defun "reverse"
### liftPure T.reverse
<#> textParam "s"
=#> textResult "Reversed `s`"
#? "Reverses a string."
-- | Returns a substring, using Lua's string indexing rules.
sub :: DocumentedFunction e
sub = defun "sub"
### liftPure3 substring
<#> textParam "s"
<#> textIndex "i" "substring start position"
<#> textOptionalIndex "j" "substring end position"
=#> textResult "text substring"
#? "Returns a substring, using Lua's string indexing rules."
where
substring :: Text -> Int -> Maybe Int -> Text
substring s i jopt =
let j = fromMaybe (-1) jopt
fromStart = if i >= 0 then i - 1 else T.length s + i
fromEnd = if j < 0 then -j - 1 else T.length s - j
in T.dropEnd fromEnd . T.drop fromStart $ s
-- | Wrapper for @'T.toUpper'@.
upper :: DocumentedFunction e
upper = defun "upper"
### liftPure T.toUpper
<#> textParam "s"
=#> textResult "Uppercase copy of `s`"
#? "Converts a string to upper case."
--
-- Parameters
--
textParam :: Text -> Parameter e Text
textParam name =
parameter peekText "string" name "UTF-8 encoded string"
textIndex :: Text -> Text -> Parameter e Int
textIndex = parameter (peekIntegral @Int) "integer"
textOptionalIndex :: Text -> Text -> Parameter e (Maybe Int)
textOptionalIndex = optionalParameter (peekIntegral @Int) "integer"
--
-- Results
--
textResult :: Text -- ^ Description
-> FunctionResults e Text
textResult = functionResult pushText "string"
intResult :: Text -- ^ Description
-> FunctionResults e Int
intResult = functionResult (pushIntegral @Int) "integer"