Improve performance of RDF.PrefixMap.prefixed_name_to_iri/2

This commit is contained in:
Marcel Otto 2020-10-30 11:42:07 +01:00
parent 15002a0bbb
commit c17cf8297c

View file

@ -396,12 +396,15 @@ defmodule RDF.PrefixMap do
@spec prefixed_name_to_iri(t, String.t()) :: IRI.t() | nil
def prefixed_name_to_iri(%__MODULE__{} = prefix_map, prefixed_name)
when is_binary(prefixed_name) do
Enum.find_value(prefix_map, fn {prefix, namespace} ->
case String.replace_leading(prefixed_name, "#{prefix}:", IRI.to_string(namespace)) do
^prefixed_name -> nil
iri -> IRI.new(iri)
end
end)
case String.split(prefixed_name, ":", parts: 2) do
[prefix, name] ->
if ns = namespace(prefix_map, prefix) do
RDF.iri(ns.value <> name)
end
_ ->
nil
end
end
defimpl Enumerable do