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