Click here to Skip to main content
15,886,873 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Source Generators in C# and TDD misadventure time Pin
Graeme_Grant1-Feb-24 8:31
mvaGraeme_Grant1-Feb-24 8:31 
GeneralCommand line fun in .NET Pin
honey the codewitch27-Jan-24 7:10
mvahoney the codewitch27-Jan-24 7:10 
GeneralRe: Command line fun in .NET Pin
PIEBALDconsult27-Jan-24 7:24
mvePIEBALDconsult27-Jan-24 7:24 
GeneralRe: Command line fun in .NET Pin
honey the codewitch27-Jan-24 7:37
mvahoney the codewitch27-Jan-24 7:37 
GeneralMicrosoft example includes obsolete code: Why? Pin
raddevus25-Jan-24 2:15
mvaraddevus25-Jan-24 2:15 
GeneralRe: Microsoft example includes obsolete code: Why? Pin
Bruno van Dooren25-Jan-24 5:28
mvaBruno van Dooren25-Jan-24 5:28 
GeneralRe: Microsoft example includes obsolete code: Why? Pin
raddevus25-Jan-24 8:50
mvaraddevus25-Jan-24 8:50 
GeneralFound an interesting algorithm Pin
honey the codewitch21-Jan-24 5:16
mvahoney the codewitch21-Jan-24 5:16 
Update 3: Main-Lorentz isn't quite as slick as I hoped. Since it subdivides it will find foofoofoofoofoof and make it (foofoo){2}foof. Still it does alright, and I augmented it to find the extra foo in some cases like below:
Terminal
var nfa = FA.Parse(@"(ba[rz])*(foo){3}f");
nfa.ToString("e") = @"(foofoofoof|ba[rz](ba[rz])*foofoofoof)"
nfa.ToString("r") = @"(ba[rz])*(foo){3}f"
RegexOrExpression: foofoofoof|ba[rz](ba[rz])*foofoofoof
    RegexLiteralExpression: foofoofoof
    RegexConcatExpression: ba[rz](ba[rz])*foofoofoof
        RegexLiteralExpression: ba
        RegexCharsetExpression: [rz]
        RegexRepeatExpression: (ba[rz])*
            RegexConcatExpression: ba[rz]
                RegexLiteralExpression: ba
                RegexCharsetExpression: [rz]
        RegexLiteralExpression: foofoofoof
Reducing...
RegexConcatExpression: (ba[rz])*(foo){3}f
    RegexRepeatExpression: (ba[rz])*
        RegexConcatExpression: ba[rz]
            RegexLiteralExpression: ba
            RegexCharsetExpression: [rz]
    RegexRepeatExpression: (foo){3}
        RegexLiteralExpression: foo
    RegexLiteralExpression: f
(ba[rz])*(foo){3}f

Update 2: I got the algorithm working. Now I just need to port away from my C++ comfort zone into C# and make it part of my lib.

Update: I still haven't gotten the algorithm to work but it's the final piece of the puzzle for my particular current test case:

Terminal
var nfa = FA.Parse(@"(ba[rz])*(foo){3}f");
nfa.ToString("e") = @"(foofoofoof|ba[rz](ba[rz])*foofoofoof)"
nfa.ToString("r") = @"(ba[rz])*foofoofoof"
RegexOrExpression: foofoofoof|ba[rz](ba[rz])*foofoofoof
    RegexLiteralExpression: foofoofoof
    RegexConcatExpression: ba[rz](ba[rz])*foofoofoof
        RegexLiteralExpression: ba
        RegexCharsetExpression: [rz]
        RegexRepeatExpression: (ba[rz])*
            RegexConcatExpression: ba[rz]
                RegexLiteralExpression: ba
                RegexCharsetExpression: [rz]
        RegexLiteralExpression: foofoofoof
Reducing...
RegexConcatExpression: (ba[rz])*foofoofoof
    RegexRepeatExpression: (ba[rz])*
        RegexConcatExpression: ba[rz]
            RegexLiteralExpression: ba
            RegexCharsetExpression: [rz]
    RegexLiteralExpression: foofoofoof
Final reduce yields: (ba[rz])*foofoofoof


Oh boy, a bit of background first:

I have (soon to be past tense) a programming problem wherein I'm trying to reduce the complexity and/or redundancy of a regular expression. This is primarily because converting from a state machine to a regular expression yields a lot of redundancy and weird loop boundaries and stuff. It's correct, but even harder to read than a typical regex expression.

So I load this into an abstract syntax tree representing the expression through things like RegexConcatExpression and RegexLiteralExpression.

I then keep trying to reduce the whole tree until no more changes have occurred. Each expression class has it's own reduction process. Right now I'm working on RegexLiteralExpression

The big idea here is to reduce expressions like "bazfoofoofoobar" into "baz(foo){3}bar"

So i need an algorithm to find me "foofoofoo" in that string.

Enter this clever bit of mathiness:

Finding repetitions - Algorithms for Competitive Programming[^]

Pretty cool stuff. I ported it to C#.

Edit: And after trying a second implementation as well as the above, I don't understand the results I'm getting. Cry | :((
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix


modified 23-Jan-24 9:28am.

GeneralRe: Found an interesting algorithm Pin
Nelek21-Jan-24 6:15
protectorNelek21-Jan-24 6:15 
GeneralRe: Found an interesting algorithm Pin
Greg Utas21-Jan-24 13:32
professionalGreg Utas21-Jan-24 13:32 
GeneralMicrosoft Regex Weirdness Pin
honey the codewitch7-Jan-24 4:17
mvahoney the codewitch7-Jan-24 4:17 
GeneralRe: Microsoft Regex Weirdness Pin
PIEBALDconsult7-Jan-24 5:30
mvePIEBALDconsult7-Jan-24 5:30 
GeneralRe: Microsoft Regex Weirdness Pin
k50547-Jan-24 7:34
mvek50547-Jan-24 7:34 
GeneralRe: Microsoft Regex Weirdness Pin
PIEBALDconsult7-Jan-24 7:59
mvePIEBALDconsult7-Jan-24 7:59 
GeneralRe: Microsoft Regex Weirdness Pin
Brisingr Aerowing7-Jan-24 8:40
professionalBrisingr Aerowing7-Jan-24 8:40 
GeneralRe: Microsoft Regex Weirdness Pin
PIEBALDconsult7-Jan-24 8:58
mvePIEBALDconsult7-Jan-24 8:58 
GeneralRe: Microsoft Regex Weirdness Pin
honey the codewitch7-Jan-24 14:40
mvahoney the codewitch7-Jan-24 14:40 
GeneralRe: Microsoft Regex Weirdness Pin
PIEBALDconsult7-Jan-24 15:34
mvePIEBALDconsult7-Jan-24 15:34 
GeneralRe: Microsoft Regex Weirdness Pin
honey the codewitch7-Jan-24 15:49
mvahoney the codewitch7-Jan-24 15:49 
GeneralRe: Microsoft Regex Weirdness Pin
PIEBALDconsult7-Jan-24 16:35
mvePIEBALDconsult7-Jan-24 16:35 
GeneralRe: Microsoft Regex Weirdness Pin
honey the codewitch7-Jan-24 17:16
mvahoney the codewitch7-Jan-24 17:16 
GeneralRe: Microsoft Regex Weirdness Pin
Peter_in_27807-Jan-24 15:16
professionalPeter_in_27807-Jan-24 15:16 
GeneralRe: Microsoft Regex Weirdness Pin
Richard Deeming9-Jan-24 1:16
mveRichard Deeming9-Jan-24 1:16 
GeneralRe: Microsoft Regex Weirdness Pin
Brisingr Aerowing9-Jan-24 15:27
professionalBrisingr Aerowing9-Jan-24 15:27 
GeneralHow to make thing more complicated for nothing... Pin
Maximilien29-Nov-23 2:58
Maximilien29-Nov-23 2:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.