Skip to content

Commit

Permalink
Add printable and visible guards
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Mar 14, 2020
1 parent 84dca94 commit 22f64e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
16 changes: 14 additions & 2 deletions lib/unicode_guards.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,19 @@ defmodule Unicode.Guards do
"""
defguard is_printable(codepoint)
when is_integer(codepoint) and match?(codepoint,
"[[\u0020-\u007e][\u0100-\u01FF][\u00A0..\uD7FF][\uE000-\uFFFD][\u10000-\u10FFFF]\n\r\t\v\b\f\e\d\a]")
when is_integer(codepoint) and match?(codepoint, "[[:printable:]]")

@doc """
Guards whether a UTF8 codepoint is a visible.
The definition is a combination of `L`, `N`,
`M`, `P`, `S` and `Zs` categories.
This is the solution most commonly cited on stackexchange
although it has no standardization behind it.
"""
defguard is_visible(codepoint)
when is_integer(codepoint) and match?(codepoint, "[[:visible:]]")

end
8 changes: 4 additions & 4 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
%{
"benchee": {:hex, :benchee, "1.0.1", "66b211f9bfd84bd97e6d1beaddf8fc2312aaabe192f776e8931cb0c16f53a521", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}], "hexpm", "3ad58ae787e9c7c94dd7ceda3b587ec2c64604563e049b2a0e8baafae832addb"},
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
"earmark": {:hex, :earmark, "1.4.2", "3aa0bd23bc4c61cf2f1e5d752d1bb470560a6f8539974f767a38923bb20e1d7f", [:mix], [], "hexpm", "5e8806285d8a3a8999bd38e4a73c58d28534c856bc38c44818e5ba85bbda16fb"},
"ex_doc": {:hex, :ex_doc, "0.21.2", "caca5bc28ed7b3bdc0b662f8afe2bee1eedb5c3cf7b322feeeb7c6ebbde089d6", [:mix], [{:earmark, "~> 1.3.3 or ~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "f1155337ae17ff7a1255217b4c1ceefcd1860b7ceb1a1874031e7a861b052e39"},
"ex_unicode": {:hex, :ex_unicode, "1.4.0", "3842fd0962d69cb06b8e9317732a8ccd13ba3e987dbf8af85b4453b84ec01ae1", [:mix], [], "hexpm", "70dd444cf3c60685fb489df45cfb0f52da4f7a8f7d9cc6c8f7f9bca4b1c68343"},
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"},
"ex_doc": {:hex, :ex_doc, "0.21.3", "857ec876b35a587c5d9148a2512e952e24c24345552259464b98bfbb883c7b42", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "0db1ee8d1547ab4877c5b5dffc6604ef9454e189928d5ba8967d4a58a801f161"},
"ex_unicode": {:hex, :ex_unicode, "1.5.0", "78541349402d395a9c01c076659c4610dd93031e4021427414adc3b835a9a824", [:mix], [], "hexpm", "92a79acde664afcecb6659574c05f755b7bceea482f5526a34f6799091e1e3b7"},
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "a10c6eb62cca416019663129699769f0c2ccf39428b3bb3c0cb38c718a0c186d"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm", "589b5af56f4afca65217a1f3eb3fee7e79b09c40c742fddc1c312b3ac0b3399f"},
"unicode_set": {:hex, :unicode_set, "0.5.0", "5927f56a62f06d89ea9de3703ced3d54258f957ae408b8f1892be2b8f75624cf", [:mix], [{:ex_unicode, "~> 1.4", [hex: :ex_unicode, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 0.5", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "974b29109ef71eb72311cbfaf60336f24ad6eedfc46f5a618ac627c92ffc8e2e"},
"unicode_set": {:hex, :unicode_set, "0.5.1", "17433e103392a863e7ec3e4be330e23186daec986b46d28c5f255631ca11bc05", [:mix], [{:ex_unicode, "~> 1.5", [hex: :ex_unicode, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 0.5", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "6cd855f958152edd7cb06af05763ff3c4831970ba2bb872f2f1e78c2d99eb7b1"},
}
14 changes: 14 additions & 0 deletions test/unicode_guards_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ defmodule Unicode.GuardsTest do
assert Quotes.ambidextrous("\"") == :ambidextrous

end

test "printable guards" do
defmodule Printable do
import Unicode.Guards

def printable(<< x :: utf8, _rest :: binary >>) when is_printable(x), do: :printable
def printable(_other), do: :not_printable

end

assert Printable.printable("a") == :printable
assert Printable.printable(<< 0 :: utf8 >>) == :not_printable

end
end

0 comments on commit 22f64e6

Please sign in to comment.