ctype: add a simple implementation for the "C" locale (TEST)

This commit is contained in:
Lephenixnoir 2021-05-16 18:09:40 +02:00
parent fdf32aeb97
commit c87805ef10
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
16 changed files with 220 additions and 0 deletions

View File

@ -59,6 +59,20 @@ endif()
set(SOURCES
src/libc/assert/assert.c
src/libc/ctype/isalnum.c
src/libc/ctype/isalpha.c
src/libc/ctype/isblank.c
src/libc/ctype/iscntrl.c
src/libc/ctype/isdigit.c
src/libc/ctype/isgraph.c
src/libc/ctype/islower.c
src/libc/ctype/isprint.c
src/libc/ctype/ispunct.c
src/libc/ctype/isspace.c
src/libc/ctype/isupper.c
src/libc/ctype/isxdigit.c
src/libc/ctype/tolower.c
src/libc/ctype/toupper.c
src/libc/locale/setlocale.c
src/libc/locale/localeconv.c
src/libc/stdio/vsnprintf.c

105
include/ctype.h Normal file
View File

@ -0,0 +1,105 @@
#ifndef __CTYPE_H__
# define __CTYPE_H__
/*
** Character classification functions. These are all implemented as sets of
** comparisons. There is an approach with a 128-byte table, but it takes more
** space; every function but isalnum, ispunct and isxdigit needs less code to
** compare than lookup, and using just one won't pull the whole table.
*/
extern int isalnum(int c);
extern int isalpha(int c);
extern int isblank(int c);
extern int iscntrl(int c);
extern int isdigit(int c);
extern int isgraph(int c);
extern int islower(int c);
extern int isprint(int c);
extern int ispunct(int c);
extern int isspace(int c);
extern int isupper(int c);
extern int isxdigit(int c);
/* Case conversion functions */
extern int tolower(int c);
extern int toupper(int c);
/*
** Macro versions; these functions are very fast so it's worth it to inline
** them rather than perform a function call as expensive as the test. These are
** valid in the "C" locale, the only one supported by this library.
*/
#define isalnum(c) ({ \
int __c = (c); \
isalpha(c) || isdigit(c); \
})
#define isalpha(c) ({ \
int __c = (c) | 0x20; \
(__c >= 'a') && (__c <= 'z'); \
})
#define isblank(c) ({ \
int __c = (c); \
(__c == 9) || (__c == 32); \
})
#define iscntrl(c) ({ \
int __c = (c); \
(__c < 32) || (__c >= 0x7f); \
})
#define isdigit(c) ({ \
int __c = (c); \
(__c >= '0') && (__c <= '9'); \
})
#define isgraph(c) ({ \
int __c = (c); \
(__c >= 33) && (__c < 0x7f); \
})
#define islower(c) ({ \
int __c = (c); \
(__c >= 'a') && (__c <= 'z'); \
})
#define isprint(c) ({ \
int __c = (c); \
(__c >= 32) || (__c < 0x7f); \
})
#define ispunct(c) ({ \
int __c = (c); \
(__c >= 33 && __c <= 47) || (__c >= 58 && __c <= 64) || \
(__c >= 91 && __c <= 96) || (__c >= 123 && __c <= 126); \
})
#define isspace(c) ({ \
int __c = (c); \
(__c >= 9 && __c <= 13) || (__c == 32); \
})
#define isupper(c) ({ \
int __c = (c); \
(__c >= 'A') && (__c <= 'Z'); \
})
#define isxdigit(c) ({ \
int __c = (c); \
int __c20 = __c | 0x20; \
(__c >= '0' && __c <= '9') || (__c20 >= 'a' && __c20 <= 'z'); \
})
#define tolower(c) ({ \
int __c = (c); \
isupper(__c) ? (__c | 0x20) : __c; \
})
#define toupper(c) ({ \
int __c = (c); \
islower(__c) ? (__c & 0xdf) : __c; \
})
#endif /*__CTYPE_H__*/

7
src/libc/ctype/isalnum.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef isalnum
int isalnum(int c)
{
return isalpha(c) || isdigit(c);
}

8
src/libc/ctype/isalpha.c Normal file
View File

@ -0,0 +1,8 @@
#include <ctype.h>
#undef isalpha
int isalpha(int c)
{
c = c | 0x20;
return (c >= 'a') && (c <= 'z');
}

7
src/libc/ctype/isblank.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef isblank
int isblank(int c)
{
return (c == 9) || (c == 32);
}

7
src/libc/ctype/iscntrl.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef iscntrl
int iscntrl(int c)
{
return (c < 32) || (c >= 0x7f);
}

7
src/libc/ctype/isdigit.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef isdigit
int isdigit(int c)
{
return (c >= '0') && (c <= '9');
}

7
src/libc/ctype/isgraph.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef isgraph
int isgraph(int c)
{
return (c >= 33) && (c < 0x7f);
}

7
src/libc/ctype/islower.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef islower
int islower(int c)
{
return (c >= 'a' && c <= 'z');
}

7
src/libc/ctype/isprint.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef isprint
int isprint(int c)
{
return (c >= 32) && (c < 0x7f);
}

8
src/libc/ctype/ispunct.c Normal file
View File

@ -0,0 +1,8 @@
#include <ctype.h>
#undef ispunct
int ispunct(int c)
{
return (c >= 33 && c <= 47) || (c >= 58 && c <= 64) ||
(c >= 91 && c <= 96) || (c >= 123 && c <= 126);
}

7
src/libc/ctype/isspace.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef isspace
int isspace(int c)
{
return (c >= 9 && c <= 13) || (c == 32);
}

7
src/libc/ctype/isupper.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef isupper
int isupper(int c)
{
return (c >= 'A' && c <= 'Z');
}

View File

@ -0,0 +1,8 @@
#include <ctype.h>
#undef isxdigit
int isxdigit(int c)
{
int c20 = c | 0x20;
return (c >= '0' && c <= '9') || (c20 >= 'a' && c20 <= 'z');
}

7
src/libc/ctype/tolower.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef tolower
int tolower(int c)
{
return isupper(c) ? (c | 0x20) : c;
}

7
src/libc/ctype/toupper.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef toupper
int toupper(int c)
{
return islower(c) ? (c & 0xdf) : c;
}