[Include] Rob Pike's regex matcher
#1

Hey, everyone. Here's a implementation of Rob Pike's simple regex algorithm.

The matcher handles:
c matches any literal character c
. matches any single character
^ matches the beginning of the input string
$ matches the end of the input string
* matches zero or more occurrences of the previous character
* It doesn't handle parentheses (its purpose is to be simple not a complex regex engine)
- but it's not hard to implement it, we can use a recursive descent parser or a elaborated Shunting-yard algorithm, and send the result to the matcher (next version, maybe )

Код:
sregex_match(regexp[], text[])
Return values:
0 - doesn't match
1 - found a match

Include: sregex.inc

Anyway, have fun.
Reply
#2

Or... we just use the regex plugin, which is a lot faster and supports all of regex.

Good effort though.
Reply
#3

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Or... we just use the regex plugin, which is a lot faster and supports all of regex.

Good effort though.
Which one exactly?

This one (https://sampforum.blast.hk/showthread.php?tid=526725) causes crashes.
Reply
#4

Quote:
Originally Posted by Kevln
Посмотреть сообщение
Which one exactly?

This one (https://sampforum.blast.hk/showthread.php?tid=526725) causes crashes.
Why choose that one over this one? You think newer is always better? Shnot...

As long as you manage the memory right it works like a charm!
Reply
#5

I guess this could be an alternative to a plugin if you only need to use a regex once or twice. To me it always seems silly to load an entire plugin in order to be able to use a function once. Unfortunately, this code is GPL, which is a really crappy license because it is completely incompatible with any other license and it basically requires you to make your entire code base open source.
Reply
#6

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Why choose that one over this one? You think newer is always better? Shnot...

As long as you manage the memory right it works like a charm!
What's the difference?

http://forum.sa-mp.com/showpost.php?...&postcount=130
Reply
#7

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
I've honestly never had a crash with it, or the other one.

Also. It'd be quite simple implementing a library such as this into a new plugin. Or even ******'s RE2 library would do, but of course that shit looks a bit more complicated... xD
Reply
#8

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Or... we just use the regex plugin, which is a lot faster and supports all of regex.

Good effort though.
I didn't run any tests, but I think this code is faster than the regex library because it doesn't use any kind of "automata simulation"(constructing states, simulating, ARGH!). Run some tests, it would be nice.

Anyway, this include doesn't mean to be better than whatever plugin you use, its purpose is to help people on simple things(that's what this area is for) without the need to use the whole plugin - as you can see, it covers a small set of operations -

Have fun!
Reply
#9

Quote:
Originally Posted by dxhj
Посмотреть сообщение
I didn't run any tests, but I think this code is faster than the regex library because it doesn't use any kind of "automata simulation"(constructing states, simulating, ARGH!). Run some tests, it would be nice.

Anyway, this include doesn't mean to be better than whatever plugin you use, its purpose is to help people on simple things(that's what this area is for) without the need to use the whole plugin - as you can see, it covers a small set of operations -

Have fun!
Could you make it process parentheses and able to execute the following?

pawn Код:
forward bool:IsAdvertisement(text[]);
public bool:IsAdvertisement(text[])
{
    new message[128], expression[] = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.+){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", match;
    strcpy(message, text, sizeof(message));

    for(new i = 0, j = strlen(message); i < j; i ++)
    {
        switch(message[i])
        {
            case '0'..'9': continue;
            case '.': continue;
            default:
            {
                strdel(message, i, i + 1);
                strins(message, ".", i);
            }
        }
    }

    match = sregex_match(expression, message);

    if(match) return true;
    return false;
}
Reply
#10

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Could you make it process parentheses and able to execute the following?

pawn Код:
forward bool:IsAdvertisement(text[]);
public bool:IsAdvertisement(text[])
{
    new message[128], expression[] = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.+){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", match;
    strcpy(message, text, sizeof(message));

    for(new i = 0, j = strlen(message); i < j; i ++)
    {
        switch(message[i])
        {
            case '0'..'9': continue;
            case '.': continue;
            default:
            {
                strdel(message, i, i + 1);
                strins(message, ".", i);
            }
        }
    }

    match = sregex_match(expression, message);

    if(match) return true;
    return false;
}
Yes. I'm thinking about a more complete version, probably not the same algorithm.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)