编程小知识之 Lua split 函数

栏目: Lua · 发布时间: 5年前

内容简介:本文简单介绍了如何在 Lua 中实现 split 函数Lua 的标准库并没有提供字符串的 split 函数,不过自己实现一下也并不困难,网上其实也早有了很多实现版本:之前有童鞋使用 Lua 实现了自己的 split 版本还与 JS(基于

本文简单介绍了如何在 Lua 中实现 split 函数

Lua 的标准库并没有提供字符串的 split 函数,不过自己实现一下也并不困难,网上其实也早有了很多实现版本:

之前有童鞋使用 Lua 实现了自己的 split 版本还与 JS(基于 V8 ) 中的标准实现进行了性能比较,有兴趣的朋友可以看看,过程其实挺有趣的,只是成文较早,文章中涉及的代码部分已经有了不少变化,阅读的时候注意一下即可(文章在 这里 )

lua-users.org 上甚至有篇专门的 wiki 讨论了这个话题,在 这里 .

各个实现的基本功能都是类似的,但是对于一些边界情况的处理则不尽相同,参考 JS 中对于 split 函数的 规范定义 ,我也尝试实现了一下自己的 split 函数版本,有兴趣的朋友可以参考一下:

function string:split_lite(sep)
    local splits = {}
    
    if sep == nil then
        -- return table with whole str
        table.insert(splits, self)
    elseif sep == "" then
        -- return table with each single character
        local len = #self
        for i = 1, len do
            table.insert(splits, self:sub(i, i))
        end
    else
        -- normal split use gmatch
        local pattern = "[^" .. sep .. "]+"
        for str in string.gmatch(self, pattern) do
            table.insert(splits, str)
        end
    end
    
    return splits
end

-- usage
local str = "a,,b"
string.split_lite(str, ",")
string.split_lite(str, ";")
string.split_lite(str)
string.split_lite(str, "")
str:split_lite("")

其实比起实现,我更好奇的是为什么 Lua 不将 split 函数加入到标准库中,毕竟标准库中已经有了 table.concat 函数(可以认为是 split 的反函数).

简单搜索了一下相关的 Lua mailing list ,发现了一个相关 回答 ,引用如下:

“Because it is quite easy to write a join in C, and it is much more  efficient than its equivalent in Lua. A split in C is more difficult  (among other things because of its endless variations), and would offer  no significant performance gain over its equivalent in Lua.”  – Roberto

大意是说使用 C 来实现(等同于加入标准库?) split 并不会有很大的性能提升,所以就不实现了.

不过个人觉得应该还有更多的细节考量,所以在 Lua mailing list 中又询问了一下,等有了更多答复我再来更新一下这篇博文~


以上所述就是小编给大家介绍的《编程小知识之 Lua split 函数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

JavaScript and Ajax for the Web, Sixth Edition

JavaScript and Ajax for the Web, Sixth Edition

Tom Negrino、Dori Smith / Peachpit Press / August 28, 2006 / $24.99

Book Description Need to learn JavaScript fast? This best-selling reference’s visual format and step-by-step, task-based instructions will have you up and running with JavaScript in no time. In thi......一起来看看 《JavaScript and Ajax for the Web, Sixth Edition》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具