Coverage Report

Created: 2024-09-08 06:23

/src/git/reftable/basics.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
Copyright 2020 Google LLC
3
4
Use of this source code is governed by a BSD-style
5
license that can be found in the LICENSE file or at
6
https://developers.google.com/open-source/licenses/bsd
7
*/
8
9
#include "basics.h"
10
11
void put_be24(uint8_t *out, uint32_t i)
12
0
{
13
0
  out[0] = (uint8_t)((i >> 16) & 0xff);
14
0
  out[1] = (uint8_t)((i >> 8) & 0xff);
15
0
  out[2] = (uint8_t)(i & 0xff);
16
0
}
17
18
uint32_t get_be24(uint8_t *in)
19
0
{
20
0
  return (uint32_t)(in[0]) << 16 | (uint32_t)(in[1]) << 8 |
21
0
         (uint32_t)(in[2]);
22
0
}
23
24
void put_be16(uint8_t *out, uint16_t i)
25
0
{
26
0
  out[0] = (uint8_t)((i >> 8) & 0xff);
27
0
  out[1] = (uint8_t)(i & 0xff);
28
0
}
29
30
size_t binsearch(size_t sz, int (*f)(size_t k, void *args), void *args)
31
0
{
32
0
  size_t lo = 0;
33
0
  size_t hi = sz;
34
35
  /* Invariants:
36
   *
37
   *  (hi == sz) || f(hi) == true
38
   *  (lo == 0 && f(0) == true) || fi(lo) == false
39
   */
40
0
  while (hi - lo > 1) {
41
0
    size_t mid = lo + (hi - lo) / 2;
42
0
    int ret = f(mid, args);
43
0
    if (ret < 0)
44
0
      return sz;
45
46
0
    if (ret > 0)
47
0
      hi = mid;
48
0
    else
49
0
      lo = mid;
50
0
  }
51
52
0
  if (lo)
53
0
    return hi;
54
55
0
  return f(0, args) ? 0 : 1;
56
0
}
57
58
void free_names(char **a)
59
0
{
60
0
  char **p;
61
0
  if (!a) {
62
0
    return;
63
0
  }
64
0
  for (p = a; *p; p++) {
65
0
    reftable_free(*p);
66
0
  }
67
0
  reftable_free(a);
68
0
}
69
70
size_t names_length(const char **names)
71
0
{
72
0
  const char **p = names;
73
0
  while (*p)
74
0
    p++;
75
0
  return p - names;
76
0
}
77
78
void parse_names(char *buf, int size, char ***namesp)
79
0
{
80
0
  char **names = NULL;
81
0
  size_t names_cap = 0;
82
0
  size_t names_len = 0;
83
84
0
  char *p = buf;
85
0
  char *end = buf + size;
86
0
  while (p < end) {
87
0
    char *next = strchr(p, '\n');
88
0
    if (next && next < end) {
89
0
      *next = 0;
90
0
    } else {
91
0
      next = end;
92
0
    }
93
0
    if (p < next) {
94
0
      REFTABLE_ALLOC_GROW(names, names_len + 1, names_cap);
95
0
      names[names_len++] = xstrdup(p);
96
0
    }
97
0
    p = next + 1;
98
0
  }
99
100
0
  REFTABLE_REALLOC_ARRAY(names, names_len + 1);
101
0
  names[names_len] = NULL;
102
0
  *namesp = names;
103
0
}
104
105
int names_equal(const char **a, const char **b)
106
0
{
107
0
  size_t i = 0;
108
0
  for (; a[i] && b[i]; i++)
109
0
    if (strcmp(a[i], b[i]))
110
0
      return 0;
111
0
  return a[i] == b[i];
112
0
}
113
114
int common_prefix_size(struct strbuf *a, struct strbuf *b)
115
0
{
116
0
  int p = 0;
117
0
  for (; p < a->len && p < b->len; p++) {
118
0
    if (a->buf[p] != b->buf[p])
119
0
      break;
120
0
  }
121
122
0
  return p;
123
0
}