face-hh / bussin

An esolang in TypeScript, for heaven's sake.
Apache License 2.0
566 stars 37 forks source link

MAJOR FLAW when replacing from bsx #8

Closed prakash-niroula closed 9 months ago

prakash-niroula commented 11 months ago

Consider this

lit x be "Hey do you want this ? fr fr !" rn
waffle(x)

Expected : Hey do you want this ? fr fr ! Got : Hey do you want this ? == == !

Polygons1 commented 11 months ago

true

face-hh commented 11 months ago

Interesting, it must be a glitch inside the Regular Expression:

String.prototype.replace_fr = function (target: string, replacement: string): string {
    const pattern = new RegExp('(?<![\'"`])\\b' + target + '\\b(?!["\'`])', 'g');

    return this.replace(pattern, replacement);
}

It's supposed to replace keywords that are not inside strings, and it works well for words like "lit".

Maybe someone who's more experienced in RegExp can look into this?

prakash-niroula commented 11 months ago

Interesting, it must be a glitch inside the Regular Expression:

String.prototype.replace_fr = function (target: string, replacement: string): string {
    const pattern = new RegExp('(?<![\'"`])\\b' + target + '\\b(?!["\'`])', 'g');

    return this.replace(pattern, replacement);
}

It's supposed to replace keywords that are not inside strings, and it works well for words like "lit".

Maybe someone who's more experienced in RegExp can look into this?

yeah it's because of regexp image

prakash-niroula commented 11 months ago

I think it's because of space. " fr " is a match but "fr" isn't

prakash-niroula commented 11 months ago

This regex works well if implemented recursively :

String.prototype.replace_fr = function (target: string, replacement: string): string {
    const pattern = new RegExp('^([^\'"`]+|[^\'"`]+[\'"`][^\'"`]+[\'"`][^\'"`]+)' + target,'gs')
    let tmpStr = this.slice()
    while ( tmpStr.match(pattern) ) {
        tmpStr = tmpStr.replace(pattern, '$1' + replacement)
    }
    return tmpStr;
}
prakash-niroula commented 11 months ago

It's supposed to replace keywords that are not inside strings, and it works well for words like "lit".

@face-hh It doesn't

waffle("Regex check")
lit word be "hey lit x be 10 fr 10 rn " rn
waffle(word)

Produces

hey let x = 10 == 10 ;
Symmettry commented 11 months ago

fixed in #30

face-hh commented 9 months ago

Fixed in #30