hslua-module-text-1.1.1/0000755000000000000000000000000007346545000013264 5ustar0000000000000000hslua-module-text-1.1.1/CHANGELOG.md0000644000000000000000000000502407346545000015076 0ustar0000000000000000# Changelog `hslua-module-text` uses [PVP Versioning][]. ## hslua-module-text-1.1.1 Released 2024-01-18. - Relaxed upper bound for text, allowing text-2.1. ## hslua-module-text-1.1.0.1 Released 2023-03-26. - Improved doc strings. ## hslua-module-text-1.1.0 Released 2023-03-13. - Update to hslua-2.3; this includes the addition of type initializers to the module and type specifiers to the fields. ## hslua-module-text-1.0.3.1 Released 2023-01-06. - Generalized a test to avoid failures with stack nightly. - Added GHC 9.4 to test matrix. ## hslua-module-text-1.0.3 Released 2023-01-03. - Added new functions `fromencoding` and `toencoding`. These can be used to convert from or to a different (non UTF-8) encoding. This is particularly helpful when opening files on system that don't use UTF-8 for their file system, most notably Windows. ## hslua-module-text-1.0.2 Released 2022-02-19. - Adjusted package bounds, for hslua-core, hslua-marshalling, and hslua-packaging. ## hslua-module-text-1.0.1 Released 2022-01-29. - Relaxed upper bound of hslua-core, hslua-marshalling, and hslua-packaging, allowing their respective version 2.1. ## hslua-module-text-1.0.0 Released 2021-10-22. - Use hslua 2.0. ## hslua-module-text-0.3.0.1 Released 2020-10-16. - Relaxed upper bound for hslua, allow `hslua-1.3.*`. ## hslua-module-text-0.3.0 Released 2020-08-15. - Use self-documenting module. This allows to include documentation with the module definition, and to auto-generate documentation from that. Requires hslua-1.2.0 or newer. - Run CI tests with all GHC 8 versions, test stack builds. ## hslua-module-text-0.2.1 Released 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. ## hslua-module-text-0.2.0 Released 2018-09-24. - Use hslua 1.0. ## hslua-module-text-0.1.2.2 Released 2018-03-09. - Relax upper bound for base. ## hslua-module-text-0.1.2.1 Released 2017-11-24. - Add missing test file in the sources archive. This oversight had caused some stackage test failures. ## hslua-module-text-0.1.2 Released 2017-11-17. - Run tests with Travis CI. - Fix problems with GHC 7.8 ## hslua-module-text-0.1.1 Released 2017-11-16. - Lift restriction on base to allow GHC 7.8. ## hslua-module-text-0.1 Released 2017-11-15. - First version. Released on an unsuspecting world. [PVP Versioning]: https://pvp.haskell.org hslua-module-text-1.1.1/LICENSE0000644000000000000000000000205107346545000014267 0ustar0000000000000000Copyright (c) 2017-2024 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-1.1.1/Setup.hs0000644000000000000000000000005607346545000014721 0ustar0000000000000000import Distribution.Simple main = defaultMain hslua-module-text-1.1.1/hslua-module-text.cabal0000644000000000000000000000526007346545000017634 0ustar0000000000000000cabal-version: 2.2 name: hslua-module-text version: 1.1.1 synopsis: Lua module for text description: UTF-8 aware subset of Lua's `string` module. . This package is part of HsLua, a Haskell framework built around the embeddable scripting language . homepage: https://github.com/hslua/hslua bug-reports: https://github.com/hslua/hslua/issues license: MIT license-file: LICENSE author: Albert Krewinkel maintainer: tarleb@hslua.org copyright: © 2017-2024 Albert Krewinkel category: Foreign extra-source-files: CHANGELOG.md , test/test-text.lua tested-with: GHC == 8.4.4 , GHC == 8.6.5 , GHC == 8.8.3 , GHC == 8.10.7 , GHC == 9.0.2 , GHC == 9.2.8 , GHC == 9.4.8 , GHC == 9.6.3 , GHC == 9.8.1 source-repository head type: git location: https://github.com/hslua/hslua.git subdir: hslua-module-text common common-options default-language: Haskell2010 build-depends: base >= 4.11 && < 5 , hslua-core >= 2.3 && < 2.4 , hslua-packaging >= 2.3 && < 2.4 , text >= 1.2 && < 2.2 ghc-options: -Wall -Wincomplete-record-updates -Wnoncanonical-monad-instances -Wredundant-constraints if impl(ghc >= 8.2) ghc-options: -Wcpp-undef -Werror=missing-home-modules if impl(ghc >= 8.4) ghc-options: -Widentities -Wincomplete-uni-patterns -Wpartial-fields -fhide-source-paths library import: common-options exposed-modules: HsLua.Module.Text hs-source-dirs: src build-depends: hslua-marshalling >= 2.1 && < 2.4 other-extensions: OverloadedStrings test-suite test-hslua-module-text import: common-options type: exitcode-stdio-1.0 main-is: test-hslua-module-text.hs hs-source-dirs: test ghc-options: -Wall -threaded build-depends: hslua-module-text , tasty >= 0.11 , tasty-hunit >= 0.9 , tasty-lua >= 1.0 && < 1.2 , text hslua-module-text-1.1.1/src/HsLua/Module/0000755000000000000000000000000007346545000016314 5ustar0000000000000000hslua-module-text-1.1.1/src/HsLua/Module/Text.hs0000644000000000000000000001437607346545000017607 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} {-| Module : HsLua.Module.Text Copyright : © 2017–2023 Albert Krewinkel License : MIT Maintainer : Albert Krewinkel 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 = Module { moduleName = "text" , moduleOperations = [] , moduleFields = [] , moduleFunctions = [ fromencoding , len , lower , reverse , sub , toencoding , upper ] , moduleDescription = "UTF-8 aware text manipulation functions, implemented in Haskell." , moduleTypeInitializers = [] } -- -- 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 hslua-module-text-1.1.1/test/0000755000000000000000000000000007346545000014243 5ustar0000000000000000hslua-module-text-1.1.1/test/test-hslua-module-text.hs0000644000000000000000000000323307346545000021136 0ustar0000000000000000{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} {-| Module : Main Copyright : © 2017-2024 Albert Krewinkel License : MIT Maintainer : Albert Krewinkel Stability : stable Portability : Requires language extensions ForeignFunctionInterface, OverloadedStrings. Tests for the `text` Lua module. -} module Main (main) where import Control.Monad (void) import HsLua.Core as Lua import HsLua.Packaging import HsLua.Module.Text (documentedModule) import Test.Tasty (TestTree, defaultMain, testGroup) import Test.Tasty.HUnit (assertEqual, testCase) import Test.Tasty.Lua (translateResultsFromFile) main :: IO () main = do luaTest <- run @Lua.Exception $ do openlibs registerModule documentedModule pop 1 translateResultsFromFile "test/test-text.lua" defaultMain $ testGroup "hslua-module-text" [tests, luaTest] -- | HSpec tests tests :: TestTree tests = testGroup "FromLuaStack" [ testCase "text module can be pushed to the stack" $ run (void (pushModule documentedModule) :: Lua ()) , testCase "text module can be added to the preloader" . run $ do openlibs preloadModule documentedModule assertEqual' "function not added to preloader" TypeFunction =<< do _ <- dostring "return package.preload.text" ltype top , testCase "text module can be loaded as hstext" . run $ do openlibs preloadModuleWithName documentedModule "hstext" assertEqual' "loading the module fails " OK =<< dostring "require 'hstext'" ] assertEqual' :: (Show a, Eq a) => String -> a -> a -> Lua () assertEqual' msg expected = liftIO . assertEqual msg expected hslua-module-text-1.1.1/test/test-text.lua0000644000000000000000000001032407346545000016707 0ustar0000000000000000-- -- Tests for the text module -- local text = require 'text' local tasty = require 'tasty' local group = tasty.test_group local test = tasty.test_case local assert = tasty.assert return { group 'len' { test('ASCII', function () tasty.assert.are_equal(text.len 'five!', 5) end), test('German sz', function () tasty.assert.are_equal(text.len 'Straße', 6) end), test('string with small letter e accute', function () tasty.assert.are_equal(text.len 'Charité', 7) end), test('Unicode snowman', function () tasty.assert.are_equal(text.len '☃', 1) end) }, group 'lower' { test('uppercase ASCII', function () assert.are_equal(text.lower 'YELLING', 'yelling') end), test('lowercase ASCII', function () assert.are_equal(text.lower 'talking', 'talking') end), test('capitalized word with umlaut', function () assert.are_equal(text.lower 'Lübeck', 'lübeck') end), }, group 'upper' { test('uppercase ASCII', function () assert.are_equal(text.upper 'YELLING', 'YELLING') end), test('lowercase ASCII', function () assert.are_equal(text.upper 'silence', 'SILENCE') end), test('capitalized word with umlaut', function () assert.are_equal(text.upper 'Lübeck', 'LÜBECK') end), test('German eszett becomes double S', function () assert.are_equal(text.upper 'Spaß', 'SPASS') end), }, group 'reverse' { test('reverse word with accent circumflex', function () assert.are_equal(text.reverse 'être', 'ertê') end) }, group 'sub' { test('behaves like string.sub for ASCII text', function () local hw = 'Hello, World' assert.are_equal(text.sub(hw, 6), string.sub(hw, 6)) assert.are_equal(text.sub(hw, -1, -1), string.sub(hw, -1, -1)) assert.are_equal(text.sub(hw, -7, -2), string.sub(hw, -7, -2)) assert.are_equal(text.sub(hw, 7, -2), string.sub(hw, 7, -2)) assert.are_equal(text.sub(hw, 1, 5), string.sub(hw, 1, 5)) assert.are_equal(text.sub(hw, 5, 0), string.sub(hw, 5, 0)) assert.are_equal(text.sub(hw, 0, 2), string.sub(hw, 0, 2)) assert.are_equal(text.sub(hw, -19, 5), string.sub(hw, -19, 5)) end), test('respects UTF-8', function () assert.are_equal(text.sub('Für dich', 5), 'dich') assert.are_equal(text.sub('☢ radioactive', 0, 1), '☢') assert.are_equal(text.sub('☢ radioactive', -11, -1), 'radioactive') end) }, group 'fromencoding' { test('decode UTF-16, big endian', function () local utf16be = '\0S\0t\0r\0a\0\xdf\0e' local decoded = text.fromencoding(utf16be, 'utf16be') assert.are_equal(decoded, "Straße") end), test('throws error for unknown encoding', function () assert.error_matches( function () text.toencoding('a', 'utf9') end, "unknown encoding" ) end), test('throws error if input cannot be decoded', function () assert.error_matches( function () text.fromencoding('\xff\xff\xff\ff', 'utf16le') end, "invalid" ) end), test('throws error if input is not a string', function () assert.error_matches( function () text.fromencoding({}, 'utf16le') end, "string expected, got table" ) end), }, group 'toencoding' { test('encode as UTF-16, big endian', function () local encoded = text.toencoding('Straße', 'utf16be') assert.are_equal(encoded, '\0S\0t\0r\0a\0\xdf\0e') end), test('encode as UTF-16, little endian', function () local encoded = text.toencoding('Straße', 'utf16le') assert.are_equal(encoded, 'S\0t\0r\0a\0\xdf\0e\0') end), test('encode as UTF-32, little endian', function () local encoded = text.toencoding('Straße', 'UTF-32LE') assert.are_equal(encoded, 'S\0\0\0t\0\0\0r\0\0\0a\0\0\0\xdf\0\0\0e\0\0\0') end), test('throws error for unknown encoding', function () assert.error_matches( function () text.toencoding('a', 'utf9') end, "unknown encoding" ) end), test('throws error if input cannot be encoded', function () assert.error_matches( function () text.toencoding('😊', 'latin1') end, "invalid" ) end), } }