Click here to Skip to main content
15,562,223 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: The Magician's String, what you see is not what you get. Pin
Bernhard Hiller11-Aug-14 23:03
Bernhard Hiller11-Aug-14 23:03 
GeneralRe: The Magician's String, what you see is not what you get. Pin
PIEBALDconsult11-Aug-14 5:28
professionalPIEBALDconsult11-Aug-14 5:28 
GeneralRe: The Magician's String, what you see is not what you get. Pin
Nicolas Dorier11-Aug-14 5:29
professionalNicolas Dorier11-Aug-14 5:29 
GeneralRe: The Magician's String, what you see is not what you get. Pin
Rob Grainger14-Aug-14 0:33
Rob Grainger14-Aug-14 0:33 
GeneralRe: The Magician's String, what you see is not what you get. Pin
Ian Shlasko14-Aug-14 4:46
Ian Shlasko14-Aug-14 4:46 
GeneralRe: The Magician's String, what you see is not what you get. Pin
KP Lee21-Aug-14 20:28
KP Lee21-Aug-14 20:28 
GeneralRe: The Magician's String, what you see is not what you get. Pin
Nicolas Dorier22-Aug-14 0:42
professionalNicolas Dorier22-Aug-14 0:42 
GeneralRe: The Magician's String, what you see is not what you get. Pin
KP Lee22-Aug-14 16:47
KP Lee22-Aug-14 16:47 
Looks like the same problem to me. One character is ASCII and the other is Unicode. (Well, they both are Unicode and one isn't ASCII. You of course, could have both not ASCII
C#
static void Main(string[] args)
{
    String str1 = "http://toto.com/";
    String str2 = "http://toto.com‏/";
    //             123456789 123456
    TestStrs(str1, str2);
    str1 = "аrnold";
    str2 = "arnold";
    TestStrs(str1, str2);
    str1 = "http://toto.com/";
    str2 = "http://toto.com/";
    TestStrs(str1, str2);
    Console.Read();
}
static bool TestStrs(string str1, string str2)
{
    bool eq = str1 == str2;
    if (eq)
    {
        Console.WriteLine(string.Format("Two Strings ({0}) are the same", str1));
        return eq;
    }
    Console.WriteLine(string.Format("Mismatch, two Strings ({0}) ({1})are not the same", str1, str2));
    int j = str1.Length, i = str2.Length;
    if (j > i)
    {
        j = i;
    }
    for (i = 0; i < j; i++)
    {
        if (str1[i] != str2[i])
            Console.WriteLine(string.Format("Mismatch found index={0}, char(2),int(2) = {1}-{2},{3}-{4}", i, str1[i], str2[i], (int)str1[i], (int)str2[i]));
    }
    return eq;
}

Of course this has a bug in it too. Multiple true Unicode strings would blow up with an overindex error. Help says exactly what I thought it said, which is patently wrong:
String . Length Property (System) - MSDN – the Microsoft ...‎
The Length property returns the number of Char objects in this instance, not the number of Unicode characters. The reason is that a Unicode character might be ...

http://msdn.microsoft.com/en-us/library/system.string.length[^]
If it did what it said it would do, I wouldn't have spotted the bug in the first place.
No, I'm wrong. I was under the impression that char could hold one or two bytes. I guess instead when a true Unicode character is indexed in the string, the next index location points to the start of the next Unicode character, otherwise your string would have a bunch of mismatches in the loop. So true multiple UNICODE strings should overindex the looped character and blow up. Oh well, every time you're wrong, you get a chance to learn something. I didn't test my theory, by making sure both strings are concatenated so both strings have one Unicode character each and prove it blows up with an overindex.

Well, one more thing to read: Use the System.Globalization.StringInfo class to work with Unicode characters instead of Char objects. Hope that has a true Length property based on the length of true Unicode characters. Hmmm, is there a Unicode type similar to char?
GeneralRe: The Magician's String, what you see is not what you get. Pin
Nicolas Dorier23-Aug-14 13:02
professionalNicolas Dorier23-Aug-14 13:02 
GeneralRe: The Magician's String, what you see is not what you get. Pin
KP Lee23-Aug-14 22:22
KP Lee23-Aug-14 22:22 
GeneralRe: The Magician's String, what you see is not what you get. Pin
Nicolas Dorier24-Aug-14 1:02
professionalNicolas Dorier24-Aug-14 1:02 
GeneralRe: The Magician's String, what you see is not what you get. Pin
KP Lee24-Aug-14 16:28
KP Lee24-Aug-14 16:28 
GeneralRe: The Magician's String, what you see is not what you get. Pin
Nicolas Dorier25-Aug-14 2:09
professionalNicolas Dorier25-Aug-14 2:09 
GeneralRe: The Magician's String, what you see is not what you get. Pin
KP Lee23-Aug-14 23:02
KP Lee23-Aug-14 23:02 
GeneralRe: The Magician's String, what you see is not what you get. Pin
Nicolas Dorier24-Aug-14 1:05
professionalNicolas Dorier24-Aug-14 1:05 
GeneralWow, Just Wow Pin
Kevin Marois7-Aug-14 7:12
professionalKevin Marois7-Aug-14 7:12 
GeneralRe: Wow, Just Wow Pin
SoMad7-Aug-14 7:32
professionalSoMad7-Aug-14 7:32 
GeneralRe: Wow, Just Wow Pin
Vark1117-Aug-14 7:55
Vark1117-Aug-14 7:55 
GeneralRe: Wow, Just Wow Pin
Super Lloyd10-Aug-14 16:30
Super Lloyd10-Aug-14 16:30 
GeneralRe: Wow, Just Wow Pin
Ranjan.D7-Aug-14 8:10
professionalRanjan.D7-Aug-14 8:10 
GeneralRe: Wow, Just Wow Pin
DaveAuld7-Aug-14 8:24
professionalDaveAuld7-Aug-14 8:24 
GeneralRe: Wow, Just Wow Pin
KP Lee21-Aug-14 20:44
KP Lee21-Aug-14 20:44 
GeneralRe: Wow, Just Wow Pin
Pete O'Hanlon7-Aug-14 8:21
mvaPete O'Hanlon7-Aug-14 8:21 
GeneralRe: Wow, Just Wow Pin
Rob Grainger7-Aug-14 8:45
Rob Grainger7-Aug-14 8:45 
GeneralRe: Wow, Just Wow Pin
fixthebugg7-Aug-14 11:11
fixthebugg7-Aug-14 11:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.