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

JuliaLang / julia / #37932

14 Oct 2024 03:27AM UTC coverage: 86.441% (-1.3%) from 87.739%
#37932

push

local

web-flow
🤖 [master] Bump the Pkg stdlib from fbaa2e337 to 27c1b1ee5 (#56146)

77755 of 89952 relevant lines covered (86.44%)

16248852.99 hits per line

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

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

3
### Parsing utilities
4

5
_directives(::Type{DateFormat{S,T}}) where {S,T} = T.parameters
15✔
6

7
character_codes(df::Type{DateFormat{S,T}}) where {S,T} = character_codes(_directives(df))
10✔
8
function character_codes(directives::Core.SimpleVector)
15✔
9
    letters = sizehint!(Char[], length(directives))
15✔
10
    for (i, directive) in enumerate(directives)
30✔
11
        if directive <: DatePart
129✔
12
            letter = first(directive.parameters)
69✔
13
            push!(letters, letter)
69✔
14
        end
15
    end
243✔
16
    return letters
15✔
17
end
18

19
genvar(t::DataType) = Symbol(lowercase(string(nameof(t))))
127✔
20

21
"""
22
    tryparsenext_core(str::AbstractString, pos::Int, len::Int, df::DateFormat, raise=false)
23

24
Parse the string according to the directives within the `DateFormat`. Parsing will start at
25
character index `pos` and will stop when all directives are used or we have parsed up to
26
the end of the string, `len`. When a directive cannot be parsed the returned value
27
will be `nothing` if `raise` is false otherwise an exception will be thrown.
28

29
If successful, return a 3-element tuple `(values, pos, num_parsed)`:
30
* `values::Tuple`: A tuple which contains a value
31
  for each `DatePart` within the `DateFormat` in the order
32
  in which they occur. If the string ends before we finish parsing all the directives
33
  the missing values will be filled in with default values.
34
* `pos::Int`: The character index at which parsing stopped.
35
* `num_parsed::Int`: The number of values which were parsed and stored within `values`.
36
  Useful for distinguishing parsed values from default values.
37
"""
38
@generated function tryparsenext_core(str::AbstractString, pos::Int, len::Int,
13✔
39
                                      df::DateFormat, raise::Bool=false)
40
    directives = _directives(df)
5✔
41
    letters = character_codes(directives)
5✔
42

43
    tokens = Type[CONVERSION_SPECIFIERS[letter] for letter in letters]
5✔
44
    value_names = Symbol[genvar(t) for t in tokens]
5✔
45
    value_defaults = Tuple(CONVERSION_DEFAULTS[t] for t in tokens)
5✔
46

47
    # Pre-assign variables to defaults. Allows us to use `@goto done` without worrying about
48
    # unassigned variables.
49
    assign_defaults = Expr[]
5✔
50
    for (name, default) in zip(value_names, value_defaults)
10✔
51
        push!(assign_defaults, quote
23✔
52
            $name = $default
13✔
53
        end)
54
    end
41✔
55

56
    vi = 1
5✔
57
    parsers = Expr[]
5✔
58
    for i = 1:length(directives)
5✔
59
        if directives[i] <: DatePart
43✔
60
            name = value_names[vi]
23✔
61
            vi += 1
23✔
62
            push!(parsers, quote
23✔
63
                pos > len && @goto done
47✔
64
                let val = tryparsenext(directives[$i], str, pos, len, locale)
94✔
65
                    val === nothing && @goto error
47✔
66
                    $name, pos = val
47✔
67
                end
68
                num_parsed += 1
47✔
69
                directive_index += 1
47✔
70
            end)
71
        else
72
            push!(parsers, quote
20✔
73
                pos > len && @goto done
37✔
74
                let val = tryparsenext(directives[$i], str, pos, len, locale)
72✔
75
                    val === nothing && @goto error
36✔
76
                    delim, pos = val
36✔
77
                end
78
                directive_index += 1
36✔
79
            end)
80
        end
81
    end
43✔
82

83
    return quote
5✔
84
        directives = df.tokens
13✔
85
        locale::DateLocale = df.locale
13✔
86

87
        num_parsed = 0
13✔
88
        directive_index = 1
13✔
89

90
        $(assign_defaults...)
91
        $(parsers...)
92

93
        pos > len || @goto error
12✔
94

95
        @label done
96
        return $(Expr(:tuple, value_names...)), pos, num_parsed
13✔
97

98
        @label error
99
        if raise
×
100
            if directive_index > length(directives)
×
101
                throw(ArgumentError("Found extra characters at the end of date time string"))
×
102
            else
103
                d = directives[directive_index]
×
104
                throw(ArgumentError("Unable to parse date time. Expected directive $d at char $pos"))
×
105
            end
106
        end
107
        return nothing
×
108
    end
109
end
110

111
"""
112
    tryparsenext_internal(::Type{<:TimeType}, str, pos, len, df::DateFormat, raise=false)
113

114
Parse the string according to the directives within the `DateFormat`. The specified `TimeType`
115
type determines the type of and order of tokens returned. If the given `DateFormat` or string
116
does not provide a required token a default value will be used. When the string cannot be
117
parsed the returned value will be `nothing` if `raise` is false otherwise an exception will
118
be thrown.
119

120
If successful, returns a 2-element tuple `(values, pos)`:
121
* `values::Tuple`: A tuple which contains a value
122
  for each token as specified by the passed in type.
123
* `pos::Int`: The character index at which parsing stopped.
124
"""
125
@generated function tryparsenext_internal(::Type{T}, str::AbstractString, pos::Int, len::Int,
26✔
126
                                          df::DateFormat, raise::Bool=false) where T<:TimeType
127
    letters = character_codes(df)
20✔
128

129
    tokens = Type[CONVERSION_SPECIFIERS[letter] for letter in letters]
10✔
130
    value_names = Symbol[genvar(t) for t in tokens]
10✔
131

132
    output_tokens = CONVERSION_TRANSLATIONS[T]
10✔
133
    output_names = Symbol[genvar(t) for t in output_tokens]
68✔
134
    output_defaults = Tuple(CONVERSION_DEFAULTS[t] for t in output_tokens)
10✔
135

136
    # Pre-assign output variables to defaults. Ensures that all output variables are
137
    # assigned as the value tuple returned from `tryparsenext_core` may not include all
138
    # of the required variables.
139
    assign_defaults = Expr[
68✔
140
        quote
141
            $name = $default
13✔
142
        end
143
        for (name, default) in zip(output_names, output_defaults)
144
    ]
145

146
    # Unpacks the value tuple returned by `tryparsenext_core` into separate variables.
147
    value_tuple = Expr(:tuple, value_names...)
10✔
148

149
    return quote
10✔
150
        val = tryparsenext_core(str, pos, len, df, raise)
13✔
151
        val === nothing && return nothing
13✔
152
        values, pos, num_parsed = val
13✔
153
        $(assign_defaults...)
154
        $value_tuple = values
13✔
155
        return $(Expr(:tuple, output_names...)), pos
13✔
156
    end
157
end
158

159
@inline function tryparsenext_sign(str::AbstractString, i::Int, len::Int)
160
    i > len && return nothing
29✔
161
    c, ii = iterate(str, i)::Tuple{Char, Int}
58✔
162
    if c == '+'
29✔
163
        return 1, ii
×
164
    elseif c == '-'
29✔
165
        return -1, ii
×
166
    else
167
        return nothing
29✔
168
    end
169
end
170

171
@inline function tryparsenext_base10(str::AbstractString, i::Int, len::Int, min_width::Int=1, max_width::Int=0)
172
    i > len && return nothing
191✔
173
    min_pos = min_width <= 0 ? i : i + min_width - 1
202✔
174
    max_pos = max_width <= 0 ? len : min(i + max_width - 1, len)
155✔
175
    d::Int64 = 0
155✔
176
    @inbounds while i <= max_pos
155✔
177
        c, ii = iterate(str, i)::Tuple{Char, Int}
838✔
178
        if '0' <= c <= '9'
419✔
179
            d = d * 10 + (c - '0')
365✔
180
        else
181
            break
54✔
182
        end
183
        i = ii
365✔
184
    end
365✔
185
    if i <= min_pos
155✔
186
        return nothing
×
187
    else
188
        return d, i
155✔
189
    end
190
end
191

192
@inline function tryparsenext_word(str::AbstractString, i, len, locale, maxchars=0)
×
193
    word_start, word_end = i, 0
×
194
    max_pos = maxchars <= 0 ? len : min(len, nextind(str, i, maxchars-1))
×
195
    @inbounds while i <= max_pos
×
196
        c, ii = iterate(str, i)::Tuple{Char, Int}
×
197
        if isletter(c)
×
198
            word_end = i
×
199
        else
200
            break
×
201
        end
202
        i = ii
×
203
    end
×
204
    if word_end == 0
×
205
        return nothing
×
206
    else
207
        return SubString(str, word_start, word_end), i
×
208
    end
209
end
210

211
function Base.parse(::Type{DateTime}, s::AbstractString, df::typeof(ISODateTimeFormat))
18✔
212
    i, end_pos = firstindex(s), lastindex(s)
36✔
213
    i > end_pos && throw(ArgumentError("Cannot parse an empty string as a DateTime"))
18✔
214

215
    coefficient = 1
18✔
216
    local dy
217
    dm = dd = Int64(1)
18✔
218
    th = tm = ts = tms = Int64(0)
18✔
219

220
    # Optional sign
221
    let val = tryparsenext_sign(s, i, end_pos)
36✔
222
        if val !== nothing
18✔
223
            coefficient, i = val
×
224
        end
225
    end
226

227
    let val = tryparsenext_base10(s, i, end_pos, 1)
36✔
228
        val === nothing && @goto error
18✔
229
        dy, i = val
18✔
230
        i > end_pos && @goto done
18✔
231
    end
232

233
    c, i = iterate(s, i)::Tuple{Char, Int}
36✔
234
    c != '-' && @goto error
18✔
235
    i > end_pos && @goto done
18✔
236

237
    let val = tryparsenext_base10(s, i, end_pos, 1, 2)
36✔
238
        val === nothing && @goto error
18✔
239
        dm, i = val
18✔
240
        i > end_pos && @goto done
18✔
241
    end
242

243
    c, i = iterate(s, i)::Tuple{Char, Int}
36✔
244
    c != '-' && @goto error
18✔
245
    i > end_pos && @goto done
18✔
246

247
    let val = tryparsenext_base10(s, i, end_pos, 1, 2)
36✔
248
        val === nothing && @goto error
18✔
249
        dd, i = val
18✔
250
        i > end_pos && @goto done
18✔
251
    end
252

253
    c, i = iterate(s, i)::Tuple{Char, Int}
34✔
254
    c != 'T' && @goto error
17✔
255
    i > end_pos && @goto done
17✔
256

257
    let val = tryparsenext_base10(s, i, end_pos, 1, 2)
34✔
258
        val === nothing && @goto error
17✔
259
        th, i = val
17✔
260
        i > end_pos && @goto done
17✔
261
    end
262

263
    c, i = iterate(s, i)::Tuple{Char, Int}
34✔
264
    c != ':' && @goto error
17✔
265
    i > end_pos && @goto done
17✔
266

267
    let val = tryparsenext_base10(s, i, end_pos, 1, 2)
34✔
268
        val === nothing && @goto error
17✔
269
        tm, i = val
17✔
270
        i > end_pos && @goto done
17✔
271
    end
272

273
    c, i = iterate(s, i)::Tuple{Char, Int}
34✔
274
    c != ':' && @goto error
17✔
275
    i > end_pos && @goto done
17✔
276

277
    let val = tryparsenext_base10(s, i, end_pos, 1, 2)
34✔
278
        val === nothing && @goto error
17✔
279
        ts, i = val
17✔
280
        i > end_pos && @goto done
17✔
281
    end
282

283
    c, i = iterate(s, i)::Tuple{Char, Int}
14✔
284
    c != '.' && @goto error
7✔
285
    i > end_pos && @goto done
3✔
286

287
    let val = tryparsenext_base10(s, i, end_pos, 1, 3)
6✔
288
        val === nothing && @goto error
3✔
289
        tms, j = val
3✔
290
        tms *= 10 ^ (3 - (j - i))
3✔
291
        j > end_pos || @goto error
3✔
292
    end
293

294
    @label done
295
    return DateTime(dy * coefficient, dm, dd, th, tm, ts, tms)
13✔
296

297
    @label error
298
    throw(ArgumentError("Invalid DateTime string"))
5✔
299
end
300

301
function Base.parse(::Type{T}, str::AbstractString, df::DateFormat=default_format(T)) where T<:TimeType
16✔
302
    pos, len = firstindex(str), lastindex(str)
47✔
303
    pos > len && throw(ArgumentError("Cannot parse an empty string as a Date or Time"))
13✔
304
    val = tryparsenext_internal(T, str, pos, len, df, true)
26✔
305
    @assert val !== nothing
13✔
306
    values, endpos = val
13✔
307
    return T(values...)::T
13✔
308
end
309

310
function Base.tryparse(::Type{T}, str::AbstractString, df::DateFormat=default_format(T)) where T<:TimeType
×
311
    pos, len = firstindex(str), lastindex(str)
×
312
    pos > len && return nothing
×
313
    res = tryparsenext_internal(T, str, pos, len, df, false)
×
314
    res === nothing && return nothing
×
315
    values, endpos = res
×
316
    if validargs(T, values...) === nothing
×
317
        # TODO: validargs gets called twice, since it's called again in the T constructor
318
        return T(values...)::T
×
319
    end
320
    return nothing
×
321
end
322

323
"""
324
    parse_components(str::AbstractString, df::DateFormat) -> Array{Any}
325

326
Parse the string into its components according to the directives in the `DateFormat`.
327
Each component will be a distinct type, typically a subtype of Period. The order of the
328
components will match the order of the `DatePart` directives within the `DateFormat`. The
329
number of components may be less than the total number of `DatePart`.
330
"""
331
@generated function parse_components(str::AbstractString, df::DateFormat)
×
332
    letters = character_codes(df)
×
333
    tokens = Type[CONVERSION_SPECIFIERS[letter] for letter in letters]
×
334

335
    return quote
×
336
        pos, len = firstindex(str), lastindex(str)
×
337
        val = tryparsenext_core(str, pos, len, df, #=raise=#true)
×
338
        @assert val !== nothing
×
339
        values, pos, num_parsed = val
×
340
        types = $(Expr(:tuple, tokens...))
×
341
        result = Vector{Any}(undef, num_parsed)
×
342
        for (i, typ) in enumerate(types)
×
343
            i > num_parsed && break
×
344
            result[i] = typ(values[i])  # Constructing types takes most of the time
×
345
        end
×
346
        return result
×
347
    end
348
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