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

JuliaLang / julia / #37854

29 Jul 2024 05:47AM UTC coverage: 87.526% (+0.5%) from 86.979%
#37854

push

local

web-flow
adjust the name and remarks of const prop heuristics (#55260)

`const_prop_entry_heuristics` currently checks the return type only, so
I have given it a name that reflects this and adjusted the remarks
accordingly. There are no changes to the basic behavior of inference.

10 of 11 new or added lines in 1 file covered. (90.91%)

60 existing lines in 15 files now uncovered.

77607 of 88667 relevant lines covered (87.53%)

15176436.63 hits per line

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

92.76
/base/strings/substring.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
"""
4
    SubString(s::AbstractString, i::Integer, j::Integer=lastindex(s))
5
    SubString(s::AbstractString, r::UnitRange{<:Integer})
6

7
Like [`getindex`](@ref), but returns a view into the parent string `s`
8
within range `i:j` or `r` respectively instead of making a copy.
9

10
The [`@views`](@ref) macro converts any string slices `s[i:j]` into
11
substrings `SubString(s, i, j)` in a block of code.
12

13
# Examples
14
```jldoctest
15
julia> SubString("abc", 1, 2)
16
"ab"
17

18
julia> SubString("abc", 1:2)
19
"ab"
20

21
julia> SubString("abc", 2)
22
"bc"
23
```
24
"""
25
struct SubString{T<:AbstractString} <: AbstractString
26
    string::T
27
    offset::Int
28
    ncodeunits::Int
29

30
    function SubString{T}(s::T, i::Int, j::Int) where T<:AbstractString
14,158,955✔
31
        i ≤ j || return new(s, 0, 0)
15,035,161✔
32
        @boundscheck begin
13,282,810✔
33
            checkbounds(s, i:j)
13,282,831✔
34
            @inbounds isvalid(s, i) || string_index_err(s, i)
13,282,791✔
35
            @inbounds isvalid(s, j) || string_index_err(s, j)
13,282,797✔
36
        end
37
        return new(s, i-1, nextind(s,j)-i)
13,282,777✔
38
    end
39
    function SubString{T}(s::T, i::Int, j::Int, ::Val{:noshift}) where T<:AbstractString
576✔
40
        @boundscheck if !(i == j == 0)
576✔
41
            si, sj = i + 1, prevind(s, j + i + 1)
558✔
42
            @inbounds isvalid(s, si) || string_index_err(s, si)
558✔
43
            @inbounds isvalid(s, sj) || string_index_err(s, sj)
558✔
44
        end
45
        new(s, i, j)
576✔
46
    end
47
end
48

49
@propagate_inbounds SubString(s::T, i::Int, j::Int) where {T<:AbstractString} = SubString{T}(s, i, j)
14,205,867✔
50
@propagate_inbounds SubString(s::T, i::Int, j::Int, v::Val{:noshift}) where {T<:AbstractString} = SubString{T}(s, i, j, v)
541✔
51
@propagate_inbounds SubString(s::AbstractString, i::Integer, j::Integer=lastindex(s)) = SubString(s, Int(i), Int(j))
10,274,941✔
52
@propagate_inbounds SubString(s::AbstractString, r::AbstractUnitRange{<:Integer}) = SubString(s, first(r), last(r))
134,850✔
53

54
@propagate_inbounds function SubString(s::SubString, i::Int, j::Int)
91✔
55
    @boundscheck i ≤ j && checkbounds(s, i:j)
917,655✔
56
    SubString(s.string, s.offset+i, s.offset+j)
917,647✔
57
end
58

59
SubString(s::AbstractString) = SubString(s, 1, lastindex(s)::Int)
885✔
UNCOV
60
SubString{T}(s::T) where {T<:AbstractString} = SubString{T}(s, 1, lastindex(s)::Int)
×
61

62
@propagate_inbounds view(s::AbstractString, r::AbstractUnitRange{<:Integer}) = SubString(s, r)
2,168✔
63
@propagate_inbounds maybeview(s::AbstractString, r::AbstractUnitRange{<:Integer}) = view(s, r)
3✔
64
@propagate_inbounds maybeview(s::AbstractString, args...) = getindex(s, args...)
6✔
65

66
convert(::Type{SubString{S}}, s::AbstractString) where {S<:AbstractString} =
161✔
67
    SubString(convert(S, s))::SubString{S}
68
convert(::Type{T}, s::T) where {T<:SubString} = s
2✔
69

70
# Regex match allows only Union{String, SubString{String}} so define conversion to this type
71
convert(::Type{Union{String, SubString{String}}}, s::String) = s
×
72
convert(::Type{Union{String, SubString{String}}}, s::SubString{String}) = s
×
73
convert(::Type{Union{String, SubString{String}}}, s::AbstractString) = convert(String, s)::String
×
74

75
function String(s::SubString{String})
1✔
76
    parent = s.string
752,092✔
77
    copy = GC.@preserve parent unsafe_string(pointer(parent, s.offset+1), s.ncodeunits)
752,092✔
78
    return copy
752,092✔
79
end
80

81
ncodeunits(s::SubString) = s.ncodeunits
154,434,247✔
82
codeunit(s::SubString) = codeunit(s.string)::CodeunitType
×
83
length(s::SubString) = length(s.string, s.offset+1, s.offset+s.ncodeunits)
199,006✔
84

85
function codeunit(s::SubString, i::Integer)
86
    @boundscheck checkbounds(s, i)
14,882,687✔
87
    @inbounds return codeunit(s.string, s.offset + i)
14,882,687✔
88
end
89

90
function iterate(s::SubString, i::Integer=firstindex(s))
307✔
91
    i == ncodeunits(s)+1 && return nothing
32,653,839✔
92
    @boundscheck checkbounds(s, i)
18,171,564✔
93
    y = iterate(s.string, s.offset + i)
36,048,805✔
94
    y === nothing && return nothing
18,171,560✔
95
    c, i = y::Tuple{AbstractChar,Int}
18,171,217✔
96
    return c, i - s.offset
18,171,560✔
97
end
98

99
function getindex(s::SubString, i::Integer)
316✔
100
    @boundscheck checkbounds(s, i)
11,257,712✔
101
    @inbounds return getindex(s.string, s.offset + i)
21,225,689✔
102
end
103

104
isascii(ss::SubString{String}) = isascii(codeunits(ss))
×
105

106
function isvalid(s::SubString, i::Integer)
107
    ib = true
197✔
108
    @boundscheck ib = checkbounds(Bool, s, i)
29,540,182✔
109
    @inbounds return ib && isvalid(s.string, s.offset + i)::Bool
59,075,709✔
110
end
111

112
thisind(s::SubString{String}, i::Int) = _thisind_str(s, i)
8,197,591✔
113
nextind(s::SubString{String}, i::Int) = _nextind_str(s, i)
9,118,723✔
114

115
parent(s::SubString) = s.string
1✔
116
parentindices(s::SubString) = (s.offset + 1 : thisind(s.string, s.offset + s.ncodeunits),)
1✔
117

118
function ==(a::Union{String, SubString{String}}, b::Union{String, SubString{String}})
14,075✔
119
    sizeof(a) == sizeof(b) && _memcmp(a, b) == 0
11,497,654✔
120
end
121

122
function cmp(a::SubString{String}, b::SubString{String})
×
123
    c = _memcmp(a, b)
×
124
    return c < 0 ? -1 : c > 0 ? +1 : cmp(sizeof(a), sizeof(b))
×
125
end
126

127
# don't make unnecessary copies when passing substrings to C functions
128
cconvert(::Type{Ptr{UInt8}}, s::SubString{String}) = s
×
129
cconvert(::Type{Ptr{Int8}}, s::SubString{String}) = s
2✔
130

131
function unsafe_convert(::Type{Ptr{R}}, s::SubString{String}) where R<:Union{Int8, UInt8}
132
    convert(Ptr{R}, pointer(s.string)) + s.offset
1,224,835✔
133
end
134

135
pointer(x::SubString{String}) = pointer(x.string) + x.offset
5,266,021✔
136
pointer(x::SubString{String}, i::Integer) = pointer(x.string) + x.offset + (i-1)
64✔
137

138
function hash(s::SubString{String}, h::UInt)
139
    h += memhash_seed
260✔
140
    ccall(memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), s, sizeof(s), h % UInt32) + h
243,316✔
141
end
142

143
_isannotated(::SubString{T}) where {T} = _isannotated(T)
×
144

145
"""
146
    reverse(s::AbstractString) -> AbstractString
147

148
Reverses a string. Technically, this function reverses the codepoints in a string and its
149
main utility is for reversed-order string processing, especially for reversed
150
regular-expression searches. See also [`reverseind`](@ref) to convert indices in `s` to
151
indices in `reverse(s)` and vice-versa, and `graphemes` from module `Unicode` to
152
operate on user-visible "characters" (graphemes) rather than codepoints.
153
See also [`Iterators.reverse`](@ref) for
154
reverse-order iteration without making a copy. Custom string types must implement the
155
`reverse` function themselves and should typically return a string with the same type
156
and encoding. If they return a string with a different encoding, they must also override
157
`reverseind` for that string type to satisfy `s[reverseind(s,i)] == reverse(s)[i]`.
158

159
# Examples
160
```jldoctest
161
julia> reverse("JuliaLang")
162
"gnaLailuJ"
163
```
164

165
!!! note
166
    The examples below may be rendered differently on different systems.
167
    The comments indicate how they're supposed to be rendered
168

169
Combining characters can lead to surprising results:
170

171
```jldoctest
172
julia> reverse("ax̂e") # hat is above x in the input, above e in the output
173
"êxa"
174

175
julia> using Unicode
176

177
julia> join(reverse(collect(graphemes("ax̂e")))) # reverses graphemes; hat is above x in both in- and output
178
"ex̂a"
179
```
180
"""
181
function reverse(s::Union{String,SubString{String}})::String
3,434✔
182
    # Read characters forwards from `s` and write backwards to `out`
183
    out = _string_n(sizeof(s))
3,434✔
184
    offs = sizeof(s) + 1
3,434✔
185
    for c in s
6,744✔
186
        offs -= ncodeunits(c)
108,051✔
187
        __unsafe_string!(out, c, offs)
109,775✔
188
    end
211,906✔
189
    return out
3,434✔
190
end
191

192
string(a::String)            = String(a)
47,662✔
193
string(a::SubString{String}) = String(a)
80✔
194

195
function Symbol(s::SubString{String})
196
    return ccall(:jl_symbol_n, Ref{Symbol}, (Ptr{UInt8}, Int), s, sizeof(s))
324✔
197
end
198

199
@inline function __unsafe_string!(out, c::Char, offs::Integer) # out is a (new) String (or StringVector)
200
    x = bswap(reinterpret(UInt32, c))
2,717,199✔
201
    n = ncodeunits(c)
2,717,199✔
202
    GC.@preserve out begin
2,717,249✔
203
        unsafe_store!(pointer(out, offs), x % UInt8)
2,717,249✔
204
        n == 1 && return n
2,717,199✔
205
        x >>= 8
379,323✔
206
        unsafe_store!(pointer(out, offs+1), x % UInt8)
379,323✔
207
        n == 2 && return n
379,323✔
208
        x >>= 8
375,541✔
209
        unsafe_store!(pointer(out, offs+2), x % UInt8)
375,541✔
210
        n == 3 && return n
375,541✔
211
        x >>= 8
2,652✔
212
        unsafe_store!(pointer(out, offs+3), x % UInt8)
2,652✔
213
    end
214
    return n
2,652✔
215
end
216

217
@assume_effects :nothrow @inline function __unsafe_string!(out, s::String, offs::Integer)
218
    n = sizeof(s)
19,247,881✔
219
    GC.@preserve s out unsafe_copyto!(pointer(out, offs), pointer(s), n)
19,247,881✔
220
    return n
19,247,881✔
221
end
222

223
@inline function __unsafe_string!(out, s::SubString{String}, offs::Integer)
224
    n = sizeof(s)
962,035✔
225
    GC.@preserve s out unsafe_copyto!(pointer(out, offs), pointer(s), n)
962,035✔
226
    return n
962,035✔
227
end
228

229
@assume_effects :nothrow @inline function __unsafe_string!(out, s::Symbol, offs::Integer)
230
    n = sizeof(s)
20,199✔
231
    GC.@preserve s out unsafe_copyto!(pointer(out, offs), unsafe_convert(Ptr{UInt8},s), n)
20,199✔
232
    return n
20,199✔
233
end
234

235
# nothrow needed here because for v in a can't prove the indexing is inbounds.
236
@assume_effects :foldable :nothrow string(a::Union{Char, String, Symbol}...) = _string(a...)
7,683,209✔
237

238
string(a::Union{Char, String, SubString{String}, Symbol}...) = _string(a...)
1,105,511✔
239

240
function _string(a::Union{Char, String, SubString{String}, Symbol}...)
10,104,698✔
241
    n = 0
3,359✔
242
    for v in a
10,104,777✔
243
        # 4 types is too many for automatic Union-splitting, so we split manually
244
        # and allow one specializable call site per concrete type
245
        if v isa Char
2,690,587✔
246
            n += ncodeunits(v)
1,002,325✔
247
        elseif v isa String
2,121,005✔
248
            n += sizeof(v)
19,247,881✔
249
        elseif v isa SubString{String}
979,220✔
250
            n += sizeof(v)
962,035✔
251
        else
252
            n += sizeof(v::Symbol)
20,199✔
253
        end
254
    end
30,471,652✔
255
    out = _string_n(n)
10,104,827✔
256
    offs = 1
3,359✔
257
    for v in a
10,104,777✔
258
        if v isa Char
2,690,587✔
259
            offs += __unsafe_string!(out, v, offs)
1,374,553✔
260
        elseif v isa String || v isa SubString{String}
3,100,225✔
261
            offs += __unsafe_string!(out, v, offs)
20,209,916✔
262
        else
263
            offs += __unsafe_string!(out, v::Symbol, offs)
20,199✔
264
        end
265
    end
30,471,652✔
266
    return out
10,104,777✔
267
end
268

269
# don't assume effects for general integers since we cannot know their implementation
270
# not nothrow because r<0 throws
271
@assume_effects :foldable repeat(s::String, r::BitInteger) = @invoke repeat(s::String, r::Integer)
2,088,577✔
272

273
function repeat(s::Union{String, SubString{String}}, r::Integer)
2,088,759✔
274
    r < 0 && throw(ArgumentError("can't repeat a string $r times"))
2,088,880✔
275
    r == 0 && return ""
2,088,872✔
276
    r == 1 && return String(s)
1,569,479✔
277
    n = sizeof(s)
1,280,996✔
278
    out = _string_n(n*r)
1,280,996✔
279
    if n == 1 # common case: repeating a single-byte string
1,280,996✔
280
        @inbounds b = codeunit(s, 1)
1,239,583✔
281
        memset(unsafe_convert(Ptr{UInt8}, out), b, r)
1,239,583✔
282
    else
283
        for i = 0:r-1
41,413✔
284
            GC.@preserve s out unsafe_copyto!(pointer(out, i*n+1), pointer(s), n)
2,065,308✔
285
        end
2,065,616✔
286
    end
287
    return out
1,280,996✔
288
end
289

290
function filter(f, s::Union{String, SubString{String}})
42✔
291
    out = StringVector(sizeof(s))
84✔
292
    offset = 1
3✔
293
    for c in s
84✔
294
        if f(c)
884✔
295
            offset += __unsafe_string!(out, c, offset)
660✔
296
        end
297
    end
1,637✔
298
    resize!(out, offset-1)
42✔
299
    sizehint!(out, offset-1)
42✔
300
    return String(out)
42✔
301
end
302

303
getindex(s::AbstractString, r::AbstractUnitRange{<:Integer}) = SubString(s, r)
14,033✔
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