Lua 5.2 的字符串分割功能

我遇到了一些困难,我相信这是一个常用的 Lua 5.1 字符串分割函数:

utils = {
split = function(str, pat)
   local t = {}  -- 注意:在 Lua 5.0 中使用 {n = 0}
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
     table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end
}

我正在使用 Lua 5.2 版本,想知道有没有人知道或者有没有一个适用于 Lua 5.2 的字符串分割函数,或者能否确认或否认运行该代码是否会有问题?这里是我的原始问题的 链接。

点赞
用户1601606
用户1601606

将下面翻译成中文并且保留原本的 markdown 格式

There will be no issues with that split function, from my POV.

note comment will be added, because of old(5.0) table length syntax.
[http://www.lua.org/pil/19.1.html](http://www.lua.org/pil/19.1.html)

There was nothing, that can cause an error in such split implementation( It's known utility function, and I used in in multiple 5.2 projects, never have any problem's)

从我的角度来看,这个 split 函数不会出现任何问题。

需要注意的是,由于旧的(5.0)表格长度语法,需要添加注意注释。

参考资料:http://www.lua.org/pil/19.1.html

这种 split 实现方式不会出现任何错误(这是一个已知的实用函数,我在多个 5.2 项目中使用过,从未出现过任何问题)。

2014-01-15 16:23:53