Tuesday | 21 MAY 2024
[ previous ]
[ next ]

GREP in BASIC

Title:
Date: 2023-04-30
Tags:  regex, BASIC

I wrote an iterative, single file GREP for BASIC that can handle the regexes of using the following characters: ^.*$. It was a fun little detour and I think I got most of it right. At least it works for the general case.

I orginally wanted to port Rob Pike's beautiful code and then make that iterative instead of recursive but I scrapped that plan and wrote it myself. It was surprisingly straightforward especially when I started from the basest possible case. I simple started off by writing a subroutine to find a match between two strings and then built on that to handle the dot character and then the star character. After that I worked on the start character and end character.

I used Ben Hoyt's test cases to test my GREP command and I got everything to pass so that was great.

The downside so far seems that it takes a while to actually find matches. It takes about 30 seconds to go through 1 million lines which is way too long. I'll need to figure out if I'm doing something dumb or if something in my code is more expensive than I think. My way is faster than the recursive method that I had originally ported which makes my GREP actually useable.