Coverage Report

Created: 2026-06-22 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/NameToCharCode.cc
Line
Count
Source
1
//========================================================================
2
//
3
// NameToCharCode.cc
4
//
5
// Copyright 2001-2003 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <string.h>
12
#include "gmem.h"
13
#include "gmempp.h"
14
#include "NameToCharCode.h"
15
16
//------------------------------------------------------------------------
17
18
struct NameToCharCodeEntry {
19
  char *name;
20
  CharCode c;
21
};
22
23
//------------------------------------------------------------------------
24
25
34.1k
NameToCharCode::NameToCharCode() {
26
34.1k
  int i;
27
28
34.1k
  size = 31;
29
34.1k
  len = 0;
30
34.1k
  tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry));
31
1.09M
  for (i = 0; i < size; ++i) {
32
1.05M
    tab[i].name = NULL;
33
1.05M
  }
34
34.1k
}
35
36
34.1k
NameToCharCode::~NameToCharCode() {
37
34.1k
  int i;
38
39
288M
  for (i = 0; i < size; ++i) {
40
288M
    if (tab[i].name) {
41
76.1M
      gfree(tab[i].name);
42
76.1M
    }
43
288M
  }
44
34.1k
  gfree(tab);
45
34.1k
}
46
47
76.1M
void NameToCharCode::add(const char *name, CharCode c) {
48
76.1M
  NameToCharCodeEntry *oldTab;
49
76.1M
  int h, i, oldSize;
50
51
  // expand the table if necessary
52
76.1M
  if (len >= size / 2) {
53
221k
    oldSize = size;
54
221k
    oldTab = tab;
55
221k
    size = 2*size + 1;
56
221k
    tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry));
57
574M
    for (h = 0; h < size; ++h) {
58
574M
      tab[h].name = NULL;
59
574M
    }
60
287M
    for (i = 0; i < oldSize; ++i) {
61
286M
      if (oldTab[i].name) {
62
143M
  h = hash(oldTab[i].name);
63
209M
  while (tab[h].name) {
64
66.1M
    if (++h == size) {
65
0
      h = 0;
66
0
    }
67
66.1M
  }
68
143M
  tab[h] = oldTab[i];
69
143M
      }
70
286M
    }
71
221k
    gfree(oldTab);
72
221k
  }
73
74
  // add the new name
75
76.1M
  h = hash(name);
76
316M
  while (tab[h].name && strcmp(tab[h].name, name)) {
77
240M
    if (++h == size) {
78
51.1k
      h = 0;
79
51.1k
    }
80
240M
  }
81
76.1M
  if (!tab[h].name) {
82
76.1M
    tab[h].name = copyString(name);
83
76.1M
  }
84
76.1M
  tab[h].c = c;
85
86
76.1M
  ++len;
87
76.1M
}
88
89
4.20M
CharCode NameToCharCode::lookup(const char *name) {
90
4.20M
  int h;
91
92
4.20M
  h = hash(name);
93
5.51M
  while (tab[h].name) {
94
5.46M
    if (!strcmp(tab[h].name, name)) {
95
4.15M
      return tab[h].c;
96
4.15M
    }
97
1.31M
    if (++h == size) {
98
0
      h = 0;
99
0
    }
100
1.31M
  }
101
47.9k
  return 0;
102
4.20M
}
103
104
223M
int NameToCharCode::hash(const char *name) {
105
223M
  const char *p;
106
223M
  unsigned int h;
107
108
223M
  h = 0;
109
2.60G
  for (p = name; *p; ++p) {
110
2.38G
    h = 17 * h + (int)(*p & 0xff);
111
2.38G
  }
112
223M
  return (int)(h % size);
113
223M
}