From a383cf0cb7d653b8514c4bcecb90c833a7e49b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 17 Apr 2023 13:15:53 +0200 Subject: Allow binaries: :as_strings to override :base --- lib/elixir/lib/inspect.ex | 4 ++-- lib/elixir/lib/inspect/algebra.ex | 18 +++++++++--------- lib/elixir/test/elixir/inspect_test.exs | 1 + 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/elixir/lib/inspect.ex b/lib/elixir/lib/inspect.ex index 3734a1389..53797e58f 100644 --- a/lib/elixir/lib/inspect.ex +++ b/lib/elixir/lib/inspect.ex @@ -158,8 +158,8 @@ defimpl Inspect, for: BitString do def inspect(term, opts) when is_binary(term) do %Inspect.Opts{binaries: bins, base: base, printable_limit: printable_limit} = opts - if base == :decimal and - (bins == :as_strings or (bins == :infer and String.printable?(term, printable_limit))) do + if bins == :as_strings or + (bins == :infer and String.printable?(term, printable_limit) and base == :decimal) do inspected = case Identifier.escape(term, ?", printable_limit) do {escaped, ""} -> [?", escaped, ?"] diff --git a/lib/elixir/lib/inspect/algebra.ex b/lib/elixir/lib/inspect/algebra.ex index ac47cf4d0..cfd790f77 100644 --- a/lib/elixir/lib/inspect/algebra.ex +++ b/lib/elixir/lib/inspect/algebra.ex @@ -4,9 +4,8 @@ defmodule Inspect.Opts do The following fields are available: - * `:base` - prints integers as `:binary`, `:octal`, `:decimal`, or `:hex`, - defaults to `:decimal`. When inspecting binaries any `:base` other than - `:decimal` implies `binaries: :as_binaries`. + * `:base` - prints integers and binaries as `:binary`, `:octal`, `:decimal`, + or `:hex`. Defaults to `:decimal`. * `:binaries` - when `:as_binaries` all binaries will be printed in bit syntax. @@ -14,9 +13,9 @@ defmodule Inspect.Opts do When `:as_strings` all binaries will be printed as strings, non-printable bytes will be escaped. - When the default `:infer`, the binary will be printed as a string if it - is printable, otherwise in bit syntax. See `String.printable?/1` to learn - when a string is printable. + When the default `:infer`, the binary will be printed as a string if `:base` + is `:decimal` and if it is printable, otherwise in bit syntax. See + `String.printable?/1` to learn when a string is printable. * `:charlists` - when `:as_charlists` all lists will be printed as charlists, non-printable elements will be escaped. @@ -32,9 +31,10 @@ defmodule Inspect.Opts do to pass the custom options through. It supports some pre-defined keys: - - `:sort_maps` (since v1.15.0) - if set to `true`, sorts key-value pairs in maps. - This can be helpful to make map inspection deterministic for testing, - especially since key order is random since OTP 26. + + - `:sort_maps` (since v1.14.4) - if set to `true`, sorts key-value pairs + in maps. This can be helpful to make map inspection deterministic for + testing, given maps key order is random. * `:inspect_fun` (since v1.9.0) - a function to build algebra documents. Defaults to `Inspect.Opts.default_inspect_fun/0`. diff --git a/lib/elixir/test/elixir/inspect_test.exs b/lib/elixir/test/elixir/inspect_test.exs index 6fbb02d31..286fe5b2a 100644 --- a/lib/elixir/test/elixir/inspect_test.exs +++ b/lib/elixir/test/elixir/inspect_test.exs @@ -169,6 +169,7 @@ defmodule Inspect.BitStringTest do assert inspect(<<"john", 193, "doe">>, binaries: :as_strings) == ~s("john\\xC1doe") assert inspect(<<"john">>, binaries: :as_strings) == ~s("john") assert inspect(<<193>>, binaries: :as_strings) == ~s("\\xC1") + assert inspect(<<193>>, base: :hex, binaries: :as_strings) == ~s("\\xC1") end test "as binaries" do -- cgit v1.2.1