r/programming_funny • u/jai523 • Aug 25 '21
Need some assist with homework (basicapps)
Hey folks!
I've been struggling with adding ignorecase feature to our grep app.
Uploaded my code here
It's mostly copy and paste of Bohdan's code hehe but I added ignorecase logic here - https://github.com/jjhala/basicapps/blob/main/main.go#L39
and I added another function casecheck (which seems to work) and another formatter
To run this you would do something like:
`FILE_PATH="hamlet.txt" KEY_STRING="Fortinbras" IGNORE_CASE="true" ./basicapps`
or `FILE_PATH="hamlet.txt" KEY_STRING="FORTINBRAS" IGNORE_CASE="true" ./basicapps`
And it works but the problems is with strings.ReplaceAll method, it actually replaces the matched output with my key_string. I don't like the idea that it changes the source file like that so looking for some ideas on the formatter part, and of course any general suggestions about my code. :)
1
u/Inconstant_Moo Aug 26 '21
But why would it change the source file? You're not saving anything, and your function doesn't change anything, it just returns a value. Am I missing something?
1
u/Inconstant_Moo Aug 29 '21
So yes, follow-up, I used a regex t do case-insensitivity. This introduces the problem that it can get several hits. "The" can match with "The" or "the" and you'll need to highlight both of them. So you need to pass out a list of hits. The good news is that this approach will work with any regex, not just case-insensitive search. I don't know if anyone else has better ideas, it's what I went with.