Snippet - Optimized Method to parse a file that contains delimited tags

Optimized Method to parse a file that contains delimited tags

This method parse a file that contains tags delimited by a char such as $mytag$ for example.

The optimization is due to the way the file is parsed. Instead of using a split that deald with double memory using (one for the file, another for the splitted string), is uses 2 buffers, one for source, and one for destination.
When starting, the destination is empty, and during the process, it is fill progressively while the source is reduce as well. At the end of the process, the source is empty. That way, the same quantity of memory is used along the global execution.

this can be very useful to parse html pages for example, and fill several INPUT or other Tags with values you need on Load…


@ LousCpro - This looks like a good idea. Do you have any performance figures to compare against the split method?

@ LouisCpro - Nice one, but may I ask you why not to use StringBuilder for building output string? I’m expecting many new allocations of string object, when you join strings using + operator.


JohnySeven beat me to it… strings are immutable. However, if the runtime is smart enough then adding to the end of an immutable string isn’t really a problem. I just have little confidence the NETMF compiler/runtime is built that way. I’m curious to see your performance results. I’d be even more interested in seeing your results if you used a char[] instead of a string for your destination.

Sorry to have only see your post right now !

I have no comparison on performance, but one thing I’m sure it is the only way to parse a long string without making my STM32 Crash with an OutOfMemoryException !

Why not a stringbuilder ?.

I tried but did not change the treatment time so far…

Use of char[]

Possibly better, but at this point, I’m not sure of the gain optimization_cost/optimization_ratio. Maybe RLP would make more sense…

Regarding to the performance, Html pages in my website are refreshing with a really good rendering time (more over it also uses SDCard File.Read/Write during the post) and my memory use never exceed the source.Length, that was the intitial goal…

Thanks for all of your feedback and asks !