Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

java switch

Name: Anonymous 2011-10-20 11:32

Why does java not allow String objects to be used in the switch/case statements? Couldn't the compile invoke hashcode() on those strings thus producing a unique integer value to represent the constant in the cases?

Name: Anonymous 2011-10-21 6:56

As promised here is my C string switch:


/*
strswitch.h
Written in 2004 by Anonymous.
ENTERPRISE-GRADE C
*/

#ifndef _STRSWITCH_H
#define _STRSWITCH_H

#include <stdlib.h>
#include <string.h>

void *strswitch_var;
int   strswitch_loop;
int   strswitch_enter;

int strswitch_match(char *s) {
  char *p;
  if(strswitch_enter)
    return 1;
  if(*(char**)strswitch_var) {
    if(*s=='"') {
      p = malloc(strlen(s)+1);
      if(!p)
        return 0;
      strcpy(p++,s);
      *strchr(p,'"') = 0;
      strswitch_enter = !strcmp(p,*(char**)strswitch_var);
      free(p-1);
    }
  }
  else
    strswitch_enter = !strncmp(s,"NULL",4);
  return strswitch_enter;
}

#define strswitch(var)  strswitch_var = &var;\
                        strswitch_enter = 0;\
                        for(strswitch_loop=1;strswitch_loop;strswitch_loop=0)

#define case(c)         if(strswitch_match(#c))

#define default()       strswitch_enter = 1;

#endif


Sample usage:


#include <stdio.h>
#include "strswitch.h"

int main(int argc, char **argv) {
  strswitch(argv[1]) {
    case(NULL) {
    }
    case("--help") {
      printf("usage:\n");
      break;
    }
    case("--version") {
      printf("%s 0.1\n",argv[0]);
      break;
    }
    default() {
      printf("non option\n");
      break;
    }
  }

  return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List