Coverage Report

Created: 2026-03-15 06:39

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
29.5k
NameToCharCode::NameToCharCode() {
26
29.5k
  int i;
27
28
29.5k
  size = 31;
29
29.5k
  len = 0;
30
29.5k
  tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry));
31
944k
  for (i = 0; i < size; ++i) {
32
914k
    tab[i].name = NULL;
33
914k
  }
34
29.5k
}
35
36
29.5k
NameToCharCode::~NameToCharCode() {
37
29.5k
  int i;
38
39
249M
  for (i = 0; i < size; ++i) {
40
249M
    if (tab[i].name) {
41
65.8M
      gfree(tab[i].name);
42
65.8M
    }
43
249M
  }
44
29.5k
  gfree(tab);
45
29.5k
}
46
47
65.8M
void NameToCharCode::add(const char *name, CharCode c) {
48
65.8M
  NameToCharCodeEntry *oldTab;
49
65.8M
  int h, i, oldSize;
50
51
  // expand the table if necessary
52
65.8M
  if (len >= size / 2) {
53
191k
    oldSize = size;
54
191k
    oldTab = tab;
55
191k
    size = 2*size + 1;
56
191k
    tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry));
57
496M
    for (h = 0; h < size; ++h) {
58
496M
      tab[h].name = NULL;
59
496M
    }
60
248M
    for (i = 0; i < oldSize; ++i) {
61
248M
      if (oldTab[i].name) {
62
123M
  h = hash(oldTab[i].name);
63
181M
  while (tab[h].name) {
64
57.1M
    if (++h == size) {
65
0
      h = 0;
66
0
    }
67
57.1M
  }
68
123M
  tab[h] = oldTab[i];
69
123M
      }
70
248M
    }
71
191k
    gfree(oldTab);
72
191k
  }
73
74
  // add the new name
75
65.8M
  h = hash(name);
76
273M
  while (tab[h].name && strcmp(tab[h].name, name)) {
77
207M
    if (++h == size) {
78
44.2k
      h = 0;
79
44.2k
    }
80
207M
  }
81
65.8M
  if (!tab[h].name) {
82
65.8M
    tab[h].name = copyString(name);
83
65.8M
  }
84
65.8M
  tab[h].c = c;
85
86
65.8M
  ++len;
87
65.8M
}
88
89
4.91M
CharCode NameToCharCode::lookup(const char *name) {
90
4.91M
  int h;
91
92
4.91M
  h = hash(name);
93
6.51M
  while (tab[h].name) {
94
6.44M
    if (!strcmp(tab[h].name, name)) {
95
4.84M
      return tab[h].c;
96
4.84M
    }
97
1.60M
    if (++h == size) {
98
0
      h = 0;
99
0
    }
100
1.60M
  }
101
66.8k
  return 0;
102
4.91M
}
103
104
194M
int NameToCharCode::hash(const char *name) {
105
194M
  const char *p;
106
194M
  unsigned int h;
107
108
194M
  h = 0;
109
2.26G
  for (p = name; *p; ++p) {
110
2.06G
    h = 17 * h + (int)(*p & 0xff);
111
2.06G
  }
112
194M
  return (int)(h % size);
113
194M
}