root/trunk/minid/charlib.d

Revision 354, 4.1 kB (checked in by JarrettBillingsley, 3 months ago)

And a few more.

Line 
1 /******************************************************************************
2 License:
3 Copyright (c) 2008 Jarrett Billingsley
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the
7 use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it freely,
11 subject to the following restrictions:
12
13     1. The origin of this software must not be misrepresented; you must not
14     claim that you wrote the original software. If you use this software in a
15     product, an acknowledgment in the product documentation would be
16     appreciated but is not required.
17
18     2. Altered source versions must be plainly marked as such, and must not
19     be misrepresented as being the original software.
20
21     3. This notice may not be removed or altered from any source distribution.
22 ******************************************************************************/
23
24 module minid.charlib;
25
26 import minid.ex;
27 import minid.interpreter;
28 import minid.types;
29
30 import tango.stdc.ctype;
31 import Uni = tango.text.Unicode;
32 import Utf = tango.text.convert.Utf;
33
34 struct CharLib
35 {
36 static:
37     public void init(MDThread* t)
38     {
39         newNamespace(t, "char");
40             newFunction(t, &toLower, "char.toLower");       fielda(t, -2, "toLower");
41             newFunction(t, &toLower, "char.toLower");       fielda(t, -2, "toLower");
42             newFunction(t, &toUpper, "char.toUpper");       fielda(t, -2, "toUpper");
43             newFunction(t, &isAlpha, "char.isAlpha");       fielda(t, -2, "isAlpha");
44             newFunction(t, &isAlNum, "char.isAlNum");       fielda(t, -2, "isAlNum");
45             newFunction(t, &isLower, "char.isLower");       fielda(t, -2, "isLower");
46             newFunction(t, &isUpper, "char.isUpper");       fielda(t, -2, "isUpper");
47             newFunction(t, &isDigit, "char.isDigit");       fielda(t, -2, "isDigit");
48             newFunction(t, &isCtrl, "char.isCtrl");         fielda(t, -2, "isCtrl");
49             newFunction(t, &isPunct, "char.isPunct");       fielda(t, -2, "isPunct");
50             newFunction(t, &isSpace, "char.isSpace");       fielda(t, -2, "isSpace");
51             newFunction(t, &isHexDigit, "char.isHexDigit"); fielda(t, -2, "isHexDigit");
52             newFunction(t, &isAscii, "char.isAscii");       fielda(t, -2, "isAscii");
53             newFunction(t, &isValid, "char.isValid");       fielda(t, -2, "isValid");
54         setTypeMT(t, MDValue.Type.Char);
55     }
56
57     uword toLower(MDThread* t, uword numParams)
58     {
59         dchar[4] outbuf = void;
60         dchar c = checkCharParam(t, 0);
61         pushChar(t, safeCode(t, Uni.toLower((&c)[0 .. 1], outbuf)[0]));
62         return 1;
63     }
64
65     uword toUpper(MDThread* t, uword numParams)
66     {
67         dchar[4] outbuf = void;
68         dchar c = checkCharParam(t, 0);
69         pushChar(t, safeCode(t, Uni.toUpper((&c)[0 .. 1], outbuf)[0]));
70         return 1;
71     }
72
73     uword isAlpha(MDThread* t, uword numParams)
74     {
75         pushBool(t, Uni.isLetter(checkCharParam(t, 0)));
76         return 1;
77     }
78
79     uword isAlNum(MDThread* t, uword numParams)
80     {
81         pushBool(t, Uni.isLetterOrDigit(checkCharParam(t, 0)));
82         return 1;
83     }
84
85     uword isLower(MDThread* t, uword numParams)
86     {
87         pushBool(t, Uni.isLower(checkCharParam(t, 0)));
88         return 1;
89     }
90
91     uword isUpper(MDThread* t, uword numParams)
92     {
93         pushBool(t, Uni.isUpper(checkCharParam(t, 0)));
94         return 1;
95     }
96
97     uword isDigit(MDThread* t, uword numParams)
98     {
99         pushBool(t, Uni.isDigit(checkCharParam(t, 0)));
100         return 1;
101     }
102
103     uword isCtrl(MDThread* t, uword numParams)
104     {
105         pushBool(t, cast(bool)iscntrl(checkCharParam(t, 0)));
106         return 1;
107     }
108
109     uword isPunct(MDThread* t, uword numParams)
110     {
111         pushBool(t, cast(bool)ispunct(checkCharParam(t, 0)));
112         return 1;
113     }
114
115     uword isSpace(MDThread* t, uword numParams)
116     {
117         pushBool(t, cast(bool)isspace(checkCharParam(t, 0)));
118         return 1;
119     }
120
121     uword isHexDigit(MDThread* t, uword numParams)
122     {
123         pushBool(t, cast(bool)isxdigit(checkCharParam(t, 0)));
124         return 1;
125     }
126
127     uword isAscii(MDThread* t, uword numParams)
128     {
129         pushBool(t, checkCharParam(t, 0) <= 0x7f);
130         return 1;
131     }
132
133     uword isValid(MDThread* t, uword numParams)
134     {
135         pushBool(t, Utf.isValid(checkCharParam(t, 0)));
136         return 1;
137     }
138 }
Note: See TracBrowser for help on using the browser.