Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Belfer <arrbee@arrbee.com>2012-03-03 03:09:40 +0400
committerRussell Belfer <arrbee@arrbee.com>2012-03-03 03:09:40 +0400
commitce49c7a8a902bd3a74a59a356dd11886e83d2e92 (patch)
treee336ef425b5bda0e7c05acfbcf51fabb758ca662 /src/crlf.c
parentd377fe80b1396b82f8af7bfcd76f869410865001 (diff)
Add filter tests and fix some bugs
This adds some initial unit tests for file filtering and fixes some simple bugs in filter application.
Diffstat (limited to 'src/crlf.c')
-rw-r--r--src/crlf.c60
1 files changed, 20 insertions, 40 deletions
diff --git a/src/crlf.c b/src/crlf.c
index 404156d6a..f0ec7b736 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -104,52 +104,32 @@ static int crlf_load_attributes(struct crlf_attrs *ca, git_repository *repo, con
static int drop_crlf(git_buf *dest, const git_buf *source)
{
- size_t psize = source->size - 1;
- size_t i = 0;
+ const char *scan = source->ptr, *next;
+ const char *scan_end = source->ptr + source->size;
- /* Initial scan: see if we can reach the end of the document
- * without finding a single carriage return */
- while (i < psize && source->ptr[i] != '\r')
- i++;
-
- /* Clean file? Tell the library to skip this filter */
- if (i == psize)
- return -1;
-
- /* Main scan loop. Keep moving forward until we find a carriage
- * return, and then copy the whole chunk to the destination
- * buffer.
- *
- * Note that we only scan until `size - 1`, because we cannot drop a
- * carriage return if it's the last character in the file (what a weird
- * file, anyway)
+ /* Main scan loop. Find the next carriage return and copy the
+ * whole chunk up to that point to the destination buffer.
*/
- while (i < psize) {
- size_t org = i;
+ while ((next = memchr(scan, '\r', scan_end - scan)) != NULL) {
+ /* copy input up to \r */
+ if (next > scan)
+ git_buf_put(dest, scan, next - scan);
- while (i < psize && source->ptr[i] != '\r')
- i++;
-
- if (i > org)
- git_buf_put(dest, source->ptr + org, i - org);
-
- /* We found a carriage return. Is the next character a newline?
- * If it is, we just keep moving. The newline will be copied
- * to the dest in the next chunk.
- *
- * If it's not a newline, we need to insert the carriage return
- * into the dest buffer, because we don't drop lone CRs.
- */
- if (source->ptr[i + 1] != '\n') {
+ /* Do not drop \r unless it is followed by \n */
+ if (*(next + 1) != '\n')
git_buf_putc(dest, '\r');
- }
-
- i++;
+
+ scan = next + 1;
}
- /* Copy the last character in the file */
- git_buf_putc(dest, source->ptr[psize]);
- return 0;
+ /* If there was no \r, then tell the library to skip this filter */
+ if (scan == source->ptr)
+ return -1;
+
+ /* Copy remaining input into dest */
+ git_buf_put(dest, scan, scan_end - scan);
+
+ return git_buf_lasterror(dest);
}
static int crlf_apply_to_odb(git_filter *self, git_buf *dest, const git_buf *source)