Pascal Strings

« Return to Article
  • NaN 2008-04-11 08:05
    *Blink*.
  • Vollhorst 2008-04-11 08:09
    public static final char[] FILENOTFOUND = {'F','i','l','e',' ','n','o','t',' ','f','o','u','n','d'}; is missing!
  • Rick 2008-04-11 08:12
    I see the WTF.
    public static final char[] NaN = {'N','a','N'};
    should be:
    public static final byte[] NaN = {'N','a','N'};
  • SlyEcho 2008-04-11 08:14

    switch(pstring.length)
    {
    case 1:
    return pstring[]PSTRING_SIZE_OFFSET] == 0;
    case 16 < 0:


    How does it even compile, can you switch on booleans and ints at the same time? It would work in C but in Java?

    Or is it just a typo?
  • Someone You Know 2008-04-11 08:16
    Oh My Lack Of God...
  • Someone You Know 2008-04-11 08:17
    SlyEcho:
    Or is it just a typo?


    Yes, it is; look at the other cases. It's probably meant to be 16 << 0, though why anyone would type such a thing is beyond me.
  • wtf 2008-04-11 08:17
    WTF?
  • Ben Blok 2008-04-11 08:19
    wtf:
    WTF?

    QFT
  • J. Walter Weatherman 2008-04-11 08:19
    That's brilliant as I drop a massive shit.
  • Christian Vogel 2008-04-11 08:32
    Vollhorst:
    public static final char[] FILENOTFOUND = {'F','i','l','e',' ','n','o','t',' ','f','o','u','n','d'};


    No, that would be {14,'F','i','l','e',' ','n','o','t',' ','f','o','u','n','d'}.
  • {'M','y',' ','N','a','m','e'} 2008-04-11 08:33
    So how often do they convert the MIN_INT_VALUE string to a number in a typical application, so they could compare other numbers to it?
  • Zombie_Hunter 2008-04-11 08:35
    My brain hurts! (Mmmm Braaains)
  • M 2008-04-11 08:37
    SlyEcho:
    How does it even compile, can you switch on booleans and ints at the same time? It would work in C but in Java? Or is it just a typo?
    You're right, the implicit boolean to integer conversion does not work in Java. And that "pstring[]PSTRING_SIZE_OFFSET]" part is also great.
  • Zied 2008-04-11 08:43
    WTF ?
  • anonymous 2008-04-11 08:48
    Why are chars [a-z] in in the char array intDigits? Is this being used for Base36?

    The rest of the WTFs just look like someone having no idea how to code... at least in Java (or probably any newer-generation languages). This has the earmarks of an old-school mainframe coder.
  • M.L. 2008-04-11 08:51
    It would be nice to know the context of this code.

    It may be a part of some kind simulation of abstract machine or processor where those constructs make much more sense.
  • Proko 2008-04-11 08:55
    It hurts, it physically hurts.
    Why, why would anyone do THAT???
  • Martin 2008-04-11 08:58
    M.L.:
    It would be nice to know the context of this code.

    It may be a part of some kind simulation of abstract machine or processor where those constructs make much more sense.


    Simulation of Brillant Paula's brain? Now it makes sense!
  • SlyEcho 2008-04-11 09:05
    Well if it had been written in C#, it would look much simpler because instead of

    private final static char[] intDigits =
    {'0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    'u' , 'v' , 'w' , 'x' , 'y' , 'z' };

    you can just write:

    private const string intDigits = "0123456789abcdefghijklmnopqrstuvwxyz";

    </Troll>
  • Ilya Ehrenburg 2008-04-11 09:06
    My eyes! The goggles do nothing!
    Really, that hurts.
  • Steve 2008-04-11 09:18
    anonymous:
    The rest of the WTFs just look like someone having no idea how to code... at least in Java (or probably any newer-generation languages). This has the earmarks of an old-school mainframe coder.
    Hey! I resemble that remark! (Started in the middle 1960s when punched cards were all the rage and we didn't need any newfangled text editors, green screens, or operating systems -- just put the compiler deck in the hopper, slap the source code deck on top, and press "START".)

    But, yeah, seriously, WTF? I'd really like to know the context of this project. Maybe it was a class assignment of some sort.
  • DOA 2008-04-11 09:24
    I don't suppose anyone could tell me what the hell this is supposed to do?
  • abigo 2008-04-11 09:35
    SlyEcho:
    Well if it had been written in C#, it would look much simpler because instead of

    private final static char[] intDigits =
    {'0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    'u' , 'v' , 'w' , 'x' , 'y' , 'z' };

    you can just write:

    private const string intDigits = "0123456789abcdefghijklmnopqrstuvwxyz";

    </Troll>

    could have been
    char[] thingie = "string".toCharArray();
  • diaphanein 2008-04-11 09:35
    This is purely an experiment in entropy.
  • y0y 2008-04-11 09:49
    I'd really like to comment something witty and intelligent, but clearly I lack the cerebral capabilities to fully appreciate and comprehend the genius at work here. Someday.. someday..
  • Mark 2008-04-11 10:16
    Even my code isn't that f*cked up.

    Seriously, as somebody who left programming eight years ago for a systems analyst role, I'd forgotten this simple truth about development in many corporate environments: Incompetence is immortal.

    What I mean by that is that in most other functions, when something is built/done by somebody who is truly incompetent it will usually be replaced or repaired. But when it happens in development (and bad code makes it into production) it takes on a life of its own because nobody wants to touch the code (and rightly so).
  • Reuben 2008-04-11 10:18
    In java you could write:

    private final static char[] intDigits = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();
  • Easter Bunny 2008-04-11 10:31
    Hmmm.

    I think this is the 14th Sign of the Apocalypse.
  • Tom Woolf 2008-04-11 10:36
    I wonder how many weeks he/she got off for worker's comp after their carpal tunnel surgery...
  • Fraggle My Rock 2008-04-11 10:39
    SlyEcho:
    Well if it had been written in C#, it would look much simpler because instead of

    private final static char[] intDigits =
    {'0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    'u' , 'v' , 'w' , 'x' , 'y' , 'z' };

    you can just write:

    private const string intDigits = "0123456789abcdefghijklmnopqrstuvwxyz";

    </Troll>


    Pffft. You're being silly.
  • SomeCoder 2008-04-11 10:39
    Ilya Ehrenburg:
    My eyes! The goggles do nothing!
    Really, that hurts.


    2nd.

    This is just... GAH!
  • Konrads 2008-04-11 10:46
    Just brilliant. This has got to be the best WTF: Programmer gave it a thought and dedicated considerable effort just to do something utterly wrong.
  • ubersoldat 2008-04-11 11:17
    But why? Oh my! Why? WTF?!? Please, someone slap this person out of programming. I mean, this person and ALL of his team mates, because someone had to read THAT on a CVS/SVN changes email.
  • MeRp 2008-04-11 11:26
    DOA:
    I don't suppose anyone could tell me what the hell this is supposed to do?


    Ok, keep in mind that I'm only going to give a brief description of what this is supposed to do, not a) what it actually does nor b) why they did it.

    In Pascal strings, instead of being null terminated (like in C e.g. c-strings), instead always have the length of the string as the first element of the array. This collection of what can only (and loosely at that) be referred to as "java statements" appears to be an attempt to implement this in java, perhaps so as to somehow interact with Pascal... for some reason.
  • Maks Verver 2008-04-11 12:27
    In Pascal strings, instead of being null terminated (like in C e.g. c-strings), instead always have the length of the string as the first element of the array.

    The irony of course, is that in Java an array of characters is the same thing.

    This collection of what can only (and loosely at that) be referred to as "java statements" appears to be an attempt to implement this in java, perhaps so as to somehow interact with Pascal...

    That almost make sense. Doesn't really explain the huge look-up tables though, and why the switch isn't just implemented like this:
    switch(pstring.length)
    
    {
    case 1:
    case 16 << 0:
    case 16 << 1:
    case 16 << 2:
    case 16 << 3:
    case 16 << 4:
    case 16 << 5:
    case 16 << 6:
    case 16 << 7:
    case 16 << 8:
    case 16 << 9:
    case 16 << 10:
    case 16 << 11:
    case 16 << 12:
    return pstring[PSTRING_SIZE_OFFSET] < pstring.length;
    default:
    return false;
    }


    Or even shorter (although a bit more complicated):
    int n = pstring.length;
    
    return (n == 1 || (n >= 16 && n <= 65536 && (n&(n-1)) == 0)) && pstring[0] < n;

    I don't see the point of making PSTRING_SIZE_OFFSET a constant if your code will only ever work if it is set to zero (the empty-string case).
  • FluffGrrl 2008-04-11 12:28
    Yo' C-pound ain't no array like tha Java be. Go home.
  • Smash 2008-04-11 12:29
    public static final char[] SMART_COMMENT = {'S','m','a','r','t',' ','C','o','m','m','e','n','t'};
  • operagost 2008-04-11 12:34
    Someone You Know:
    Oh My Lack Of God...

    Congrats, you're the theological version of this guy.
  • operagost 2008-04-11 12:35
    Steve:
    anonymous:
    The rest of the WTFs just look like someone having no idea how to code... at least in Java (or probably any newer-generation languages). This has the earmarks of an old-school mainframe coder.
    Hey! I resemble that remark! (Started in the middle 1960s when punched cards were all the rage and we didn't need any newfangled text editors, green screens, or operating systems -- just put the compiler deck in the hopper, slap the source code deck on top, and press "START".)

    So they not only looked like washing machines, but worked like them?
  • Smash 2008-04-11 12:44
    On regard to the DigitTens and DigitOnes arrays, the bloke obviously wanted to do something like this (I'll do it in C because it's been a while since I last touched any java):
    //current value of n = 73
    
    int n1 = DigitOnes[n]; // n1 = 3
    int n10 = DigitTens[n];// n10 = 7
    I wonder what will happen when the need to check for hundreds or even thousands arise. It probably will result in tendinitis
  • real_aardvark 2008-04-11 12:48
    Mark:
    Even my code isn't that f*cked up.

    Seriously, as somebody who left programming eight years ago for a systems analyst role, I'd forgotten this simple truth about development in many corporate environments: Incompetence is immortal.

    What I mean by that is that in most other functions, when something is built/done by somebody who is truly incompetent it will usually be replaced or repaired. But when it happens in development (and bad code makes it into production) it takes on a life of its own because nobody wants to touch the code (and rightly so).

    More to the point, this is a work of art. Would you re-shape the smile on the Mona Lisa? Would you paint a smiley face on Munch's "The Scream?" Would you edit the captions for Mandatory Fun Day?

    If I came across this in production code, I would refuse to edit it on aesthetic grounds (well, maybe retouch it so that it compiles, but that's it). I'd rather refactor all the other code that depends on it, to make inferior stuff work.
  • Nigel 2008-04-11 12:49

    I'm glad to see that MIN_INT_VALUE goes to ELEVEN!

    final static char[] MIN_INT_VALUE =
    
    { 11, '-', '2', '1', '4', '7', '4', '8', '3', '6', '4', '8' };


    Nigel: You see, most blokes will be playing at 10. You’re on 10, all the way up, all the way up...Where can you go from there? Nowhere. What we do, is if we need that extra push over the cliff...Eleven. One louder.

    DiBergi: Why don’t you just make 10 louder and make 10 be the top number, and make that a little louder?

    Nigel: These go to 11


    If I had to maintain this code I would definitely be looking for a cliff...
  • iddJoe 2008-04-11 12:53

    I don't suppose anyone could tell me what the hell this is supposed to do?


    Who cares? It's Pretty :)
  • Someone You Know 2008-04-11 12:55
    operagost:

    <bait tempting="false">
    Congrats, you're the theological version of this guy.
    </bait>


    Fixed that for you. ;)
  • Anthony 2008-04-11 13:02
    I like how each function is bracketed with }; because that's the face I make when I'm reading this WTF.
  • jimlangrunner 2008-04-11 13:09
    Oh.

    My.

    I tried gouging out mine eyes, and it didn't work. They still burn.

    Wait. No. Mace. Mace'll do that to you.

    Heed well, young Jedi: not even Mace can take away the pain of bad code.
  • DiBergi 2008-04-11 13:14
    Actually, the MIN_INT_VALUE doesn't go up to anything. They're using the 11 to tell you how many characters in the string '-2147483648', which is the minimum signed number in a 4 byte representation. That is why the other solutions about just initializing to a string won't work as this appears to have a requirement that the first 'char' actually be a number indicating how many more characters are in the string.

    That being said, my amp goes to 12...
  • Gumby 2008-04-11 13:38
    I hate it when I have to write code like that. I wish someone would invent a computer that could do arithmetic.
  • 0xbaadf00d 2008-04-11 13:50
    What kills me is that DigitTens and DigitOnes are only useful if indexed mod 100.

    If the table is only useful if indexed mod 100, can I assume the author's knowledge of mod 100?

    If the author knows how to mod 100, can I assume the author's knowledge of div 100?

    If the author knows how to mod and div, then why, oh, why the arrays of digits?
  • foo 2008-04-11 14:42
    operagost:
    Steve:
    anonymous:
    The rest of the WTFs just look like someone having no idea how to code... at least in Java (or probably any newer-generation languages). This has the earmarks of an old-school mainframe coder.
    Hey! I resemble that remark! (Started in the middle 1960s when punched cards were all the rage and we didn't need any newfangled text editors, green screens, or operating systems -- just put the compiler deck in the hopper, slap the source code deck on top, and press "START".)

    So they not only looked like washing machines, but worked like them?


    Not only that, but when your code didn't work you could literally shuffle it around until it did.

    In FORTRAN we always used the same variable names. That way, when you needed to add a line of code, you might already have it in one of your other programs so you could just dig through your old decks and find it. Saved waiting for a free 029.

    We could put a lot of FORTRAN WTFs up here, but it would be shooting fish in a barrel.
  • John Robo 2008-04-11 16:00
    Hey!
    I thought that *I* was the only one who re-used cards with variables to avoid waiting for a free 029!!!
  • iMalc 2008-04-11 16:12
    Oooh I found a bug:
    case 16 < 0:
    should be
    case 16 << 0:
  • CynicalTyler 2008-04-11 16:14
    Eyes myEyes = goggles.doNothing();
  • mister 2008-04-11 16:16
    iMalc:
    Oooh I found a bug:
    case 16 < 0:
    should be
    case 16 << 0:

    Actually, it's not an error. The original intention was "case 0:", but was replaced with "16 < 0" (evaluates to false, 0) so that it looks better.
  • Val 2008-04-11 16:41

    switch(pstring.length)
    
    {
    case 1:
    case 16 << 0:
    case 16 << 1:
    case 16 << 2:
    case 16 << 3:
    case 16 << 4:
    case 16 << 5:
    case 16 << 6:
    case 16 << 7:
    case 16 << 8:
    case 16 << 9:
    case 16 << 10:
    case 16 << 11:
    case 16 << 12:
    return pstring[PSTRING_SIZE_OFFSET] < pstring.length;
    default:
    return false;
    }




    It's sad you can't do that in C#. The break is mandatory :(
    (And that's nearly the only thing why I hate that language)

    How could they do it?? Why did they destroy Duff's device? Why, oh, why???

    [/whining]
  • Alistair Wall 2008-04-11 16:49
    Was this anonymised by transation from Pascal to C?
  • DirtyPunk 2008-04-11 17:29
    >It's sad you can't do that in C#. The break is mandatory :(

    Actually, you can fall through if a bunch of cases map to the same behavior:
    http://msdn2.microsoft.com/en-us/vcsharp/aa336815.aspx

    You can also use goto case, then cut your own throat and cry yourself to a gargling bloody death.
  • JB 2008-04-11 18:22
    DOA:
    I don't suppose anyone could tell me what the hell this is supposed to do?


    I'm pretty sure that the author doesn't know what the hell this is supposed to do
  • incoherent 2008-04-11 19:22
    Well, this is it, folks. We've found the real WTF. We should all just go home now.
  • mxsscott 2008-04-11 19:22
    My jaw just dropped.

    I do not understand how anyone could ever think such code was necessary. It is just lunacy. Do they think that everyone has to do that if they use the language?

    Definitely a case of fingers hitting the keys and looking at the screen but not much going on behind the eyes.
  • fraserofthenight 2008-04-11 19:24
    Java's String class is 24 bytes larger than a char[] array on a 32-bit processor (4 for object monitor, 4 for vtbl pointer, 4 for array reference, 4 for start position in array, 4 for end position in array, 4 because JVMs like to align stuff on 8-byte word boundaries). This means that in Java, this line of code:
    private static String s = "";
    takes up 40 bytes (likely 72 on a 64-bit processor). In my open-source (Eclipse-based) Java project, I've been slowly changing the codebase over from using Strings to using char[]s and some of the code is actually starting to look like this WTF.
  • fraserofthenight 2008-04-11 19:27
    Oops, the empty string is actually 44 bytes of memory. Forgot to add space for the reference itself.
  • mxsscott 2008-04-11 19:28
    ubersoldat:
    But why? Oh my! Why? WTF?!? Please, someone slap this person out of programming. I mean, this person and ALL of his team mates, because someone had to read THAT on a CVS/SVN changes email.


    Hey, some of us still have to use RCS.
  • protected static 2008-04-11 19:52

    It's sad you can't do that in C#. The break is mandatory :(


    Psst. Those are 'return' statements. A break would be superfluous. It'll still compile, but you'll get a warning about unreachable code.
  • fred 2008-04-11 21:11
    Congrats for the n&(n-1)==0. And I was just reading draft of the 4th AOCP on bit manipulations...
  • fred 2008-04-11 21:20
    Val:

    switch(pstring.length)
    
    {
    case 1:
    case 16 << 0:
    .../...
    case 16 << 12:
    return pstring[PSTRING_SIZE_OFFSET] < pstring.length;
    default:
    return false;
    }




    It's sad you can't do that in C#. The break is mandatory :(
    (And that's nearly the only thing why I hate that language)

    How could they do it?? Why did they destroy Duff's device? Why, oh, why???

    [/whining]


    That is not Duff's device. Duff's device is intermixing while and case loops:

    int n = (count+7)/8;
    switch (count%8)
    {
    case 0: do { *to = *from++;
    case 7: *to = *from++;
    case 6: *to = *from++;
    case 5: *to = *from++;
    case 4: *to = *from++;
    case 3: *to = *from++;
    case 2: *to = *from++;
    case 1: *to = *from++;
    } while (--n>0);

    (notice how the do starts inside the 'case 0')
  • guest 2008-04-11 21:42
    So they were passing numbers from pascal as strings and didn't want them to go on the heap?
  • Jeff 2008-04-11 22:13
    Oh really?
  • PseudoNoise 2008-04-12 00:55
    I've seen something similar to the powers of 10 that actually made sense -- on an embedded processor, had to do fast saturation inside the tightest loop (signal processing), and it turned out it was faster to index an array than put in the saturation logic.

    But not something to be done lightly. I doubt there was that good a reason here.
  • Zorkon II 2008-04-12 02:11
    If The water leaked from it, it cost a lot more to fix, though.
  • Zorkon II 2008-04-12 02:14
    [quote user="operagost"][quote user="Steve"][quote user="anonymous"]The rest of the WTFs just look like someone having no idea how to code... at least in Java (or probably any newer-generation languages). This has the earmarks of an old-school mainframe coder.[/quote]Hey! I resemble that remark! (Started in the middle 1960s when punched cards were all the rage and we didn't need any newfangled text editors, green screens, or operating systems -- just put the compiler deck in the hopper, slap the source code deck on top, and press "START".)
    [/quote]
    So they not only looked like washing machines, but worked like them?[/quote

    If the water leaked out of them, it cost a lot more, though.
  • Epaminaidos 2008-04-12 05:34
    Our whole productive code is based on char-arrays since some years ago a designer meant that Strings are just too slow.

    BTW: Index-Checking in arrays was too slow for him as well:

    int indexOf(char[] text, char search) {
    int i = 0;
    try {
    while(true) {
    if (char[i] == search())
    return i;
    i++;
    }
    } catch (ArrayIndexOutOfBoundsException e) {
    return -1;
    }
    }
  • Rob 2008-04-12 06:49
    Well, that's a blast from the past - writing this is probably a mistake (assuming that most people here want to "burn the witch") but I think it's worth putting some context in for posterity, it also helps explain what looks like insane code. (You can of course still disagree with the reasoning we used for implementing it).

    The year: 2002/2003ish

    Imagine a real-time distributed system, hundreds of machines, each with several processes on them. The processes talk to each other via socket connections using a highly optimised low-latency message passing library.

    In general you could pass data around with this library (which did it in a binary format) with no problem at all as the data tended to be doubles and ints, but every now and then there was the need to pass strings around. The library happilly handled strings, but the receiving process would have to allocate memory for the incoming string. There was also a lot of wasted memory form int/double to string conversion.

    Remember, this was a realtime system, and at the bleeding edge of JVM use (JRocket from memory). Garbage Collection ergonomics weren't as good as they are nowadays - this was a problem because if the GC kicked in, it stopped the world. Bad news for a real-time system.

    So, passing primitives between processes was fine as there was practically no memory consumed by doing so (socket overheads aside).

    The senior architect decided that we needed to find a way to stop wasting memory on transient strings and string manipulations. Hence PStrings were born. (passing char[]s around instead of Strings).

    So, where are we:
    1. Stop using Java Strings, start using char array backed 'pstrings' and keep a pool of suitable char arrays kicking around. Reuse the pre-existing char[]s as much as you can to avoid memory growth.
    2. A need to convert doubles and ints into Pstrings, as you can no longer use Java's built in String converters/parsers (take a look, they're great, but they do need to allocate memory).

    As I wrote the PString library you'd think I'd recognise isPString, but I don't. Looking at it here it looks plain wrong so I can only assume it has been modified since, or something is missing. Perhaps it's been refactored since originally developed and needs further cleaning up? I expect the JIT optimizes it down so it's not a performance hit, but the code's rubbish. I'm also surprised at the lack of comments, I tend to comment heavily, especially for something unclear.

    All the other code in the WTF relates to number to string representation conversion, which you'll find dotted around the web and in Sun's Java source code. I used (with permission) code from the following book I believe:
    http://jlbtc.eduunix.cn/index/html/java/O'Reilly%20-%20Java.Performance.Tuning.2nd.Edition.eBook-LiB/0596003773_javapt2-chp-5-sect-3.html
    There were a couple of bugs in it which we helped fix, but as you can see, efficient double->string conversion is not a simple task, there are lots of look-up tables used to speed things up.

    This appears to be someone else who has used the same library:
    http://www.koders.com/java/fid9B2B30D4084F831F061F3440EB40815111402793.aspx?s=mdef%3Ainsert

    Finally, let's pose a few questions

    Q. Are all of those lookup tables and crazy looking 'NaN' and 'Infinity' char[] wrong?
    A. No, that's how you do high-speed number to string conversion. For speed, look-up tables can be your friends whilst sacrificing memory.

    Q. Are they worthy of a WTF
    A. Hell yeah. I scratched my head fixing a bug in the code from the book for quite a while. It would have taken me months to write one from scratch, and it wouldn't have been nearly as efficient.

    Q. Are 'PStrings' a good idea?
    A. Back then, after repeated analysis with OptimizeIt and JProbe against our processes, I'd say it was the right thing to do. Nowadays, and having talked to Brian Goetz about modern day GCing, I'd say almost definitely not. JVMs have moved on and the GC is much improved nowadays.

    Q. Am I sure it's my code?
    A. It might not be, but I definitely coined PStrings back in 2003 for our project, and it supported number<->string conversion. Let's be honest though, it's not exactly the most creatively named library (Hmm, pascal style strings, what could we call it?).

    Cheers,
    Rob
  • holli 2008-04-12 08:27
    note to self: whenever my code looks like ascii art, there's something wrong with it.
  • Anon 2008-04-12 09:54
    SlyEcho:
    Well if it had been written in C#, it would look much simpler because instead of

    private final static char[] intDigits =
    {'0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    'u' , 'v' , 'w' , 'x' , 'y' , 'z' };

    you can just write:

    private const string intDigits = "0123456789abcdefghijklmnopqrstuvwxyz";

    </Troll>


    You can do the same in Java.
  • real_aardvark 2008-04-12 12:49
    Epaminaidos:
    Our whole productive code is based on char-arrays since some years ago a designer meant that Strings are just too slow.

    BTW: Index-Checking in arrays was too slow for him as well:

    int indexOf(char[] text, char search) {
    int i = 0;
    try {
    while(true) {
    if (char[i] == search())
    return i;
    i++;
    }
    } catch (ArrayIndexOutOfBoundsException e) {
    return -1;
    }
    }
    What???

    Strings (unmeasured) are too slow for him, exceptions (unmeasured) are perfectly fast enough, yet Java (unmeasured) was a speedy enough solution for his problems?

    How many pills did you have to feed this guy daily? And of what kind?

    I'm assuming that "productive" is a typo here.
  • real_aardvark 2008-04-12 13:17
    Rob:
    Well, that's a blast from the past - writing this is probably a mistake (assuming that most people here want to "burn the witch") but I think it's worth putting some context in for posterity, it also helps explain what looks like insane code. (You can of course still disagree with the reasoning we used for implementing it).

    The year: 2002/2003ish

    <huge snip> with apologies, but the original is directly above</huge snip>

    Cheers,
    Rob
    This is actually fascinating, and explains a lot. Unfortunately, it glosses over why anybody would choose to implement a real-time system in Java in 2002/3. AFAIR, the early versions of Java used to have a huge warning on them from Sun, pointing out that "the use of the language in a real-time system is not supported and is heavily discouraged."

    In fact, pace JSR001, I can't see a single good (or even justifiable) reason to implement a real-time system in Java in the foreseeable future. There are plenty of good reasons not to do so:

    (1) It requires a JVM. Huge memory footprint, which may or may not be a problem. Plus, JIT solutions can approach "native code" solutions asymptotically (and I'm a huge fan of Python's Psyco), but you're still giving up on the essentials for real-time programming (which Rob mentions).
    (2) The threading model. Say no more. (Although I'm sure that JSRxxx does.)
    (3) Java is not a natural language for real-time programmers. I have a lot of sympathy for people who say that C++ isn't either (although I'd argue that it's much closer), and therefore I'd expect to see real-time programs written in C, or maybe Forth, or a home-brew equivalent. I'd imagine that learning the discipline to program in real-time Java would be painful and would provide no obvious benefit.
    (4) Leaving the language to one side, if you insist on hiring a team to program real-time Java, you're going to have a huge problem with libraries. Now, I don't like Java's libraries at all, but that's not the point. If you hire a Java programmer to write a real-time system, they are going to use them -- and why shouldn't they? It's part of their natural environment. They are, in fact, Doing The Right Thing. Unfortunately, Java libraries (Sun- or Third-Party) are not designed to be real-time. This way lies madness.

    Arguing about whether Java can, or should, be used as a real-time language is fun, and might lead to innovative high-level solutions (such as switching the goddamn garbage collector off altogether).

    Arguing about low-level crap like string-to-int conversion makes sense in a C environment, but none whatsoever in a Java environment. It doesn't work at that level. Why the hell do you need to convert between the two in the first place, in a real-time system?

    This whole thing (and I think Rob's solution is pretty neat, under the circumstances) appears to me to be the insane result of so-called architects with a preposterous political mission, no doubt mandated by some idiot in Marketing who wants to be the first to announce a Java Real Time System to the world.

    There are, obviously, better ways to do it.
  • curtmack 2008-04-12 20:50
    SlyEcho:
    Well if it had been written in C#, it would look much simpler because instead of

    private final static char[] intDigits =
    {'0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    'u' , 'v' , 'w' , 'x' , 'y' , 'z' };

    you can just write:

    private const string intDigits = "0123456789abcdefghijklmnopqrstuvwxyz";

    </Troll>


    Wouldn't have to be C#, C++ can do that too.

    </pointless observation>
  • Rob 2008-04-13 02:05
    real_aardvark

    The industry within which I work takes a laissez-faire approach to terminology. Hard/soft real-time are not typically understood and are used to mean 'high-performance' regularly, a mistake I made in my reply above (my audience differs here). This was very much a high-performance system, and not a real-time one. Apologies for the confusion.

    Genuine real-time systems are a completely different ballgame, and fascinating in their own right.
  • SpamBot 2008-04-13 08:34
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaarghhhhhhhhhhhhh!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  • Ilya Ehrenburg 2008-04-13 09:02
    Sometimes there's a good reason for inflicting pain. And although I cannot really judge whether your reasons were good enough, I now believe they were.
    It's still terribly ugly and I hope I will never have the need to do anything remotely like it, though.
  • real_aardvark 2008-04-13 10:10
    Rob:
    real_aardvark

    The industry within which I work takes a laissez-faire approach to terminology. Hard/soft real-time are not typically understood and are used to mean 'high-performance' regularly, a mistake I made in my reply above (my audience differs here). This was very much a high-performance system, and not a real-time one. Apologies for the confusion.

    Genuine real-time systems are a completely different ballgame, and fascinating in their own right.

    I don't think you need to apologise for the confusion at all; after all, you took the trouble to post a pretty lucid explanation of why this code was created.

    The people who need to be burnt at the stake^W^W^W^W^W confess their sins are, as usual, the marketing department. I've lost track of the number of idiots who proclaim that a VB.Net or WinCE system is "real-time." Presumably, some defenceless purchasers out there actually believe it.

    The only justification for the OP, in other words, is, as you say, "high performance." It sounds like you put a lot of thought into this. I just have this horrible suspicion that the "architects" in charge failed to profile the system in a realistic setting, or (at a lower level) to minimise the type clashes across function boundaries. High-performance does not imply bit-headed coding (although this is all too often mandated by those in charge).

    I stand by my comment that the result is a thing of beauty, however, and I wouldn't change it for the world...
  • St. Matty 2008-04-13 18:56
    private final static char[] intDigits = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();

    Anything C# can do, Java can do with only a geometric increase in stack depth.
  • Spybob 2008-04-14 01:13
    Yep.. C# is the way to go.. You wouldn't even have to make things this complicated, because no matter what you do, the program is dog slow and basically useless :-}
  • anonymous 2008-04-14 11:05
    tl;dr
  • Cellar 2008-04-15 11:21
    Yes. Yes! YES! YEEEEEESSS!!!!!1!111!1!

    BUZZGAZM! Oh yeah baby, give it to me once more!

    B U Z Z G A Z Z M !

    Massively Distributed RealTime Java!

    Now if that ain't Rocket Science... I'll burst the dotcom bubble myself!

    Oh wait.
  • tedbilly 2008-04-15 17:57
    Well Rob's last comment was excellent. Every once and awhile there are cases for exceptions to the rule (for example using goto in classic C).

    In today's times yes that looks like questionable code, but at that moment in time, it was likely the best choice.

    I don't know how many of you do game programming but they sometimes have to resort to tricks like this because CPU cycles are more valuable than RAM or disk space.
  • Mike 2008-04-16 01:34
    uh, oh. You seem to have shut up the trolls.
  • Simmo 2008-04-21 04:00
    Nigel:

    I'm glad to see that MIN_INT_VALUE goes to ELEVEN!

    final static char[] MIN_INT_VALUE =
    
    { 11, '-', '2', '1', '4', '7', '4', '8', '3', '6', '4', '8' };


    Nigel: You see, most blokes will be playing at 10. You’re on 10, all the way up, all the way up...Where can you go from there? Nowhere. What we do, is if we need that extra push over the cliff...Eleven. One louder.

    DiBergi: Why don’t you just make 10 louder and make 10 be the top number, and make that a little louder?

    Nigel: These go to 11


    If I had to maintain this code I would definitely be looking for a cliff...


    Made my day... Thanks for that
  • 111 2008-11-10 07:33
    情趣用品情趣用品情趣用品情趣用品情趣情趣情趣
    情趣按摩棒跳蛋充氣娃娃
    情境坊歡愉用品[/url[url=http://www.show688.net/shop/index.php?wid=twjason]情趣用品情人節禮物情惑用品性易構免費A片AV女優美女視訊情色交友免費AV色情網站辣妹視訊美女交友色情影片成人影片成人網站A片H漫18成人成人圖片成人漫畫情色網日本A片免費A片下載性愛成人交友嘟嘟成人網成人電影成人成人貼圖成人小說成人文章成人圖片區免費成人影片成人遊戲微風成人愛情公寓情色情色貼圖情色文學做愛色情聊天室色情小說一葉情貼圖片區情色小說色情寄情築園小遊戲色情遊戲情色視訊情色電影AIO交友愛情館言情小說愛情小說色情A片情色論壇色情影片視訊聊天室免費視訊聊天免費視訊視訊美女視訊交友視訊聊天免費視訊聊天室AIOA片下載AV[url=http://www.ing.net/042]AV片A漫AV DVDAV成人網聊天室成人論壇本土自拍自拍A片


  • bmorayta 2008-12-05 10:26
    Rob:
    Remember, this was a realtime system, and at the bleeding edge of JVM use (JRocket from memory).

    Sorry if my opinion releases some dark rage from the Java advocates, but TRWTF is using Java (or ANY interpreted language, for that matter) for a Real Time System...
  • cindy 2010-12-21 09:03