| 1 |
/* |
|---|
| 2 |
* Written by Don Clugston |
|---|
| 3 |
* Placed into the Public Domain |
|---|
| 4 |
*/ |
|---|
| 5 |
/** |
|---|
| 6 |
* Simple ASCII character classification functions. Compile-time equivalents of std.ctype |
|---|
| 7 |
* All these functions return false if presented with a non-ASCII character. |
|---|
| 8 |
*/ |
|---|
| 9 |
module meta.ctype; |
|---|
| 10 |
// Naive implementation. This works fine, because it is only executed at compile time. |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
template isalnum(dchar c) |
|---|
| 14 |
{ |
|---|
| 15 |
static if (isalpha!(c) || isdigit!(c)) |
|---|
| 16 |
const bool isalnum=true; |
|---|
| 17 |
else const bool isalnum=false; |
|---|
| 18 |
} |
|---|
| 19 |
|
|---|
| 20 |
template isalpha(dchar c) |
|---|
| 21 |
{ |
|---|
| 22 |
static if ((c>='A' && c<='Z') || (c>='a' && c<='z')) |
|---|
| 23 |
const bool isalpha=true; |
|---|
| 24 |
else const bool isalpha=false; |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
template iscntrl(dchar c) |
|---|
| 28 |
{ |
|---|
| 29 |
static if (c<0x20 || c==0x7F) const bool iscntrl=true; |
|---|
| 30 |
else const bool iscntrl = false; |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
template isdigit(dchar c) |
|---|
| 34 |
{ |
|---|
| 35 |
static if (c>='0' && c<='9') const bool isdigit=true; |
|---|
| 36 |
else const bool isdigit = false; |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
template isspace(dchar c) |
|---|
| 40 |
{ |
|---|
| 41 |
static if (c==' ' || (c>=0xA && c<=0xD) ) const bool isspace=true; |
|---|
| 42 |
else const bool isspace=false; |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
template isupper(dchar c) |
|---|
| 46 |
{ |
|---|
| 47 |
static if (c>='A' && c<='Z') const bool isupper=true; |
|---|
| 48 |
else const bool isupper=false; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
template islower(dchar c) |
|---|
| 52 |
{ |
|---|
| 53 |
static if (c>='a' && c<='z') const bool islower=true; |
|---|
| 54 |
else const bool islower=false; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
template ispunct(dchar c) |
|---|
| 58 |
{ |
|---|
| 59 |
static if (c>' ' && c<0x7F && !isdigit!(c) && !isalpha!(c) ) const bool ispunct=true; |
|---|
| 60 |
else const bool ispunct=false; |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
template isxdigit(dchar c) |
|---|
| 64 |
{ |
|---|
| 65 |
static if (isdigit!(c) || (c>='a' && c<='f') || (c>='A' && c<='F')) const bool isxdigit=true; |
|---|
| 66 |
else const bool isxdigit=false; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
template isgraph(dchar c) |
|---|
| 70 |
{ |
|---|
| 71 |
static if (c>' ' && c<0x7F) const bool isgraph=true; |
|---|
| 72 |
else const bool isgraph=false; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
template isprint(dchar c) |
|---|
| 76 |
{ |
|---|
| 77 |
static if (c>=' ' && c<0x7F) const bool isprint=true; |
|---|
| 78 |
else const bool isprint=false; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
template isascii(dchar c) |
|---|
| 82 |
{ |
|---|
| 83 |
static if (c<=0x7F) const bool isascii=true; |
|---|
| 84 |
else const bool isascii=false; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
template toupper(dchar c) |
|---|
| 88 |
{ |
|---|
| 89 |
static if (islower!(c)) const dchar toupper = c + 'A'-'a'; |
|---|
| 90 |
else const dchar toupper = c; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
template tolower(dchar c) |
|---|
| 94 |
{ |
|---|
| 95 |
static if (c>='A' && c<='Z') const dchar tolower = c + 'a'-'A'; |
|---|
| 96 |
else const dchar tolower = c; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
version(testmeta) { |
|---|
| 100 |
static assert(isspace!(' ')); |
|---|
| 101 |
static assert(!isspace!('w')); |
|---|
| 102 |
static assert(islower!('r')); |
|---|
| 103 |
static assert(toupper!('f')=='F'); |
|---|
| 104 |
static assert(tolower!('Z')=='z'); |
|---|
| 105 |
static assert(isdigit!('4')); |
|---|
| 106 |
static assert(!isxdigit!('G')); |
|---|
| 107 |
static assert(ispunct!('@')); |
|---|
| 108 |
static assert(isupper!('A')); |
|---|
| 109 |
} |
|---|