Skip to content

StringLiteral#match(RegexLiteral) and similar methods #16425

@HertzDevil

Description

@HertzDevil

Recently, the standard library gained a few AST node methods dealing with regex matching:

It makes sense that the more fundamental method, #match, should be present as well:

module Crystal::Macros
  class StringLiteral
    def match(regex : RegexLiteral) : HashLiteral(NumberLiteral | StringLiteral)
    end
  end

  # ditto for SymbolLiteral and MacroId, via `def_string_methods`

  class RegexLiteral
    def match(regex : StringLiteral | SymbolLiteral | MacroId) : HashLiteral(NumberLiteral | StringLiteral)
    end
  end
end

The returned match data has the same structure as the individual elements returned by StringLiteral#scan.

There are also cases where the match data is not important, only whether a match is successful. So I propose adding #matches? too:

The existing #=~ behaves like #matches? instead of the actual String#=~, as it returns a BoolLiteral rather than a NumberLiteral of the string index.


Add a 👍 reaction to issues you find important.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions