Skip to content

string.match returns no value instead of nil when there is no match #543

Description

@eldologuzzo
  • GopherLua is a Lua5.1 implementation. You should be familiar with Lua programming language. Have you read Lua 5.1 reference manual carefully?
  • GopherLua is a Lua5.1 implementation. In Lua, to keep it simple, it is more important to remove functionalities rather than to add functionalities unlike other languages . If you are going to introduce some new cool functionalities into the GopherLua code base and the functionalities can be implemented by existing APIs, It should be implemented as a library.
  1. What version of GopherLua are you using? : master (75f4976) and v1.1.1
  2. What version of Go are you using? : 1.26.2
  3. What operating system and processor architecture are you using? : darwin/arm64
  4. What did you do? :
print(select("#", ("abc"):match("(%d+)")))
print(pcall(tonumber, ("abc"):match("(%d+)")))
  1. What did you expect to see? : string.match returns nil when there is no match (Lua 5.1 manual: "If it finds one, then match returns the captures from the pattern; otherwise it returns nil"). Lua 5.1.5:
1
true	nil
  1. What did you see instead? :
0
false	bad argument #1 to (anonymous) (value expected)

strMatch pushes LNil but returns 0, so no value is returned. Common code such as tonumber(s:match("(%d+)")) fails with "value expected".

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions