编程小知识之 Lua split 函数

本文简单介绍了如何在 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 中又询问了一下,等有了更多答复我再来更新一下这篇博文~


更新 2019/06/15

从邮件列表的答复来看, 标准库不实现 table.split 的原因是难以定义一种较通用的 table.split 语义,困难在于边界情况非常之多,且难以处理; string.split(可以认为是 table.split 的特例版本) 的情况相对简单,标准库已经实现了类似的函数 : string.match.

对于需要 split 函数的朋友,建议根据项目情况自己实现 split 函数,方式方法可以参考之前的说明.

发布了142 篇原创文章 · 获赞 146 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/tkokof1/article/details/90728915