robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

master
3.2 KiB110 linesraw
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) {
10  if ((b & 0xF0) != 0) { c += 4; b >>= 4; }
11  if ((b & 0x0C) != 0) { c += 2; b >>= 2; }
12  if ((b & 0x02) != 0) { c += 1; }
13  return 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) {
24  int low0, low1, high0, high1;
25  for (low1 = 0; low1 < CHARSETSIZE && cs[low1] == 0; low1++)
26    /* find lowest byte with a 1-bit */;
27  if (low1 == CHARSETSIZE)
28    return IFail;  /* no characters in set */
29  for (high1 = CHARSETSIZE - 1; cs[high1] == 0; high1--)
30    /* find highest byte with a 1-bit; low1 is a sentinel */;
31  if (low1 == high1) {  /* only one byte with 1-bits? */
32    int b = cs[low1];
33    if ((b & (b - 1)) == 0) {  /* does byte has only one 1-bit? */
34      info->offset = onlybit(low1 * BITSPERCHAR, b);  /* get that bit */
35      return IChar;  /* single character */
36    }
37  }
38  for (low0 = 0; low0 < CHARSETSIZE && cs[low0] == 0xFF; low0++)
39    /* find lowest byte with a 0-bit */;
40  if (low0 == CHARSETSIZE)
41    return IAny;  /* set has all bits set */
42  for (high0 = CHARSETSIZE - 1; cs[high0] == 0xFF; high0--)
43    /* find highest byte with a 0-bit; low0 is a sentinel */;
44  if (high1 - low1 <= high0 - low0) {  /* range of 1s smaller than of 0s? */
45    info->offset = low1;
46    info->size = high1 - low1 + 1;
47    info->deflt = 0;  /* all discharged bits were 0 */
48  }
49  else {
50    info->offset = low0;
51    info->size = high0 - low0 + 1;
52    info->deflt = 0xFF;  /* all discharged bits were 1 */
53  }
54  info->cs = cs + info->offset;
55  return 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) {
65  if (index < info->size)
66    return info->cs[index];
67  else 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) {
76  switch (tree->tag) {
77    case TChar: {  /* only one char */
78      assert(0 <= tree->u.n && tree->u.n <= UCHAR_MAX);
79      clearset(cs->cs);  /* erase all chars */
80      setchar(cs->cs, tree->u.n);  /* add that one */
81      return 1;
82    }
83    case TAny: {
84      fillset(cs->cs, 0xFF);  /* add all characters to the set */
85      return 1;
86    }
87    case TFalse: {
88      clearset(cs->cs);  /* empty set */
89      return 1;
90    }
91    case TSet: {  /* fill set */
92      int i;
93      fillset(cs->cs, tree->u.set.deflt);
94      for (i = 0; i < tree->u.set.size; i++)
95        cs->cs[tree->u.set.offset + i] = treebuffer(tree)[i];
96      return 1;
97    }
98    default: return 0;
99  }
100}
101
102
103void tree2cset (TTree *tree, charsetinfo *info) {
104  assert(tree->tag == TSet);
105  info->offset = tree->u.set.offset;
106  info->size = tree->u.set.size;
107  info->deflt = tree->u.set.deflt;
108  info->cs = treebuffer(tree);
109}
110