1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.core.BasicClass;
    18  
    19  /**
    20   * {@code AsciiUtil} provides common routines for ASCII char validations and
    21   * conversations.
    22   * 
    23   * @author Igor Sadovskiy
    24   */
    25  
    26  class org.as2lib.regexp.AsciiUtil extends BasicClass {
    27      
    28      public static var UPPER:Number   = 0x00000100;
    29      
    30      public static var LOWER:Number   = 0x00000200;
    31      
    32      public static var DIGIT:Number   = 0x00000400;
    33      
    34      public static var SPACE:Number   = 0x00000800;
    35      
    36      public static var PUNCT:Number   = 0x00001000;
    37      
    38      public static var CNTRL:Number   = 0x00002000;
    39      
    40      public static var BLANK:Number   = 0x00004000;
    41      
    42      public static var HEX:Number     = 0x00008000;
    43      
    44      public static var UNDER:Number   = 0x00010000;
    45      
    46      public static var ASCII:Number   = 0x0000FF00;
    47  
    48      public static var ALPHA:Number   = (UPPER|LOWER);
    49  
    50      public static var ALNUM:Number   = (UPPER|LOWER|DIGIT);
    51  
    52      public static var GRAPH:Number   = (PUNCT|UPPER|LOWER|DIGIT);
    53  
    54      public static var WORD:Number    = (UPPER|LOWER|UNDER|DIGIT);
    55  
    56      public static var XDIGIT:Number  = HEX;
    57  
    58      private static var charTypes:Array = [
    59          CNTRL,                  /* 00 (NUL) */
    60          CNTRL,                  /* 01 (SOH) */
    61          CNTRL,                  /* 02 (STX) */
    62          CNTRL,                  /* 03 (ETX) */
    63          CNTRL,                  /* 04 (EOT) */
    64          CNTRL,                  /* 05 (ENQ) */
    65          CNTRL,                  /* 06 (ACK) */
    66          CNTRL,                  /* 07 (BEL) */
    67          CNTRL,                  /* 08 (BS)  */
    68          SPACE+CNTRL+BLANK,      /* 09 (HT)  */
    69          SPACE+CNTRL,            /* 0A (LF)  */
    70          SPACE+CNTRL,            /* 0B (VT)  */
    71          SPACE+CNTRL,            /* 0C (FF)  */
    72          SPACE+CNTRL,            /* 0D (CR)  */
    73          CNTRL,                  /* 0E (SI)  */
    74          CNTRL,                  /* 0F (SO)  */
    75          CNTRL,                  /* 10 (DLE) */
    76          CNTRL,                  /* 11 (DC1) */
    77          CNTRL,                  /* 12 (DC2) */
    78          CNTRL,                  /* 13 (DC3) */
    79          CNTRL,                  /* 14 (DC4) */
    80          CNTRL,                  /* 15 (NAK) */
    81          CNTRL,                  /* 16 (SYN) */
    82          CNTRL,                  /* 17 (ETB) */
    83          CNTRL,                  /* 18 (CAN) */
    84          CNTRL,                  /* 19 (EM)  */
    85          CNTRL,                  /* 1A (SUB) */
    86          CNTRL,                  /* 1B (ESC) */
    87          CNTRL,                  /* 1C (FS)  */
    88          CNTRL,                  /* 1D (GS)  */
    89          CNTRL,                  /* 1E (RS)  */
    90          CNTRL,                  /* 1F (US)  */
    91          SPACE+BLANK,            /* 20 SPACE */
    92          PUNCT,                  /* 21 !     */
    93          PUNCT,                  /* 22 "     */
    94          PUNCT,                  /* 23 #     */
    95          PUNCT,                  /* 24 $     */
    96          PUNCT,                  /* 25 %     */
    97          PUNCT,                  /* 26 &     */
    98          PUNCT,                  /* 27 '     */
    99          PUNCT,                  /* 28 (     */
   100          PUNCT,                  /* 29 )     */
   101          PUNCT,                  /* 2A *     */
   102          PUNCT,                  /* 2B +     */
   103          PUNCT,                  /* 2C ,     */
   104          PUNCT,                  /* 2D -     */
   105          PUNCT,                  /* 2E .     */
   106          PUNCT,                  /* 2F /     */
   107          DIGIT+HEX+0,            /* 30 0     */
   108          DIGIT+HEX+1,            /* 31 1     */
   109          DIGIT+HEX+2,            /* 32 2     */
   110          DIGIT+HEX+3,            /* 33 3     */
   111          DIGIT+HEX+4,            /* 34 4     */
   112          DIGIT+HEX+5,            /* 35 5     */
   113          DIGIT+HEX+6,            /* 36 6     */
   114          DIGIT+HEX+7,            /* 37 7     */
   115          DIGIT+HEX+8,            /* 38 8     */
   116          DIGIT+HEX+9,            /* 39 9     */
   117          PUNCT,                  /* 3A :     */
   118          PUNCT,                  /* 3B ;     */
   119          PUNCT,                  /* 3C <     */
   120          PUNCT,                  /* 3D =     */
   121          PUNCT,                  /* 3E >     */
   122          PUNCT,                  /* 3F ?     */
   123          PUNCT,                  /* 40 @     */
   124          UPPER+HEX+10,           /* 41 A     */
   125          UPPER+HEX+11,           /* 42 B     */
   126          UPPER+HEX+12,           /* 43 C     */
   127          UPPER+HEX+13,           /* 44 D     */
   128          UPPER+HEX+14,           /* 45 E     */
   129          UPPER+HEX+15,           /* 46 F     */
   130          UPPER+16,               /* 47 G     */
   131          UPPER+17,               /* 48 H     */
   132          UPPER+18,               /* 49 I     */
   133          UPPER+19,               /* 4A J     */
   134          UPPER+20,               /* 4B K     */
   135          UPPER+21,               /* 4C L     */
   136          UPPER+22,               /* 4D M     */
   137          UPPER+23,               /* 4E N     */
   138          UPPER+24,               /* 4F O     */
   139          UPPER+25,               /* 50 P     */
   140          UPPER+26,               /* 51 Q     */
   141          UPPER+27,               /* 52 R     */
   142          UPPER+28,               /* 53 S     */
   143          UPPER+29,               /* 54 T     */
   144          UPPER+30,               /* 55 U     */
   145          UPPER+31,               /* 56 V     */
   146          UPPER+32,               /* 57 W     */
   147          UPPER+33,               /* 58 X     */
   148          UPPER+34,               /* 59 Y     */
   149          UPPER+35,               /* 5A Z     */
   150          PUNCT,                  /* 5B [     */
   151          PUNCT,                  /* 5C \     */
   152          PUNCT,                  /* 5D ]     */
   153          PUNCT,                  /* 5E ^     */
   154          PUNCT|UNDER,            /* 5F _     */
   155          PUNCT,                  /* 60 `     */
   156          LOWER+HEX+10,           /* 61 a     */
   157          LOWER+HEX+11,           /* 62 b     */
   158          LOWER+HEX+12,           /* 63 c     */
   159          LOWER+HEX+13,           /* 64 d     */
   160          LOWER+HEX+14,           /* 65 e     */
   161          LOWER+HEX+15,           /* 66 f     */
   162          LOWER+16,               /* 67 g     */
   163          LOWER+17,               /* 68 h     */
   164          LOWER+18,               /* 69 i     */
   165          LOWER+19,               /* 6A j     */
   166          LOWER+20,               /* 6B k     */
   167          LOWER+21,               /* 6C l     */
   168          LOWER+22,               /* 6D m     */
   169          LOWER+23,               /* 6E n     */
   170          LOWER+24,               /* 6F o     */
   171          LOWER+25,               /* 70 p     */
   172          LOWER+26,               /* 71 q     */
   173          LOWER+27,               /* 72 r     */
   174          LOWER+28,               /* 73 s     */
   175          LOWER+29,               /* 74 t     */
   176          LOWER+30,               /* 75 u     */
   177          LOWER+31,               /* 76 v     */
   178          LOWER+32,               /* 77 w     */
   179          LOWER+33,               /* 78 x     */
   180          LOWER+34,               /* 79 y     */
   181          LOWER+35,               /* 7A z     */
   182          PUNCT,                  /* 7B {     */
   183          PUNCT,                  /* 7C |     */
   184          PUNCT,                  /* 7D }     */
   185          PUNCT,                  /* 7E ~     */
   186          CNTRL                   /* 7F (DEL) */
   187      ]; 
   188      
   189  	private function AsciiUtil(Void) {
   190  		super();
   191  	}
   192  
   193      public static function getType(ch:Number):Number {
   194          return ((ch & 0xFFFFFF80) == 0 ? charTypes[ch] : 0);
   195      }
   196  
   197      public static function isType(ch:Number, type:Number):Boolean {
   198          return (getType(ch) & type) != 0;
   199      }
   200  
   201      public static function isAscii(ch:Number):Boolean {
   202          return ((ch & 0xFFFFFF80) == 0);
   203      }
   204  
   205      public static function isAlpha(ch:Number):Boolean {
   206          return isType(ch, ALPHA);
   207      }
   208  
   209      public static function isDigit(ch:Number):Boolean {
   210          return ((ch - 0x30) | (0x39 - ch)) >= 0;
   211      }
   212  
   213      public static function isAlnum(ch:Number):Boolean {
   214          return isType(ch, ALNUM);
   215      }
   216  
   217      public static function isGraph(ch:Number):Boolean {
   218          return isType(ch, GRAPH);
   219      }
   220  
   221      public static function isPrint(ch:Number):Boolean {
   222          return ((ch - 0x20) | (0x7E - ch)) >= 0;
   223      }
   224  
   225      public static function isPunct(ch:Number):Boolean {
   226          return isType(ch, PUNCT);
   227      }
   228  
   229      public static function isSpace(ch:Number):Boolean {
   230          return isType(ch, SPACE);
   231      }
   232  
   233      public static function isHexDigit(ch:Number):Boolean {
   234          return isType(ch, HEX);
   235      }
   236  
   237      public static function isOctDigit(ch:Number):Boolean {
   238          return ((ch - 0x30) | (0x37 - ch)) >= 0;
   239      }
   240  
   241      public static function isCntrl(ch:Number):Boolean {
   242          return isType(ch, CNTRL);
   243      }
   244  
   245      public static function isLower(ch:Number):Boolean {
   246          return ((ch - 0x61) | (0x7A - ch)) >= 0;
   247      }
   248  
   249      public static function isUpper(ch:Number):Boolean {
   250          return ((ch - 0x41) | (0x5A - ch)) >= 0;
   251      }
   252  
   253      public static function isWord(ch:Number):Boolean {
   254          return isType(ch, WORD);
   255      }
   256  
   257      public static function toDigit(ch:Number):Number {
   258          return (charTypes[ch & 0x7F] & 0x3F);
   259      }
   260  
   261      public static function toLower(ch:Number):Number {
   262          return isUpper(ch) ? (ch + 0x20) : ch;
   263      }
   264  
   265      public static function toUpper(ch:Number):Number {
   266          return isLower(ch) ? (ch - 0x20) : ch;
   267      }
   268  
   269  }