String.Replace("HelperClass", "RealHelpfulClass")



  • I found these two methods when looking though a Windows Mobile C# 'helper' class library, I think this might need a .Replace("ThisCode", "GoodCode")
    I'm no mobile programmer but this just screams WTF to me, looks more like a job for RegEx thing, I guess it could have been all regex for EACH swap instead of .Replace() LOL

           private string encodeURIComponent(string Component)
            {
                return Component.Replace(@"%", @"%25").Replace(@"+", @"%2B")
                    .Replace(@" ", @"%20").Replace(@"<", @"%3C")
                    .Replace(@">", @"%3E").Replace(@"#", @"%23")
                    .Replace(@"{", @"%7B").Replace(@"}", @"%7D")
                    .Replace(@"|", @"%7C").Replace(@"\", @"%5C")
                    .Replace(@"^", @"%5E").Replace(@"~", @"%7E")
                    .Replace(@"[", @"%5B").Replace(@"]", @"%5D")
                    .Replace(@"`", @"%60").Replace(@";", @"%3B")
                    .Replace(@"/", @"%2F").Replace(@"?", @"%3F")
                    .Replace(@":", @"%3A").Replace(@"@", @"%40")
                    .Replace(@"=", @"%3D").Replace(@"&", @"%26")
                    .Replace(@"$", @"%24").Replace(@",", @"%2C");
            }
            private string decodeURIComponet(string Component)
            {
                return Component.Replace(@"%25", @"%").Replace(@"%2B", @"+")
                    .Replace(@"%20", @" ").Replace(@"%3C", @"<")
                    .Replace(@"%3E", @">").Replace(@"%23", @"#")
                    .Replace(@"%7B", @"{").Replace(@"%7D", @"}")
                    .Replace(@"%7C", @"|").Replace(@"%5C", @"\")
                    .Replace(@"%5E", @"^").Replace(@"%7E", @"~")
                    .Replace(@"%5B", @"[").Replace(@"%5D", @"]")
                    .Replace(@"%60", @"`").Replace(@"%3B", @";")
                    .Replace(@"%2F", @"/").Replace(@"%3F", @"?")
                    .Replace(@"%3A", @":").Replace(@"%40", @"@")
                    .Replace(@"%3D", @"=").Replace(@"%26", @"&")
                    .Replace(@"%24", @"$").Replace(@"%2C", @",");
            }

    I'm just glad it wasn't somthing like


    string returnString =  Component.Replace(@"%", @"%25");
    returnString = returnString.Replace(@"+", @"%2B");
    ...
    return returnString;



  •  Not to sure about Mobile C#, but seeing this I guess HTMLEncode and XMLEncode aren't available.  No need for regex if you have those.


  • Considered Harmful

    System.Web.HttpUtility.UrlEncode( Component );



  • Assuming the mobile framework has the System.Web.HttpUtility class, this is a definite wtf.  If it doesn't, I would argue that it's still a wtf in that he's creating 24 strings.  If he wanted to write the code in this manner he should have used a StringBuilder (and I would be very surprised if the mobile framework didn't have StringBuilder). 



  • After actually "trying" (running a test case on it) the code, I noticed a bigger WTF. The decode swaps the %25 to a % BEFORE doing the other swaps, which means that

    "50%253Cats" would come out as "\ats" not "3Cats"



  • @KattMan said:

     Not to sure about Mobile C#, but seeing this I guess HTMLEncode and XMLEncode aren't available.  No need for regex if you have those.

    The compact framework doesn't have HTMLEncode. It's only in the full version of the framework. I ran into this problem myself on a project. I used a stringbuilder to resolve it in the end because it was easier to understand than the RegEx and less prone to errors.

      



  • Pardon my ignorance, but how exactly would you use a RegEx to do this sort of character-to-character-sequence substitution?

    I'd use a StringBuffer with a switch on each iterated character for efficiency. For readibility I'd probably write something like the original coder wrote.



  • BTW: decodeURIComponet ... whose typo was that?



  • I'm not seeing how a Regex would help anything.  It can still only do one replace at a time.  Even if there were some magical regex syntax that could do it all at once, you're still better off not invoking Regexes in a slow, low-memory mobile app when a few simple string.Replace calls will do.

    As others pointed out, if HttpUtility were available then this would be redundant, but it's not in Mobile.

    Now, this code is wrong, particularly on the Decode method, but the principle is sound enough. 


Log in to reply