Coverage Report

Created: 2025-07-09 06:54

/src/mercurial/mercurial/thirdparty/xdiff/xutils.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  LibXDiff by Davide Libenzi ( File Differential Library )
3
 *  Copyright (C) 2003  Davide Libenzi
4
 *
5
 *  This library is free software; you can redistribute it and/or
6
 *  modify it under the terms of the GNU Lesser General Public
7
 *  License as published by the Free Software Foundation; either
8
 *  version 2.1 of the License, or (at your option) any later version.
9
 *
10
 *  This library is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 *  Lesser General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Lesser General Public
16
 *  License along with this library; if not, see
17
 *  <http://www.gnu.org/licenses/>.
18
 *
19
 *  Davide Libenzi <davidel@xmailserver.org>
20
 *
21
 */
22
23
#include <limits.h>
24
#include <assert.h>
25
#include "xinclude.h"
26
27
28
29
30
8.50k
int64_t xdl_bogosqrt(int64_t n) {
31
8.50k
  int64_t i;
32
33
  /*
34
   * Classical integer square root approximation using shifts.
35
   */
36
35.8k
  for (i = 1; n > 0; n >>= 2)
37
27.3k
    i <<= 1;
38
39
8.50k
  return i;
40
8.50k
}
41
42
43
void *xdl_mmfile_first(mmfile_t *mmf, int64_t *size)
44
11.3k
{
45
11.3k
  *size = mmf->size;
46
11.3k
  return mmf->ptr;
47
11.3k
}
48
49
50
int64_t xdl_mmfile_size(mmfile_t *mmf)
51
5.15k
{
52
5.15k
  return mmf->size;
53
5.15k
}
54
55
56
8.50k
int xdl_cha_init(chastore_t *cha, int64_t isize, int64_t icount) {
57
58
8.50k
  cha->head = cha->tail = NULL;
59
8.50k
  cha->isize = isize;
60
8.50k
  cha->nsize = icount * isize;
61
8.50k
  cha->ancur = cha->sncur = NULL;
62
8.50k
  cha->scurr = 0;
63
64
8.50k
  return 0;
65
8.50k
}
66
67
68
8.50k
void xdl_cha_free(chastore_t *cha) {
69
8.50k
  chanode_t *cur, *tmp;
70
71
27.0k
  for (cur = cha->head; (tmp = cur) != NULL;) {
72
18.5k
    cur = cur->next;
73
18.5k
    xdl_free(tmp);
74
18.5k
  }
75
8.50k
}
76
77
78
23.4M
void *xdl_cha_alloc(chastore_t *cha) {
79
23.4M
  chanode_t *ancur;
80
23.4M
  void *data;
81
82
23.4M
  if (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) {
83
18.5k
    if (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) {
84
85
0
      return NULL;
86
0
    }
87
18.5k
    ancur->icurr = 0;
88
18.5k
    ancur->next = NULL;
89
18.5k
    if (cha->tail)
90
10.5k
      cha->tail->next = ancur;
91
18.5k
    if (!cha->head)
92
7.97k
      cha->head = ancur;
93
18.5k
    cha->tail = ancur;
94
18.5k
    cha->ancur = ancur;
95
18.5k
  }
96
97
23.4M
  data = (char *) ancur + sizeof(chanode_t) + ancur->icurr;
98
23.4M
  ancur->icurr += cha->isize;
99
100
23.4M
  return data;
101
23.4M
}
102
103
5.66k
int64_t xdl_guess_lines(mmfile_t *mf, int64_t sample) {
104
5.66k
  int64_t nl = 0, size, tsize = 0;
105
5.66k
  char const *data, *cur, *top;
106
107
5.66k
  if ((cur = data = xdl_mmfile_first(mf, &size)) != NULL) {
108
459k
    for (top = data + size; nl < sample && cur < top; ) {
109
453k
      nl++;
110
453k
      if (!(cur = memchr(cur, '\n', top - cur)))
111
948
        cur = top;
112
452k
      else
113
452k
        cur++;
114
453k
    }
115
5.66k
    tsize += (long) (cur - data);
116
5.66k
  }
117
118
5.66k
  if (nl && tsize)
119
5.15k
    nl = xdl_mmfile_size(mf) / (tsize / nl);
120
121
5.66k
  return nl + 1;
122
5.66k
}
123
124
int xdl_recmatch(const char *l1, int64_t s1, const char *l2, int64_t s2)
125
25.9M
{
126
25.9M
  if (s1 == s2 && !memcmp(l1, l2, s1))
127
25.9M
    return 1;
128
4.08k
  return 0;
129
25.9M
}
130
131
23.3M
uint64_t xdl_hash_record(char const **data, char const *top) {
132
23.3M
  uint64_t ha = 5381;
133
23.3M
  char const *ptr = *data;
134
135
25.2M
  for (; ptr < top && *ptr != '\n'; ptr++) {
136
1.87M
    ha += (ha << 5);
137
1.87M
    ha ^= (unsigned long) *ptr;
138
1.87M
  }
139
23.3M
  *data = ptr < top ? ptr + 1: ptr;
140
141
23.3M
  return ha;
142
23.3M
}
143
144
8.50k
unsigned int xdl_hashbits(int64_t size) {
145
8.50k
  int64_t val = 1;
146
8.50k
  unsigned int bits = 0;
147
148
65.7k
  for (; val < size && bits < (int64_t) CHAR_BIT * sizeof(unsigned int); val <<= 1, bits++);
149
8.50k
  return bits ? bits: 1;
150
8.50k
}