Commit aa4bfb32 by Edward Thomson

parse: introduce parse_ctx_contains_s

parent 8d44f8b7
...@@ -42,6 +42,15 @@ typedef struct { ...@@ -42,6 +42,15 @@ typedef struct {
} patch_parse_ctx; } patch_parse_ctx;
GIT_INLINE(bool) parse_ctx_contains(
patch_parse_ctx *ctx, const char *str, size_t len)
{
return (ctx->line_len >= len && memcmp(ctx->line, str, len) == 0);
}
#define parse_ctx_contains_s(ctx, str) \
parse_ctx_contains(ctx, str, sizeof(str) - 1)
static void parse_advance_line(patch_parse_ctx *ctx) static void parse_advance_line(patch_parse_ctx *ctx)
{ {
ctx->line += ctx->line_len; ctx->line += ctx->line_len;
...@@ -593,7 +602,7 @@ static int parse_hunk_body( ...@@ -593,7 +602,7 @@ static int parse_hunk_body(
* localized. Because `diff` optimizes for the case where you * localized. Because `diff` optimizes for the case where you
* want to apply the patch by hand. * want to apply the patch by hand.
*/ */
if (ctx->line_len >= 2 && memcmp(ctx->line, "\\ ", 2) == 0 && if (parse_ctx_contains_s(ctx, "\\ ") &&
git_array_size(patch->base.lines) > 0) { git_array_size(patch->base.lines) > 0) {
line = git_array_get(patch->base.lines, git_array_size(patch->base.lines) - 1); line = git_array_get(patch->base.lines, git_array_size(patch->base.lines) - 1);
...@@ -625,7 +634,7 @@ static int parsed_patch_header( ...@@ -625,7 +634,7 @@ static int parsed_patch_header(
/* This might be a hunk header without a patch header, provide a /* This might be a hunk header without a patch header, provide a
* sensible error message. */ * sensible error message. */
if (memcmp(ctx->line, "@@ -", 4) == 0) { if (parse_ctx_contains_s(ctx, "@@ -")) {
size_t line_num = ctx->line_num; size_t line_num = ctx->line_num;
git_patch_hunk hunk; git_patch_hunk hunk;
...@@ -647,7 +656,7 @@ static int parsed_patch_header( ...@@ -647,7 +656,7 @@ static int parsed_patch_header(
break; break;
/* A proper git patch */ /* A proper git patch */
if (ctx->line_len >= 11 && memcmp(ctx->line, "diff --git ", 11) == 0) { if (parse_ctx_contains_s(ctx, "diff --git ")) {
error = parse_header_git(patch, ctx); error = parse_header_git(patch, ctx);
goto done; goto done;
} }
...@@ -671,16 +680,15 @@ static int parsed_patch_binary_side( ...@@ -671,16 +680,15 @@ static int parsed_patch_binary_side(
git_off_t len; git_off_t len;
int error = 0; int error = 0;
if (ctx->line_len >= 8 && memcmp(ctx->line, "literal ", 8) == 0) { if (parse_ctx_contains_s(ctx, "literal ")) {
type = GIT_DIFF_BINARY_LITERAL; type = GIT_DIFF_BINARY_LITERAL;
parse_advance_chars(ctx, 8); parse_advance_chars(ctx, 8);
} } else if (parse_ctx_contains_s(ctx, "delta ")) {
else if (ctx->line_len >= 6 && memcmp(ctx->line, "delta ", 6) == 0) {
type = GIT_DIFF_BINARY_DELTA; type = GIT_DIFF_BINARY_DELTA;
parse_advance_chars(ctx, 6); parse_advance_chars(ctx, 6);
} } else {
else { error = parse_err(
error = parse_err("unknown binary delta type at line %d", ctx->line_num); "unknown binary delta type at line %d", ctx->line_num);
goto done; goto done;
} }
...@@ -777,8 +785,7 @@ static int parsed_patch_hunks( ...@@ -777,8 +785,7 @@ static int parsed_patch_hunks(
git_patch_hunk *hunk; git_patch_hunk *hunk;
int error = 0; int error = 0;
for (; ctx->line_len > 4 && memcmp(ctx->line, "@@ -", 4) == 0; ) { while (parse_ctx_contains_s(ctx, "@@ -")) {
hunk = git_array_alloc(patch->base.hunks); hunk = git_array_alloc(patch->base.hunks);
GITERR_CHECK_ALLOC(hunk); GITERR_CHECK_ALLOC(hunk);
...@@ -799,10 +806,10 @@ done: ...@@ -799,10 +806,10 @@ done:
static int parsed_patch_body( static int parsed_patch_body(
git_patch_parsed *patch, patch_parse_ctx *ctx) git_patch_parsed *patch, patch_parse_ctx *ctx)
{ {
if (ctx->line_len >= 16 && memcmp(ctx->line, "GIT binary patch", 16) == 0) if (parse_ctx_contains_s(ctx, "GIT binary patch"))
return parsed_patch_binary(patch, ctx); return parsed_patch_binary(patch, ctx);
else if (ctx->line_len >= 4 && memcmp(ctx->line, "@@ -", 4) == 0) else if (parse_ctx_contains_s(ctx, "@@ -"))
return parsed_patch_hunks(patch, ctx); return parsed_patch_hunks(patch, ctx);
return 0; return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment