Coverage Report

Created: 2026-04-12 06:43

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
44.3k
NameToCharCode::NameToCharCode() {
26
44.3k
  int i;
27
28
44.3k
  size = 31;
29
44.3k
  len = 0;
30
44.3k
  tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry));
31
1.42M
  for (i = 0; i < size; ++i) {
32
1.37M
    tab[i].name = NULL;
33
1.37M
  }
34
44.3k
}
35
36
44.3k
NameToCharCode::~NameToCharCode() {
37
44.3k
  int i;
38
39
374M
  for (i = 0; i < size; ++i) {
40
374M
    if (tab[i].name) {
41
99.0M
      gfree(tab[i].name);
42
99.0M
    }
43
374M
  }
44
44.3k
  gfree(tab);
45
44.3k
}
46
47
99.1M
void NameToCharCode::add(const char *name, CharCode c) {
48
99.1M
  NameToCharCodeEntry *oldTab;
49
99.1M
  int h, i, oldSize;
50
51
  // expand the table if necessary
52
99.1M
  if (len >= size / 2) {
53
288k
    oldSize = size;
54
288k
    oldTab = tab;
55
288k
    size = 2*size + 1;
56
288k
    tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry));
57
747M
    for (h = 0; h < size; ++h) {
58
746M
      tab[h].name = NULL;
59
746M
    }
60
373M
    for (i = 0; i < oldSize; ++i) {
61
373M
      if (oldTab[i].name) {
62
186M
  h = hash(oldTab[i].name);
63
272M
  while (tab[h].name) {
64
85.9M
    if (++h == size) {
65
0
      h = 0;
66
0
    }
67
85.9M
  }
68
186M
  tab[h] = oldTab[i];
69
186M
      }
70
373M
    }
71
288k
    gfree(oldTab);
72
288k
  }
73
74
  // add the new name
75
99.1M
  h = hash(name);
76
411M
  while (tab[h].name && strcmp(tab[h].name, name)) {
77
312M
    if (++h == size) {
78
66.5k
      h = 0;
79
66.5k
    }
80
312M
  }
81
99.1M
  if (!tab[h].name) {
82
99.0M
    tab[h].name = copyString(name);
83
99.0M
  }
84
99.1M
  tab[h].c = c;
85
86
99.1M
  ++len;
87
99.1M
}
88
89
8.23M
CharCode NameToCharCode::lookup(const char *name) {
90
8.23M
  int h;
91
92
8.23M
  h = hash(name);
93
10.8M
  while (tab[h].name) {
94
10.7M
    if (!strcmp(tab[h].name, name)) {
95
8.16M
      return tab[h].c;
96
8.16M
    }
97
2.60M
    if (++h == size) {
98
0
      h = 0;
99
0
    }
100
2.60M
  }
101
70.6k
  return 0;
102
8.23M
}
103
104
293M
int NameToCharCode::hash(const char *name) {
105
293M
  const char *p;
106
293M
  unsigned int h;
107
108
293M
  h = 0;
109
3.40G
  for (p = name; *p; ++p) {
110
3.11G
    h = 17 * h + (int)(*p & 0xff);
111
3.11G
  }
112
293M
  return (int)(h % size);
113
293M
}