• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37846

22 Jul 2024 01:06AM UTC coverage: 86.908% (+0.8%) from 86.098%
#37846

push

local

web-flow
Preserve Git objects from being garbage collected (#55142)

This issue has been discussed
[here](https://discourse.julialang.org/t/preserve-against-garbage-collection-in-libgit2/117095).

In most cases, thanks to the specialization of `Base.unsafe_convert`, it
is sufficient to replace `obj.ptr` by `obj` in `ccalls` to fix the
issue.

In other cases, for example when a pointer to an internal string is
returned, the code has to be wrapped in `GC.https://github.com/preserve
obj begin ... end` block.

All `LibGit2` tests run successfully. I have left a few `FIXME` comments
where I have doubts about the code, notably with `Ptr{Ptr{Cvoid}}`
arguments.

71 of 73 new or added lines in 15 files covered. (97.26%)

560 existing lines in 28 files now uncovered.

76034 of 87488 relevant lines covered (86.91%)

15443040.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/stdlib/Markdown/src/parse/util.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
macro dotimes(n, body)
4
    quote
5
        for i = 1:$(esc(n))
38,772✔
6
            $(esc(body))
42,564✔
7
        end
46,356✔
8
    end
9
end
10

11
const whitespace = " \t\r"
12

13
"""
14
Skip any leading whitespace. Returns io.
15
"""
16
function skipwhitespace(io::IO; newlines = true)
209,172✔
17
    while !eof(io) && (peek(io, Char) in whitespace || (newlines && peek(io) == UInt8('\n')))
261,025✔
18
        read(io, Char)
51,861✔
19
    end
51,861✔
20
    return io
104,586✔
21
end
22

23
"""
24
Skip any leading blank lines. Returns the number skipped.
25
"""
26
function skipblank(io::IO)
69,715✔
27
    start = position(io)
69,715✔
UNCOV
28
    i = 0
×
29
    for c in readeach(io, Char)
131,701✔
30
        c == '\n' && (start = position(io); i+=1; continue)
90,029✔
31
        c == '\r' && (start = position(io); i+=1; continue)
86,088✔
32
        c in whitespace || break
86,088✔
33
    end
58,306✔
34
    seek(io, start)
139,430✔
35
    return i
69,715✔
36
end
37

38
"""
39
Return true if the line contains only (and, unless allowempty,
40
at least one of) the characters given.
41
"""
42
function linecontains(io::IO, chars; allow_whitespace = true,
120,004✔
43
                                     eat = true,
44
                                     allowempty = false)
45
    start = position(io)
60,002✔
46
    l = readline(io)
60,002✔
47
    length(l) == 0 && return allowempty
60,002✔
48

UNCOV
49
    result = allowempty
×
50
    for c in l
76,918✔
51
        c in whitespace && (allow_whitespace ? continue : (result = false; break))
39,403✔
52
        c in chars && (result = true; continue)
38,459✔
53
        result = false; break
38,459✔
54
    end
1,880✔
55
    !(result && eat) && seek(io, start)
76,920✔
56
    return result
38,461✔
57
end
58

59
blankline(io::IO; eat = true) =
120,004✔
60
    linecontains(io, "",
61
                 allow_whitespace = true,
62
                 allowempty = true,
63
                 eat = eat)
64

65
"""
66
Test if the stream starts with the given string.
67
`eat` specifies whether to advance on success (true by default).
68
`padding` specifies whether leading whitespace should be ignored.
69
"""
70
function startswith(stream::IO, s::AbstractString; eat = true, padding = false, newlines = true)
1,176,808✔
71
    start = position(stream)
588,404✔
72
    padding && skipwhitespace(stream, newlines = newlines)
588,404✔
73
    result = true
3,357✔
74
    for char in s
1,176,808✔
75
        !eof(stream) && read(stream, Char) == char ||
644,905✔
76
            (result = false; break)
3,080✔
77
    end
169,565✔
78
    !(result && eat) && seek(stream, start)
1,120,245✔
79
    return result
588,404✔
80
end
81

82
function startswith(stream::IO, c::AbstractChar; eat = true)
411,624✔
83
    if !eof(stream) && peek(stream) == UInt8(c)
411,038✔
84
        eat && read(stream, Char)
82,029✔
85
        return true
82,029✔
86
    else
87
        return false
329,009✔
88
    end
89
end
90

91
function startswith(stream::IO, ss::Vector{<:AbstractString}; kws...)
×
92
    any(s->startswith(stream, s; kws...), ss)
×
93
end
94

95
function startswith(stream::IO, r::Regex; eat = true, padding = false)
205,756✔
96
    @assert Base.startswith(r.pattern, "^")
102,878✔
97
    start = position(stream)
102,878✔
98
    padding && skipwhitespace(stream)
102,878✔
99
    line = readline(stream)
102,878✔
100
    seek(stream, start)
205,756✔
101
    m = match(r, line)
102,878✔
102
    m === nothing && return ""
102,878✔
103
    eat && @dotimes length(m.match) read(stream, Char)
39,313✔
104
    return m.match
39,313✔
105
end
106

107
"""
108
Executes the block of code, and if the return value is `nothing`,
109
returns the stream to its initial position.
110
"""
111
function withstream(f, stream)
42,743✔
112
    pos = position(stream)
581,176✔
113
    result = f()
581,200✔
114
    (result ≡ nothing || result ≡ false) && seek(stream, pos)
953,657✔
115
    return result
581,176✔
116
end
117

118
"""
119
Consume the standard allowed markdown indent of
120
three spaces. Returns false if there are more than
121
three present.
122
"""
123
function eatindent(io::IO, n = 3)
124
    withstream(io) do
251,694✔
125
        m = 0
113,261✔
126
        while startswith(io, ' ') m += 1 end
165,650✔
127
        return m <= n
113,261✔
128
    end
129
end
130

131
"""
132
Read the stream until startswith(stream, delim)
133
The delimiter is consumed but not included.
134
Returns nothing and resets the stream if delim is
135
not found.
136
"""
137
function readuntil(stream::IO, delimiter; newlines = false, match = nothing)
50,426✔
138
    withstream(stream) do
50,426✔
139
        buffer = IOBuffer()
50,272✔
140
        count = 0
485✔
141
        while !eof(stream)
459,552✔
142
            if startswith(stream, delimiter)
459,398✔
143
                if count == 0
13,675✔
144
                    return String(take!(buffer))
50,118✔
145
                else
146
                    count -= 1
54✔
147
                    write(buffer, delimiter)
54✔
148
                    continue
54✔
149
                end
150
            end
151
            char = read(stream, Char)
409,226✔
152
            char == match && (count += 1)
126,492✔
153
            !newlines && char == '\n' && break
409,226✔
154
            write(buffer, char)
409,226✔
155
        end
409,280✔
156
    end
157
end
158

159
# TODO: refactor this. If we're going to assume
160
# the delimiter is a single character + a minimum
161
# repeat we may as well just pass that into the
162
# function.
163

164
"""
165
Parse a symmetrical delimiter which wraps words.
166
i.e. `*word word*` but not `*word * word`.
167
`repeat` specifies whether the delimiter can be repeated.
168
Escaped delimiters are not yet supported.
169
"""
170
function parse_inline_wrapper(stream::IO, delimiter::AbstractString; rep = false)
44,386✔
171
    delimiter, nmin = string(delimiter[1]), length(delimiter)
44,386✔
172
    withstream(stream) do
22,193✔
173
        if position(stream) >= 1
22,193✔
174
            # check the previous byte isn't a delimiter
175
            skip(stream, -1)
24,348✔
176
            (read(stream, Char) in delimiter) && return nothing
12,174✔
177
        end
178
        n = nmin
22,193✔
179
        startswith(stream, delimiter^n) || return nothing
43,932✔
180
        while startswith(stream, delimiter); n += 1; end
455✔
181
        !rep && n > nmin && return nothing
454✔
182
        !eof(stream) && peek(stream, Char) in whitespace && return nothing
454✔
183

184
        buffer = IOBuffer()
442✔
185
        for char in readeach(stream, Char)
884✔
186
            write(buffer, char)
7,671✔
187
            if !(char in whitespace || char == '\n' || char in delimiter) && startswith(stream, delimiter^n)
14,562✔
188
                trailing = 0
2✔
189
                while startswith(stream, delimiter); trailing += 1; end
334✔
190
                trailing == 0 && return String(take!(buffer))
331✔
191
                write(buffer, delimiter ^ (n + trailing))
3✔
192
            end
193
        end
7,343✔
194
    end
195
end
196

197
function showrest(io::IO)
×
198
    start = position(io)
×
199
    show(read(io, String))
×
200
    println()
×
201
    seek(io, start)
×
202
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc