| 1 |
|
| /* |
| 2 |
|
| * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 |
|
| * |
| 4 |
|
| * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 |
|
| * a Linking Exception. For full terms see the included COPYING file. |
| 6 |
|
| */ |
| 7 |
|
| #include "parse.h" |
| 8 |
|
| |
| 9 |
277 |
| int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len) |
| 10 |
|
| { |
| 11 |
277 |
▶ | if (content && content_len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (content && content_len) { |
277 |
0 |
277 |
| if (content && content_len) { |
263 |
14 |
277 |
|
| 12 |
263 |
| ctx->content = content; |
| 13 |
263 |
| ctx->content_len = content_len; |
| 14 |
|
| } else { |
| 15 |
14 |
| ctx->content = ""; |
| 16 |
14 |
| ctx->content_len = 0; |
| 17 |
|
| } |
| 18 |
|
| |
| 19 |
277 |
| ctx->remain = ctx->content; |
| 20 |
277 |
| ctx->remain_len = ctx->content_len; |
| 21 |
277 |
| ctx->line = ctx->remain; |
| 22 |
277 |
| ctx->line_len = git__linenlen(ctx->line, ctx->remain_len); |
| 23 |
277 |
| ctx->line_num = 1; |
| 24 |
|
| |
| 25 |
277 |
| return 0; |
| 26 |
|
| } |
| 27 |
|
| |
| 28 |
51 |
| void git_parse_ctx_clear(git_parse_ctx *ctx) |
| 29 |
|
| { |
| 30 |
51 |
| memset(ctx, 0, sizeof(*ctx)); |
| 31 |
51 |
| ctx->content = ""; |
| 32 |
51 |
| } |
| 33 |
|
| |
| 34 |
2969 |
| void git_parse_advance_line(git_parse_ctx *ctx) |
| 35 |
|
| { |
| 36 |
2969 |
| ctx->line += ctx->line_len; |
| 37 |
2969 |
| ctx->remain_len -= ctx->line_len; |
| 38 |
2969 |
| ctx->line_len = git__linenlen(ctx->line, ctx->remain_len); |
| 39 |
2969 |
| ctx->line_num++; |
| 40 |
2969 |
| } |
| 41 |
|
| |
| 42 |
3253 |
| void git_parse_advance_chars(git_parse_ctx *ctx, size_t char_cnt) |
| 43 |
|
| { |
| 44 |
3253 |
| ctx->line += char_cnt; |
| 45 |
3253 |
| ctx->remain_len -= char_cnt; |
| 46 |
3253 |
| ctx->line_len -= char_cnt; |
| 47 |
3253 |
| } |
| 48 |
|
| |
| 49 |
58 |
| int git_parse_advance_expected( |
| 50 |
|
| git_parse_ctx *ctx, |
| 51 |
|
| const char *expected, |
| 52 |
|
| size_t expected_len) |
| 53 |
|
| { |
| 54 |
58 |
▶ | if (ctx->line_len < expected_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->line_len < expected_len) |
0 |
58 |
58 |
|
| 55 |
0 |
| return -1; |
| 56 |
|
| |
| 57 |
58 |
▶ | if (memcmp(ctx->line, expected, expected_len) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(ctx->line, expected, expected_len) != 0) |
0 |
58 |
58 |
|
| 58 |
0 |
| return -1; |
| 59 |
|
| |
| 60 |
58 |
| git_parse_advance_chars(ctx, expected_len); |
| 61 |
58 |
| return 0; |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
2915 |
| int git_parse_advance_ws(git_parse_ctx *ctx) |
| 65 |
|
| { |
| 66 |
2915 |
| int ret = -1; |
| 67 |
|
| |
| 68 |
2915 |
| while (ctx->line_len > 0 && |
| 69 |
9749 |
▶ | ctx->line[0] != '\n' && |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->line[0] != '\n' && |
4879 |
0 |
4879 |
|
| 70 |
4870 |
| git__isspace(ctx->line[0])) { |
| 71 |
1964 |
| ctx->line++; |
| 72 |
1964 |
| ctx->line_len--; |
| 73 |
1964 |
| ctx->remain_len--; |
| 74 |
1964 |
| ret = 0; |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
2915 |
| return ret; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
0 |
| int git_parse_advance_nl(git_parse_ctx *ctx) |
| 81 |
|
| { |
| 82 |
0 |
▶ | if (ctx->line_len != 1 || ctx->line[0] != '\n') |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->line_len != 1 || ctx->line[0] != '\n') |
0 |
0 |
0 |
| if (ctx->line_len != 1 || ctx->line[0] != '\n') |
0 |
0 |
0 |
|
| 83 |
0 |
| return -1; |
| 84 |
|
| |
| 85 |
0 |
| git_parse_advance_line(ctx); |
| 86 |
0 |
| return 0; |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
8 |
| int git_parse_advance_digit(int64_t *out, git_parse_ctx *ctx, int base) |
| 90 |
|
| { |
| 91 |
|
| const char *end; |
| 92 |
|
| int ret; |
| 93 |
|
| |
| 94 |
8 |
▶ | if (ctx->line_len < 1 || !git__isdigit(ctx->line[0])) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->line_len < 1 || !git__isdigit(ctx->line[0])) |
8 |
0 |
8 |
| if (ctx->line_len < 1 || !git__isdigit(ctx->line[0])) |
0 |
8 |
8 |
|
| 95 |
0 |
| return -1; |
| 96 |
|
| |
| 97 |
8 |
▶ | if ((ret = git__strntol64(out, ctx->line, ctx->line_len, &end, base)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ret = git__strntol64(out, ctx->line, ctx->line_len, &end, base)) < 0) |
0 |
8 |
8 |
|
| 98 |
0 |
| return -1; |
| 99 |
|
| |
| 100 |
8 |
| git_parse_advance_chars(ctx, (end - ctx->line)); |
| 101 |
8 |
| return 0; |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
78 |
| int git_parse_advance_oid(git_oid *out, git_parse_ctx *ctx) |
| 105 |
|
| { |
| 106 |
78 |
▶ | if (ctx->line_len < GIT_OID_HEXSZ) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->line_len < GIT_OID_HEXSZ) |
0 |
78 |
78 |
|
| 107 |
0 |
| return -1; |
| 108 |
78 |
▶ | if ((git_oid_fromstrn(out, ctx->line, GIT_OID_HEXSZ)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((git_oid_fromstrn(out, ctx->line, GIT_OID_HEXSZ)) < 0) |
0 |
78 |
78 |
|
| 109 |
0 |
| return -1; |
| 110 |
78 |
| git_parse_advance_chars(ctx, GIT_OID_HEXSZ); |
| 111 |
78 |
| return 0; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
6000 |
| int git_parse_peek(char *out, git_parse_ctx *ctx, int flags) |
| 115 |
|
| { |
| 116 |
6000 |
| size_t remain = ctx->line_len; |
| 117 |
6000 |
| const char *ptr = ctx->line; |
| 118 |
|
| |
| 119 |
8904 |
▶ | while (remain) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (remain) { |
7962 |
942 |
8904 |
|
| 120 |
7962 |
| char c = *ptr; |
| 121 |
|
| |
| 122 |
13770 |
| if ((flags & GIT_PARSE_PEEK_SKIP_WHITESPACE) && |
| 123 |
5808 |
| git__isspace(c)) { |
| 124 |
2904 |
| remain--; |
| 125 |
2904 |
| ptr++; |
| 126 |
2904 |
| continue; |
| 127 |
|
| } |
| 128 |
|
| |
| 129 |
5058 |
| *out = c; |
| 130 |
5058 |
| return 0; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
942 |
| return -1; |
| 134 |
|
| } |