1 3b0f3d61 2020-01-22 neels /* Split source by line breaks, and calculate a simplistic checksum. */
3 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
5 3b0f3d61 2020-01-22 neels * Permission to use, copy, modify, and distribute this software for any
6 3b0f3d61 2020-01-22 neels * purpose with or without fee is hereby granted, provided that the above
7 3b0f3d61 2020-01-22 neels * copyright notice and this permission notice appear in all copies.
9 3b0f3d61 2020-01-22 neels * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 3b0f3d61 2020-01-22 neels * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 3b0f3d61 2020-01-22 neels * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 3b0f3d61 2020-01-22 neels * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 3b0f3d61 2020-01-22 neels * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 3b0f3d61 2020-01-22 neels * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 3b0f3d61 2020-01-22 neels * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 3b0f3d61 2020-01-22 neels #include <diff/diff_main.h>
21 61a7b578 2020-05-06 neels diff_data_atomize_text_lines(struct diff_data *d)
23 3b0f3d61 2020-01-22 neels const uint8_t *pos = d->data;
24 3b0f3d61 2020-01-22 neels const uint8_t *end = pos + d->len;
26 3b0f3d61 2020-01-22 neels unsigned int array_size_estimate = d->len / 50;
27 3b0f3d61 2020-01-22 neels unsigned int pow2 = 1;
28 3b0f3d61 2020-01-22 neels while (array_size_estimate >>= 1)
31 3b0f3d61 2020-01-22 neels ARRAYLIST_INIT(d->atoms, 1 << pow2);
33 3b0f3d61 2020-01-22 neels while (pos < end) {
34 3b0f3d61 2020-01-22 neels const uint8_t *line_end = pos;
35 3b0f3d61 2020-01-22 neels unsigned int hash = 0;
37 3b0f3d61 2020-01-22 neels while (line_end < end && *line_end != '\r' && *line_end != '\n') {
38 3b0f3d61 2020-01-22 neels hash = hash * 23 + *line_end;
39 3b0f3d61 2020-01-22 neels line_end++;
42 3b0f3d61 2020-01-22 neels /* When not at the end of data, the line ending char ('\r' or '\n') must follow */
43 3b0f3d61 2020-01-22 neels if (line_end < end)
44 3b0f3d61 2020-01-22 neels line_end++;
45 3b0f3d61 2020-01-22 neels /* If that was an '\r', also pull in any following '\n' */
46 3b0f3d61 2020-01-22 neels if (line_end[0] == '\r' && line_end < end && line_end[1] == '\n')
47 3b0f3d61 2020-01-22 neels line_end++;
49 3b0f3d61 2020-01-22 neels /* Record the found line as diff atom */
50 3b0f3d61 2020-01-22 neels struct diff_atom *atom;
51 3b0f3d61 2020-01-22 neels ARRAYLIST_ADD(atom, d->atoms);
53 3b0f3d61 2020-01-22 neels return DIFF_RC_ENOMEM;
55 3b0f3d61 2020-01-22 neels *atom = (struct diff_atom){
57 3b0f3d61 2020-01-22 neels .len = line_end - pos,
58 3b0f3d61 2020-01-22 neels .hash = hash,
61 3b0f3d61 2020-01-22 neels /* Starting point for next line: */
62 3b0f3d61 2020-01-22 neels pos = line_end;
65 3b0f3d61 2020-01-22 neels return DIFF_RC_OK;
68 61a7b578 2020-05-06 neels enum diff_rc
69 61a7b578 2020-05-06 neels diff_atomize_text_by_line(void *func_data, struct diff_data *left, struct diff_data *right)
71 3b0f3d61 2020-01-22 neels enum diff_rc rc;
72 3b0f3d61 2020-01-22 neels rc = diff_data_atomize_text_lines(left);
73 3b0f3d61 2020-01-22 neels if (rc != DIFF_RC_OK)
75 3b0f3d61 2020-01-22 neels return diff_data_atomize_text_lines(right);