charcutter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: afb17205fc7226a6a055a6f37f4a4680883fc0d8dd7137fd72e1cb5614e73610
4
+ data.tar.gz: 854784fe64707c2664e8e7be4586747ffb7d5ccdd0c9f05d9fc2f732d28b3c6d
5
+ SHA512:
6
+ metadata.gz: c76cb80045c2e404c151a75e4d828b946b8a23e9c825e9b6e6ea7d5e0e87fd561be6a477f998f3bfadbc13b6fdee3fad57c6421ef62836efe71e24bf6e4862d5
7
+ data.tar.gz: 7fe2550f1498174a43c2f2adbddc4a31a74f3721daedbc878ec5ed39ab36c2e70c0e810f8c4f644f1ba6a9b4d4794df2adfffa8c6d02e2aaa7fec30d5cc97362
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ /pkg
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## CHANGELOG
2
+
3
+ ### 0.1.0
4
+
5
+ * Initial release; Only supports removing/restricting to ignorables
6
+
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at opensource@janlelis.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'minitest'
6
+ gem 'minitest-mock'
7
+ gem 'rake'
8
+ gem 'irb'
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2026 Jan Lelis, https://janlelis.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Charcutter [![[version]](https://badge.fury.io/rb/charcutter.svg)](https://badge.fury.io/rb/charcutter) [![[ci]](https://github.com/janlelis/charcutter/workflows/Test/badge.svg)](https://github.com/janlelis/charcutter/actions?query=workflow%3ATest)
2
+
3
+ Be in control of what codepoints your string contains. Charcutter provides Unicode-based regexs to restrict or permit certain characters in your user input.
4
+
5
+ Version 0.0.1: Only ignorable characters are supported.
6
+
7
+ Unicode version: **18.0.0** (September 2026)
8
+
9
+ ## Gemfile
10
+
11
+ ```ruby
12
+ gem "charcutter"
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ require "charcutter"
19
+
20
+ string = "A\u{FE00}B" # U+FE00 is VARIATION SELECTOR-1, an ignorable codepoint
21
+
22
+ # Wrap string in Charcutter[] and call deny with codepoint types to remove
23
+ Charcutter[string].deny(:ignorable) # => "AB"
24
+ ```
25
+
26
+ See [unicode-confusable](https://github.com/janlelis/unicode-confusable) for for an example usage of this library.
27
+
28
+ ## MIT License
29
+
30
+ - Copyright (C) 2026 Jan Lelis <https://janlelis.com>. Released under the MIT license.
31
+ - Unicode data: https://www.unicode.org/copyright.html#Exhibit1
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ # # #
2
+ # Get gemspec info
3
+
4
+ gemspec_file = Dir['*.gemspec'].first
5
+ gemspec = eval File.read(gemspec_file), binding, gemspec_file
6
+ info = "#{gemspec.name} | #{gemspec.version} | " \
7
+ "#{gemspec.runtime_dependencies.size} dependencies | " \
8
+ "#{gemspec.files.size} files"
9
+
10
+ # # #
11
+ # Gem build and install task
12
+
13
+ desc info
14
+ task :gem do
15
+ puts info + "\n\n"
16
+ print " "; sh "gem build #{gemspec_file}"
17
+ FileUtils.mkdir_p 'pkg'
18
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
19
+ puts; sh %{gem install --no-document pkg/#{gemspec.name}-#{gemspec.version}.gem}
20
+ end
21
+
22
+ # # #
23
+ # Start an IRB session with the gem loaded
24
+
25
+ desc "#{gemspec.name} | IRB"
26
+ task :irb do
27
+ sh "irb -I ./lib -r #{gemspec.name.gsub '-','/'}"
28
+ end
29
+
30
+ # # #
31
+ # Run Specs
32
+
33
+ desc "#{gemspec.name} | Spec"
34
+ task :spec do
35
+ if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
36
+ sh "for %f in (spec/\*.rb) do ruby spec/%f"
37
+ else
38
+ sh "for file in spec/*.rb; do ruby $file; done"
39
+ end
40
+ end
41
+ task default: :spec
42
+
43
+ # # #
44
+ # Generate regex
45
+
46
+ desc "#{gemspec.name} | Generates all regex constants and saves them to charcutter/{constructed,native} directories"
47
+ task :update_regexes do
48
+ load "scripts/update_regexes.rb", true
49
+ end
50
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + "/lib/charcutter/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "charcutter"
7
+ gem.version = Charcutter::VERSION
8
+ gem.summary = "Be in charge of your strings."
9
+ gem.description = "[Unicode #{Charcutter::UNICODE_VERSION}] Be in charge of your strings."
10
+ gem.authors = ["Jan Lelis"]
11
+ gem.email = ["hi@ruby.consulting"]
12
+ gem.homepage = "https://github.com/janlelis/charcutter"
13
+ gem.license = "MIT"
14
+
15
+ gem.files = Dir["{**/}{.*,*}"].select{ |path| File.file?(path) && path !~ /^pkg|data/ }
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.require_paths = ["lib"]
18
+ gem.metadata = { "rubygems_mfa_required" => "true" }
19
+
20
+ gem.required_ruby_version = ">= 3.2"
21
+ end
@@ -0,0 +1,7 @@
1
+ # This file is auto-generated. See scripts/update_regexes.rb for more info.
2
+
3
+ class Charcutter
4
+ module Regex
5
+ IGNORABLE = /[­͏؜ᅟ-ᅠ឴-឵᠋-᠍᠎᠏​-‏‪-‮⁠-⁤⁥⁦-ㅤ︀-️ᅠ￰-￸𛲠-𛲣𝅳-𝅺󠀀󠀁󠀂-󠀟󠀠-󠁿󠂀-󠃿󠄀-󠇯󠇰-󠿿]/
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # This file is auto-generated. See scripts/update_regexes.rb for more info.
2
+
3
+ class Charcutter
4
+ module Regex
5
+ IGNORABLE_NEGATED = /[^­͏؜ᅟ-ᅠ឴-឵᠋-᠍᠎᠏​-‏‪-‮⁠-⁤⁥⁦-ㅤ︀-️ᅠ￰-￸𛲠-𛲣𝅳-𝅺󠀀󠀁󠀂-󠀟󠀠-󠁿󠂀-󠃿󠄀-󠇯󠇰-󠿿]/
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # This file is auto-generated. See scripts/update_regexes.rb for more info.
2
+
3
+ class Charcutter
4
+ module Regex
5
+ IGNORABLE = /\p{di}/
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # This file is auto-generated. See scripts/update_regexes.rb for more info.
2
+
3
+ class Charcutter
4
+ module Regex
5
+ IGNORABLE_NEGATED = /\p{^di}/
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ require "rbconfig"
2
+
3
+ class Charcutter
4
+ module Regex
5
+ MODE = UNICODE_VERSION == RbConfig::CONFIG["UNICODE_VERSION"] ?
6
+ "native" :
7
+ "constructed"
8
+
9
+ autoload :IGNORABLE, File.expand_path("regex/#{MODE}/ignorable", __dir__)
10
+ autoload :IGNORABLE_NEGATED, File.expand_path("regex/#{MODE}/ignorable_negated", __dir__)
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "../charcutter"
2
+
3
+ # Optional string extension for your convenience
4
+ class String
5
+ def allow(...)
6
+ Charcutter[self].allow(...)
7
+ end
8
+
9
+ def deny(...)
10
+ Charcutter[self].deny(...)
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Charcutter
4
+ VERSION = "0.1.0"
5
+ UNICODE_VERSION = "18.0.0"
6
+ end
data/lib/charcutter.rb ADDED
@@ -0,0 +1,31 @@
1
+ require_relative "charcutter/version"
2
+ require_relative "charcutter/regex"
3
+
4
+ class Charcutter
5
+ EMPTY = ""
6
+
7
+ class << self
8
+ alias [] new
9
+ end
10
+
11
+ def initialize(string)
12
+ @string = string
13
+ end
14
+
15
+ def allow(*props, **)
16
+ if props[0] == :ignorable
17
+ @string.gsub(Regex::IGNORABLE_NEGATED, EMPTY)
18
+ else
19
+ raise ArgumentError, "only :ignorable supported as of 0.0.1"
20
+ end
21
+ end
22
+
23
+ def deny(*props, **)
24
+ if props[0] == :ignorable
25
+ @string.gsub(Regex::IGNORABLE, EMPTY)
26
+ else
27
+ raise ArgumentError, "only :ignorable supported as of 0.0.1"
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,107 @@
1
+ # Based on radarek contribution to unicode-emoji
2
+
3
+ require "zlib"
4
+
5
+ index_filename = (File.dirname(__FILE__) + "/../data/charcutter.marshal.gz").freeze
6
+
7
+ File.open(index_filename, "rb") do |file|
8
+ serialized_data = Zlib::GzipReader.new(file).read
9
+ serialized_data.force_encoding Encoding::BINARY
10
+ INDEX = Marshal.load(serialized_data)
11
+ end
12
+
13
+ def write_regexes(regexes, dirpath)
14
+ regexes.each do |const_name, regex|
15
+ write_regex(const_name, regex, dirpath)
16
+ end
17
+ end
18
+
19
+ def write_regex(const_name, regex, dirpath)
20
+ filename = const_name.to_s.downcase
21
+ filepath = File.join(dirpath, "#{filename}.rb")
22
+
23
+ File.write(filepath, <<~CONTENT)
24
+ # This file is auto-generated. See scripts/update_regexes.rb for more info.
25
+
26
+ class Charcutter
27
+ module Regex
28
+ #{const_name} = #{regex.inspect}
29
+ end
30
+ end
31
+ CONTENT
32
+ puts "#{const_name} written to #{filepath}"
33
+ end
34
+
35
+ # Converts [1, 2, 3, 5, 6, 20, 21, 22, 23, 100] (it does not need to be sorted) to [[1, 2, 3], [5, 6], [20, 21, 22, 23], [100]]
36
+ def groupify(arr)
37
+ arr = arr.sort
38
+ prev = nil
39
+ arr.slice_before do |el|
40
+ (prev.nil? || el != prev + 1).tap { prev = el }
41
+ end
42
+ end
43
+
44
+ # Converts [1, 2, 3, 5, 6, 20, 21, 22, 23, 100] (it does not need to be sorted) to [1..3, 5, 6, 20..23, 100]
45
+ def rangify(arr)
46
+ groupify(arr).map do |group|
47
+ group.size < 3 ? group : Range.new(group.first, group.last)
48
+ end.flatten
49
+ end
50
+
51
+ def rangify_from_limits(arr)
52
+ arr.map do |el_or_limits|
53
+ if el_or_limits.is_a? Array
54
+ Range.new(*el_or_limits)
55
+ else
56
+ el_or_limits
57
+ end
58
+ end
59
+ end
60
+
61
+ def pack(ord)
62
+ Regexp.escape(Array(ord).pack("U*"))
63
+ end
64
+
65
+ def join(*strings)
66
+ "(?:" + strings.join("|") + ")"
67
+ end
68
+
69
+ def character_class(ords_with_ranges)
70
+ "[" + ords_with_ranges.map{ |ord_or_range|
71
+ ord_or_range.is_a?(Range) ?
72
+ pack(ord_or_range.first) + "-" + pack(ord_or_range.last) :
73
+ pack(ord_or_range)
74
+ }.join +
75
+ "]"
76
+ end
77
+
78
+ def pack_and_join(ords)
79
+ if ords.any? { |e| e.is_a?(Array) }
80
+ join(*ords.map { |ord| pack(ord) })
81
+ else
82
+ character_class(rangify(ords))
83
+ end
84
+ end
85
+
86
+ def add_negation(regex_source)
87
+ regex_source.sub(/(?<=\[|\\p{)/,"^")
88
+ end
89
+
90
+ def compile(ignorable:)
91
+ regexes = {}
92
+
93
+ regexes[:IGNORABLE] = Regexp.compile(ignorable)
94
+ regexes[:IGNORABLE_NEGATED] = Regexp.compile(add_negation(ignorable))
95
+
96
+ regexes
97
+ end
98
+
99
+ constructed_regexes = compile(
100
+ ignorable: character_class(rangify_from_limits(INDEX[:IGNORABLE]))
101
+ )
102
+ write_regexes(constructed_regexes, File.expand_path("../lib/charcutter/regex/constructed", __dir__))
103
+
104
+ native_regexes = compile(
105
+ ignorable: "\\p{di}",
106
+ )
107
+ write_regexes(native_regexes, File.expand_path("../lib/charcutter/regex/native", __dir__))
@@ -0,0 +1,24 @@
1
+ require_relative "../lib/charcutter"
2
+ require "minitest/autorun"
3
+
4
+ describe Charcutter do
5
+ describe ".[]" do
6
+ it "creates a new charcutter instance" do
7
+ assert_instance_of Charcutter, Charcutter["A"]
8
+ end
9
+ end
10
+
11
+ describe "#allow" do
12
+ it "will only allow the given character type in the string" do
13
+ assert_equal "\u{FE00}", Charcutter["A\u{FE00}B"].allow(:ignorable)
14
+ end
15
+ end
16
+
17
+ describe "#deny" do
18
+ it "will remove given character type in the string" do
19
+ assert_equal "AB", Charcutter["A\u{FE00}B"].deny(:ignorable)
20
+ end
21
+ end
22
+
23
+ end
24
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: charcutter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan Lelis
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: "[Unicode 18.0.0] Be in charge of your strings."
13
+ email:
14
+ - hi@ruby.consulting
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".gitignore"
20
+ - CHANGELOG.md
21
+ - CODE_OF_CONDUCT.md
22
+ - Gemfile
23
+ - MIT-LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - charcutter.gemspec
27
+ - lib/charcutter.rb
28
+ - lib/charcutter/regex.rb
29
+ - lib/charcutter/regex/constructed/ignorable.rb
30
+ - lib/charcutter/regex/constructed/ignorable_negated.rb
31
+ - lib/charcutter/regex/native/ignorable.rb
32
+ - lib/charcutter/regex/native/ignorable_negated.rb
33
+ - lib/charcutter/string_ext.rb
34
+ - lib/charcutter/version.rb
35
+ - scripts/update_regexes.rb
36
+ - spec/charcutter_spec.rb
37
+ homepage: https://github.com/janlelis/charcutter
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ rubygems_mfa_required: 'true'
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '3.2'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 4.0.10
57
+ specification_version: 4
58
+ summary: Be in charge of your strings.
59
+ test_files: []