On 07/08/12 11:29, Chris Green wrote:
However, for a long string that *could* use as much space as the length of the string * (size of a pointer) couldn't it? That's worse than using a buffer the size of the string.
That rather depends on the data, yes. However I couldn't quite get my head around how your approach would work without getting quite messy (albeit that sometimes messy is how it gets when you're on a microcontroller).
Er, no, this is on a microcontroller with no OS as such so there aren't any built in facilities like 'grep'. It only has 2k of RAM so conserving RAM is a primary requirement, hence my question.
Maybe no grep, but the grep source might be an interesting resource, or (to return to the original post) a good search keyword: what you want(ed) to do is implement grep-like pattern matching within the microcontroller. However your actual task turns out to be much simpler.
Out of curiosity, which controller and what environment are you developing in? The closest I've got to embedded code recently is a Raspberry Pi (ie not very close, and sheer luxury compared with the microcontroller code I played with a decade or more ago).
I think I have actually solved the problem with a simpler approach because the string I am looking for is an XML identifier (or whatever they're called) so I know it starts with '<' and ends with '>' and that there aren't any embedded '<' characters i it. So, knowing that I can always restart my search when I hit a '<', simplifies things immensely. (there may be special cases where one can have an embedded '<' but they're not going to happen in this code).
This does get much simpler yes. By definition you don't need to worry about rolling back since the first character is never in the rest of the string. Find first char, keep matching until end, reset on failure. You don't need to specifically reset when you see "<" (by definition, since it can't be in the search string, your matching code will fail at that point anyway).