Coverage Report

Created: 2026-03-07 07:37

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