-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathText.hs
More file actions
194 lines (180 loc) · 6.14 KB
/
Text.hs
File metadata and controls
194 lines (180 loc) · 6.14 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-|
Module : HsLua.Module.Text
Copyright : © 2017–2024 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <tarleb@hslua.org>
Stability : alpha
Portability : GHC only
Provides a Lua module containing a selection of useful Text functions.
-}
module HsLua.Module.Text
( -- * Module
documentedModule
-- ** Functions
, fromencoding
, len
, lower
, reverse
, sub
, toencoding
, upper
) where
import Prelude hiding (reverse)
import Data.Text (Text)
import Data.Maybe (fromMaybe)
import Foreign.Marshal.Alloc (alloca)
import HsLua.Core (LuaError)
import HsLua.Packaging
import Lua (lua_pushlstring, lua_tolstring)
import System.IO.Error (tryIOError)
import qualified Data.Text as T
import qualified Foreign.Storable as F
import qualified GHC.Foreign as GHC
import qualified GHC.IO.Encoding as GHC
import qualified HsLua.Core as Lua
import qualified HsLua.Marshalling as Lua
-- | The @text@ module.
documentedModule :: LuaError e => Module e
documentedModule = defmodule "text"
`withFunctions`
[ fromencoding
, len
, lower
, reverse
, sub
, toencoding
, upper
]
`withDescription`
"UTF-8 aware text manipulation functions, implemented in Haskell."
--
-- Functions
--
-- | Recodes a string as UTF-8.
fromencoding :: LuaError e => DocumentedFunction e
fromencoding = defun "fromencoding"
### (\strIdx menc -> do
l <- Lua.state
result <- Lua.liftIO . tryIOError $ do
encoding <- maybe getFileSystemEncoding GHC.mkTextEncoding menc
alloca $ \lenPtr -> do
cstr <- lua_tolstring l strIdx lenPtr
-- cstr cannot be NULL, or stringIndex would have failed.
cstrLen <- F.peek lenPtr
GHC.peekCStringLen encoding (cstr, fromIntegral cstrLen)
case result of
Right s -> pure $ T.pack s
Left err -> Lua.failLua (show err))
<#> parameter stringIndex "string" "s" "string to be converted"
<#> opt (stringParam "encoding" "target encoding")
=#> functionResult Lua.pushText "string" "UTF-8 string"
#? T.unlines
[ "Converts a string to UTF-8. The `encoding` parameter specifies the"
, "encoding of the input string. On Windows, that parameter defaults"
, "to the current ANSI code page; on other platforms the function"
, "will try to use the file system's encoding."
, ""
, "The set of known encodings is system dependent, but includes at"
, "least `UTF-8`, `UTF-16BE`, `UTF-16LE`, `UTF-32BE`, and `UTF-32LE`."
, "Note that the default code page on Windows is available through"
, "`CP0`."
]
where
stringIndex idx = do
isstr <- Lua.liftLua (Lua.isstring idx)
if isstr
then pure idx
else Lua.typeMismatchMessage "string" idx >>= Lua.failPeek
-- | Wrapper for @'T.length'@.
len :: DocumentedFunction e
len = defun "len"
### liftPure T.length
<#> textParam "s" "UTF-8 encoded string"
=#> integralResult "length"
#? "Returns the length of a UTF-8 string, i.e., the number of characters."
-- | Wrapper for @'T.toLower'@.
lower :: DocumentedFunction e
lower = defun "lower"
### liftPure T.toLower
<#> textParam "s" "UTF-8 string to convert to lowercase"
=#> textResult "Lowercase copy of `s`"
#? "Returns a copy of a UTF-8 string, converted to lowercase."
-- | Wrapper for @'T.reverse'@.
reverse :: DocumentedFunction e
reverse = defun "reverse"
### liftPure T.reverse
<#> textParam "s" "UTF-8 string to revert"
=#> textResult "Reversed `s`"
#? "Returns a copy of a UTF-8 string, with characters reversed."
-- | Returns a substring, using Lua's string indexing rules.
sub :: DocumentedFunction e
sub = defun "sub"
### liftPure3 substring
<#> textParam "s" "UTF-8 string"
<#> textIndex "i" "substring start position"
<#> opt (textIndex "j" "substring end position")
=#> textResult "text substring"
#? T.unlines
[ "Returns a substring of a UTF-8 string, 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
-- | Converts a UTF-8 string to a different encoding.
toencoding :: LuaError e => DocumentedFunction e
toencoding = defun "toencoding"
### (\s menc -> do
l <- Lua.state
result <- Lua.liftIO . tryIOError $ do
encoding <- maybe getFileSystemEncoding GHC.mkTextEncoding menc
GHC.withCStringLen encoding (T.unpack s) $ \(sPtr, sLen) ->
lua_pushlstring l sPtr (fromIntegral sLen)
case result of
Right () -> pure ()
Left err -> Lua.failLua (show err))
<#> textParam "s" "UTF-8 string"
<#> opt (stringParam "enc" "target encoding")
=#> functionResult (const (pure ())) "string" "re-encoded string"
#? T.unlines
[ "Converts a UTF-8 string to a different encoding. The `encoding`"
, "parameter defaults to the current ANSI code page on Windows; on"
, "other platforms it will try to guess the file system's encoding."
, ""
, "The set of known encodings is system dependent, but includes at"
, "least `UTF-8`, `UTF-16BE`, `UTF-16LE`, `UTF-32BE`, and `UTF-32LE`."
, "Note that the default code page on Windows is available through"
, "`CP0`."
]
-- | Wrapper for @'T.toUpper'@.
upper :: DocumentedFunction e
upper = defun "upper"
### liftPure T.toUpper
<#> textParam "s" "UTF-8 string to convert to uppercase"
=#> textResult "Uppercase copy of `s`"
#? "Returns a copy of a UTF-8 string, converted to uppercase."
--
-- Parameters
--
-- | String index parameter
textIndex :: Text -- ^ parameter name
-> Text -- ^ parameter description
-> Parameter e Int
textIndex = integralParam @Int
--
-- Helpers
--
getFileSystemEncoding :: IO GHC.TextEncoding
getFileSystemEncoding =
#if defined(mingw32_HOST_OS)
GHC.mkTextEncoding "CP0" -- a.k.a CP_ACP
#else
GHC.getFileSystemEncoding
#endif