Changeset 125 for trunk/src/bytelex.h


Ignore:
Timestamp:
May 2, 2008, 10:10:52 AM (11 years ago)
Author:
lindanl
Message:

Character references: calculation and error detection.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bytelex.h

    r124 r125  
    2020inline bool AtQuote(unsigned char x8data[]) {
    2121  return (x8data[0] == Ord<C, '"'>::value) | (x8data[0] == Ord<C, '\''>::value);
     22}
     23
     24/* In both ASCII and EBCDIC, digits are in a contiguous range
     25   from 0 through 9 */
     26template<CodeUnit_Base C>
     27inline bool at_Digit(unsigned char x8data[]) {
     28  return (x8data[0] >= Ord<C, '0'>::value) & (x8data[0] <= Ord<C, '9'>::value);
     29}
     30
     31template<CodeUnit_Base C>
     32inline unsigned int DigitVal(unsigned char d) {
     33  return d - Ord<C, '0'>::value;
     34}
     35
     36/* In both ASCII and EBCDIC, digits are in a contiguous range
     37   from 0 through 9.  Similarly the hex characters A through F
     38   and a through f are also in contiguous ranges that differ in
     39   only one bit position (Ord<C, 'a'>::value ^ Ord<C, 'A'>::value).*/
     40template<CodeUnit_Base C>
     41inline bool at_HexDigit(unsigned char x8data[]) {
     42  const unsigned char case_bit = Ord<C, 'a'>::value ^ Ord<C, 'A'>::value;
     43  const unsigned char canon_A =  Ord<C, 'a'>::value | case_bit;
     44  const unsigned char canon_F =  Ord<C, 'f'>::value | case_bit;
     45  unsigned char ch = x8data[0];
     46  unsigned char canon_ch = ch | case_bit;
     47  return ((ch >= Ord<C, '0'>::value) & (ch <= Ord<C, '9'>::value)) |
     48         ((canon_ch >= canon_A) & (canon_ch <= canon_F));
     49}
     50
     51template<CodeUnit_Base C>
     52inline unsigned int HexVal(unsigned char ch) {
     53  const unsigned char case_bit = Ord<C, 'a'>::value ^ Ord<C, 'A'>::value;
     54  const unsigned char canon_A =  Ord<C, 'a'>::value | case_bit;
     55  unsigned char canon_ch = ch | case_bit;
     56  if ((ch >= Ord<C, '0'>::value) & (ch <= Ord<C, '9'>::value)) return ch - Ord<C, '0'>::value;
     57  else return (ch | case_bit) - canon_A + 10;
    2258}
    2359
Note: See TracChangeset for help on using the changeset viewer.