Monday, 15 March 2010

What is the most simple (and correct) escaping algorithm? -



What is the most simple (and correct) escaping algorithm? -

disclaimer: theoretical question purpose of increasing understanding. know can utilize tools json library solve problem.

suppose want create comma separated list of values may contain comma's. these commas need escaped first. assuming utilize . escape character ["a", "b,c"] becomes a,b.,c.

however, if 1 of values contained ., sequence we'd need escape before escaping commas. [ "a", "b.,c" ] becomes a,b..,c.

however, if 1 of values contained .., sequence we'd need escape before escaping commas. [ "a", "b..,c" ] becomes a,b...,c.

however, if 1 of values contained ..., sequence we'd need escape before escaping commas. [ "a", "b...,c" ] becomes a,b....,c.

etc...

the decoding process must perform reversed recursion.

however, suspect if i'm making hard , there simpler way. there simpler way?

thanks @mkbeckish' comment realized algorithm can implemented as:

// encoding text.replace(escape, escape + escape); text.replace(delim , escape + delim); // decoding text.replace(escape + delim , delim); text.replace(escape + escape, escape);

example implementation.

it's silly didn't realize myself..

algorithm escaping

No comments:

Post a Comment