robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1 2#include "lptypes.h" 3#include "lpcset.h" 4 5 6/* 7** Add to 'c' the index of the (only) bit set in byte 'b' 8*/ 9static int onlybit (int c ,int b ) { 10if ((b & 0xF0 )!= 0 ) {c += 4 ;b >>=4 ; } 11if ((b & 0x0C )!= 0 ) {c += 2 ;b >>=2 ; } 12if ((b & 0x02 )!= 0 ) {c += 1 ; } 13return c ; 14} 15 16 17/* 18** Check whether a charset is empty (returns IFail), singleton (IChar), 19** full (IAny), or none of those (ISet). When singleton, 'info.offset' 20** returns which character it is. When generic set, 'info' returns 21** information about its range. 22*/ 23Opcode charsettype (const byte * cs ,charsetinfo * info ) { 24int low0 ,low1 ,high0 ,high1 ; 25for (low1 = 0 ;low1 < CHARSETSIZE && cs [low1 ]== 0 ;low1 ++ ) 26/* find lowest byte with a 1-bit */ ; 27if (low1 == CHARSETSIZE ) 28return IFail ;/* no characters in set */ 29for (high1 = CHARSETSIZE - 1 ;cs [high1 ]== 0 ;high1 -- ) 30/* find highest byte with a 1-bit; low1 is a sentinel */ ; 31if (low1 == high1 ) {/* only one byte with 1-bits? */ 32int b = cs [low1 ]; 33if ((b & (b - 1 ))== 0 ) {/* does byte has only one 1-bit? */ 34info -> offset = onlybit (low1 * BITSPERCHAR ,b );/* get that bit */ 35return IChar ;/* single character */ 36 } 37 } 38for (low0 = 0 ;low0 < CHARSETSIZE && cs [low0 ]== 0xFF ;low0 ++ ) 39/* find lowest byte with a 0-bit */ ; 40if (low0 == CHARSETSIZE ) 41return IAny ;/* set has all bits set */ 42for (high0 = CHARSETSIZE - 1 ;cs [high0 ]== 0xFF ;high0 -- ) 43/* find highest byte with a 0-bit; low0 is a sentinel */ ; 44if (high1 - low1 <=high0 - low0 ) {/* range of 1s smaller than of 0s? */ 45info -> offset = low1 ; 46info -> size = high1 - low1 + 1 ; 47info -> deflt = 0 ;/* all discharged bits were 0 */ 48 } 49else { 50info -> offset = low0 ; 51info -> size = high0 - low0 + 1 ; 52info -> deflt = 0xFF ;/* all discharged bits were 1 */ 53 } 54info -> cs = cs + info -> offset ; 55return ISet ; 56} 57 58 59/* 60** Get a byte from a compact charset. If index is inside the charset 61** range, get the byte from the supporting charset (correcting it 62** by the offset). Otherwise, return the default for the set. 63*/ 64byte getbytefromcharset (const charsetinfo * info ,int index ) { 65if (index < info -> size ) 66return info -> cs [index ]; 67else return info -> deflt ; 68} 69 70 71/* 72** If 'tree' is a 'char' pattern (TSet, TChar, TAny, TFalse), convert it 73** into a charset and return 1; else return 0. 74*/ 75int tocharset (TTree * tree ,Charset * cs ) { 76switch (tree -> tag ) { 77case TChar : {/* only one char */ 78assert (0 <=tree -> u .n && tree -> u .n <=UCHAR_MAX ); 79clearset (cs -> cs );/* erase all chars */ 80setchar (cs -> cs ,tree -> u .n );/* add that one */ 81return 1 ; 82 } 83case TAny : { 84fillset (cs -> cs ,0xFF );/* add all characters to the set */ 85return 1 ; 86 } 87case TFalse : { 88clearset (cs -> cs );/* empty set */ 89return 1 ; 90 } 91case TSet : {/* fill set */ 92int i ; 93fillset (cs -> cs ,tree -> u .set .deflt ); 94for (i = 0 ;i < tree -> u .set .size ;i ++ ) 95cs -> cs [tree -> u .set .offset + i ]= treebuffer (tree )[i ]; 96return 1 ; 97 } 98default :return 0 ; 99 } 100} 101 102 103void tree2cset (TTree * tree ,charsetinfo * info ) { 104assert (tree -> tag == TSet ); 105info -> offset = tree -> u .set .offset ; 106info -> size = tree -> u .set .size ; 107info -> deflt = tree -> u .set .deflt ; 108info -> cs = treebuffer (tree ); 109} 110