-module(gleam@format). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([sprintf/2, printf/2]). -export_type([format_error/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type format_error() :: {format_error, binary()}. -file("src/gleam/format.gleam", 55). ?DOC( " Returns a formatted string using the given format string and arguments or an error.\n" " \n" " Note: The function can take a list of arguments or a tuple. If you pass a tuple, it will be unpacked into a list. \n" " If the user passes a tuple as a single argument, they must provide a format string with appropriate format specifier \n" " for each element in the tuple. Erlang treats user defined types as tuples. See the examples below.\n" " ## Examples\n" " ```gleam\n" " import gleam/format.{sprintf}\n" " \n" " // Format a string with a single argument\n" " sprintf(\"Hello ~s\", <<119, 111, 114, 108, 100, 33>>)\n" " //=> Ok(\"Hello world!\")\n" " \n" " // Format a string with multiple arguments by passing a list\n" " sprintf(\"Hello ~s ~s\", [\"world!\", \"Extra!\"])\n" " //=> Ok(\"Hello world! Extra!\")\n" " \n" " // Format a string with multiple different types of arguments by passing a tuple\n" " sprintf(\"Hello ~s ~b\", #(\"world!\", 100))\n" " //=> Ok(\"Hello world! 100\")\n" " \n" " sprintf(\"1, 2, 3, ~b\", [4])\n" " //=> Ok(\"1, 2, 3, 4\")\n" " \n" " let t = TestClass(\"Test\", 69)\n" " sprintf(\"Hello ~p\", [t])\n" " // => Ok(\"Hello {test_class,<<\\\"Test\\\">>,69}\")\n" "\n" " sprintf(\"Hello ~s ~s ~b\", t)\n" " //=> Ok(\"Hello test_class Test 69\")\n" " \n" " sprintf(\"~p\", #([1, 3]))\n" " //=> Ok(\"[1,3]\")\n" " \n" " sprintf(\"~p\", [[1, 3]])\n" " //=> Ok(\"[1,3]\")\n" " \n" " sprintf(\"~p\", #(#(1, 3)))\n" " //=> Ok(\"{1,3}\")\n" " \n" " sprintf(\"This should ~a\", \"fail\")\n" " //=> Error(\"Make sure the arguments match the format string.\")\n" " ```\n" " \n" " Visit this erlang doc page for more information on string formatting:\n" " https://www.erlang.org/doc/apps/stdlib/io#fwrite/1\n" ). -spec sprintf(binary(), any()) -> {ok, binary()} | {error, format_error()}. sprintf(Format, Args) -> gleam_format:erl_format(Format, Args). -file("src/gleam/format.gleam", 103). ?DOC( " Print a formatted string to the standard output or an error message if the format fails.\n" " \n" " Note: The function can take a list of arguments or a tuple. If you pass a tuple, it will be unpacked into a list. \n" " If the user passes a tuple as a single argument, they must provide a format string with appropriate format specifier \n" " for each element in the tuple. Erlang treats user defined types as tuples. See the examples below.\n" " ## Examples\n" " ```gleam\n" " import gleam/format.{printf}\n" " \n" " // Format a string with a single argument\n" " printf(\"Hello ~s\", <<119, 111, 114, 108, 100, 33>>)\n" " //=> \"Hello world!\"\n" " \n" " // Format a string with multiple arguments by passing a list\n" " printf(\"Hello ~s ~s\", [\"world!\", \"Extra!\"])\n" " //=> \"Hello world! Extra!\"\n" " \n" " // Format a string with multiple different types of arguments by passing a tuple\n" " printf(\"Hello ~s ~b\", #(\"world!\", 100))\n" " //=> \"Hello world! 100\"\n" " \n" " printf(\"1, 2, 3, ~b\", [4])\n" " //=> \"1, 2, 3, 4\"\n" " \n" " let t = TestClass(\"Test\", 69)\n" " printf(\"Hello ~p\", [t])\n" " // => \"Hello {test_class,<<\\\"Test\\\">>,69}\"\n" "\n" " printf(\"Hello ~s ~s ~b\", t)\n" " //=> \"Hello test_class Test 69\"\n" " \n" " printf(\"~p\", #([1, 3]))\n" " //=> \"[1,3]\"\n" " \n" " printf(\"~p\", [[1, 3]])\n" " //=> \"[1,3]\"\n" " \n" " printf(\"~p\", #(#(1, 3)))\n" " //=> \"{1,3}\"\n" " \n" " printf(\"This should ~a\", \"fail\")\n" " //=> \"Make sure the arguments match the format string.\"\n" " ```\n" " \n" " Visit this erlang doc page for more information on string formatting:\n" " https://www.erlang.org/doc/apps/stdlib/io#fwrite/1\n" ). -spec printf(binary(), any()) -> nil. printf(Format, Args) -> case gleam_format:erl_format(Format, Args) of {ok, Result} -> gleam_stdlib:print(Result); {error, Err} -> gleam_stdlib:println_error( <<"ERROR: "/utf8, (erlang:element(2, Err))/binary>> ) end.