Skip to content

Commit 22f64e6

Browse files
committed
Add printable and visible guards
1 parent 84dca94 commit 22f64e6

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

lib/unicode_guards.ex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,19 @@ defmodule Unicode.Guards do
152152
153153
"""
154154
defguard is_printable(codepoint)
155-
when is_integer(codepoint) and match?(codepoint,
156-
"[[\u0020-\u007e][\u0100-\u01FF][\u00A0..\uD7FF][\uE000-\uFFFD][\u10000-\u10FFFF]\n\r\t\v\b\f\e\d\a]")
155+
when is_integer(codepoint) and match?(codepoint, "[[:printable:]]")
156+
157+
@doc """
158+
Guards whether a UTF8 codepoint is a visible.
159+
160+
The definition is a combination of `L`, `N`,
161+
`M`, `P`, `S` and `Zs` categories.
162+
163+
This is the solution most commonly cited on stackexchange
164+
although it has no standardization behind it.
165+
166+
"""
167+
defguard is_visible(codepoint)
168+
when is_integer(codepoint) and match?(codepoint, "[[:visible:]]")
157169

158170
end

mix.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
%{
22
"benchee": {:hex, :benchee, "1.0.1", "66b211f9bfd84bd97e6d1beaddf8fc2312aaabe192f776e8931cb0c16f53a521", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}], "hexpm", "3ad58ae787e9c7c94dd7ceda3b587ec2c64604563e049b2a0e8baafae832addb"},
33
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
4-
"earmark": {:hex, :earmark, "1.4.2", "3aa0bd23bc4c61cf2f1e5d752d1bb470560a6f8539974f767a38923bb20e1d7f", [:mix], [], "hexpm", "5e8806285d8a3a8999bd38e4a73c58d28534c856bc38c44818e5ba85bbda16fb"},
5-
"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"},
6-
"ex_unicode": {:hex, :ex_unicode, "1.4.0", "3842fd0962d69cb06b8e9317732a8ccd13ba3e987dbf8af85b4453b84ec01ae1", [:mix], [], "hexpm", "70dd444cf3c60685fb489df45cfb0f52da4f7a8f7d9cc6c8f7f9bca4b1c68343"},
4+
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"},
5+
"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"},
6+
"ex_unicode": {:hex, :ex_unicode, "1.5.0", "78541349402d395a9c01c076659c4610dd93031e4021427414adc3b835a9a824", [:mix], [], "hexpm", "92a79acde664afcecb6659574c05f755b7bceea482f5526a34f6799091e1e3b7"},
77
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "a10c6eb62cca416019663129699769f0c2ccf39428b3bb3c0cb38c718a0c186d"},
88
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
99
"nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm", "589b5af56f4afca65217a1f3eb3fee7e79b09c40c742fddc1c312b3ac0b3399f"},
10-
"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"},
10+
"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"},
1111
}

test/unicode_guards_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,18 @@ defmodule Unicode.GuardsTest do
4444
assert Quotes.ambidextrous("\"") == :ambidextrous
4545

4646
end
47+
48+
test "printable guards" do
49+
defmodule Printable do
50+
import Unicode.Guards
51+
52+
def printable(<< x :: utf8, _rest :: binary >>) when is_printable(x), do: :printable
53+
def printable(_other), do: :not_printable
54+
55+
end
56+
57+
assert Printable.printable("a") == :printable
58+
assert Printable.printable(<< 0 :: utf8 >>) == :not_printable
59+
60+
end
4761
end

0 commit comments

Comments
 (0)