hslua-module-text-0.2.1/0000755000000000000000000000000000000000000013222 5ustar0000000000000000hslua-module-text-0.2.1/ChangeLog.md0000755000000000000000000000127700000000000015405 0ustar0000000000000000# Revision history for hslua-module-text ## 0.2.1 -- 2019-05-04 - Require at least HsLua v1.0.3: that version has better support for modules. - Rename `pushModuleText` to `pushModule`. The old name is keeped as an alias for now. ## 0.2.0 -- 2018-09-24 - Use hslua 1.0. ## 0.1.2.2 -- 2018-03-09 - Relax upper bound for base. ## 0.1.2.1 -- 2017-11-24 - Add missing test file in the sources archive. This oversight had caused some stackage test failures. ## 0.1.2 -- 2017-11-17 - Run tests with Travis CI. - Fix problems with GHC 7.8 ## 0.1.1 -- 2017-11-16 - Lift restriction on base to allow GHC 7.8. ## 0.1 -- 2017-11-15 - First version. Released on an unsuspecting world. hslua-module-text-0.2.1/LICENSE0000644000000000000000000000204400000000000014227 0ustar0000000000000000Copyright (c) 2017 Albert Krewinkel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. hslua-module-text-0.2.1/Setup.hs0000644000000000000000000000005600000000000014657 0ustar0000000000000000import Distribution.Simple main = defaultMain hslua-module-text-0.2.1/hslua-module-text.cabal0000644000000000000000000000316500000000000017574 0ustar0000000000000000name: hslua-module-text version: 0.2.1 synopsis: Lua module for text description: UTF-8 aware subset of Lua's `string` module. homepage: https://github.com/hslua/hslua-module-text license: MIT license-file: LICENSE author: Albert Krewinkel maintainer: albert+hslua@zeitkraut.de copyright: © 2017–2019 Albert Krewinkel category: Foreign build-type: Simple extra-source-files: ChangeLog.md test/hstext-test.lua cabal-version: >=1.10 tested-with: GHC == 7.10.3 , GHC == 8.0.2 , GHC == 8.2.2 , GHC == 8.4.4 , GHC == 8.6.5 source-repository head type: git location: https://github.com/hslua/hslua-module-text.git library exposed-modules: Foreign.Lua.Module.Text build-depends: base >= 4.7 && < 5 , bytestring >= 0.10.2 && < 0.11 , hslua >= 1.0.3 && < 1.2 , text >= 1 && < 1.3 hs-source-dirs: src default-language: Haskell2010 other-extensions: OverloadedStrings test-suite test-hslua default-language: Haskell2010 type: exitcode-stdio-1.0 main-is: test-hslua-module-text.hs hs-source-dirs: test ghc-options: -Wall -threaded build-depends: base , hslua , hslua-module-text , tasty , tasty-hunit , texthslua-module-text-0.2.1/src/Foreign/Lua/Module/0000755000000000000000000000000000000000000017350 5ustar0000000000000000hslua-module-text-0.2.1/src/Foreign/Lua/Module/Text.hs0000644000000000000000000000343400000000000020634 0ustar0000000000000000{-# LANGUAGE OverloadedStrings #-} {-| Module : Foreign.Lua.Module.Text Copyright : © 2017–2019 Albert Krewinkel License : MIT Maintainer : Albert Krewinkel Stability : alpha Portability : ForeignFunctionInterface Provide a lua module containing a selection of useful Text functions. -} module Foreign.Lua.Module.Text ( pushModule , pushModuleText , preloadTextModule )where import Control.Applicative ((<$>)) import Data.ByteString (ByteString) import Data.Text (Text) import Data.Maybe (fromMaybe) import Foreign.Lua (NumResults, Lua, Peekable, Pushable, ToHaskellFunction) import qualified Foreign.Lua as Lua import qualified Data.Text as T -- | Pushes the @text@ module to the Lua stack. pushModule :: Lua NumResults pushModule = do Lua.newtable Lua.addfunction "lower" (return . T.toLower :: Text -> Lua Text) Lua.addfunction "upper" (return . T.toUpper :: Text -> Lua Text) Lua.addfunction "reverse" (return . T.reverse :: Text -> Lua Text) Lua.addfunction "len" (return . fromIntegral . T.length :: Text -> Lua Lua.Integer) Lua.addfunction "sub" sub return 1 -- | Legacy alias for '@pushModule@'. pushModuleText :: Lua NumResults pushModuleText = pushModule -- | Add the text module under the given name to the table of preloaded -- packages. preloadTextModule :: String -> Lua () preloadTextModule = flip Lua.preloadhs pushModule -- | Returns a substring, using Lua's string indexing rules. sub :: Text -> Lua.Integer -> Lua.Optional Lua.Integer -> Lua Text sub s i j = let i' = fromIntegral i j' = fromIntegral . fromMaybe (-1) $ Lua.fromOptional j 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 return . T.dropEnd fromEnd . T.drop fromStart $ s hslua-module-text-0.2.1/test/0000755000000000000000000000000000000000000014201 5ustar0000000000000000hslua-module-text-0.2.1/test/hstext-test.lua0000755000000000000000000000166000000000000017206 0ustar0000000000000000-- -- Tests for the hstext module -- local hstext = require 'hstext' assert(hstext.lower 'YELLING' == 'yelling') assert(hstext.upper 'silence' == 'SILENCE') assert(hstext.len 'five' == 4) -- Test UTF-8 assert(hstext.upper 'Lübeck' == 'LÜBECK') assert(hstext.upper 'Spaß' == 'SPASS') assert(hstext.len 'Straße' == 6) assert(hstext.len 'Charité' == 7) assert(hstext.len '☃' == 1) assert(hstext.reverse 'être' == 'ertê') assert(hstext.reverse 'être' == 'ertê') local hw = 'Hello, World' assert(string.sub(hw, 6) == hstext.sub(hw, 6)) assert(string.sub(hw, -1, -1) == hstext.sub(hw, -1, -1)) assert(string.sub(hw, -7, -2) == hstext.sub(hw, -7, -2)) assert(string.sub(hw, 7, -2) == hstext.sub(hw, 7, -2)) assert(string.sub(hw, 1, 5) == hstext.sub(hw, 1, 5)) assert(string.sub(hw, 5, 0) == hstext.sub(hw, 5, 0)) assert(string.sub(hw, 0, 2) == hstext.sub(hw, 0, 2)) assert(string.sub(hw, -19, 5) == hstext.sub(hw, -19, 5)) hslua-module-text-0.2.1/test/test-hslua-module-text.hs0000644000000000000000000000471000000000000021075 0ustar0000000000000000{- Copyright © 2017–2019 Albert Krewinkel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -} {-# LANGUAGE OverloadedStrings #-} import Control.Monad (void, when) import Foreign.Lua (Lua) import Foreign.Lua.Module.Text (preloadTextModule, pushModuleText) import Test.Tasty (TestTree, defaultMain, testGroup) import Test.Tasty.HUnit (assertEqual, testCase) import qualified Foreign.Lua as Lua main :: IO () main = defaultMain $ testGroup "hslua-module-text" [tests] -- | HSpec tests tests :: TestTree tests = testGroup "FromLuaStack" [ testCase "text module can be pushed to the stack" $ Lua.run (void pushModuleText) , testCase "text module can be added to the preloader" . Lua.run $ do Lua.openlibs preloadTextModule "hstext" assertEqual' "function not added to preloader" Lua.TypeFunction =<< do Lua.getglobal' "package.preload.hstext" Lua.ltype (-1) , testCase "text module can be loaded as hstext" . Lua.run $ do Lua.openlibs preloadTextModule "hstext" assertEqual' "loading the module fails " Lua.OK =<< Lua.dostring "require 'hstext'" , testCase "Lua tests pass" . Lua.run $ do Lua.openlibs preloadTextModule "hstext" assertEqual' "error while running lua tests" Lua.OK =<< do st <- Lua.loadfile "test/hstext-test.lua" when (st == Lua.OK) $ Lua.call 0 0 return st ] assertEqual' :: (Show a, Eq a) => String -> a -> a -> Lua () assertEqual' msg expected = Lua.liftIO . assertEqual msg expected