Skip to content

Commit f0f04eb

Browse files
committed
Merge from develop for v0.9.8
2 parents 2468e46 + a89f1f4 commit f0f04eb

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# LuaXP Change Log #
2+
3+
## 0.9.8 ##
4+
5+
* Implement split( string, sep ) function; splits string to array on sep (e.g. split( "1,2,3", ",") returns array [1,2,3]). Sep is a Lua pattern, so special chars must be escaped in the Lua pattern way (e.g. to use "+" as a separator, must specify "%+").
6+
* Implement join( array, sep ) function; joins array elements with sep into string result (e.g. join( list( 1,2,3 ), ":") result is "1:2:3" ).
7+
8+
## 0.9.7 ##
9+
10+
* Support deferred evaluation for if() and iterate(), and logical operators (&&/and, ||/or)
11+
* Change license to MIT License (previously GPL3)
12+
13+
## 0.9.4 ##
14+
15+
* Numerous bug fixes.
16+
* Test suite.
17+
* Iteration, lists (arrays), assignments (issue #3).
18+
* Improve error reporting (issue #2).

luaxp.lua

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
local _M = {}
1212

13-
_M._VERSION = "0.9.7"
14-
_M._VNUMBER = 000907
13+
_M._VERSION = "0.9.8"
14+
_M._VNUMBER = 000908
1515
_M._DEBUG = false -- Caller may set boolean true or function(msg)
1616

1717
-- Binary operators and precedence (lower prec is higher precedence)
@@ -449,13 +449,30 @@ local function xp_tlen( t )
449449
return n
450450
end
451451

452+
local function xp_split( argv )
453+
local str = tostring( argv[1] or "" )
454+
local sep = argv[2] or ","
455+
local arr = {}
456+
if #str == 0 then return arr, 0 end
457+
local rest = string.gsub( str or "", "([^" .. sep .. "]*)" .. sep, function( m ) table.insert( arr, m ) return "" end )
458+
table.insert( arr, rest )
459+
return arr, #arr
460+
end
461+
462+
local function xp_join( argv )
463+
local a = argv[1] or {}
464+
if type(a) ~= "table" then evalerror("Argument 1 to join() is not an array") end
465+
local d = argv[2] or ","
466+
return table.concat( a, d )
467+
end
468+
452469
-- ??? All these tostrings() need to be coerce()
453470
local nativeFuncs = {
454471
['abs'] = { nargs = 1, impl = function( argv ) if argv[1] < 0 then return -argv[1] else return argv[1] end end }
455472
, ['sgn'] = { nargs = 1, impl = function( argv ) if argv[1] < 0 then return -1 elseif (argv[1] == 0) then return 0 else return 1 end end }
456473
, ['floor'] = { nargs = 1, impl = function( argv ) return math.floor(argv[1]) end }
457474
, ['ceil'] = { nargs = 1, impl = function( argv ) return math.ceil(argv[1]) end }
458-
, ['round'] = { nargs = 1, impl = function( argv ) local n = argv[1] local p = argv[2] or 0 return math.floor( n * xp_pow(10, p) + 0.5 ) / xp_pow(10, p) end }
475+
, ['round'] = { nargs = 1, impl = function( argv ) local n = argv[1] local p = argv[2] or 0 return math.floor( n * (10^p) + 0.5 ) / (10^p) end }
459476
, ['cos'] = { nargs = 1, impl = function( argv ) return math.cos(argv[1]) end }
460477
, ['sin'] = { nargs = 1, impl = function( argv ) return math.sin(argv[1]) end }
461478
, ['tan'] = { nargs = 1, impl = function( argv ) return math.tan(argv[1]) end }
@@ -476,6 +493,8 @@ local nativeFuncs = {
476493
, ['tostring'] = { nargs = 1, impl = function( argv ) if isNull(argv[1]) then return "" else return tostring(argv[1]) end end }
477494
, ['tonumber'] = { nargs = 1, impl = function( argv ) if base.type(argv[1]) == "boolean" then if argv[1] then return 1 else return 0 end end return tonumber(argv[1], argv[2] or 10) or evalerror('Argument could not be converted to number') end }
478495
, ['format'] = { nargs = 1, impl = function( argv ) return string.format( unpack(argv) ) end }
496+
, ['split'] = { nargs = 1, impl = xp_split }
497+
, ['join'] = { nargs = 1, impl = xp_join }
479498
, ['time'] = { nargs = 0, impl = function( argv ) return xp_parse_time( argv[1] ) end }
480499
, ['strftime'] = { nargs = 1, impl = function( argv ) return os.date(unpack(argv)) end }
481500
, ['dateadd'] = { nargs = 2, impl = function( argv ) return xp_date_add( argv ) end }

test/test.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ local function doStringFuncTests()
369369
eval("tonumber('377',8)", 255)
370370
eval("tonumber('-377',8)", nil, nil, "Known limitation in Lua tonumber(), ignore this case.")
371371
eval("tonumber('1001',2)", 9)
372+
eval("split('A,B,C,D,E', ',')", nil, nil, "Array of 5 elements")
373+
eval("split('F,G,H')", nil, nil, "Array of 3 elements")
374+
eval("join(split('Z+Y+X+W+V+U', '%+'), 'M')", "ZMYMXMWMVMU")
372375
end
373376

374377
local function doMiscFuncTests()

0 commit comments

Comments
 (0)