Skip to content

Commit

Permalink
Don't assume that SubString has pointer and copy instead (#506)
Browse files Browse the repository at this point in the history
* Don't assume that `SubString` has `pointer` and copy instead

* Still assume `Substring{String}` has `pointer`

* Test with `Test.GenericString`
  • Loading branch information
LilithHafner authored Oct 6, 2024
1 parent 98c202a commit ad5e2b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/parse_stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ function ParseStream(text::String, index::Integer=1; version=VERSION)
ParseStream(unsafe_wrap(Vector{UInt8}, text),
text, index, version)
end
function ParseStream(text::SubString, index::Integer=1; version=VERSION)
function ParseStream(text::SubString{String}, index::Integer=1; version=VERSION)
# See also IOBuffer(SubString("x"))
ParseStream(unsafe_wrap(Vector{UInt8}, pointer(text), sizeof(text)),
text, index, version)
Expand Down
13 changes: 12 additions & 1 deletion test/parse_stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ using JuliaSyntax: ParseStream,
peek, peek_token,
bump, bump_trivia, bump_invisible,
emit, emit_diagnostic, TRIVIA_FLAG, INFIX_FLAG,
ParseStreamPosition, first_child_position, last_child_position
ParseStreamPosition, first_child_position, last_child_position,
parsestmt

# Here we manually issue parse events in the order the Julia parser would issue
# them
Expand Down Expand Up @@ -147,3 +148,13 @@ end
@test first_child_position(st, position(st)) == ParseStreamPosition(4, 1)
@test last_child_position(st, position(st)) == ParseStreamPosition(7, 2)
end

@testset "SubString{GenericString} (issue #505)" begin
x = Test.GenericString("1 2")
@test x == "1 2"
y = split(x)[1]
@test y == "1"
@test y isa SubString{GenericString}
@test ParseStream(y) isa ParseStream
@test parsestmt(Expr, y) == parsestmt(Expr, "1")
end

0 comments on commit ad5e2b9

Please sign in to comment.