Timeline for Replace n or all occurrences of substring in C
Current License: CC BY-SA 4.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jul 6, 2021 at 20:32 | comment | added | klutt | @Deduplicator Fixed a lot of things. You can have a look if you're interested stackoverflow.com/a/68237099/6699433 | |
Jul 6, 2021 at 8:50 | comment | added | klutt | @Deduplicator Ah, I see. Valid point. | |
Jul 6, 2021 at 8:43 | comment | added | Deduplicator |
@klutt allocateBuffer() allocates a big enough buffer, but it does part of the sizing work itself and outsources part to countOccurrences() . There is no function returning the size of the buffer needed.
|
|
Jul 6, 2021 at 7:56 | comment | added | klutt |
Implementation 1) Will fix. Makes more sense. 2) I'll think about it. I often mix them, it it depends on if I focus on the array or the pointer. 3) You certainly have a point here. replace is not needed. 4) I needed to put the exp in parenthesis to suppress warnings. That's why I added != NULL . Because then it does not look like you have too many parenthesis. Yes, I know it does not make sense. 5) Maybe 6) Did not care about that. It's not really a part of the whole. Basically just added it so others can have a quick test case.
|
|
Jul 6, 2021 at 7:47 | comment | added | klutt | Algorithm 1) Yes, I agree. I'll write another more efficient variant. 2) Thanks for bug report. | |
Jul 6, 2021 at 7:45 | comment | added | klutt |
Very good points. I'm going to write a more efficient variant. This was mostly intended to be clear code with most focus on reusing functions. I wrote replace with the intention of using it in replaceN and that's why I chose the return value that makes more sense in that regard. And it can still be used as a boolean. But I'm not sure what you mean by "Provide some way to properly size the destination buffer." I have a function for that, which is allocateBuffer
|
|
Jul 6, 2021 at 6:47 | comment | added | Toby Speight | Oh, actually I see that while the next search starts from the right place, we repeatedly copy the string tail, and that's the bit that's quadratic. Good catch! | |
Jul 6, 2021 at 6:46 | comment | added | Toby Speight |
I'm pretty sure we're not using Shlemiel's algorithm, because src gets advanced within the loop (that being the reason the worker function returns the end of the first match). However, if you missed that, it says something about the clarity of that part of the code!
|
|
Jul 6, 2021 at 1:34 | history | answered | Deduplicator | CC BY-SA 4.0 |