Skip to content

Instantly share code, notes, and snippets.

@evnu
Last active November 9, 2023 01:45
Show Gist options
  • Save evnu/f1da2ae067423bbe760a28936deee716 to your computer and use it in GitHub Desktop.
Save evnu/f1da2ae067423bbe760a28936deee716 to your computer and use it in GitHub Desktop.
Find all documented Erlang NIF functions and when they were introduced
#!/usr/bin/escript
%%%
%%% Output NIF functions and since when they are supported
%%%
%%% Usage:
%%%
%%% Run `funcs.escript` without any arguments to output all NIF functions.
%%% To filter NIFs by a specific version of OTP, use `funcs.escript <version>`,
%%% where matching versions must have the same prefix as the one supplied on
%%% the command line.
%%%
-include_lib("xmerl/include/xmerl.hrl").
main(Args) ->
FetchPaths = filelib:wildcard("lib/erl_docgen/priv/*"),
{ParseResult, _} = xmerl_scan:file("erts/doc/src/erl_nif.xml", [{fetch_path, FetchPaths}]),
Funcs = xmerl_xpath:string("/cref/funcs/func/name", ParseResult),
Pairs = [
begin
Inner = Func#xmlElement.content,
[NameText] = [ NameText || NameText = #xmlElement{name = nametext} <- Inner ],
[Name] = [ prettify_name(Name#xmlText.value) || Name = #xmlText{} <- NameText#xmlElement.content ],
[Since] = [ prettify_since(Attr#xmlAttribute.value) || Attr = #xmlAttribute{} <- Func#xmlElement.attributes ],
{Since, Name}
end
|| Func <- Funcs
],
FilteredPairs = case Args of
[] -> Pairs;
[VersionPrefix] -> filter(Pairs, VersionPrefix)
end,
output(FilteredPairs).
prettify_name(Str) ->
[Name | _] = string:tokens(Str, "("),
Name.
prettify_since("") -> "UNKNOWN ";
prettify_since(Since) -> Since.
filter(Pairs, VersionPrefix) ->
[
{Since, Name} || {Since, Name} <- Pairs, string:find(Since, VersionPrefix) /= nomatch
].
output(Pairs) ->
[
io:format("~10.s ~20.s ~s~n", [Since, Name, url(Name)]) || {Since, Name} <- Pairs
].
url(Name) ->
io_lib:format("http://erlang.org/doc/man/erl_nif.html#~s", [Name]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment