source src/parse.c
Line | Flow | Count | Block(s) | Source |
---|---|---|---|---|
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 | 23780 | 2 | int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len) | |
10 | - | { | ||
11 | 23780 | 2,3 | if (content && content_len) { | |
12 | 22905 | 4 | ctx->content = content; | |
13 | 22905 | 4 | ctx->content_len = content_len; | |
14 | - | } else { | ||
15 | 875 | 5 | ctx->content = ""; | |
16 | 875 | 5 | ctx->content_len = 0; | |
17 | - | } | ||
18 | - | |||
19 | 23780 | 6 | ctx->remain = ctx->content; | |
20 | 23780 | 6 | ctx->remain_len = ctx->content_len; | |
21 | 23780 | 6 | ctx->line = ctx->remain; | |
22 | 23780 | 6 | ctx->line_len = git__linenlen(ctx->line, ctx->remain_len); | |
23 | 23782 | 7 | ctx->line_num = 1; | |
24 | - | |||
25 | 23782 | 7 | return 0; | |
26 | - | } | ||
27 | - | |||
28 | 4863 | 2 | void git_parse_ctx_clear(git_parse_ctx *ctx) | |
29 | - | { | ||
30 | 4863 | 2 | memset(ctx, 0, sizeof(*ctx)); | |
31 | 4863 | 2 | ctx->content = ""; | |
32 | 4863 | 2 | } | |
33 | - | |||
34 | 182351 | 2 | void git_parse_advance_line(git_parse_ctx *ctx) | |
35 | - | { | ||
36 | 182351 | 2 | ctx->line += ctx->line_len; | |
37 | 182351 | 2 | ctx->remain_len -= ctx->line_len; | |
38 | 182351 | 2 | ctx->line_len = git__linenlen(ctx->line, ctx->remain_len); | |
39 | 182349 | 3 | ctx->line_num++; | |
40 | 182349 | 3 | } | |
41 | - | |||
42 | 99557 | 2 | void git_parse_advance_chars(git_parse_ctx *ctx, size_t char_cnt) | |
43 | - | { | ||
44 | 99557 | 2 | ctx->line += char_cnt; | |
45 | 99557 | 2 | ctx->remain_len -= char_cnt; | |
46 | 99557 | 2 | ctx->line_len -= char_cnt; | |
47 | 99557 | 2 | } | |
48 | - | |||
49 | 2965 | 2 | int git_parse_advance_expected( | |
50 | - | git_parse_ctx *ctx, | ||
51 | - | const char *expected, | ||
52 | - | size_t expected_len) | ||
53 | - | { | ||
54 | 2965 | 2 | if (ctx->line_len < expected_len) | |
55 | ##### | 3 | return -1; | |
56 | - | |||
57 | 2965 | 4 | if (memcmp(ctx->line, expected, expected_len) != 0) | |
58 | ##### | 5 | return -1; | |
59 | - | |||
60 | 2965 | 6 | git_parse_advance_chars(ctx, expected_len); | |
61 | 2965 | 7 | return 0; | |
62 | - | } | ||
63 | - | |||
64 | 178930 | 2 | int git_parse_advance_ws(git_parse_ctx *ctx) | |
65 | - | { | ||
66 | 178930 | 2 | int ret = -1; | |
67 | - | |||
68 | 379758 | 2,4,5 | while (ctx->line_len > 0 && | |
69 | 379726 | 5,7 | ctx->line[0] != '\n' && | |
70 | 378682 | 6 | git__isspace(ctx->line[0])) { | |
71 | 200828 | 3 | ctx->line++; | |
72 | 200828 | 3 | ctx->line_len--; | |
73 | 200828 | 3 | ctx->remain_len--; | |
74 | 200828 | 3 | ret = 0; | |
75 | - | } | ||
76 | - | |||
77 | 178916 | 8 | return ret; | |
78 | - | } | ||
79 | - | |||
80 | 147 | 2 | int git_parse_advance_nl(git_parse_ctx *ctx) | |
81 | - | { | ||
82 | 147 | 2,3 | if (ctx->line_len != 1 || ctx->line[0] != '\n') | |
83 | 1 | 4 | return -1; | |
84 | - | |||
85 | 146 | 5 | git_parse_advance_line(ctx); | |
86 | 146 | 6 | return 0; | |
87 | - | } | ||
88 | - | |||
89 | 1034 | 2 | int git_parse_advance_digit(int64_t *out, git_parse_ctx *ctx, int base) | |
90 | - | { | ||
91 | - | const char *end; | ||
92 | - | int ret; | ||
93 | - | |||
94 | 1034 | 2-4 | if (ctx->line_len < 1 || !git__isdigit(ctx->line[0])) | |
95 | 2 | 5 | return -1; | |
96 | - | |||
97 | 1032 | 6,7 | if ((ret = git__strntol64(out, ctx->line, ctx->line_len, &end, base)) < 0) | |
98 | ##### | 8 | return -1; | |
99 | - | |||
100 | 1032 | 9 | git_parse_advance_chars(ctx, (end - ctx->line)); | |
101 | 1032 | 10 | return 0; | |
102 | - | } | ||
103 | - | |||
104 | 1491 | 2 | int git_parse_advance_oid(git_oid *out, git_parse_ctx *ctx) | |
105 | - | { | ||
106 | 1491 | 2 | if (ctx->line_len < GIT_OID_HEXSZ) | |
107 | ##### | 3 | return -1; | |
108 | 1491 | 4,5 | if ((git_oid_fromstrn(out, ctx->line, GIT_OID_HEXSZ)) < 0) | |
109 | 1 | 6 | return -1; | |
110 | 1490 | 7 | git_parse_advance_chars(ctx, GIT_OID_HEXSZ); | |
111 | 1490 | 8 | return 0; | |
112 | - | } | ||
113 | - | |||
114 | 270578 | 2 | int git_parse_peek(char *out, git_parse_ctx *ctx, int flags) | |
115 | - | { | ||
116 | 270578 | 2 | size_t remain = ctx->line_len; | |
117 | 270578 | 2 | const char *ptr = ctx->line; | |
118 | - | |||
119 | 525758 | 2,8 | while (remain) { | |
120 | 471518 | 3 | char c = *ptr; | |
121 | - | |||
122 | 471518 | 3,5 | if ((flags & GIT_PARSE_PEEK_SKIP_WHITESPACE) && | |
123 | 432785 | 4 | git__isspace(c)) { | |
124 | 255180 | 6 | remain--; | |
125 | 255180 | 6 | ptr++; | |
126 | 255180 | 6 | continue; | |
127 | - | } | ||
128 | - | |||
129 | 216386 | 7 | *out = c; | |
130 | 216386 | 7 | return 0; | |
131 | - | } | ||
132 | - | |||
133 | 54240 | 9 | return -1; | |
134 | - | } |