To be able to edit code and run cells, you need to run the notebook yourself. Where would you like to run the notebook?

This notebook takes about 20 seconds to run.

In the cloud (experimental)

Binder is a free, open source service that runs scientific notebooks in the cloud! It will take a while, usually 2-7 minutes to get a session.

On your computer

(Recommended if you want to store your changes.)

  1. Copy the notebook URL:
  2. Run Pluto

    (Also see: How to install Julia and Pluto)

  3. Paste URL in the Open box

Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

Author 1
👀 Reading hidden code
using PlutoTest
11.6 ms
👀 Reading hidden code
using PlutoUI
190 ms
ends_with_semicolon_original (generic function with 1 method)
👀 Reading hidden code
2.0 ms
ends_with_semicolon2 (generic function with 2 methods)
👀 Reading hidden code
begin

let matchend = Dict("\"" => r"\"", "\"\"\"" => r"\"\"\"", "'" => r"'",
"`" => r"`", "```" => r"```", "#" => r"$"m, "#=" => r"=#|#=")
function _rm_strings_and_comments(code::Union{String,SubString{String}})
buf = IOBuffer(sizehint = sizeof(code))
pos = 1
while true
i = findnext(r"\"(?!\"\")|\"\"\"|'|`(?!``)|```|#(?!=)|#=", code, pos)
isnothing(i) && break
match = SubString(code, i)
j = findnext(matchend[match]::Regex, code, nextind(code, last(i)))
if match == "#=" # possibly nested
nested = 1
while j !== nothing
nested += SubString(code, j) == "#=" ? +1 : -1
iszero(nested) && break
j = findnext(r"=#|#=", code, nextind(code, last(j)))
end
elseif match[1] != '#' # quote match: check non-escaped
while j !== nothing
notbackslash = findprev(!=('\\'), code, prevind(code, first(j)))::Int
isodd(first(j) - notbackslash) && break # not escaped
j = findnext(matchend[match]::Regex, code, nextind(code, first(j)))
end
end
isnothing(j) && break
if match[1] == '#'
print(buf, SubString(code, pos, prevind(code, first(i))))
else
print(buf, SubString(code, pos, last(i)), ' ', SubString(code, j))
end
pos = nextind(code, last(j))
end
print(buf, SubString(code, pos, lastindex(code)))
return String(take!(buf))
end
end
ends_with_semicolon2(code::Union{String,SubString{String}}) =
contains(_rm_strings_and_comments(code), r";\s*$")
ends_with_semicolon2(code::AbstractString) = ends_with_semicolon2(String(code))
end
98.6 ms
ends_with_semicolon_new (generic function with 1 method)
function ends_with_semicolon_new(line::AbstractString)
stripped_line = _remove_strings_and_comments(line)
match = findlast(isequal(';'), stripped_line)::Union{Nothing,Int}
return match !== nothing && all(isspace, stripped_line[(match + 1):end])
end
👀 Reading hidden code
721 μs
_remove_strings_and_comments (generic function with 1 method)
function _remove_strings_and_comments(s)
# remove string literals
# """hello"""
s = replace(s, r"\"\"\".*?[^\\]\"\"\""s => "julia")
# "hello"
s = replace(s, r"\"(\\\\\"|[^\"])*\"" => "philip")
# ```hello```
s = replace(s, r"```.*?```"s => "the")
# `hello`
s = replace(s, r"`(\\`|[^`])*`" => "corgi")

# remove multiline #= comments =#
# s = replace(s, r"\#=(?:[^(\#=)(=\#)]+|(?R))*+=\#" => "")
s = replace(s, r"\#=(?:([^\#\=]|\=(?!\#)|\#(?!\=))+|(?R))*+=\#" => "")

# remove single line # comments
s = replace(s, r"#.*" => "")
end
👀 Reading hidden code
3.6 ms
r"\"(\\\\\"|[^\"])*\""
rr = r"\"(\\\\\"|[^\"])*\""
👀 Reading hidden code
67.0 μs
"(\\"|[^"])*"
r"\"(\\\\\"|[^\"])*\"".pattern |> Text
👀 Reading hidden code
2.0 ms
eachmatch(rr, ss) |> collect
👀 Reading hidden code
30.0 ms
 "asfd" ee " a\"b " c
	
"""wow "asdf """
(e"f()= 1; # a " + " no" eee)
    r"  \" ff"f
    r` \` `f e``e
	
Text(ss)
👀 Reading hidden code
12.3 μs
" \"asfd\" ee \" a\\\"b \" c\n\t\n\"\"\"wow \"asdf \"\"\"\n(e\"f()= 1; # a \" + \" no\" eee)\n    r\"  \\\" ff\"f\n    r` \\` `f e``e\n\t"
ss = """
"asfd" ee " a\\"b " c
\"\"\"wow "asdf \"\"\"
(e"f()= 1; # a " + " no" eee)
r" \\" ff"f
r` \\` `f e``e
"""
👀 Reading hidden code
11.0 μs
 philip ee philip c
	
julia
(ephilip + philip eee)
    rphilipf
    rcorgif ecorgie
	
👀 Reading hidden code
25.1 μs
julia
_remove_strings_and_comments("""
""\"
asdf
""\"
""") |> Text
👀 Reading hidden code
19.4 μs

_remove_strings_and_comments("""
#=#= #
asdf
=#b=#
""") |> Text
👀 Reading hidden code
18.4 μs
a; 


_remove_strings_and_comments("""
a; #=#=#
=#b=#
# test
#=
foobar
=##bazbax
""") |> Text
👀 Reading hidden code
19.7 μs

philip dont remove me!

leave me 

fons  juju

_remove_strings_and_comments("""

" # " dont remove me!

leave me # im gone

fons #= van #= der =# plas =# juju

""") |> Text
👀 Reading hidden code
19.9 μs
" asd ` " , ` ""\` `
👀 Reading hidden code
3.7 ms

Before / after

Use function:

👀 Reading hidden code
349 ms

New test cases

👀 Reading hidden code
179 μs
" \"asfd\" ee \" a\\\"b \" c\n\t\n\"\"\"wow \"asdf \"\"\"\n(e\"f()= 1; # a \" + \" no\" eee)\n    r\"  \\\" ff\"f\n    r` \\` `f e``e\n\t"
begin
ss # """
ss; # """
end
👀 Reading hidden code
14.7 μs
foo # """
 bar; # """ 
let
"foo # \"\"\"\n bar; # \"\"\" " |> Text
end
👀 Reading hidden code
18.1 μs
ends_with_semicolon("\tss # \"\"\"\n\tss; # \"\"\" \n")
true
@test ends_with_semicolon("""
ss # ""\"
ss; # ""\"
""")
👀 Reading hidden code
515 ms
ends_with_semicolon("f()= 1; # a ; 2")
false
@test ends_with_semicolon("f()= 1; # a ; 2")
👀 Reading hidden code
❔
Test Failed at /home/runner/work/disorganised-mess/disorganised-mess/ends_with_semicolon.jl#==#94187d97-12c0-4cb4-8dcd-8494f0a99b27:1
  Expression: ends_with_semicolon("f()= 1; # a ; 2")
   Evaluated: 
246 ms
!(ends_with_semicolon("f()= 1; \"asdf\"")) !
false
true
@test !ends_with_semicolon("f()= 1; \"asdf\"")
👀 Reading hidden code
7.9 ms
!(ends_with_semicolon("\";\"\n")) !
false
true
@test !ends_with_semicolon("""
";"
""")
👀 Reading hidden code
1.8 ms
!(ends_with_semicolon("\";\" # asdf\n")) !
false
true
@test !ends_with_semicolon("""
";" # asdf
""")
👀 Reading hidden code
1.8 ms
ends_with_semicolon("f()= 1; # a")
true
@test ends_with_semicolon(
"f()= 1; # a"
)
👀 Reading hidden code
1.2 ms
!(ends_with_semicolon("(\"f()= 1; # a\")\n")) !
true
false
@test !ends_with_semicolon(
"""
("f()= 1; # a")
"""
)
👀 Reading hidden code
❔
Test Failed at /home/runner/work/disorganised-mess/disorganised-mess/ends_with_semicolon.jl#==#7b51546f-1b95-4f9f-ba6e-b054df5bcb85:1
  Expression: !(ends_with_semicolon("(\"f()= 1; # a\")\n"))
   Evaluated: 
2.0 ms
!(ends_with_semicolon("\"f()= 1; # a\"\n")) !
true
false
@test !ends_with_semicolon(
"""
"f()= 1; # a"
"""
)
👀 Reading hidden code
❔
Test Failed at /home/runner/work/disorganised-mess/disorganised-mess/ends_with_semicolon.jl#==#546bf639-efb0-4d7e-8e5c-dcf29b09de43:1
  Expression: !(ends_with_semicolon("\"f()= 1; # a\"\n"))
   Evaluated: 
2.0 ms
ends_with_semicolon("f()= 1;")
true
@test ends_with_semicolon("f()= 1;")
👀 Reading hidden code
1.2 ms
!(ends_with_semicolon("1;" * " #= =# 2")) !(ends_with_semicolon(
"1; #= =# 2"
))
!
false
true
@test !ends_with_semicolon(
"1;" * " #= =# 2"
)
👀 Reading hidden code
171 ms
true
@test_nowarn ends_with_semicolon(
"1;" * " #=# 2"
)
👀 Reading hidden code
99.1 ms
!(ends_with_semicolon("a # asdf ;")) !
true
false
@test !ends_with_semicolon("a # asdf ;")
👀 Reading hidden code
❔
Test Failed at /home/runner/work/disorganised-mess/disorganised-mess/ends_with_semicolon.jl#==#adf3404a-c691-4a0a-8c42-25cab39da17a:1
  Expression: !(ends_with_semicolon("a # asdf ;"))
   Evaluated: 
2.0 ms
!(ends_with_semicolon("a # asdf ;")) !
true
false
@test !ends_with_semicolon("a # asdf ;")
👀 Reading hidden code
❔
Test Failed at /home/runner/work/disorganised-mess/disorganised-mess/ends_with_semicolon.jl#==#d192bf86-bb28-42f9-970c-7b7bd634cd67:1
  Expression: !(ends_with_semicolon("a # asdf ;"))
   Evaluated: 
2.0 ms
ends_with_semicolon("a * \"#\" ;")
true
@test ends_with_semicolon("""a * "#" ;""")
👀 Reading hidden code
1.2 ms
!(ends_with_semicolon("\"\\\";\"#\"")) !
false
true
@test !ends_with_semicolon("\"\\\";\"#\"")
👀 Reading hidden code
1.8 ms
"\";"
"\";"#"
👀 Reading hidden code
16.1 μs
"\";"#"
Text(
"\"\\\";\"#\""
)
👀 Reading hidden code
11.6 μs
ends_with_semicolon("\"\\\\\";#\"")
true
@test ends_with_semicolon(
"\"\\\\\";#\""
)
👀 Reading hidden code
1.2 ms
ends_with_semicolon("é; #é \"é\"")
true
@test ends_with_semicolon(
"é; #é \"é\""
)
👀 Reading hidden code
1.2 ms

👀 Reading hidden code
68.8 μs
ends_with_semicolon("1;\n#text\n")
true
begin
@test ends_with_semicolon("""1;\n#text\n""")
end
👀 Reading hidden code
1.6 ms
ends_with_semicolon("a; #=#=# =# =#\n")
true
@test ends_with_semicolon("a; #=#=# =# =#\n")
👀 Reading hidden code
1.1 ms
!(ends_with_semicolon("begin\na;\nb;\nend")) !
false
true
@test !ends_with_semicolon("begin\na;\nb;\nend")
👀 Reading hidden code
1.7 ms
!(ends_with_semicolon("begin\na; #=#=#\n=#b=#\nend")) !
false
true
@test !ends_with_semicolon("begin\na; #=#=#\n=#b=#\nend")
👀 Reading hidden code
1.8 ms
ends_with_semicolon("\na; #=#=#\n=#b=#\n# test\n#=\nfoobar\n=##bazbax\n")
true
@test ends_with_semicolon("\na; #=#=#\n=#b=#\n# test\n#=\nfoobar\n=##bazbax\n")
👀 Reading hidden code
1.2 ms

Old test cases

👀 Reading hidden code
197 μs
!(ends_with_semicolon("")) !
false
true
@test !ends_with_semicolon("")
👀 Reading hidden code
1.8 ms
ends_with_semicolon(";")
true
@test ends_with_semicolon(";")
👀 Reading hidden code
1.1 ms
!(ends_with_semicolon("a")) !
false
true
@test !ends_with_semicolon("a")
👀 Reading hidden code
1.8 ms
ends_with_semicolon("1;")
true
@test ends_with_semicolon("1;")
👀 Reading hidden code
1.2 ms
ends_with_semicolon("1;\n")
true
@test ends_with_semicolon("1;\n")
👀 Reading hidden code
1.2 ms
ends_with_semicolon("1;\r")
true
@test ends_with_semicolon("1;\r")
👀 Reading hidden code
1.2 ms
ends_with_semicolon("1;\r\n \t\f")
true
@test ends_with_semicolon("1;\r\n \t\f")
👀 Reading hidden code
1.2 ms
@test ends_with_semicolon("1;#text\n")
👀 Reading hidden code
1.1 ms
ends_with_semicolon("a; #=#=# =# =#\n")
true
@test ends_with_semicolon("a; #=#=# =# =#\n")
👀 Reading hidden code
1.1 ms
!(ends_with_semicolon("begin\na;\nb;\nend")) !
false
true
@test !ends_with_semicolon("begin\na;\nb;\nend")
👀 Reading hidden code
1.8 ms
!(ends_with_semicolon("begin\na; #=#=#\n=#b=#\nend")) !
false
true
@test !ends_with_semicolon("begin\na; #=#=#\n=#b=#\nend")
👀 Reading hidden code
1.8 ms
ends_with_semicolon("\na; #=#=#\n=#b=#\n# test\n#=\nfoobar\n=##bazbax\n")
true
@test ends_with_semicolon("\na; #=#=#\n=#b=#\n# test\n#=\nfoobar\n=##bazbax\n")
👀 Reading hidden code
1.1 ms