Sync with maint
[git] / xdiff / xutils.c
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, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 long xdl_bogosqrt(long n) {
31         long i;
32
33         /*
34          * Classical integer square root approximation using shifts.
35          */
36         for (i = 1; n > 0; n >>= 2)
37                 i <<= 1;
38
39         return i;
40 }
41
42
43 int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
44                      xdemitcb_t *ecb) {
45         int i = 2;
46         mmbuffer_t mb[3];
47
48         mb[0].ptr = (char *) pre;
49         mb[0].size = psize;
50         mb[1].ptr = (char *) rec;
51         mb[1].size = size;
52         if (size > 0 && rec[size - 1] != '\n') {
53                 mb[2].ptr = (char *) "\n\\ No newline at end of file\n";
54                 mb[2].size = strlen(mb[2].ptr);
55                 i++;
56         }
57         if (ecb->outf(ecb->priv, mb, i) < 0) {
58
59                 return -1;
60         }
61
62         return 0;
63 }
64
65 void *xdl_mmfile_first(mmfile_t *mmf, long *size)
66 {
67         *size = mmf->size;
68         return mmf->ptr;
69 }
70
71
72 long xdl_mmfile_size(mmfile_t *mmf)
73 {
74         return mmf->size;
75 }
76
77
78 int xdl_cha_init(chastore_t *cha, long isize, long icount) {
79
80         cha->head = cha->tail = NULL;
81         cha->isize = isize;
82         cha->nsize = icount * isize;
83         cha->ancur = cha->sncur = NULL;
84         cha->scurr = 0;
85
86         return 0;
87 }
88
89
90 void xdl_cha_free(chastore_t *cha) {
91         chanode_t *cur, *tmp;
92
93         for (cur = cha->head; (tmp = cur) != NULL;) {
94                 cur = cur->next;
95                 xdl_free(tmp);
96         }
97 }
98
99
100 void *xdl_cha_alloc(chastore_t *cha) {
101         chanode_t *ancur;
102         void *data;
103
104         if (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) {
105                 if (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) {
106
107                         return NULL;
108                 }
109                 ancur->icurr = 0;
110                 ancur->next = NULL;
111                 if (cha->tail)
112                         cha->tail->next = ancur;
113                 if (!cha->head)
114                         cha->head = ancur;
115                 cha->tail = ancur;
116                 cha->ancur = ancur;
117         }
118
119         data = (char *) ancur + sizeof(chanode_t) + ancur->icurr;
120         ancur->icurr += cha->isize;
121
122         return data;
123 }
124
125
126 void *xdl_cha_first(chastore_t *cha) {
127         chanode_t *sncur;
128
129         if (!(cha->sncur = sncur = cha->head))
130                 return NULL;
131
132         cha->scurr = 0;
133
134         return (char *) sncur + sizeof(chanode_t) + cha->scurr;
135 }
136
137
138 void *xdl_cha_next(chastore_t *cha) {
139         chanode_t *sncur;
140
141         if (!(sncur = cha->sncur))
142                 return NULL;
143         cha->scurr += cha->isize;
144         if (cha->scurr == sncur->icurr) {
145                 if (!(sncur = cha->sncur = sncur->next))
146                         return NULL;
147                 cha->scurr = 0;
148         }
149
150         return (char *) sncur + sizeof(chanode_t) + cha->scurr;
151 }
152
153
154 long xdl_guess_lines(mmfile_t *mf, long sample) {
155         long nl = 0, size, tsize = 0;
156         char const *data, *cur, *top;
157
158         if ((cur = data = xdl_mmfile_first(mf, &size)) != NULL) {
159                 for (top = data + size; nl < sample && cur < top; ) {
160                         nl++;
161                         if (!(cur = memchr(cur, '\n', top - cur)))
162                                 cur = top;
163                         else
164                                 cur++;
165                 }
166                 tsize += (long) (cur - data);
167         }
168
169         if (nl && tsize)
170                 nl = xdl_mmfile_size(mf) / (tsize / nl);
171
172         return nl + 1;
173 }
174
175 int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
176 {
177         int i1, i2;
178
179         if (s1 == s2 && !memcmp(l1, l2, s1))
180                 return 1;
181         if (!(flags & XDF_WHITESPACE_FLAGS))
182                 return 0;
183
184         i1 = 0;
185         i2 = 0;
186
187         /*
188          * -w matches everything that matches with -b, and -b in turn
189          * matches everything that matches with --ignore-space-at-eol.
190          *
191          * Each flavor of ignoring needs different logic to skip whitespaces
192          * while we have both sides to compare.
193          */
194         if (flags & XDF_IGNORE_WHITESPACE) {
195                 goto skip_ws;
196                 while (i1 < s1 && i2 < s2) {
197                         if (l1[i1++] != l2[i2++])
198                                 return 0;
199                 skip_ws:
200                         while (i1 < s1 && XDL_ISSPACE(l1[i1]))
201                                 i1++;
202                         while (i2 < s2 && XDL_ISSPACE(l2[i2]))
203                                 i2++;
204                 }
205         } else if (flags & XDF_IGNORE_WHITESPACE_CHANGE) {
206                 while (i1 < s1 && i2 < s2) {
207                         if (XDL_ISSPACE(l1[i1]) && XDL_ISSPACE(l2[i2])) {
208                                 /* Skip matching spaces and try again */
209                                 while (i1 < s1 && XDL_ISSPACE(l1[i1]))
210                                         i1++;
211                                 while (i2 < s2 && XDL_ISSPACE(l2[i2]))
212                                         i2++;
213                                 continue;
214                         }
215                         if (l1[i1++] != l2[i2++])
216                                 return 0;
217                 }
218         } else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL) {
219                 while (i1 < s1 && i2 < s2 && l1[i1++] == l2[i2++])
220                         ; /* keep going */
221         }
222
223         /*
224          * After running out of one side, the remaining side must have
225          * nothing but whitespace for the lines to match.  Note that
226          * ignore-whitespace-at-eol case may break out of the loop
227          * while there still are characters remaining on both lines.
228          */
229         if (i1 < s1) {
230                 while (i1 < s1 && XDL_ISSPACE(l1[i1]))
231                         i1++;
232                 if (s1 != i1)
233                         return 0;
234         }
235         if (i2 < s2) {
236                 while (i2 < s2 && XDL_ISSPACE(l2[i2]))
237                         i2++;
238                 return (s2 == i2);
239         }
240         return 1;
241 }
242
243 static unsigned long xdl_hash_record_with_whitespace(char const **data,
244                 char const *top, long flags) {
245         unsigned long ha = 5381;
246         char const *ptr = *data;
247
248         for (; ptr < top && *ptr != '\n'; ptr++) {
249                 if (XDL_ISSPACE(*ptr)) {
250                         const char *ptr2 = ptr;
251                         int at_eol;
252                         while (ptr + 1 < top && XDL_ISSPACE(ptr[1])
253                                         && ptr[1] != '\n')
254                                 ptr++;
255                         at_eol = (top <= ptr + 1 || ptr[1] == '\n');
256                         if (flags & XDF_IGNORE_WHITESPACE)
257                                 ; /* already handled */
258                         else if (flags & XDF_IGNORE_WHITESPACE_CHANGE
259                                  && !at_eol) {
260                                 ha += (ha << 5);
261                                 ha ^= (unsigned long) ' ';
262                         }
263                         else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
264                                  && !at_eol) {
265                                 while (ptr2 != ptr + 1) {
266                                         ha += (ha << 5);
267                                         ha ^= (unsigned long) *ptr2;
268                                         ptr2++;
269                                 }
270                         }
271                         continue;
272                 }
273                 ha += (ha << 5);
274                 ha ^= (unsigned long) *ptr;
275         }
276         *data = ptr < top ? ptr + 1: ptr;
277
278         return ha;
279 }
280
281 #ifdef XDL_FAST_HASH
282
283 #define ONEBYTES        0x0101010101010101ul
284 #define NEWLINEBYTES    0x0a0a0a0a0a0a0a0aul
285 #define HIGHBITS        0x8080808080808080ul
286
287 /* Return the high bit set in the first byte that is a zero */
288 static inline unsigned long has_zero(unsigned long a)
289 {
290         return ((a - ONEBYTES) & ~a) & HIGHBITS;
291 }
292
293 static inline long count_masked_bytes(unsigned long mask)
294 {
295         if (sizeof(long) == 8) {
296                 /*
297                  * Jan Achrenius on G+: microoptimized version of
298                  * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
299                  * that works for the bytemasks without having to
300                  * mask them first.
301                  */
302                 return mask * 0x0001020304050608 >> 56;
303         } else {
304                 /*
305                  * Modified Carl Chatfield G+ version for 32-bit *
306                  *
307                  * (a) gives us
308                  *   -1 (0, ff), 0 (ffff) or 1 (ffffff)
309                  * (b) gives us
310                  *   0 for 0, 1 for (ff ffff ffffff)
311                  * (a+b+1) gives us
312                  *   correct 0-3 bytemask count result
313                  */
314                 long a = (mask - 256) >> 23;
315                 long b = mask & 1;
316                 return a + b + 1;
317         }
318 }
319
320 unsigned long xdl_hash_record(char const **data, char const *top, long flags)
321 {
322         unsigned long hash = 5381;
323         unsigned long a = 0, mask = 0;
324         char const *ptr = *data;
325         char const *end = top - sizeof(unsigned long) + 1;
326
327         if (flags & XDF_WHITESPACE_FLAGS)
328                 return xdl_hash_record_with_whitespace(data, top, flags);
329
330         ptr -= sizeof(unsigned long);
331         do {
332                 hash += hash << 5;
333                 hash ^= a;
334                 ptr += sizeof(unsigned long);
335                 if (ptr >= end)
336                         break;
337                 a = *(unsigned long *)ptr;
338                 /* Do we have any '\n' bytes in this word? */
339                 mask = has_zero(a ^ NEWLINEBYTES);
340         } while (!mask);
341
342         if (ptr >= end) {
343                 /*
344                  * There is only a partial word left at the end of the
345                  * buffer. Because we may work with a memory mapping,
346                  * we have to grab the rest byte by byte instead of
347                  * blindly reading it.
348                  *
349                  * To avoid problems with masking in a signed value,
350                  * we use an unsigned char here.
351                  */
352                 const char *p;
353                 for (p = top - 1; p >= ptr; p--)
354                         a = (a << 8) + *((const unsigned char *)p);
355                 mask = has_zero(a ^ NEWLINEBYTES);
356                 if (!mask)
357                         /*
358                          * No '\n' found in the partial word.  Make a
359                          * mask that matches what we read.
360                          */
361                         mask = 1UL << (8 * (top - ptr) + 7);
362         }
363
364         /* The mask *below* the first high bit set */
365         mask = (mask - 1) & ~mask;
366         mask >>= 7;
367         hash += hash << 5;
368         hash ^= a & mask;
369
370         /* Advance past the last (possibly partial) word */
371         ptr += count_masked_bytes(mask);
372
373         if (ptr < top) {
374                 assert(*ptr == '\n');
375                 ptr++;
376         }
377
378         *data = ptr;
379
380         return hash;
381 }
382
383 #else /* XDL_FAST_HASH */
384
385 unsigned long xdl_hash_record(char const **data, char const *top, long flags) {
386         unsigned long ha = 5381;
387         char const *ptr = *data;
388
389         if (flags & XDF_WHITESPACE_FLAGS)
390                 return xdl_hash_record_with_whitespace(data, top, flags);
391
392         for (; ptr < top && *ptr != '\n'; ptr++) {
393                 ha += (ha << 5);
394                 ha ^= (unsigned long) *ptr;
395         }
396         *data = ptr < top ? ptr + 1: ptr;
397
398         return ha;
399 }
400
401 #endif /* XDL_FAST_HASH */
402
403 unsigned int xdl_hashbits(unsigned int size) {
404         unsigned int val = 1, bits = 0;
405
406         for (; val < size && bits < CHAR_BIT * sizeof(unsigned int); val <<= 1, bits++);
407         return bits ? bits: 1;
408 }
409
410
411 int xdl_num_out(char *out, long val) {
412         char *ptr, *str = out;
413         char buf[32];
414
415         ptr = buf + sizeof(buf) - 1;
416         *ptr = '\0';
417         if (val < 0) {
418                 *--ptr = '-';
419                 val = -val;
420         }
421         for (; val && ptr > buf; val /= 10)
422                 *--ptr = "0123456789"[val % 10];
423         if (*ptr)
424                 for (; *ptr; ptr++, str++)
425                         *str = *ptr;
426         else
427                 *str++ = '0';
428         *str = '\0';
429
430         return str - out;
431 }
432
433
434 long xdl_atol(char const *str, char const **next) {
435         long val, base;
436         char const *top;
437
438         for (top = str; XDL_ISDIGIT(*top); top++);
439         if (next)
440                 *next = top;
441         for (val = 0, base = 1, top--; top >= str; top--, base *= 10)
442                 val += base * (long)(*top - '0');
443         return val;
444 }
445
446
447 int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
448                       const char *func, long funclen, xdemitcb_t *ecb) {
449         int nb = 0;
450         mmbuffer_t mb;
451         char buf[128];
452
453         memcpy(buf, "@@ -", 4);
454         nb += 4;
455
456         nb += xdl_num_out(buf + nb, c1 ? s1: s1 - 1);
457
458         if (c1 != 1) {
459                 memcpy(buf + nb, ",", 1);
460                 nb += 1;
461
462                 nb += xdl_num_out(buf + nb, c1);
463         }
464
465         memcpy(buf + nb, " +", 2);
466         nb += 2;
467
468         nb += xdl_num_out(buf + nb, c2 ? s2: s2 - 1);
469
470         if (c2 != 1) {
471                 memcpy(buf + nb, ",", 1);
472                 nb += 1;
473
474                 nb += xdl_num_out(buf + nb, c2);
475         }
476
477         memcpy(buf + nb, " @@", 3);
478         nb += 3;
479         if (func && funclen) {
480                 buf[nb++] = ' ';
481                 if (funclen > sizeof(buf) - nb - 1)
482                         funclen = sizeof(buf) - nb - 1;
483                 memcpy(buf + nb, func, funclen);
484                 nb += funclen;
485         }
486         buf[nb++] = '\n';
487
488         mb.ptr = buf;
489         mb.size = nb;
490         if (ecb->outf(ecb->priv, &mb, 1) < 0)
491                 return -1;
492
493         return 0;
494 }
495
496 int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp,
497                 int line1, int count1, int line2, int count2)
498 {
499         /*
500          * This probably does not work outside Git, since
501          * we have a very simple mmfile structure.
502          *
503          * Note: ideally, we would reuse the prepared environment, but
504          * the libxdiff interface does not (yet) allow for diffing only
505          * ranges of lines instead of the whole files.
506          */
507         mmfile_t subfile1, subfile2;
508         xdfenv_t env;
509
510         subfile1.ptr = (char *)diff_env->xdf1.recs[line1 - 1]->ptr;
511         subfile1.size = diff_env->xdf1.recs[line1 + count1 - 2]->ptr +
512                 diff_env->xdf1.recs[line1 + count1 - 2]->size - subfile1.ptr;
513         subfile2.ptr = (char *)diff_env->xdf2.recs[line2 - 1]->ptr;
514         subfile2.size = diff_env->xdf2.recs[line2 + count2 - 2]->ptr +
515                 diff_env->xdf2.recs[line2 + count2 - 2]->size - subfile2.ptr;
516         if (xdl_do_diff(&subfile1, &subfile2, xpp, &env) < 0)
517                 return -1;
518
519         memcpy(diff_env->xdf1.rchg + line1 - 1, env.xdf1.rchg, count1);
520         memcpy(diff_env->xdf2.rchg + line2 - 1, env.xdf2.rchg, count2);
521
522         xdl_free_env(&env);
523
524         return 0;
525 }