@@ -3427,6 +3427,55 @@ defmodule Kernel do
34273427 Macro . escape ( regex )
34283428 end
34293429
3430+ @ doc """
3431+ Handles the sigil %w. It returns a list of "words" split by whitespace.
3432+
3433+ ## Modifiers
3434+
3435+ - `b`: binaries (default)
3436+ - `a`: atoms
3437+ - `c`: char lists
3438+
3439+ ## Examples
3440+
3441+ iex> %w(foo \# {:bar} baz)
3442+ ["foo", "bar", "baz"]
3443+ iex> %w(--source test/enum_test.exs)
3444+ ["--source", "test/enum_test.exs"]
3445+ iex> %w(foo bar baz)a
3446+ [:foo, :bar, :baz]
3447+
3448+ """
3449+
3450+ defmacro __w__ ( { :<<>> , _line , [ string ] } , modifiers ) when is_binary ( string ) do
3451+ split_words ( Macro . unescape_binary ( string ) , modifiers )
3452+ end
3453+
3454+ defmacro __w__ ( { :<<>> , line , pieces } , modifiers ) do
3455+ binary = { :<<>> , line , Macro . unescape_tokens ( pieces ) }
3456+ split_words ( binary , modifiers )
3457+ end
3458+
3459+ @ doc """
3460+ Handles the sigil %W. It returns a list of "words" split by whitespace
3461+ without escaping nor interpreting interpolations.
3462+
3463+ ## Modifiers
3464+
3465+ - `b`: binaries (default)
3466+ - `a`: atoms
3467+ - `c`: char lists
3468+
3469+ ## Examples
3470+
3471+ iex> %W(foo \# {bar} baz)
3472+ ["foo", "\\ \# {bar}", "baz"]
3473+
3474+ """
3475+ defmacro __W__ ( { :<<>> , _line , [ string ] } , modifiers ) when is_binary ( string ) do
3476+ split_words ( string , modifiers )
3477+ end
3478+
34303479 ## Private functions
34313480
34323481 # Extracts concatenations in order to optimize many
@@ -3472,4 +3521,22 @@ defmodule Kernel do
34723521 end
34733522
34743523 defp build_cond_clauses ( [ ] , acc ) , do: acc
3524+
3525+ defp split_words ( string , modifiers ) do
3526+ quote do
3527+ mod = case unquote ( modifiers ) do
3528+ [ ] -> ?b
3529+ [ mod ] when mod in [ ?b , ?a , ?c ] -> mod
3530+ _else -> raise ArgumentError , message: "modifier must be one of: b, a, c"
3531+ end
3532+
3533+ parts = String . split ( unquote ( string ) )
3534+
3535+ case mod do
3536+ ?b -> parts
3537+ ?a -> lc p inlist parts , do: binary_to_atom ( p )
3538+ ?c -> lc p inlist parts , do: binary_to_list ( p )
3539+ end
3540+ end
3541+ end
34753542end
0 commit comments