home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / Jikes / Source / src / case.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-24  |  5.1 KB  |  190 lines

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