Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / yarr / wtf / ASCIICType.h
1 /*
2  * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef WTF_ASCIICType_h
30 #define WTF_ASCIICType_h
31
32 #include "yarr/jswtfbridge.h"
33
34 // The behavior of many of the functions in the <ctype.h> header is dependent
35 // on the current locale. But in the WebKit project, all uses of those functions
36 // are in code processing something that's not locale-specific. These equivalents
37 // for some of the <ctype.h> functions are named more explicitly, not dependent
38 // on the C library locale, and we should also optimize them as needed.
39
40 // All functions return false or leave the character unchanged if passed a character
41 // that is outside the range 0-7F. So they can be used on Unicode strings or
42 // characters if the intent is to do processing only if the character is ASCII.
43
44 namespace WTF {
45
46     inline bool isASCII(char c) { return !(c & ~0x7F); }
47     inline bool isASCII(unsigned short c) { return !(c & ~0x7F); }
48 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
49     inline bool isASCII(wchar_t c) { return !(c & ~0x7F); }
50 #endif
51     inline bool isASCII(int c) { return !(c & ~0x7F); }
52
53     inline bool isASCIIAlpha(char c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
54     inline bool isASCIIAlpha(unsigned short c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
55 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
56     inline bool isASCIIAlpha(wchar_t c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
57 #endif
58     inline bool isASCIIAlpha(int c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
59
60     inline bool isASCIIAlphanumeric(char c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
61     inline bool isASCIIAlphanumeric(unsigned short c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
62 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
63     inline bool isASCIIAlphanumeric(wchar_t c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
64 #endif
65     inline bool isASCIIAlphanumeric(int c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
66
67     inline bool isASCIIDigit(char c) { return (c >= '0') & (c <= '9'); }
68     inline bool isASCIIDigit(unsigned short c) { return (c >= '0') & (c <= '9'); }
69 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
70     inline bool isASCIIDigit(wchar_t c) { return (c >= '0') & (c <= '9'); }
71 #endif
72     inline bool isASCIIDigit(int c) { return (c >= '0') & (c <= '9'); }
73
74     inline bool isASCIIHexDigit(char c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
75     inline bool isASCIIHexDigit(unsigned short c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
76 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
77     inline bool isASCIIHexDigit(wchar_t c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
78 #endif
79     inline bool isASCIIHexDigit(int c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
80
81     inline bool isASCIIOctalDigit(char c) { return (c >= '0') & (c <= '7'); }
82     inline bool isASCIIOctalDigit(unsigned short c) { return (c >= '0') & (c <= '7'); }
83 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
84     inline bool isASCIIOctalDigit(wchar_t c) { return (c >= '0') & (c <= '7'); }
85 #endif
86     inline bool isASCIIOctalDigit(int c) { return (c >= '0') & (c <= '7'); }
87
88     inline bool isASCIILower(char c) { return c >= 'a' && c <= 'z'; }
89     inline bool isASCIILower(unsigned short c) { return c >= 'a' && c <= 'z'; }
90 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
91     inline bool isASCIILower(wchar_t c) { return c >= 'a' && c <= 'z'; }
92 #endif
93     inline bool isASCIILower(int c) { return c >= 'a' && c <= 'z'; }
94
95     inline bool isASCIIUpper(char c) { return c >= 'A' && c <= 'Z'; }
96     inline bool isASCIIUpper(unsigned short c) { return c >= 'A' && c <= 'Z'; }
97 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
98     inline bool isASCIIUpper(wchar_t c) { return c >= 'A' && c <= 'Z'; }
99 #endif
100     inline bool isASCIIUpper(int c) { return c >= 'A' && c <= 'Z'; }
101
102     /*
103         Statistics from a run of Apple's page load test for callers of isASCIISpace:
104
105             character          count
106             ---------          -----
107             non-spaces         689383
108         20  space              294720
109         0A  \n                 89059
110         09  \t                 28320
111         0D  \r                 0
112         0C  \f                 0
113         0B  \v                 0
114     */
115     inline bool isASCIISpace(char c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
116     inline bool isASCIISpace(unsigned short c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
117 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
118     inline bool isASCIISpace(wchar_t c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
119 #endif
120     inline bool isASCIISpace(int c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
121
122     inline char toASCIILower(char c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
123     inline unsigned short toASCIILower(unsigned short c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
124 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
125     inline wchar_t toASCIILower(wchar_t c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
126 #endif
127     inline int toASCIILower(int c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
128
129     inline char toASCIIUpper(char c) { return static_cast<char>(c & ~((c >= 'a' && c <= 'z') << 5)); }
130     inline unsigned short toASCIIUpper(unsigned short c) { return static_cast<unsigned short>(c & ~((c >= 'a' && c <= 'z') << 5)); }
131 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
132     inline wchar_t toASCIIUpper(wchar_t c) { return static_cast<wchar_t>(c & ~((c >= 'a' && c <= 'z') << 5)); }
133 #endif
134     inline int toASCIIUpper(int c) { return static_cast<int>(c & ~((c >= 'a' && c <= 'z') << 5)); }
135
136     inline int toASCIIHexValue(char c) { JS_ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
137     inline int toASCIIHexValue(unsigned short c) { JS_ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
138 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
139     inline int toASCIIHexValue(wchar_t c) { JS_ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
140 #endif
141     inline int toASCIIHexValue(int c) { JS_ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
142
143     inline bool isASCIIPrintable(char c) { return c >= ' ' && c <= '~'; }
144     inline bool isASCIIPrintable(unsigned short c) { return c >= ' ' && c <= '~'; }
145 #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
146     inline bool isASCIIPrintable(wchar_t c) { return c >= ' ' && c <= '~'; }
147 #endif
148     inline bool isASCIIPrintable(int c) { return c >= ' ' && c <= '~'; }
149
150 }
151
152 using WTF::isASCII;
153 using WTF::isASCIIAlpha;
154 using WTF::isASCIIAlphanumeric;
155 using WTF::isASCIIDigit;
156 using WTF::isASCIIHexDigit;
157 using WTF::isASCIILower;
158 using WTF::isASCIIOctalDigit;
159 using WTF::isASCIIPrintable;
160 using WTF::isASCIISpace;
161 using WTF::isASCIIUpper;
162 using WTF::toASCIIHexValue;
163 using WTF::toASCIILower;
164 using WTF::toASCIIUpper;
165
166 #endif