strmask 0.2.1
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.
- data/HISTORY +10 -0
- data/LICENSE +23 -0
- data/README.rdoc +47 -0
- data/lib/strmask.rb +241 -0
- data/meta/authors +1 -0
- data/meta/collection +1 -0
- data/meta/contact +1 -0
- data/meta/created +1 -0
- data/meta/description +3 -0
- data/meta/homepage +1 -0
- data/meta/license +1 -0
- data/meta/name +1 -0
- data/meta/released +1 -0
- data/meta/repository +1 -0
- data/meta/title +1 -0
- data/meta/version +1 -0
- data/test/test_strmask.rb +63 -0
- metadata +82 -0
data/HISTORY
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2009 Peter Vanbroekhoven & Thomas Sawyer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
22
|
+
|
23
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
= String::Mask
|
2
|
+
|
3
|
+
* home: http://rubyworks.github.com/strmask
|
4
|
+
* work: http://github.com/rubyworks/strmask
|
5
|
+
|
6
|
+
|
7
|
+
== DESCRIPTION
|
8
|
+
|
9
|
+
Mask provides a string utility to manipulate strings
|
10
|
+
in logicomathematical manner, ie. add, subtract, xor,
|
11
|
+
etc.
|
12
|
+
|
13
|
+
== SYNOPSIS
|
14
|
+
|
15
|
+
Mask objects can be created explicitly via #new.
|
16
|
+
|
17
|
+
m1 = String::Mask.new("abc..123", '.')
|
18
|
+
m2 = String::Mask.new("ab..789.", '.')
|
19
|
+
m1 - m2 #=> "....789."
|
20
|
+
|
21
|
+
But the String#mask extension is much easier to use.
|
22
|
+
|
23
|
+
m1 = "abc..123".mask('.')
|
24
|
+
m2 = "ab..789.".mask('.')
|
25
|
+
m1 * m2 #=> "ab..789."
|
26
|
+
|
27
|
+
The second operand can be a normal String. Mask will assume
|
28
|
+
it repesents another mask akin to the first.
|
29
|
+
|
30
|
+
"abc..123".mask('.') + "ab..789." #=> "abc.7893"
|
31
|
+
|
32
|
+
|
33
|
+
== HOW TO INSTALL
|
34
|
+
|
35
|
+
To install with RubyGems simply open a console and type:
|
36
|
+
|
37
|
+
gem install mask
|
38
|
+
|
39
|
+
|
40
|
+
== COPYRIGHT
|
41
|
+
|
42
|
+
Copyright (c) 2009 Thomas Sawyer
|
43
|
+
|
44
|
+
This program is ditributed unser the terms of the LGPL license.
|
45
|
+
|
46
|
+
See the LICENSE or COPYING file for details.
|
47
|
+
|
data/lib/strmask.rb
ADDED
@@ -0,0 +1,241 @@
|
|
1
|
+
#require 'facets/functor'
|
2
|
+
|
3
|
+
class String
|
4
|
+
|
5
|
+
# Create a mask.
|
6
|
+
def mask(re=nil)
|
7
|
+
Mask.new(self,re)
|
8
|
+
end
|
9
|
+
|
10
|
+
# = Mask
|
11
|
+
#
|
12
|
+
class Mask
|
13
|
+
|
14
|
+
ESC = "\032" # ASCII SUBSTITUTE
|
15
|
+
|
16
|
+
def self.[](string, re=nil)
|
17
|
+
new(string, re)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def initialize(string, re=nil)
|
23
|
+
@to_str = string.dup
|
24
|
+
mask!(re) if re
|
25
|
+
end
|
26
|
+
|
27
|
+
def convert(other)
|
28
|
+
case other
|
29
|
+
when Mask
|
30
|
+
other
|
31
|
+
else
|
32
|
+
self.class.new(other.to_s, re)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
public
|
37
|
+
|
38
|
+
# The underlying string object.
|
39
|
+
def to_str
|
40
|
+
@to_str
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
def to_s
|
45
|
+
to_str
|
46
|
+
end
|
47
|
+
|
48
|
+
#
|
49
|
+
def inspect
|
50
|
+
to_str.inspect
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
def [](*a)
|
55
|
+
to_str[*a]
|
56
|
+
end
|
57
|
+
|
58
|
+
def mask(re)
|
59
|
+
self.class.new(to_str,re)
|
60
|
+
end
|
61
|
+
|
62
|
+
def mask!(re)
|
63
|
+
to_str.gsub!(re){ |s| ESC * s.size }
|
64
|
+
end
|
65
|
+
|
66
|
+
# Mask subtraction. Where the characters are the same,
|
67
|
+
# the result is "empty", where they differ the result
|
68
|
+
# reflects the last string.
|
69
|
+
#
|
70
|
+
# "abc..123" "ab..789."
|
71
|
+
# - "ab..789." - "abc..123"
|
72
|
+
# ---------- ----------
|
73
|
+
# "....789." "..c..123"
|
74
|
+
#
|
75
|
+
def -(other)
|
76
|
+
other = convert(other)
|
77
|
+
i = 0
|
78
|
+
o = ''
|
79
|
+
while i < to_str.size
|
80
|
+
if to_str[i,1] == other[i,1]
|
81
|
+
o << ESC
|
82
|
+
else
|
83
|
+
o << other[i,1]
|
84
|
+
end
|
85
|
+
i += 1
|
86
|
+
end
|
87
|
+
self.class.new(o)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Mask ADD. As long as there is a value other
|
91
|
+
# then empty the character filters though.
|
92
|
+
# The last to_str takes precedence.
|
93
|
+
#
|
94
|
+
# "abc..123" "ab..789."
|
95
|
+
# + "ab..789." + "abc..123"
|
96
|
+
# ---------- ----------
|
97
|
+
# "abc.7893" "abc.7123"
|
98
|
+
#
|
99
|
+
def +(other)
|
100
|
+
other = convert(other)
|
101
|
+
i = 0
|
102
|
+
o = ''
|
103
|
+
while i < to_str.size
|
104
|
+
if other[i,1] == ESC
|
105
|
+
o << to_str[i,1]
|
106
|
+
else
|
107
|
+
o << other[i,1]
|
108
|
+
end
|
109
|
+
i += 1
|
110
|
+
end
|
111
|
+
self.class.new(o)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Mask OR is the same as ADD.
|
115
|
+
alias_method :|, :+
|
116
|
+
|
117
|
+
# Mask XAND. Where the characters are the same, the
|
118
|
+
# result is the same, where they differ the result
|
119
|
+
# reflects the later.
|
120
|
+
#
|
121
|
+
# "abc..123" "ab..789."
|
122
|
+
# * "ab..789." * "abc..123"
|
123
|
+
# ---------- ----------
|
124
|
+
# "ab..789." "abc..123"
|
125
|
+
#
|
126
|
+
def *(other)
|
127
|
+
other = convert(other)
|
128
|
+
i = 0
|
129
|
+
o = ''
|
130
|
+
while i < to_str.size
|
131
|
+
if (c = to_str[i,1]) == other[i,1]
|
132
|
+
o << c
|
133
|
+
else
|
134
|
+
o << other[i,1]
|
135
|
+
end
|
136
|
+
i += 1
|
137
|
+
end
|
138
|
+
self.class.new(o)
|
139
|
+
end
|
140
|
+
|
141
|
+
# Mask AND. Only where they are
|
142
|
+
# then same filters through.
|
143
|
+
#
|
144
|
+
# "abc..123" "ab..789."
|
145
|
+
# & "ab..789." | "abc..123"
|
146
|
+
# ---------- ----------
|
147
|
+
# "ab......" "ab......"
|
148
|
+
#
|
149
|
+
def &(other)
|
150
|
+
other = convert(other)
|
151
|
+
i = 0
|
152
|
+
o = ''
|
153
|
+
while i < to_str.size
|
154
|
+
if (c = to_str[i,1]) == other[i,1]
|
155
|
+
o << c
|
156
|
+
else
|
157
|
+
o << ESC
|
158
|
+
end
|
159
|
+
i += 1
|
160
|
+
end
|
161
|
+
self.class.new(o)
|
162
|
+
end
|
163
|
+
|
164
|
+
# Mask XOR operation. Only where there
|
165
|
+
# is an empty slot will the value filter.
|
166
|
+
#
|
167
|
+
# "abc..123" "ab..789."
|
168
|
+
# | "ab..789." | "abc..123"
|
169
|
+
# ---------- ----------
|
170
|
+
# "..c.7..3" "..c.7..3"
|
171
|
+
#
|
172
|
+
def ^(other)
|
173
|
+
other = convert(other)
|
174
|
+
i = 0
|
175
|
+
o = ''
|
176
|
+
while i < to_str.size
|
177
|
+
if to_str[i,1] == ESC
|
178
|
+
o << other[i,1]
|
179
|
+
elsif other[i,1] == ESC
|
180
|
+
o << to_str[i,1]
|
181
|
+
else
|
182
|
+
o << ESC
|
183
|
+
end
|
184
|
+
i += 1
|
185
|
+
end
|
186
|
+
self.class.new(o)
|
187
|
+
end
|
188
|
+
|
189
|
+
#
|
190
|
+
def ==(other)
|
191
|
+
case other
|
192
|
+
when Mask
|
193
|
+
to_str == other.to_str
|
194
|
+
else
|
195
|
+
to_str == other.to_s
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
# Apply a method to the internal string and return
|
200
|
+
# a new mask.
|
201
|
+
def apply(s=nil, *a, &b)
|
202
|
+
if s
|
203
|
+
to_str.send(s,*a,&b).to_mask
|
204
|
+
else
|
205
|
+
@_self ||= Functor.new do |op, *a|
|
206
|
+
to_str.send(op,*a).to_mask
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
#
|
212
|
+
def replace(string)
|
213
|
+
@to_str = string.to_s
|
214
|
+
end
|
215
|
+
|
216
|
+
#
|
217
|
+
#def instance_delegate
|
218
|
+
# @to_str
|
219
|
+
#end
|
220
|
+
|
221
|
+
# Functor on the interal string.
|
222
|
+
#def self
|
223
|
+
# @_self ||= Functor.new do |op, *a|
|
224
|
+
# @to_str = @to_str.send(op, *a)
|
225
|
+
# end
|
226
|
+
#end
|
227
|
+
|
228
|
+
# Delegate any missing methods to underlying string.
|
229
|
+
#
|
230
|
+
def method_missing(s, *a, &b)
|
231
|
+
begin
|
232
|
+
to_str.send(s, *a, &b)
|
233
|
+
rescue NoMethodError
|
234
|
+
super(s, *a, &b)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
241
|
+
|
data/meta/authors
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Thomas Sawyer
|
data/meta/collection
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rubyworks
|
data/meta/contact
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rubyworks-mailinglist@googlegroups.com
|
data/meta/created
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2009-07-19
|
data/meta/description
ADDED
data/meta/homepage
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
http://rubyworks.github.com/strmask
|
data/meta/license
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
MIT
|
data/meta/name
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
strmask
|
data/meta/released
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2010-02-24
|
data/meta/repository
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
git://github.com/rubyworks/strmask.git
|
data/meta/title
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
String::Mask
|
data/meta/version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'strmask'
|
3
|
+
|
4
|
+
class TestStringMask < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@x1 = String::Mask["abc..123", '.']
|
8
|
+
@x2 = String::Mask["ab..789.", '.']
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_sub
|
12
|
+
r = @x1 - @x2
|
13
|
+
a = String::Mask["....789.", '.']
|
14
|
+
assert_equal(a, r)
|
15
|
+
|
16
|
+
r = @x2 - @x1
|
17
|
+
a = String::Mask["..c..123", '.']
|
18
|
+
assert_equal(a, r)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_add
|
22
|
+
r = @x1 + @x2
|
23
|
+
a = String::Mask["abc.7893", '.']
|
24
|
+
assert_equal(a, r)
|
25
|
+
|
26
|
+
r = @x2 + @x1
|
27
|
+
a = String::Mask["abc.7123", '.']
|
28
|
+
assert_equal(a, r)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_xand
|
32
|
+
r = @x1 * @x2
|
33
|
+
a = String::Mask["ab..789.", '.']
|
34
|
+
assert_equal(a, r)
|
35
|
+
|
36
|
+
r = @x2 * @x1
|
37
|
+
a = String::Mask["abc..123", '.']
|
38
|
+
assert_equal(a, r)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_and
|
42
|
+
r = @x1 & @x2
|
43
|
+
a = String::Mask["ab......", '.']
|
44
|
+
assert_equal(a, r)
|
45
|
+
|
46
|
+
r = @x2 & @x1
|
47
|
+
a = String::Mask["ab......", '.']
|
48
|
+
assert_equal(a, r)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_xor
|
52
|
+
r = @x1 ^ @x2
|
53
|
+
a = String::Mask["..c.7..3", '.']
|
54
|
+
assert_equal(a, r)
|
55
|
+
|
56
|
+
r = @x2 ^ @x1
|
57
|
+
a = String::Mask["..c.7..3", '.']
|
58
|
+
assert_equal(a, r)
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: strmask
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Thomas Sawyer
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-02-23 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: |-
|
22
|
+
String::Mask provides a kind-of string algebra
|
23
|
+
useful for manipulating strings in in comparitive
|
24
|
+
ways, eg. add, subtract, xor, etc.
|
25
|
+
email:
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- lib/strmask.rb
|
34
|
+
- meta/authors
|
35
|
+
- meta/collection
|
36
|
+
- meta/contact
|
37
|
+
- meta/created
|
38
|
+
- meta/description
|
39
|
+
- meta/homepage
|
40
|
+
- meta/license
|
41
|
+
- meta/name
|
42
|
+
- meta/released
|
43
|
+
- meta/repository
|
44
|
+
- meta/title
|
45
|
+
- meta/version
|
46
|
+
- test/test_strmask.rb
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- HISTORY
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://rubyworks.github.com/strmask
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --title
|
57
|
+
- String::Mask API
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project: strmask
|
77
|
+
rubygems_version: 1.3.6.pre.3
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: String::Mask provides a kind-of string algebra
|
81
|
+
test_files:
|
82
|
+
- test/test_strmask.rb
|