typescript-to-lua 1.4.1 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/dist/CompilerOptions.d.ts +4 -1
  2. package/dist/LuaAST.d.ts +9 -7
  3. package/dist/LuaAST.js +19 -13
  4. package/dist/LuaLib.d.ts +1 -2
  5. package/dist/LuaLib.js +1 -2
  6. package/dist/LuaPrinter.js +1 -2
  7. package/dist/cli/parse.js +5 -0
  8. package/dist/lualib/ArrayConcat.lua +16 -12
  9. package/dist/lualib/ArrayEvery.lua +4 -8
  10. package/dist/lualib/ArrayFilter.lua +6 -8
  11. package/dist/lualib/ArrayFind.lua +4 -7
  12. package/dist/lualib/ArrayFindIndex.lua +4 -9
  13. package/dist/lualib/ArrayFlat.lua +17 -7
  14. package/dist/lualib/ArrayFlatMap.lua +11 -10
  15. package/dist/lualib/ArrayForEach.lua +3 -7
  16. package/dist/lualib/ArrayFrom.lua +2 -5
  17. package/dist/lualib/ArrayIncludes.lua +2 -2
  18. package/dist/lualib/ArrayIndexOf.lua +13 -21
  19. package/dist/lualib/ArrayIsArray.lua +1 -1
  20. package/dist/lualib/ArrayJoin.lua +4 -7
  21. package/dist/lualib/ArrayMap.lua +5 -9
  22. package/dist/lualib/ArrayPush.lua +6 -4
  23. package/dist/lualib/ArrayPushArray.lua +8 -0
  24. package/dist/lualib/ArrayReduce.lua +8 -8
  25. package/dist/lualib/ArrayReduceRight.lua +8 -8
  26. package/dist/lualib/ArrayReverse.lua +7 -7
  27. package/dist/lualib/ArraySetLength.lua +3 -7
  28. package/dist/lualib/ArraySlice.lua +26 -19
  29. package/dist/lualib/ArraySome.lua +4 -8
  30. package/dist/lualib/ArraySort.lua +4 -4
  31. package/dist/lualib/ArraySplice.lua +47 -58
  32. package/dist/lualib/ArrayToObject.lua +3 -7
  33. package/dist/lualib/ArrayUnshift.lua +11 -8
  34. package/dist/lualib/FunctionBind.lua +5 -11
  35. package/dist/lualib/ObjectAssign.lua +5 -7
  36. package/dist/lualib/ObjectEntries.lua +3 -1
  37. package/dist/lualib/ObjectKeys.lua +3 -1
  38. package/dist/lualib/ObjectValues.lua +3 -1
  39. package/dist/lualib/Promise.lua +8 -7
  40. package/dist/lualib/PromiseAny.lua +3 -3
  41. package/dist/lualib/PromiseRace.lua +1 -1
  42. package/dist/lualib/SourceMapTraceBack.lua +14 -1
  43. package/dist/lualib/Spread.lua +5 -7
  44. package/dist/lualib/StringReplace.lua +17 -14
  45. package/dist/lualib/StringReplaceAll.lua +36 -29
  46. package/dist/lualib/StringSplit.lua +32 -32
  47. package/dist/lualib/lualib_bundle.lua +372 -389
  48. package/dist/lualib/lualib_module_info.json +13 -23
  49. package/dist/transformation/builtins/array.d.ts +1 -1
  50. package/dist/transformation/builtins/array.js +35 -2
  51. package/dist/transformation/builtins/promise.d.ts +1 -0
  52. package/dist/transformation/builtins/promise.js +2 -1
  53. package/dist/transformation/builtins/string.js +1 -1
  54. package/dist/transformation/utils/diagnostics.d.ts +7 -2
  55. package/dist/transformation/utils/diagnostics.js +4 -1
  56. package/dist/transformation/utils/lua-ast.d.ts +1 -0
  57. package/dist/transformation/utils/lua-ast.js +6 -2
  58. package/dist/transformation/utils/typescript/index.d.ts +1 -0
  59. package/dist/transformation/utils/typescript/index.js +9 -1
  60. package/dist/transformation/visitors/call.js +4 -0
  61. package/dist/transformation/visitors/class/index.js +1 -1
  62. package/dist/transformation/visitors/class/members/accessors.js +1 -1
  63. package/dist/transformation/visitors/class/members/constructor.js +1 -1
  64. package/dist/transformation/visitors/errors.js +4 -2
  65. package/dist/transformation/visitors/function.js +4 -4
  66. package/dist/transformation/visitors/modules/import.d.ts +1 -0
  67. package/dist/transformation/visitors/modules/import.js +9 -1
  68. package/dist/transformation/visitors/spread.js +1 -7
  69. package/dist/transpilation/resolve.js +1 -0
  70. package/package.json +1 -1
  71. package/dist/lualib/ArrayShift.lua +0 -3
  72. package/dist/lualib/StringConcat.lua +0 -8
@@ -1,27 +1,34 @@
1
- local function __TS__ArraySlice(list, first, last)
2
- local len = #list
3
- local relativeStart = first or 0
4
- local k
5
- if relativeStart < 0 then
6
- k = math.max(len + relativeStart, 0)
1
+ local function __TS__ArraySlice(self, first, last)
2
+ local len = #self
3
+ first = first or 0
4
+ if first < 0 then
5
+ first = len + first
6
+ if first < 0 then
7
+ first = 0
8
+ end
7
9
  else
8
- k = math.min(relativeStart, len)
10
+ if first > len then
11
+ first = len
12
+ end
9
13
  end
10
- local relativeEnd = last
11
- if last == nil then
12
- relativeEnd = len
13
- end
14
- local final
15
- if relativeEnd < 0 then
16
- final = math.max(len + relativeEnd, 0)
14
+ last = last or len
15
+ if last < 0 then
16
+ last = len + last
17
+ if last < 0 then
18
+ last = 0
19
+ end
17
20
  else
18
- final = math.min(relativeEnd, len)
21
+ if last > len then
22
+ last = len
23
+ end
19
24
  end
20
25
  local out = {}
21
- local n = 0
22
- while k < final do
23
- out[n + 1] = list[k + 1]
24
- k = k + 1
26
+ first = first + 1
27
+ last = last + 1
28
+ local n = 1
29
+ while first < last do
30
+ out[n] = self[first]
31
+ first = first + 1
25
32
  n = n + 1
26
33
  end
27
34
  return out
@@ -1,11 +1,7 @@
1
- local function __TS__ArraySome(arr, callbackfn)
2
- do
3
- local i = 0
4
- while i < #arr do
5
- if callbackfn(nil, arr[i + 1], i, arr) then
6
- return true
7
- end
8
- i = i + 1
1
+ local function __TS__ArraySome(self, callbackfn, thisArg)
2
+ for i = 1, #self do
3
+ if callbackfn(thisArg, self[i], i - 1, self) then
4
+ return true
9
5
  end
10
6
  end
11
7
  return false
@@ -1,11 +1,11 @@
1
- local function __TS__ArraySort(arr, compareFn)
1
+ local function __TS__ArraySort(self, compareFn)
2
2
  if compareFn ~= nil then
3
3
  table.sort(
4
- arr,
4
+ self,
5
5
  function(a, b) return compareFn(nil, a, b) < 0 end
6
6
  )
7
7
  else
8
- table.sort(arr)
8
+ table.sort(self)
9
9
  end
10
- return arr
10
+ return self
11
11
  end
@@ -1,84 +1,73 @@
1
- local function __TS__ArraySplice(list, ...)
2
- local len = #list
1
+ local function __TS__ArraySplice(self, ...)
2
+ local args = {...}
3
+ local len = #self
3
4
  local actualArgumentCount = select("#", ...)
4
- local start = select(1, ...)
5
- local deleteCount = select(2, ...)
6
- local actualStart
5
+ local start = args[1]
6
+ local deleteCount = args[2]
7
7
  if start < 0 then
8
- actualStart = math.max(len + start, 0)
9
- else
10
- actualStart = math.min(start, len)
8
+ start = len + start
9
+ if start < 0 then
10
+ start = 0
11
+ end
12
+ elseif start > len then
13
+ start = len
14
+ end
15
+ local itemCount = actualArgumentCount - 2
16
+ if itemCount < 0 then
17
+ itemCount = 0
11
18
  end
12
- local itemCount = math.max(actualArgumentCount - 2, 0)
13
19
  local actualDeleteCount
14
20
  if actualArgumentCount == 0 then
15
21
  actualDeleteCount = 0
16
22
  elseif actualArgumentCount == 1 then
17
- actualDeleteCount = len - actualStart
23
+ actualDeleteCount = len - start
18
24
  else
19
- actualDeleteCount = math.min(
20
- math.max(deleteCount or 0, 0),
21
- len - actualStart
22
- )
25
+ actualDeleteCount = deleteCount or 0
26
+ if actualDeleteCount < 0 then
27
+ actualDeleteCount = 0
28
+ end
29
+ if actualDeleteCount > len - start then
30
+ actualDeleteCount = len - start
31
+ end
23
32
  end
24
33
  local out = {}
25
- do
26
- local k = 0
27
- while k < actualDeleteCount do
28
- local from = actualStart + k
29
- if list[from + 1] then
30
- out[k + 1] = list[from + 1]
31
- end
32
- k = k + 1
34
+ for k = 1, actualDeleteCount do
35
+ local from = start + k
36
+ if self[from] ~= nil then
37
+ out[k] = self[from]
33
38
  end
34
39
  end
35
40
  if itemCount < actualDeleteCount then
36
- do
37
- local k = actualStart
38
- while k < len - actualDeleteCount do
39
- local from = k + actualDeleteCount
40
- local to = k + itemCount
41
- if list[from + 1] then
42
- list[to + 1] = list[from + 1]
43
- else
44
- list[to + 1] = nil
45
- end
46
- k = k + 1
41
+ for k = start + 1, len - actualDeleteCount do
42
+ local from = k + actualDeleteCount
43
+ local to = k + itemCount
44
+ if self[from] then
45
+ self[to] = self[from]
46
+ else
47
+ self[to] = nil
47
48
  end
48
49
  end
49
- do
50
- local k = len
51
- while k > len - actualDeleteCount + itemCount do
52
- list[k] = nil
53
- k = k - 1
54
- end
50
+ for k = len - actualDeleteCount + itemCount + 1, len do
51
+ self[k] = nil
55
52
  end
56
53
  elseif itemCount > actualDeleteCount then
57
- do
58
- local k = len - actualDeleteCount
59
- while k > actualStart do
60
- local from = k + actualDeleteCount - 1
61
- local to = k + itemCount - 1
62
- if list[from + 1] then
63
- list[to + 1] = list[from + 1]
64
- else
65
- list[to + 1] = nil
66
- end
67
- k = k - 1
54
+ for k = len - actualDeleteCount, start + 1, -1 do
55
+ local from = k + actualDeleteCount
56
+ local to = k + itemCount
57
+ if self[from] then
58
+ self[to] = self[from]
59
+ else
60
+ self[to] = nil
68
61
  end
69
62
  end
70
63
  end
71
- local j = actualStart
64
+ local j = start + 1
72
65
  for i = 3, actualArgumentCount do
73
- list[j + 1] = select(i, ...)
66
+ self[j] = args[i]
74
67
  j = j + 1
75
68
  end
76
- do
77
- local k = #list - 1
78
- while k >= len - actualDeleteCount + itemCount do
79
- list[k + 1] = nil
80
- k = k - 1
81
- end
69
+ for k = #self, len - actualDeleteCount + itemCount + 1, -1 do
70
+ self[k] = nil
82
71
  end
83
72
  return out
84
73
  end
@@ -1,11 +1,7 @@
1
- local function __TS__ArrayToObject(array)
1
+ local function __TS__ArrayToObject(self)
2
2
  local object = {}
3
- do
4
- local i = 0
5
- while i < #array do
6
- object[i] = array[i + 1]
7
- i = i + 1
8
- end
3
+ for i = 1, #self do
4
+ object[i - 1] = self[i]
9
5
  end
10
6
  return object
11
7
  end
@@ -1,11 +1,14 @@
1
- local function __TS__ArrayUnshift(arr, ...)
1
+ local function __TS__ArrayUnshift(self, ...)
2
2
  local items = {...}
3
- do
4
- local i = #items - 1
5
- while i >= 0 do
6
- table.insert(arr, 1, items[i + 1])
7
- i = i - 1
8
- end
3
+ local numItemsToInsert = #items
4
+ if numItemsToInsert == 0 then
5
+ return #self
9
6
  end
10
- return #arr
7
+ for i = #self, 1, -1 do
8
+ self[i + numItemsToInsert] = self[i]
9
+ end
10
+ for i = 1, numItemsToInsert do
11
+ self[i] = items[i]
12
+ end
13
+ return #self
11
14
  end
@@ -1,17 +1,11 @@
1
- local function __TS__FunctionBind(fn, thisArg, ...)
1
+ local function __TS__FunctionBind(fn, ...)
2
2
  local boundArgs = {...}
3
3
  return function(____, ...)
4
4
  local args = {...}
5
- do
6
- local i = 0
7
- while i < #boundArgs do
8
- table.insert(args, i + 1, boundArgs[i + 1])
9
- i = i + 1
10
- end
11
- end
12
- return fn(
13
- thisArg,
14
- __TS__Unpack(args)
5
+ __TS__ArrayUnshift(
6
+ args,
7
+ __TS__Unpack(boundArgs)
15
8
  )
9
+ return fn(__TS__Unpack(args))
16
10
  end
17
11
  end
@@ -1,12 +1,10 @@
1
- local function __TS__ObjectAssign(to, ...)
1
+ local function __TS__ObjectAssign(target, ...)
2
2
  local sources = {...}
3
- if to == nil then
4
- return to
5
- end
6
- for ____, source in ipairs(sources) do
3
+ for i = 1, #sources do
4
+ local source = sources[i]
7
5
  for key in pairs(source) do
8
- to[key] = source[key]
6
+ target[key] = source[key]
9
7
  end
10
8
  end
11
- return to
9
+ return target
12
10
  end
@@ -1,7 +1,9 @@
1
1
  local function __TS__ObjectEntries(obj)
2
2
  local result = {}
3
+ local len = 0
3
4
  for key in pairs(obj) do
4
- result[#result + 1] = {key, obj[key]}
5
+ len = len + 1
6
+ result[len] = {key, obj[key]}
5
7
  end
6
8
  return result
7
9
  end
@@ -1,7 +1,9 @@
1
1
  local function __TS__ObjectKeys(obj)
2
2
  local result = {}
3
+ local len = 0
3
4
  for key in pairs(obj) do
4
- result[#result + 1] = key
5
+ len = len + 1
6
+ result[len] = key
5
7
  end
6
8
  return result
7
9
  end
@@ -1,7 +1,9 @@
1
1
  local function __TS__ObjectValues(obj)
2
2
  local result = {}
3
+ local len = 0
3
4
  for key in pairs(obj) do
4
- result[#result + 1] = obj[key]
5
+ len = len + 1
6
+ result[len] = obj[key]
5
7
  end
6
8
  return result
7
9
  end
@@ -67,19 +67,19 @@ do
67
67
  local isRejected = self.state == 2
68
68
  if onFulfilled then
69
69
  local internalCallback = self:createPromiseResolvingCallback(onFulfilled, resolve, reject)
70
- __TS__ArrayPush(self.fulfilledCallbacks, internalCallback)
70
+ local ____self_fulfilledCallbacks_1 = self.fulfilledCallbacks
71
+ ____self_fulfilledCallbacks_1[#____self_fulfilledCallbacks_1 + 1] = internalCallback
71
72
  if isFulfilled then
72
73
  internalCallback(nil, self.value)
73
74
  end
74
75
  else
75
- __TS__ArrayPush(
76
- self.fulfilledCallbacks,
77
- function() return resolve(nil, nil) end
78
- )
76
+ local ____self_fulfilledCallbacks_2 = self.fulfilledCallbacks
77
+ ____self_fulfilledCallbacks_2[#____self_fulfilledCallbacks_2 + 1] = function() return resolve(nil, nil) end
79
78
  end
80
79
  if onRejected then
81
80
  local internalCallback = self:createPromiseResolvingCallback(onRejected, resolve, reject)
82
- __TS__ArrayPush(self.rejectedCallbacks, internalCallback)
81
+ local ____self_rejectedCallbacks_3 = self.rejectedCallbacks
82
+ ____self_rejectedCallbacks_3[#____self_rejectedCallbacks_3 + 1] = internalCallback
83
83
  if isRejected then
84
84
  internalCallback(nil, self.rejectionReason)
85
85
  end
@@ -97,7 +97,8 @@ do
97
97
  end
98
98
  function __TS__Promise.prototype.finally(self, onFinally)
99
99
  if onFinally then
100
- __TS__ArrayPush(self.finallyCallbacks, onFinally)
100
+ local ____self_finallyCallbacks_4 = self.finallyCallbacks
101
+ ____self_finallyCallbacks_4[#____self_finallyCallbacks_4 + 1] = onFinally
101
102
  if self.state ~= 0 then
102
103
  onFinally(nil)
103
104
  end
@@ -6,9 +6,9 @@ local function __TS__PromiseAny(iterable)
6
6
  if item.state == 1 then
7
7
  return __TS__Promise.resolve(item.value)
8
8
  elseif item.state == 2 then
9
- __TS__ArrayPush(rejections, item.rejectionReason)
9
+ rejections[#rejections + 1] = item.rejectionReason
10
10
  else
11
- __TS__ArrayPush(pending, item)
11
+ pending[#pending + 1] = item
12
12
  end
13
13
  else
14
14
  return __TS__Promise.resolve(item)
@@ -28,7 +28,7 @@ local function __TS__PromiseAny(iterable)
28
28
  resolve(nil, data)
29
29
  end,
30
30
  function(____, reason)
31
- __TS__ArrayPush(rejections, reason)
31
+ rejections[#rejections + 1] = reason
32
32
  numResolved = numResolved + 1
33
33
  if numResolved == #pending then
34
34
  reject(nil, {name = "AggregateError", message = "All Promises rejected", errors = rejections})
@@ -7,7 +7,7 @@ local function __TS__PromiseRace(iterable)
7
7
  elseif item.state == 2 then
8
8
  return __TS__Promise.reject(item.rejectionReason)
9
9
  else
10
- __TS__ArrayPush(pending, item)
10
+ pending[#pending + 1] = item
11
11
  end
12
12
  else
13
13
  return __TS__Promise.resolve(item)
@@ -30,10 +30,23 @@ local function __TS__SourceMapTraceBack(fileName, sourceMap)
30
30
  "(%S+)%.lua:(%d+)",
31
31
  function(file, line) return replacer(nil, file .. ".lua", file .. ".ts", line) end
32
32
  )
33
+ local function stringReplacer(____, file, line)
34
+ local fileSourceMap = _G.__TS__sourcemap[file]
35
+ if fileSourceMap and fileSourceMap[line] then
36
+ local chunkName = string.match(file, "%[string \"([^\"]+)\"%]")
37
+ local sourceName = string.gsub(chunkName, ".lua$", ".ts")
38
+ local data = fileSourceMap[line]
39
+ if type(data) == "number" then
40
+ return (sourceName .. ":") .. tostring(data)
41
+ end
42
+ return (tostring(data.file) .. ":") .. tostring(data.line)
43
+ end
44
+ return (file .. ":") .. line
45
+ end
33
46
  result = string.gsub(
34
47
  result,
35
48
  "(%[string \"[^\"]+\"%]):(%d+)",
36
- function(file, line) return replacer(nil, file, "unknown", line) end
49
+ function(file, line) return stringReplacer(nil, file, line) end
37
50
  )
38
51
  return result
39
52
  end
@@ -1,16 +1,14 @@
1
1
  local function __TS__Spread(iterable)
2
2
  local arr = {}
3
3
  if type(iterable) == "string" then
4
- do
5
- local i = 0
6
- while i < #iterable do
7
- arr[#arr + 1] = __TS__StringAccess(iterable, i)
8
- i = i + 1
9
- end
4
+ for i = 0, #iterable - 1 do
5
+ arr[i + 1] = __TS__StringAccess(iterable, i)
10
6
  end
11
7
  else
8
+ local len = 0
12
9
  for ____, item in __TS__Iterator(iterable) do
13
- arr[#arr + 1] = item
10
+ len = len + 1
11
+ arr[len] = item
14
12
  end
15
13
  end
16
14
  return __TS__Unpack(arr)
@@ -1,17 +1,20 @@
1
- local function __TS__StringReplace(source, searchValue, replaceValue)
2
- local startPos, endPos = string.find(source, searchValue, nil, true)
3
- if not startPos then
4
- return source
5
- end
1
+ local __TS__StringReplace
2
+ do
6
3
  local sub = string.sub
7
- local before = sub(source, 1, startPos - 1)
8
- local ____temp_0
9
- if type(replaceValue) == "string" then
10
- ____temp_0 = replaceValue
11
- else
12
- ____temp_0 = replaceValue(nil, searchValue, startPos - 1, source)
4
+ function __TS__StringReplace(source, searchValue, replaceValue)
5
+ local startPos, endPos = string.find(source, searchValue, nil, true)
6
+ if not startPos then
7
+ return source
8
+ end
9
+ local before = sub(source, 1, startPos - 1)
10
+ local ____temp_0
11
+ if type(replaceValue) == "string" then
12
+ ____temp_0 = replaceValue
13
+ else
14
+ ____temp_0 = replaceValue(nil, searchValue, startPos - 1, source)
15
+ end
16
+ local replacement = ____temp_0
17
+ local after = sub(source, endPos + 1)
18
+ return (before .. replacement) .. after
13
19
  end
14
- local replacement = ____temp_0
15
- local after = sub(source, endPos + 1)
16
- return (before .. replacement) .. after
17
20
  end
@@ -1,35 +1,42 @@
1
- local function __TS__StringReplaceAll(source, searchValue, replaceValue)
2
- local replacer
3
- if type(replaceValue) == "string" then
4
- replacer = function() return replaceValue end
5
- else
6
- replacer = replaceValue
7
- end
8
- local parts = {}
9
- local partsIndex = 1
1
+ local __TS__StringReplaceAll
2
+ do
10
3
  local sub = string.sub
11
- if #searchValue == 0 then
12
- parts[1] = replacer(nil, "", 0, source)
13
- partsIndex = 2
14
- for i = 1, #source do
15
- parts[partsIndex] = sub(source, i, i)
16
- parts[partsIndex + 1] = replacer(nil, "", i, source)
17
- partsIndex = partsIndex + 2
4
+ local find = string.find
5
+ function __TS__StringReplaceAll(source, searchValue, replaceValue)
6
+ if type(replaceValue) == "string" then
7
+ local concat = table.concat(
8
+ __TS__StringSplit(source, searchValue),
9
+ replaceValue
10
+ )
11
+ if #searchValue == 0 then
12
+ return (replaceValue .. concat) .. replaceValue
13
+ end
14
+ return concat
18
15
  end
19
- else
20
- local find = string.find
21
- local currentPos = 1
22
- while true do
23
- local startPos, endPos = find(source, searchValue, currentPos, true)
24
- if not startPos then
25
- break
16
+ local parts = {}
17
+ local partsIndex = 1
18
+ if #searchValue == 0 then
19
+ parts[1] = replaceValue(nil, "", 0, source)
20
+ partsIndex = 2
21
+ for i = 1, #source do
22
+ parts[partsIndex] = sub(source, i, i)
23
+ parts[partsIndex + 1] = replaceValue(nil, "", i, source)
24
+ partsIndex = partsIndex + 2
25
+ end
26
+ else
27
+ local currentPos = 1
28
+ while true do
29
+ local startPos, endPos = find(source, searchValue, currentPos, true)
30
+ if not startPos then
31
+ break
32
+ end
33
+ parts[partsIndex] = sub(source, currentPos, startPos - 1)
34
+ parts[partsIndex + 1] = replaceValue(nil, searchValue, startPos - 1, source)
35
+ partsIndex = partsIndex + 2
36
+ currentPos = endPos + 1
26
37
  end
27
- parts[partsIndex] = sub(source, currentPos, startPos - 1)
28
- parts[partsIndex + 1] = replacer(nil, searchValue, startPos - 1, source)
29
- partsIndex = partsIndex + 2
30
- currentPos = endPos + 1
38
+ parts[partsIndex] = sub(source, currentPos)
31
39
  end
32
- parts[partsIndex] = sub(source, currentPos)
40
+ return table.concat(parts)
33
41
  end
34
- return table.concat(parts)
35
42
  end
@@ -1,36 +1,36 @@
1
- local function __TS__StringSplit(source, separator, limit)
2
- if limit == nil then
3
- limit = 4294967295
4
- end
5
- if limit == 0 then
6
- return {}
7
- end
8
- local out = {}
9
- local index = 0
10
- local count = 0
11
- if separator == nil or separator == "" then
12
- while index < #source - 1 and count < limit do
13
- out[count + 1] = __TS__StringAccess(source, index)
14
- count = count + 1
15
- index = index + 1
1
+ local __TS__StringSplit
2
+ do
3
+ local sub = string.sub
4
+ local find = string.find
5
+ function __TS__StringSplit(source, separator, limit)
6
+ if limit == nil then
7
+ limit = 4294967295
16
8
  end
17
- else
18
- local separatorLength = #separator
19
- local nextIndex = (string.find(source, separator, nil, true) or 0) - 1
20
- while nextIndex >= 0 and count < limit do
21
- out[count + 1] = __TS__StringSubstring(source, index, nextIndex)
22
- count = count + 1
23
- index = nextIndex + separatorLength
24
- nextIndex = (string.find(
25
- source,
26
- separator,
27
- math.max(index + 1, 1),
28
- true
29
- ) or 0) - 1
9
+ if limit == 0 then
10
+ return {}
30
11
  end
12
+ local result = {}
13
+ local resultIndex = 1
14
+ if separator == nil or separator == "" then
15
+ for i = 1, #source do
16
+ result[resultIndex] = sub(source, i, i)
17
+ resultIndex = resultIndex + 1
18
+ end
19
+ else
20
+ local currentPos = 1
21
+ while resultIndex <= limit do
22
+ local startPos, endPos = find(source, separator, currentPos, true)
23
+ if not startPos then
24
+ break
25
+ end
26
+ result[resultIndex] = sub(source, currentPos, startPos - 1)
27
+ resultIndex = resultIndex + 1
28
+ currentPos = endPos + 1
29
+ end
30
+ if resultIndex <= limit then
31
+ result[resultIndex] = sub(source, currentPos)
32
+ end
33
+ end
34
+ return result
31
35
  end
32
- if count < limit then
33
- out[count + 1] = __TS__StringSubstring(source, index)
34
- end
35
- return out
36
36
  end