proplists.erl

来自「OTP是开放电信平台的简称」· ERL 代码 · 共 635 行 · 第 1/2 页

ERL
635
字号
%% ``The contents of this file are subject to the Erlang Public License,%% Version 1.1, (the "License"); you may not use this file except in%% compliance with the License. You should have received a copy of the%% Erlang Public License along with this software. If not, it can be%% retrieved via the world wide web at http://www.erlang.org/.%% %% Software distributed under the License is distributed on an "AS IS"%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See%% the License for the specific language governing rights and limitations%% under the License.%% %% The Initial Developer of the Original Code is Richard Carlsson.%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings%% AB. All Rights Reserved.''%% %%     $Id $%% =====================================================================%% Support functions for property lists%%%% Copyright (C) 2000-2003 Richard Carlsson%% ---------------------------------------------------------------------%%%% @doc Support functions for property lists.%%%% <p>Property lists are ordinary lists containing entries in the form%% of either tuples, whose first elements are keys used for lookup and%% insertion, or atoms, which work as shorthand for tuples <code>{Atom,%% true}</code>. (Other terms are allowed in the lists, but are ignored%% by this module.) If there is more than one entry in a list for a%% certain key, the first occurrence normally overrides any later%% (irrespective of the arity of the tuples).</p>%%%% <p>Property lists are useful for representing inherited properties,%% such as options passed to a function where a user may specify options%% overriding the default settings, object properties, annotations,%% etc.</p>%%%% @type property() = atom() | tuple()-module(proplists).-export([property/1, property/2, unfold/1, compact/1, lookup/2,	 lookup_all/2, is_defined/2, get_value/2, get_value/3,	 get_all_values/2, append_values/2, get_bool/2, get_keys/1,	 delete/2, substitute_aliases/2, substitute_negations/2,	 expand/2, normalize/2, split/2]).%% @spec property(P::property()) -> property()%%%% @doc Creates a normal form (minimal) representation of a property. If%% <code>P</code> is <code>{Key, true}</code> where <code>Key</code> is%% an atom, this returns <code>Key</code>, otherwise the whole term%% <code>P</code> is returned.%%%% @see property/2property({Key, true}) when is_atom(Key) ->    Key;property(Property) ->    Property.%% @spec property(Key::term(), Value::term()) -> property()%%%% @doc Creates a normal form (minimal) representation of a simple%% key/value property. Returns <code>Key</code> if <code>Value</code> is%% <code>true</code> and <code>Key</code> is an atom, otherwise a tuple%% <code>{Key, Value}</code> is returned.%%%% @see property/1property(Key, true) when is_atom(Key) ->    Key;property(Key, Value) ->    {Key, Value}.%% ---------------------------------------------------------------------%% @spec unfold(List::[term()]) -> [term()]%%%% @doc Unfolds all occurences of atoms in <code>List</code> to tuples%% <code>{Atom, true}</code>.%%%% @see compact/1unfold([P | Ps]) ->    if is_atom(P) ->	    [{P, true} | unfold(Ps)];       true ->	    [P | unfold(Ps)]    end;unfold([]) ->    [].%% @spec compact(List::[term()]) -> [term()]%%%% @doc Minimizes the representation of all entries in the list. This is%% equivalent to <code>[property(P) || P &lt;- List]</code>.%%%% @see unfold/1%% @see property/1compact(List) ->    [property(P) || P <- List].%% ---------------------------------------------------------------------%% @spec lookup(Key::term(), List::[term()]) -> none | tuple()%%%% @doc Returns the first entry associated with <code>Key</code> in%% <code>List</code>, if one exists, otherwise returns%% <code>none</code>. For an atom <code>A</code> in the list, the tuple%% <code>{A, true}</code> is the entry associated with <code>A</code>.%%%% @see lookup_all/2%% @see get_value/2%% @see get_bool/2lookup(Key, [P | Ps]) ->    if is_atom(P), P =:= Key ->	    {Key, true};       is_tuple(P), size(P) >= 1, element(1, P) =:= Key ->	    %% Note that <code>Key</code> does not have to be an atom in this case.	    P;       true ->	    lookup(Key, Ps)    end;lookup(_Key, []) ->    none.%% @spec lookup_all(Key::term(), List::[term()]) -> [tuple()]%%%% @doc Returns the list of all entries associated with <code>Key</code>%% in <code>List</code>. If no such entry exists, the result is the%% empty list.%%%% @see lookup/2lookup_all(Key, [P | Ps]) ->    if is_atom(P), P =:= Key ->	    [{Key, true} | lookup_all(Key, Ps)];       is_tuple(P), size(P) >= 1, element(1, P) =:= Key ->	    [P | lookup_all(Key, Ps)];       true ->	    lookup_all(Key, Ps)    end;lookup_all(_Key, []) ->    [].%% ---------------------------------------------------------------------%% @spec is_defined(Key::term(), List::[term()]) -> bool()%%%% @doc Returns <code>true</code> if <code>List</code> contains at least%% one entry associated with <code>Key</code>, otherwise%% <code>false</code> is returned.is_defined(Key, [P | Ps]) ->    if is_atom(P), P =:= Key ->	    true;       is_tuple(P), size(P) >= 1, element(1, P) =:= Key ->	    true;       true ->	    is_defined(Key, Ps)    end;is_defined(_Key, []) ->    false.%% ---------------------------------------------------------------------%% @spec get_value(Key::term(), List::[term()]) -> term()%% @equiv get_value(Key, List, undefined)get_value(Key, List) ->    get_value(Key, List, undefined).%% @spec get_value(Key::term(), List::[term()], Default::term()) ->%%         term()%%%% @doc Returns the value of a simple key/value property in%% <code>List</code>. If <code>lookup(Key, List)</code> would yield%% <code>{Key, Value}</code>, this function returns the corresponding%% <code>Value</code>, otherwise <code>Default</code> is returned.%%%% @see lookup/2%% @see get_value/1%% @see get_all_values/2%% @see get_bool/2get_value(Key, [P | Ps], Default) ->    if is_atom(P), P =:= Key ->	    true;       is_tuple(P), size(P) >= 1, element(1, P) =:= Key ->	    case P of		{_, Value} ->		    Value;		_ ->		    %% Don</code>t continue the search!		    Default	    end;       true ->	    get_value(Key, Ps, Default)    end;get_value(_Key, [], Default) ->    Default.%% @spec get_all_values(Key, List) -> [term()]%%%% @doc Similar to <code>get_value/2</code>, but returns the list of%% values for <em>all</em> entries <code>{Key, Value}</code> in%% <code>List</code>. If no such entry exists, the result is the empty%% list.%%%% @see get_value/2get_all_values(Key, [P | Ps]) ->    if is_atom(P), P =:= Key ->	    [true | get_all_values(Key, Ps)];       is_tuple(P), size(P) >= 1, element(1, P) =:= Key ->	    case P of		{_, Value} ->		    [Value | get_all_values(Key, Ps)];		_ ->		    get_all_values(Key, Ps)	    end;       true ->	    get_all_values(Key, Ps)    end;get_all_values(_Key, []) ->    [].%% @spec append_values(Key::term(), List::[term()]) -> [term()]%%%% @doc Similar to <code>get_all_values/2</code>, but each value is%% wrapped in a list unless it is already itself a list, and the%% resulting list of lists is concatenated. This is often useful for%% "incremental" options; e.g., <code>append_values(a, [{a, [1,2]}, {b,%% 0}, {a, 3}, {c, -1}, {a, [4]}])</code> will return the list%% <code>[1,2,3,4]</code>.%%%% @see get_all_values/2append_values(Key, [P | Ps]) ->    if is_atom(P), P =:= Key ->	    [true | append_values(Key, Ps)];       is_tuple(P), size(P) >= 1, element(1, P) =:= Key ->	    case P of		{_, Value} when is_list(Value) ->		    Value ++ append_values(Key, Ps);		{_, Value} ->		    [Value | append_values(Key, Ps)];		_ ->		    append_values(Key, Ps)	    end;       true ->	    append_values(Key, Ps)    end;append_values(_Key, []) ->    [].%% ---------------------------------------------------------------------%% @spec get_bool(Key::term(), List::[term()]) -> bool()%%%% @doc Returns the value of a boolean key/value option. If%% <code>lookup(Key, List)</code> would yield <code>{Key, true}</code>,%% this function returns <code>true</code>; otherwise <code>false</code>%% is returned.%%%% @see lookup/2%% @see get_value/2get_bool(Key, [P | Ps]) ->    if is_atom(P), P =:= Key ->	    true;       is_tuple(P), size(P) >= 1, element(1, P) =:= Key ->	    case P of		{_, true} ->		    true;		_ ->		    %% Don't continue the search!		    false	    end;       true ->	    get_bool(Key, Ps)    end;get_bool(_Key, []) ->    false.%% ---------------------------------------------------------------------%% @spec get_keys(List::term()) -> [term()]%%%% @doc Returns an unordered list of the keys used in <code>List</code>,%% not containing duplicates.get_keys(Ps) ->    sets:to_list(get_keys(Ps, sets:new())).get_keys([P | Ps], Keys) ->    if is_atom(P) ->	    get_keys(Ps, sets:add_element(P, Keys));       is_tuple(P), size(P) >= 1 ->	    get_keys(Ps, sets:add_element(element(1, P), Keys));       true ->	    get_keys(Ps, Keys)    end;get_keys([], Keys) ->    Keys.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?