libc: ctype

Next up the libc implementation list are the ctype functions. The ctype functions are pretty simple, covering functions like islower and isalpha. These functions also have "wide" equivalents for increased character sizes (like UTF), but I have just ported the standard versions for now.


I ported the ctype.h header from musl libc. I removed the locale and wide functions, since I do not currently need this support.

I also removed the conditional definitions at the bottom of the file. These definitions allow you to choose between function or macro implementations. For now I will use functions.

Making the modifications mentioned, this is my resulting ctype.h header:

#ifndef    _CTYPE_H
#define    _CTYPE_H

#ifdef __cplusplus
extern "C" {

int   isalnum(int);
int   isalpha(int);
int   isblank(int);
int   iscntrl(int);
int   isdigit(int);
int   isgraph(int);
int   islower(int);
int   isprint(int);
int   ispunct(int);
int   isspace(int);
int   isupper(int);
int   isxdigit(int);
int   tolower(int);
int   toupper(int);

#ifdef __cplusplus


Function Implementations

The following functions were pulled directly from musl libc:

  • isalnum
  • isalpha
  • isblank
  • iscntrl
  • isdigit
  • isgraph
  • islower
  • isprint
  • ispunct
  • isspace
  • isupper
  • isxdigit
  • tolower
  • toupper

The function implementations can be found in src/ctype/ in the musl library.

These functions are relatively simple and were all directly imported. The only modifications I made were the following:

  • Removed #include "libc.h"
  • Removed #undef lines (since I am not using the macros)
  • Removed locale support

To show an example of what I mean, let's look at a before/after. Here's the original musl implementation of isalnum:

#include <ctype.h>
#include "libc.h"

int isalnum(int c)
    return isalpha(c) || isdigit(c);

int __isalnum_l(int c, locale_t l)
    return isalnum(c);

weak_alias(__isalnum_l, isalnum_l);

After my modifications, the result is now simply:

#include <ctype.h>

int isalnum(int c)
    return isalpha(c) || isdigit(c);

Putting it All Together

You can find my ctype.h header implementation and ctype function implementations in the embedded-resources github repository;