@@ -791,6 +791,20 @@ module Crystal
791791 end
792792 BoolLiteral .new(@value .includes?(piece))
793793 end
794+ when " match"
795+ interpret_check_args do |arg |
796+ unless arg.is_a?(RegexLiteral )
797+ raise " StringLiteral#match expects a regex, not #{ arg.class_desc } "
798+ end
799+
800+ regex = regex_value(arg)
801+
802+ if match_data = @value .match(regex)
803+ regex_captures_hash(match_data)
804+ else
805+ NilLiteral .new
806+ end
807+ end
794808 when " scan"
795809 interpret_check_args do |arg |
796810 unless arg.is_a?(RegexLiteral )
@@ -810,32 +824,7 @@ module Crystal
810824 )
811825
812826 @value .scan(regex) do |match_data |
813- captures = HashLiteral .new(
814- of: HashLiteral ::Entry .new(
815- Union .new([Path .global(" Int32" ), Path .global(" String" )] of ASTNode ),
816- Union .new([Path .global(" String" ), Path .global(" Nil" )] of ASTNode ),
817- )
818- )
819-
820- match_data.to_h.each do |capture , substr |
821- case capture
822- in Int32
823- key = NumberLiteral .new(capture)
824- in String
825- key = StringLiteral .new(capture)
826- end
827-
828- case substr
829- in String
830- value = StringLiteral .new(substr)
831- in Nil
832- value = NilLiteral .new
833- end
834-
835- captures.entries << HashLiteral ::Entry .new(key, value)
836- end
837-
838- matches.elements << captures
827+ matches.elements << regex_captures_hash(match_data)
839828 end
840829
841830 matches
@@ -3381,6 +3370,35 @@ private def empty_no_return_array
33813370 Crystal ::ArrayLiteral .new(of: Crystal ::Path .global(" NoReturn" ))
33823371end
33833372
3373+ private def regex_captures_hash (match_data : Regex ::MatchData )
3374+ captures = Crystal ::HashLiteral .new(
3375+ of: Crystal ::HashLiteral ::Entry .new(
3376+ Crystal ::Union .new([Crystal ::Path .global(" Int32" ), Crystal ::Path .global(" String" )] of Crystal ::ASTNode ),
3377+ Crystal ::Union .new([Crystal ::Path .global(" String" ), Crystal ::Path .global(" Nil" )] of Crystal ::ASTNode ),
3378+ )
3379+ )
3380+
3381+ match_data.to_h.each do |capture , substr |
3382+ case capture
3383+ in Int32
3384+ key = Crystal ::NumberLiteral .new(capture)
3385+ in String
3386+ key = Crystal ::StringLiteral .new(capture)
3387+ end
3388+
3389+ case substr
3390+ in String
3391+ value = Crystal ::StringLiteral .new(substr)
3392+ in Nil
3393+ value = Crystal ::NilLiteral .new
3394+ end
3395+
3396+ captures.entries << Crystal ::HashLiteral ::Entry .new(key, value)
3397+ end
3398+
3399+ captures
3400+ end
3401+
33843402private def filter (object , klass , block , interpreter , keep = true )
33853403 block_arg = block.args.first?
33863404
0 commit comments