lua file:read()中的"* all *"格式是什么? [英] What is the "*all* format in lua file:read() means?

查看:1284
本文介绍了lua file:read()中的"* all *"格式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我维护着一些用LUA编写的旧代码,有一些我听不懂的代码段,

I maintain some old code written in LUA, there are some snippet I could not understand,

    local f = io.open("someFile.lua", "r");
    local szFileContent = "return {};";
    if f then
        szFileContent = f:read("*all");
        f:close();
    end

read函数中使用的格式有些奇怪,我在lua51手册

The format used in read function is something weird, I see the format *a, and *l in lua51 manual https://www.lua.org/manual/5.1/manual.html#pdf-file:read, but not the *all format

推荐答案

从liolib.c nofollow noreferrer>读取,仅检查前两个字符("*"和"a"),其余字符串则被忽略:

in the function read from liolib.c only the first two ('*' and 'a') characters are checked, the rest of the string is ignored:

// ...
const char *p = lua_tostring(L, n);
luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
switch (p[1]) {
  case 'n':  /* number */
    success = read_number(L, f);
    break;
  case 'l':  /* line */
    success = read_line(L, f);
    break;
  case 'a':  /* file */
    read_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
    success = 1; /* always success */
    break;
  default:
    return luaL_argerror(L, n, "invalid format");
 //...

这篇关于lua file:read()中的"* all *"格式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆