1 // $Id: case.h,v 1.18 2003/09/28 00:43:57 ericb Exp $ -*- c++ -*- 2 // 3 // This software is subject to the terms of the IBM Jikes Compiler 4 // License Agreement available at the following URL: 5 // http://ibm.com/developerworks/opensource/jikes. 6 // Copyright (C) 1996, 2003 IBM Corporation and others. All Rights Reserved. 7 // You must accept the terms of that agreement to use this software. 8 // 9 10 #ifndef case_INCLUDED 11 #define case_INCLUDED 12 13 #include "platform.h" 14 15 #ifdef HAVE_JIKES_NAMESPACE 16 namespace Jikes { // Open namespace Jikes block 17 #endif 18 19 // 20 // NOTE that this class is hard-wired to work on an ASCII machine. 21 // To make it universal, one should uncomment the constructor and 22 // make the array "lower" non-static. In such a case, each object 23 // of type Case that is declared will allocate its own "lower" array. 24 // This would need to be done for i.e. an EBCDIC machine. 25 // 26 class Case 27 { 28 static char lower[128]; 29 static char upper[128]; 30 31 public: 32 IsAsciiLower(char c)33 static inline bool IsAsciiLower(char c) 34 { 35 return c == lower[(int) c]; 36 } ToAsciiLower(char c)37 static inline char ToAsciiLower(char c) 38 { 39 return (c & (char) 0x80) ? c : lower[(int) c]; 40 } ToAsciiLower(wchar_t c)41 static inline wchar_t ToAsciiLower(wchar_t c) 42 { 43 return (c < 128 ? (wchar_t) lower[(int) c] : c); 44 } 45 IsAsciiUpper(char c)46 static inline bool IsAsciiUpper(char c) 47 { 48 return c == upper[(int) c]; 49 } ToAsciiUpper(char c)50 static inline char ToAsciiUpper(char c) 51 { 52 return (c & (char) 0x80) ? c : upper[(int) c]; 53 } ToAsciiUpper(wchar_t c)54 static inline wchar_t ToAsciiUpper(wchar_t c) 55 { 56 return (c < 128 ? (wchar_t) upper[(int) c] : c); 57 } 58 IsAsciiAlpha(char c)59 static inline bool IsAsciiAlpha(char c) 60 { 61 return (c == lower[(int) c] || c == upper[(int) c]); 62 } IsAsciiAlpha(wchar_t c)63 static inline bool IsAsciiAlpha(wchar_t c) 64 { 65 return (c == (wchar_t) lower[(int) c] || 66 c == (wchar_t) upper[(int) c]); 67 } 68 69 // 70 // Find the position of the first occurrence of a character within a 71 // string. If the character is not found, return -1. 72 // Index(char * s,wchar_t c)73 static inline int Index(char *s, wchar_t c) 74 { 75 for (int i = 0; *s != U_NULL; i++, s++) 76 { 77 if ((wchar_t) *s == c) 78 return i; 79 } 80 return -1; 81 } 82 Index(wchar_t * s,wchar_t c)83 static inline int Index(wchar_t *s, wchar_t c) 84 { 85 for (int i = 0; *s != U_NULL; i++, s++) 86 { 87 if ((wchar_t) *s == c) 88 return i; 89 } 90 return -1; 91 } 92 93 // 94 // Compare two character strings segments of length n in the strings 95 // s1 and s2 to check whether or not they are equal. Note that unlike 96 // the builtin function "strncmp" the comparison always checks n characters 97 // and does not terminate if it encounters a NULL character. 98 // StringSegmentEqual(char * s1,const char * s2,int n)99 static inline bool StringSegmentEqual(char *s1, const char *s2, int n) 100 { 101 for (int i = 0; i < n; i++) 102 { 103 if (ToAsciiLower(s1[i]) != ToAsciiLower(s2[i])) 104 return false; 105 } 106 return true; 107 } 108 StringSegmentEqual(wchar_t * s1,const char * s2,int n)109 static inline bool StringSegmentEqual(wchar_t *s1, const char *s2, int n) 110 { 111 for (int i = 0; i < n; i++) 112 { 113 if (ToAsciiLower(s1[i]) != (wchar_t) ToAsciiLower(s2[i])) 114 return false; 115 } 116 return true; 117 } 118 StringSegmentEqual(char * s1,const wchar_t * s2,int n)119 static inline bool StringSegmentEqual(char *s1, const wchar_t *s2, int n) 120 { 121 for (int i = 0; i < n; i++) 122 { 123 if ((wchar_t) ToAsciiLower(s1[i]) != ToAsciiLower(s2[i])) 124 return false; 125 } 126 return true; 127 } 128 StringSegmentEqual(wchar_t * s1,const wchar_t * s2,int n)129 static inline bool StringSegmentEqual(wchar_t *s1, const wchar_t *s2, 130 int n) 131 { 132 for (int i = 0; i < n; i++) 133 { 134 if (ToAsciiLower(s1[i]) != (wchar_t) ToAsciiLower(s2[i])) 135 return false; 136 } 137 return true; 138 } 139 140 141 // 142 // Compare two null-terminated character strings, s1 and s2 143 // to check whether or not they are equal. 144 // StringEqual(char * s1,const char * s2)145 static inline bool StringEqual(char *s1, const char *s2) 146 { 147 int i; 148 for (i = 0; s1[i] && s2[i]; i++) 149 { 150 if (ToAsciiLower(s1[i]) != ToAsciiLower(s2[i])) 151 return false; 152 } 153 return (s1[i] == s2[i]); 154 } 155 StringEqual(wchar_t * s1,const char * s2)156 static inline bool StringEqual(wchar_t *s1, const char *s2) 157 { 158 int i; 159 for (i = 0; s1[i] && s2[i]; i++) 160 { 161 if (ToAsciiLower(s1[i]) != (wchar_t) ToAsciiLower(s2[i])) 162 return false; 163 } 164 return (s1[i] == (wchar_t) s2[i]); 165 } 166 StringEqual(char * s1,const wchar_t * s2)167 static inline bool StringEqual(char *s1, const wchar_t *s2) 168 { 169 int i; 170 for (i = 0; s1[i] && s2[i]; i++) 171 { 172 if ((wchar_t) ToAsciiLower(s1[i]) != ToAsciiLower(s2[i])) 173 return false; 174 } 175 return ((wchar_t) s1[i] == s2[i]); 176 } 177 StringEqual(wchar_t * s1,const wchar_t * s2)178 static inline bool StringEqual(wchar_t *s1, const wchar_t *s2) 179 { 180 int i; 181 for (i = 0; s1[i] && s2[i]; i++) 182 { 183 if (ToAsciiLower(s1[i]) != (wchar_t) ToAsciiLower(s2[i])) 184 return false; 185 } 186 return (s1[i] == (wchar_t) s2[i]); 187 } 188 189 // see comment above. 190 // 191 // Lcase() 192 // { 193 // for (int c = 0; c < 256; c++) 194 // { 195 // if (isupper(c)) 196 // lower[c] = tolower(c); 197 // else if (islower(c)) 198 // upper[c] = toupper(c); 199 // else lower[c] = upper[c] = c; 200 // } 201 // } 202 }; 203 204 #ifdef HAVE_JIKES_NAMESPACE 205 } // Close namespace Jikes block 206 #endif 207 208 #endif // case_INCLUDED 209 210