Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make match return Nullable{RegexMatch} #12033

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ function hist_from_file(hp, file)
line[1] != '#' &&
error(invalid_history_message, repr(line[1]), " at line ", countlines)
while !isempty(line)
m = match(r"^#\s*(\w+)\s*:\s*(.*?)\s*$", line)
m == nothing && break
maybe_m = match(r"^#\s*(\w+)\s*:\s*(.*?)\s*$", line)
isnull(maybe_m) && break
m = get(maybe_m)
if m.captures[1] == "mode"
mode = symbol(m.captures[2])
end
Expand Down
2 changes: 1 addition & 1 deletion base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ function completions(string, pos)
Base.incomplete_tag(parse(partial, raise=false))
end
if inc_tag in [:cmd, :string]
m = match(r"[\t\n\r\"'`@\$><=;|&\{]| (?!\\)", reverse(partial))
m = get(match(r"[\t\n\r\"'`@\$><=;|&\{]| (?!\\)", reverse(partial)))
startpos = nextind(partial, reverseind(partial, m.offset))
r = startpos:pos
paths, r, success = complete_path(replace(string[r], r"\\ ", " "), pos)
Expand Down
4 changes: 2 additions & 2 deletions base/dates/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ duplicates(slots) = any(map(x->count(y->x.period==y.period,slots),slots) .> 1)
function DateFormat(f::AbstractString,locale::AbstractString="english")
slots = Slot[]
trans = []
begtran = match(r"^.*?(?=[ymuUdHMSsEe])",f).match
begtran = get(match(r"^.*?(?=[ymuUdHMSsEe])",f)).match
ss = split(f,r"^.*?(?=[ymuUdHMSsEe])")
s = split(begtran == "" ? ss[1] : ss[2],r"[^ymuUdHMSsEe]+|(?<=([ymuUdHMSsEe])(?!\1))")
for (i,k) in enumerate(s)
k == "" && break
tran = i >= endof(s) ? r"$" : match(Regex("(?<=$(s[i])).*(?=$(s[i+1]))"),f).match
tran = i >= endof(s) ? r"$" : get(match(Regex("(?<=$(s[i])).*(?=$(s[i+1]))"),f)).match
slot = tran == "" ? FixedWidthSlot : DelimitedSlot
width = length(k)
typ = 'E' in k ? DayOfWeekSlot : 'e' in k ? DayOfWeekSlot :
Expand Down
10 changes: 6 additions & 4 deletions base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ function next(::EnvHash, i)
throw(BoundsError())
end
env::ByteString
m = match(r"^(.*?)=(.*)$"s, env)
if m == nothing
maybe_m = match(r"^(.*?)=(.*)$"s, env)
if isnull(maybe_m)
error("malformed environment entry: $env")
end
m = get(maybe_m)
(ByteString[convert(typeof(env),x) for x in m.captures], i+1)
end
end
Expand All @@ -146,10 +147,11 @@ function next(hash::EnvHash, block::Tuple{Ptr{UInt16},Ptr{UInt16}})
buf = Array(UInt16, len)
unsafe_copy!(pointer(buf), pos, len)
env = utf8(UTF16String(buf))
m = match(r"^(=?[^=]+)=(.*)$"s, env)
if m == nothing
maybe_m = match(r"^(=?[^=]+)=(.*)$"s, env)
if isnull(maybe_m)
error("malformed environment entry: $env")
end
m = get(maybe_m)
(ByteString[convert(typeof(env),x) for x in m.captures], (pos+len*2, blk))
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function versioninfo(io::IO=STDOUT, verbose::Bool=false)
if verbose
println(io, "Environment:")
for (k,v) in ENV
if !is(match(r"JULIA|PATH|FLAG|^TERM$|HOME", bytestring(k)), nothing)
if !isnull(match(r"JULIA|PATH|FLAG|^TERM$|HOME", bytestring(k)))
println(io, " $(k) = $(v)")
end
end
Expand Down
5 changes: 3 additions & 2 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ log(::Irrational{:e}, x) = log(x)

# align along = for nice Array printing
function alignment(x::Irrational)
m = match(r"^(.*?)(=.*)$", sprint(showcompact_lim, x))
m == nothing ? (length(sprint(showcompact_lim, x)), 0) :
maybe_m = match(r"^(.*?)(=.*)$", sprint(showcompact_lim, x))
isnull(maybe_m) && return (length(sprint(showcompact_lim, x)), 0)
m = get(maybe_m)
(length(m.captures[1]), length(m.captures[2]))
end
2 changes: 1 addition & 1 deletion base/markdown/Common/block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function hashheader(stream::IO, md::MD)

if c != '\n' # Empty header
h = readline(stream) |> strip
h = match(r"(.*?)( +#+)?$", h).captures[1]
h = get(match(r"(.*?)( +#+)?$", h)).captures[1]
buffer = IOBuffer()
print(buffer, h)
push!(md.content, Header(parseinline(seek(buffer, 0), md), level))
Expand Down
5 changes: 3 additions & 2 deletions base/markdown/parse/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ function startswith(stream::IO, r::Regex; eat = true, padding = false)
padding && skipwhitespace(stream)
line = chomp(readline(stream))
seek(stream, start)
m = match(r, line)
m == nothing && return ""
maybe_m = match(r, line)
isnull(maybe_m) && return ""
m = get(maybe_m)
eat && @dotimes length(m.match) read(stream, Char)
return m.match
end
Expand Down
2 changes: 1 addition & 1 deletion base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function url(m::Method)
try
d = dirname(file)
u = Git.readchomp(`config remote.origin.url`, dir=d)
u = match(Git.GITHUB_REGEX,u).captures[1]
u = get(match(Git.GITHUB_REGEX,u)).captures[1]
root = cd(d) do # dir=d confuses --show-toplevel, apparently
Git.readchomp(`rev-parse --show-toplevel`)
end
Expand Down
5 changes: 3 additions & 2 deletions base/multi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1043,8 +1043,9 @@ function read_worker_host_port(io::IO)
end

function parse_connection_info(str)
m = match(r"^julia_worker:(\d+)#(.*)", str)
if m != nothing
maybe_m = match(r"^julia_worker:(\d+)#(.*)", str)
if !isnull(maybe_m)
m = get(maybe_m)
(m.captures[2], parse(Int16, m.captures[1]))
else
("", Int16(-1))
Expand Down
16 changes: 9 additions & 7 deletions base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end
const path_ext_splitter = r"^((?:.*[/\\])?(?:\.|[^/\\\.])[^/\\]*?)(\.[^/\\\.]*|)$"

function splitdrive(path::AbstractString)
m = match(r"^(\w+:|\\\\\w+\\\w+|\\\\\?\\UNC\\\w+\\\w+|\\\\\?\\\w+:|)(.*)$", path)
m = get(match(r"^(\w+:|\\\\\w+\\\w+|\\\\\?\\UNC\\\w+\\\w+|\\\\\?\\\w+:|)(.*)$", path))
bytestring(m.captures[1]), bytestring(m.captures[2])
end
homedir() = get(ENV,"HOME",string(ENV["HOMEDRIVE"],ENV["HOMEPATH"]))
Expand All @@ -31,8 +31,9 @@ isdirpath(path::AbstractString) = ismatch(path_directory_re, splitdrive(path)[2]

function splitdir(path::ByteString)
a, b = splitdrive(path)
m = match(path_dir_splitter,b)
m == nothing && return (a,b)
maybe_m = match(path_dir_splitter,b)
isnull(maybe_m) && return (a,b)
m = get(maybe_m)
a = string(a, isempty(m.captures[1]) ? m.captures[2][1] : m.captures[1])
a, bytestring(m.captures[3])
end
Expand All @@ -43,15 +44,16 @@ basename(path::AbstractString) = splitdir(path)[2]

function splitext(path::AbstractString)
a, b = splitdrive(path)
m = match(path_ext_splitter, b)
m == nothing && return (path,"")
maybe_m = match(path_ext_splitter, b)
isnull(maybe_m) && return (path,"")
m = get(maybe_m)
a*m.captures[1], bytestring(m.captures[2])
end

function pathsep(paths::AbstractString...)
for path in paths
m = match(path_separator_re, path)
m != nothing && return m.match[1]
maybe_m = match(path_separator_re, path)
!isnull(maybe_m) && return get(maybe_m).match[1]
end
return path_separator
end
Expand Down
12 changes: 6 additions & 6 deletions base/pkg/entry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ function clone(url_or_pkg::AbstractString)
# TODO: Cache.prefetch(pkg,url)
else
url = url_or_pkg
m = match(r"(?:^|[/\\])(\w+?)(?:\.jl)?(?:\.git)?$", url)
m != nothing || error("can't determine package name from URL: $url")
pkg = m.captures[1]
maybe_m = match(r"(?:^|[/\\])(\w+?)(?:\.jl)?(?:\.git)?$", url)
isnull(maybe_m) && error("can't determine package name from URL: $url")
pkg = get(maybe_m).captures[1]
end
clone(url,pkg)
end
Expand Down Expand Up @@ -312,9 +312,9 @@ function pull_request(dir::AbstractString, commit::AbstractString="", url::Abstr
commit = isempty(commit) ? Git.head(dir=dir) :
Git.readchomp(`rev-parse --verify $commit`, dir=dir)
isempty(url) && (url = Git.readchomp(`config remote.origin.url`, dir=dir))
m = match(Git.GITHUB_REGEX, url)
m == nothing && error("not a GitHub repo URL, can't make a pull request: $url")
owner, repo = m.captures[2:3]
maybe_m = match(Git.GITHUB_REGEX, url)
isnull(maybe_m) && error("not a GitHub repo URL, can't make a pull request: $url")
owner, repo = get(maybe_m).captures[2:3]
user = GitHub.user()
info("Forking $owner/$repo to $user")
response = GitHub.fork(owner,repo)
Expand Down
6 changes: 3 additions & 3 deletions base/pkg/git.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ const GITHUB_REGEX =

function set_remote_url(url::AbstractString; remote::AbstractString="origin", dir="")
run(`config remote.$remote.url $url`, dir=dir)
m = match(GITHUB_REGEX,url)
m == nothing && return
push = "[email protected]:$(m.captures[1]).git"
maybe_m = match(GITHUB_REGEX,url)
isnull(maybe_m) && return
push = "[email protected]:$(get(maybe_m).captures[1]).git"
push != url && run(`config remote.$remote.pushurl $push`, dir=dir)
end

Expand Down
6 changes: 3 additions & 3 deletions base/pkg/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ end

function issue_url(pkg::AbstractString)
ispath(pkg,".git") || return ""
m = match(Git.GITHUB_REGEX, url(pkg))
m == nothing && return ""
return "https://github.com/" * m.captures[1] * "/issues"
maybe_m = match(Git.GITHUB_REGEX, url(pkg))
isnull(maybe_m) && return
return "https://github.com/" * get(maybe_m).captures[1] * "/issues"
end

end # module
15 changes: 8 additions & 7 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ function match(re::Regex, str::UTF8String, idx::Integer, add_opts::UInt32=UInt32
compile(re)
opts = re.match_options | add_opts
if !PCRE.exec(re.regex, str, idx-1, opts, re.match_data)
return nothing
return Nullable{RegexMatch}()
end
ovec = re.ovec
n = div(length(ovec),2) - 1
mat = SubString(str, ovec[1]+1, ovec[2])
cap = Union{Void,SubString{UTF8String}}[
ovec[2i+1] == PCRE.UNSET ? nothing : SubString(str, ovec[2i+1]+1, ovec[2i+2]) for i=1:n ]
off = Int[ ovec[2i+1]+1 for i=1:n ]
RegexMatch(mat, cap, ovec[1]+1, off, re)
Nullable(RegexMatch(mat, cap, ovec[1]+1, off, re))
end

match(re::Regex, str::Union{ByteString,SubString}, idx::Integer, add_opts::UInt32=UInt32(0)) =
Expand Down Expand Up @@ -221,10 +221,11 @@ end
compile(itr::RegexMatchIterator) = (compile(itr.regex); itr)
eltype(::Type{RegexMatchIterator}) = RegexMatch
start(itr::RegexMatchIterator) = match(itr.regex, itr.string, 1, UInt32(0))
done(itr::RegexMatchIterator, prev_match) = (prev_match == nothing)
done(itr::RegexMatchIterator, prev_match) = isnull(prev_match)

# Assumes prev_match is not nothing
function next(itr::RegexMatchIterator, prev_match)
function next(itr::RegexMatchIterator, maybe_prev_match)
prev_match = get(maybe_prev_match)
prevempty = isempty(prev_match.match)

if itr.overlap
Expand All @@ -242,7 +243,7 @@ function next(itr::RegexMatchIterator, prev_match)
mat = match(itr.regex, itr.string, offset,
prevempty ? opts_nonempty : UInt32(0))

if mat === nothing
if isnull(mat)
if prevempty && offset <= length(itr.string.data)
offset = nextind(itr.string, offset)
prevempty = false
Expand All @@ -251,10 +252,10 @@ function next(itr::RegexMatchIterator, prev_match)
break
end
else
return (prev_match, mat)
return (get(maybe_prev_match), mat)
end
end
(prev_match, nothing)
(get(maybe_prev_match), Nullable{RegexMatch}())
end

function eachmatch(re::Regex, str::AbstractString, ovr::Bool=false)
Expand Down
18 changes: 9 additions & 9 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -929,19 +929,19 @@ alignment(x::Any) = (0, length(sprint(showcompact_lim, x)))
alignment(x::Number) = (length(sprint(showcompact_lim, x)), 0)
alignment(x::Integer) = (length(sprint(showcompact_lim, x)), 0)
function alignment(x::Real)
m = match(r"^(.*?)((?:[\.eE].*)?)$", sprint(showcompact_lim, x))
m == nothing ? (length(sprint(showcompact_lim, x)), 0) :
(length(m.captures[1]), length(m.captures[2]))
maybe_m = match(r"^(.*?)((?:[\.eE].*)?)$", sprint(showcompact_lim, x))
isnull(maybe_m) ? (length(sprint(showcompact_lim, x)), 0) :
(length(get(maybe_m).captures[1]), length(get(maybe_m).captures[2]))
end
function alignment(x::Complex)
m = match(r"^(.*[\+\-])(.*)$", sprint(showcompact_lim, x))
m == nothing ? (length(sprint(showcompact_lim, x)), 0) :
(length(m.captures[1]), length(m.captures[2]))
maybe_m = match(r"^(.*[\+\-])(.*)$", sprint(showcompact_lim, x))
isnull(maybe_m) ? (length(sprint(showcompact_lim, x)), 0) :
(length(get(maybe_m).captures[1]), length(get(maybe_m).captures[2]))
end
function alignment(x::Rational)
m = match(r"^(.*?/)(/.*)$", sprint(showcompact_lim, x))
m == nothing ? (length(sprint(showcompact_lim, x)), 0) :
(length(m.captures[1]), length(m.captures[2]))
maybe_m = match(r"^(.*?/)(/.*)$", sprint(showcompact_lim, x))
isnull(maybe_m) ? (length(sprint(showcompact_lim, x)), 0) :
(length(get(maybe_m).captures[1]), length(get(maybe_m).captures[2]))
end

const undef_ref_str = "#undef"
Expand Down
5 changes: 3 additions & 2 deletions base/version.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ function split_idents(s::AbstractString)
end

VersionNumber(v::AbstractString) = begin
m = match(VERSION_REGEX, v)
m == nothing && throw(ArgumentError("invalid version string: $v"))
maybe_m = match(VERSION_REGEX, v)
isnull(maybe_m) && throw(ArgumentError("invalid version string: $v"))
m = get(maybe_m)
major, minor, patch, minus, prerl, plus, build = m.captures
major = parse(Int, major)
minor = minor != nothing ? parse(Int, minor) : 0
Expand Down
Loading