---
name: "strcase"
section: "3"
platform: "linux"
package: "strcase-dev"
version: "0.1-3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/strcase"
---

# strcase(3)

Multiway branch (switch) for short strings

## NAME

STRCASE, strcase, strcase\_tolower - Multiway branch (switch) for short strings

## SYNOPSIS

**#include \<strcase.h\>**\
 **STRCASE(***...***)**\
 **static inline uint64\_t strcase(const char \****s***);**\
 **static inline uint64\_t strcase\_tolower(const char \****s***);**

## DESCRIPTION

**strcase.h** provides two inline functions **strcase**, **strcase\_tolower**  and a macro **STRCASE**, all of them convert a *string* in a **uint64\_t** integer value. Only the *first 8 characters* are significative.

**strcase\_tolower** works like **strcase** but it converts uppercase letters as if they were lowercase.

**strcase** or **strcase\_tolower** can be used in the expression of a switch statement to convert a string to an integer type, **STRCASE** generates at compile time the integer constants for the *case* stanzas of the switch.

Actually **STRCASE** (C language):

- \*

  requires the string to be specified as comma separated characters,

- \*

  supports "strings" using only literals, digits and underscore. Other symbols can appear using their name, as in:

  ```
  STRCASE(slash, e, t, c)
  ```

-

  Regardless of its limitations this macro library is quite useful to write switch statements in C language using strings (almost) as if it were integers (as in the example below). Although **STRCASE** needs commas between characters the string is still readable, and it is simple to add cases or change the tags.

  When the string contains only one char, the value of strcase is the code of the character (e.g. the value of **strcase("a")** as well as the value of **STRCASE(a)** is **'a'**)

  Strcase is a portable alternative to multi-character constants in C.

  Strcase is available for C++ users, too. STRCASE argument in C++ is a short string.

## EXAMPLE

The following C program shows the use of **strcase**:

```
     #include <stdio.h>
     #include <strcase.h>
     int yes_or_not(const char *s) {
       switch (strcase_tolower(s)) {
         case STRCASE(y,e,s):
         case 'y':
             return 1;
         case STRCASE(n,o):
         case 'n':
             return 0;
         default:
             return -1;
       }
     }
     int main(int argc,char *argv[]) {
       for(argc--, argv++; argc > 0; argc--, argv++)
         printf("%s %d\n", *argv, yes_or_not(*argv));
     }
```

The same example can be translated in C++ as follows:

```
     #include <iostream>
     #include <strcase.h>
     using namespace std;
     int yes_or_not(const char *s) {
       switch (strcase_tolower(s)) {
         case STRCASE("yes"):
         case 'y':
             return 1;
         case STRCASE("no"):
         case 'n':
             return 0;
         default:
             return -1;
       }
     }
     int main(int argc,char *argv[]) {
       for(argc--, argv++; argc > 0; argc--, argv++)
         cout << *argv << " " << yes_or_not(*argv) << endl;
     }
```

## BUGS

Bug reports should be addressed to \<info@virtualsquare.org\>

## AUTHORS

Renzo Davoli \<renzo@cs.unibo.it\>
