Skip to content

Latest commit

 

History

History
74 lines (65 loc) · 3.07 KB

strcase.md

File metadata and controls

74 lines (65 loc) · 3.07 KB

short switch switches in C: strcase

what

A practical support for Multiway branches (switch) for short strings in C.

why

The switch C statement encodes the multiway branch in C on integer values.

  switch(value) {
    case 1: printf("1\n"); break;
    case 2: printf("2\n"); break;
    case 3: printf("3\n"); break;
  }

The switch statement does not support strings. So a multiway branch on string is usually encoded as follows:

  if (strcmp(strvalue, "one") == 0)
    printf("1\n");
  else if (strcmp(strvalue, "two") == 0)
    printf("2\n");
  else if (strcmp(strvalue, "three") == 0)
    printf("3\n");

Using strcase, multipath branches on strings up to 8 characters can be programmed in a readable (and fast) way. The example here above becomes:

  switch(strcase(strvalue)) {
    case STRCASE(o,n,e): printf("1\n"); break;
    case STRCASE(t,w,o): printf("2\n"); break;
    case STRCASE(t,h,r,e,e): printf("3\n"; break;
  }

how

The project strcase is entirely implemented in a C header file.

  • strcase (lowercase) is an inline function that encodes the string as a 64 bits unsigned integer.
  • STRCASE (uppercase) is a C preprocessor macro that is converted in the 64 bits integer constant using the same algorithm of strcase.

STRCASE argument cannot be provided as a string as the preprocessor doesnt support (yet?) a way to loop over all the characters of a string. The string must be provided char by char, using commas. The result is still readable and it is easy to add cases or change the tags.

  • strcase is fast (faster than using strcmp). There is only one linear scan of the string at run time (done by strcase to translate the string to an integer value). The switch statement compares the result of strcase with integer constants.
  • strcase is endianess neutral. Constants generated by STRCASE can be exchanged between machines having different endianess.
  • strcase maps strings composed by a single character to the ascii value of the character itself STRCASE(a) == 'a'.
  • For strings 8 characters long or more, strcase and STRCASE convert the first 8 characters. All strings having the same 8 characters prefix are converted to the same integer value.
  • alphanumerical characters and underscore (_) can be used in STRCASE. Other symbols can be inserted using their name, e.g. STRCASE(slash,e,t,c,slash) or STRCASE(a,comma,b,comma,c).
  • strcase is a practical alternative to the deprecated multi-char constants.

where

There is a specific project. Strcase has been used in many modules of vuos because it is a convenient way to parse option strings or user input (e.g. cmd/vustack.c, vubinfmt/vubinfmt.c, vudev/vudev.c, vudev_modules/vudevramdisk.c, vufs/vufs.c, vufs/vufstat.c, vufuse/vufuse_startmain.c). We plan to extend the usage of strcase to other virtualsquare projects.

wish

We would need a new preprocessor feature supporting some processing of string arguments of macros. The challenge is to provide an API for the macro like STRCASE("one").