Coverage Report

Created: 2026-08-22 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
57.1k
NameToCharCode::NameToCharCode() {
26
57.1k
  int i;
27
28
57.1k
  size = 31;
29
57.1k
  len = 0;
30
57.1k
  tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry));
31
1.82M
  for (i = 0; i < size; ++i) {
32
1.77M
    tab[i].name = NULL;
33
1.77M
  }
34
57.1k
}
35
36
57.1k
NameToCharCode::~NameToCharCode() {
37
57.1k
  int i;
38
39
482M
  for (i = 0; i < size; ++i) {
40
482M
    if (tab[i].name) {
41
127M
      gfree(tab[i].name);
42
127M
    }
43
482M
  }
44
57.1k
  gfree(tab);
45
57.1k
}
46
47
127M
void NameToCharCode::add(const char *name, CharCode c) {
48
127M
  NameToCharCodeEntry *oldTab;
49
127M
  int h, i, oldSize;
50
51
  // expand the table if necessary
52
127M
  if (len >= size / 2) {
53
371k
    oldSize = size;
54
371k
    oldTab = tab;
55
371k
    size = 2*size + 1;
56
371k
    tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry));
57
962M
    for (h = 0; h < size; ++h) {
58
961M
      tab[h].name = NULL;
59
961M
    }
60
481M
    for (i = 0; i < oldSize; ++i) {
61
480M
      if (oldTab[i].name) {
62
240M
  h = hash(oldTab[i].name);
63
350M
  while (tab[h].name) {
64
110M
    if (++h == size) {
65
0
      h = 0;
66
0
    }
67
110M
  }
68
240M
  tab[h] = oldTab[i];
69
240M
      }
70
480M
    }
71
371k
    gfree(oldTab);
72
371k
  }
73
74
  // add the new name
75
127M
  h = hash(name);
76
530M
  while (tab[h].name && strcmp(tab[h].name, name)) {
77
402M
    if (++h == size) {
78
85.7k
      h = 0;
79
85.7k
    }
80
402M
  }
81
127M
  if (!tab[h].name) {
82
127M
    tab[h].name = copyString(name);
83
127M
  }
84
127M
  tab[h].c = c;
85
86
127M
  ++len;
87
127M
}
88
89
10.8M
CharCode NameToCharCode::lookup(const char *name) {
90
10.8M
  int h;
91
92
10.8M
  h = hash(name);
93
14.2M
  while (tab[h].name) {
94
14.0M
    if (!strcmp(tab[h].name, name)) {
95
10.6M
      return tab[h].c;
96
10.6M
    }
97
3.40M
    if (++h == size) {
98
0
      h = 0;
99
0
    }
100
3.40M
  }
101
152k
  return 0;
102
10.8M
}
103
104
378M
int NameToCharCode::hash(const char *name) {
105
378M
  const char *p;
106
378M
  unsigned int h;
107
108
378M
  h = 0;
109
4.39G
  for (p = name; *p; ++p) {
110
4.01G
    h = 17 * h + (int)(*p & 0xff);
111
4.01G
  }
112
378M
  return (int)(h % size);
113
378M
}