Allow a word to have an unmatched prefix and suffix.
[wiggle/upstream.git] / split.c
bloba2880acb8a7bf6a16adc45144d30980154cda4b3
1 /*
2 * wiggle - apply rejected patches
4 * Copyright (C) 2003 Neil Brown <neilb@cse.unsw.edu.au>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
26 * Split a stream into words or lines
28 * A word is one of:
29 * string of [A-Za-z0-9_]
30 * or string of [ \t]
31 * or single char (i.e. punctuation and newlines).
33 * A line is any string that ends with \n
35 * As a special case to allow proper aligning of multiple chunks
36 * in a patch, a word starting \0 will include 20 chars with a newline
37 * second from the end.
39 * We make two passes through the stream.
40 * Firstly we count the number of item so an array can be allocated,
41 * then we store start and length of each item in the array
45 #include "wiggle.h"
46 #include <stdlib.h>
47 #include <ctype.h>
48 #include <stdlib.h>
50 #include "ccan/hash/hash.h"
52 static int split_internal(char *start, char *end, int type,
53 struct elmnt *list)
55 int cnt = 0;
57 while (start < end) {
58 char *cp = start;
60 if (*cp == '\0' && cp+19 < end && cp[18] == '\n') {
61 /* special word */
62 cp += 20;
63 } else
64 switch (type) {
65 case ByLine:
66 while (cp < end && *cp != '\n')
67 cp++;
68 if (cp < end)
69 cp++;
70 break;
71 case ByWord:
72 if (isalnum(*cp) || *cp == '_') {
74 cp++;
75 while (cp < end
76 && (isalnum(*cp)
77 || *cp == '_'));
78 } else if (*cp == ' ' || *cp == '\t') {
80 cp++;
81 while (cp < end
82 && (*cp == ' '
83 || *cp == '\t'));
84 } else
85 cp++;
86 break;
88 if (list) {
89 list->start = start;
90 list->len = cp-start;
91 list->plen = list->len;
92 list->prefix = 0;
93 if (*start)
94 list->hash = hash(start, list->len, 0);
95 else
96 list->hash = atoi(start+1);
97 list++;
99 cnt++;
100 start = cp;
102 return cnt;
105 struct file split_stream(struct stream s, int type)
107 int cnt;
108 struct file f;
110 char *c, *end;
112 end = s.body+s.len;
113 c = s.body;
115 cnt = split_internal(c, end, type, NULL);
116 f.list = xmalloc(cnt*sizeof(struct elmnt));
118 f.elcnt = split_internal(c, end, type, f.list);
119 return f;