views:

404

answers:

4

How could I check if string has already been encoded?

For example, if I encode TEST==, I get TEST%3D%3D. If I again encode last string, I get TEST%253D%253D, I would have to know before doing that if it is already encoded...

I have encoded parameters saved, and I need to search for them. I don't know for input parameters, what will they be - encoded or not, so I have to know if I have to encode or decode them before search.

+1  A: 

You can't know for sure, unless your strings conform to a certain pattern, or you keep track of your strings. As you noted by yourself, a String that is encoded can also be encoded, so you can't be 100% sure by looking at the string itself.

flybywire
+2  A: 

Decode, compare to original. If it does differ, original is encoded. If it doesn't differ, original isn't encoded. But still it says nothing about whether the newly decoded version isn't still encoded. A good task for recursion.

I hope one can't write a quine in urlencode, or this algorithm would get stuck.

SF.
You gave me the idea, how to do this. Now my SQL looks like `SELECT * FROM something WHERE param= " + param + " OR param = "+encode(param)`
Trick
How do you know that you don't need ``SELECT * FROM something WHERE param= " + param + " OR param = "+encode(param) + " OR param = "+encode(encode(param))``?That way lies infinite regress.
sverkerw
well, true except of a case where "good enough" is enough; if the 0.01% of users really want the program not to work, it won't work for them. Sometimes the extra, extreme clauses are just not worth the effort and the overhead.
SF.
A: 

Use regexp to check if your string contains illegal characters (i.e. characters which cannot be found in URL-encoded string, like whitespace).

Roman
I did not do this, but this is the solution.
Trick
So how will you differentiate between `hello%20world` and `interest20%growth` ? The first is a valid urlencoded string, the other is a string that has to be escaped and does not produce a valid unescape.
SF.
+1  A: 

Joel on software had a solution for this sometime back - http://www.joelonsoftware.com/articles/Wrong.html
Or You may add some prefix to the Strings.

Padmarag
Maybe even better: A wrapper type ``struct QuotedString {char *str;}`` to pass around, then you can __explicitly__ (and findably) mess with its insides.
sverkerw