sub_string 1.1 → 1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ChangeLog +5 -0
- data/README.en.rdoc +18 -12
- data/lib/sub_string.rb +25 -1
- data/sub_string.gemspec +4 -3
- data/test/test_sub_string.rb +23 -2
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a09c6fbaa026b755be6a24a94173f329d29f05695abc7de8ebc135b0cfa8c59
|
4
|
+
data.tar.gz: 10e9d237218dbe2fe26c987f87617547b85b4469b80692846416ef6f8cae1081
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53d20271499902491470468848a19129be4ff3e61be0687e1c7d4783c9eacd729b80344505f859f030e5da803e9bceb2c13954a01cdaa22704758d5d3c0be889
|
7
|
+
data.tar.gz: 99b0f5db08ba2aa094beee4bd96b6910dd80bda57079470bd1f15ee947efa3b8fca836b85a5158c7a517f63fad21fd69406470de51e9d826af7e3753ebfb6040
|
data/ChangeLog
CHANGED
data/README.en.rdoc
CHANGED
@@ -27,14 +27,16 @@ in {Github}[https://github.com/masasakano/sub_string]
|
|
27
27
|
This class takes three parameters in the initialization: *source*, *pos*
|
28
28
|
(starting positional character-index), and *size* (of the substring of the original String *source*.
|
29
29
|
|
30
|
-
SubString.new( source,
|
30
|
+
SubString.new( source, pos=0, size=nil, attr: arbitrary_object )
|
31
31
|
|
32
32
|
The constructed instance of this class keeps only these three pieces
|
33
|
-
of information
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
of information, plus user-supplied instance-specific additional
|
34
|
+
information +attr+, (plus a hash value, strictly speaking), and hence uses negligible internal memory space on its own,
|
35
|
+
unlike the Ruby default +String#[ i, j ]+ .
|
36
|
+
|
37
|
+
Note that the default of +pos+ is 0, and negative values are allowed
|
38
|
+
for it just like +String#[ -2, 1 ]+ . If the size is unspecified,
|
39
|
+
the maximum possible size for +source+ counted from +pos+ is used.
|
38
40
|
|
39
41
|
Then, whenever it is referred to, it reconstructs the original
|
40
42
|
sub-String object:
|
@@ -54,7 +56,8 @@ As an example, the child class {SubString}[http://rubygems.org/gems/sub_string]
|
|
54
56
|
works as:
|
55
57
|
|
56
58
|
src = "abcdef"
|
57
|
-
ss = SubString
|
59
|
+
ss = SubString(src, -4, 3, attr: (5..6)) # => Short form of SubString.new()
|
60
|
+
# => Similar to "abcdef"[-4,3]
|
58
61
|
print ss # => "cde" (STDOUT)
|
59
62
|
ss+'3p' # => "cde3p"
|
60
63
|
ss.upcase # => "CDE"
|
@@ -62,6 +65,7 @@ works as:
|
|
62
65
|
ss.is_a?(String) # => true
|
63
66
|
"xy_"+ss # => "xy_cde"
|
64
67
|
"cde" == ss # => true
|
68
|
+
ss.attr # => (5..6) # user-specified instance-specific additional information
|
65
69
|
|
66
70
|
Internally the instance holds the source object. Therefore, as long
|
67
71
|
as the instance is alive, the source object is never garbage-collected (GC).
|
@@ -172,20 +176,22 @@ Or, alternatively, download them and put the library files in one of your Ruby l
|
|
172
176
|
|
173
177
|
== Developer's note
|
174
178
|
|
175
|
-
The master of this README file is found in
|
176
|
-
{RubyGems/
|
179
|
+
The master of this README file as well as the entire package is found in
|
180
|
+
{RubyGems/sub_string}[https://rubygems.org/gems/sub_string]
|
177
181
|
|
178
|
-
|
182
|
+
The source code is maintained also in
|
183
|
+
{Github}[https://github.com/masasakano/sub_string] with no intuitive
|
184
|
+
interface for annotation but with easily-browsable {ChangeLog}[https://github.com/masasakano/sub_string/blob/master/ChangeLog]
|
179
185
|
|
180
186
|
=== Tests
|
181
187
|
|
182
|
-
Ruby codes under the directory <tt>test/</tt> are the test scripts.
|
188
|
+
The Ruby codes under the directory <tt>test/</tt> are the test scripts.
|
183
189
|
You can run them from the top directory as <tt>ruby test/test_****.rb</tt>
|
184
190
|
or simply run <tt>make test</tt>.
|
185
191
|
|
186
192
|
== Known bugs and Todo items
|
187
193
|
|
188
|
-
* This class ignores any optional (keyword) parameters for the methods. It is due to the fact Ruby {BasicObject#method_missing}[https://ruby-doc.org/core-2.6.5/BasicObject.html#method-i-method_missing] does not take them into account as of Ruby-2.6.5. It may change in future versions of Ruby. As far as the Ruby built-in methods of String are concerned, it does not matter because none of them uses one. However, if String
|
194
|
+
* This class ignores any optional (keyword) parameters for the methods of the original String class. It is due to the fact Ruby {BasicObject#method_missing}[https://ruby-doc.org/core-2.6.5/BasicObject.html#method-i-method_missing] does not take them into account as of Ruby-2.6.5. It may change in future versions of Ruby. As far as the Ruby built-in methods of String are concerned, it does not matter because none of them uses one. However, if String has methods of this kind defined by a user or in external library, it may encounter a trouble.
|
189
195
|
|
190
196
|
|
191
197
|
== Copyright
|
data/lib/sub_string.rb
CHANGED
@@ -15,8 +15,32 @@ require 'sub_object' # Gem: {SubObject}[http://rubygems.org/gems/sub_object]
|
|
15
15
|
#
|
16
16
|
class SubString < SubObject
|
17
17
|
# Symbol of the method that projects to (returns) the original-like instance;
|
18
|
-
# e.g., :to_str for String. The value should be overwritten in the child class of SubObject.
|
18
|
+
# e.g., :to_str for String. The value should be overwritten in the child class of {SubObject}, namely this class {SubString} in this case.
|
19
19
|
TO_SOURCE_METHOD = :to_str
|
20
20
|
alias_method TO_SOURCE_METHOD, :to_source
|
21
|
+
|
22
|
+
# Returns a new instance of SubString equivalent to source[ pos, size ]
|
23
|
+
#
|
24
|
+
# Difference from the original SubObject is the defaults are set for
|
25
|
+
# the second and third parameters.
|
26
|
+
#
|
27
|
+
# If the third parameter is nil, it is set as the maximum size possible
|
28
|
+
# counted from pos (the starting position)
|
29
|
+
#
|
30
|
+
# @param source [String]
|
31
|
+
# @param pos [Integer] Starting character index position.
|
32
|
+
# @param size [Integer, nil] Size of the substring to make.
|
33
|
+
# @param attr: [Object] user-specified arbitrary object
|
34
|
+
def initialize(source, pos=0, size=nil, attr: nil)
|
35
|
+
size ||= ((pos >= 0) ? source.size - pos : -pos)
|
36
|
+
super source, pos, size, attr: attr
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Function version of {SubString#initialize}
|
41
|
+
#
|
42
|
+
# @param (see SubString#initialize)
|
43
|
+
def SubString(*rest, **kwd)
|
44
|
+
SubString.new(*rest, **kwd)
|
21
45
|
end
|
22
46
|
|
data/sub_string.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'date'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = 'sub_string'.sub(/.*/){|c| (c == File.basename(Dir.pwd)) ? c : raise("ERROR: s.name=(#{c}) in gemspec seems wrong!")}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2".sub(/.*/){|c| fs = Dir.glob('changelog{,.*}', File::FNM_CASEFOLD); raise('More than one ChangeLog exist!') if fs.size > 1; warn("WARNING: Version(s.version=#{c}) already exists in #{fs[0]} - ok?") if fs.size == 1 && !IO.readlines(fs[0]).grep(/^\(Version: #{Regexp.quote c}\)$/).empty? ; c } # n.b., In macOS, changelog and ChangeLog are identical in default.
|
9
9
|
# s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
10
|
# s.bindir = 'bin'
|
11
11
|
# %w(sub_string).each do |f|
|
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
|
|
13
13
|
# File.executable?(path) ? s.executables << f : raise("ERROR: Executable (#{path}) is not executable!")
|
14
14
|
# end
|
15
15
|
s.authors = ["Masa Sakano"]
|
16
|
-
s.date = %q{2019-11-
|
16
|
+
s.date = %q{2019-11-12}.sub(/.*/){|c| (Date.parse(c) == Date.today) ? c : raise("ERROR: s.date=(#{c}) is not today!")}
|
17
17
|
s.summary = %q{Duck-typed String class with negligible memory use}
|
18
18
|
s.description = <<-EOF
|
19
19
|
Class SubString that expresses Ruby sub-String but taking up negligible memory space, as its instance holds the positional information only. It behaves exactly like String (duck-typing), except destructive modification is prohibited. If the original string is destructively altered, warning is issued.
|
@@ -38,7 +38,8 @@ Gem::Specification.new do |s|
|
|
38
38
|
s.files.reject! { |fn| File.symlink? fn }
|
39
39
|
|
40
40
|
s.add_runtime_dependency 'sub_object', '>= 1.1'
|
41
|
-
|
41
|
+
|
42
|
+
s.add_development_dependency "plain_text", [">= 0.6"]
|
42
43
|
s.homepage = "https://www.wisebabel.com"
|
43
44
|
s.rdoc_options = ["--charset=UTF-8"] # "-e UTF-8" is now Default...
|
44
45
|
|
data/test/test_sub_string.rb
CHANGED
@@ -117,12 +117,32 @@ class TestUnitSubString < MiniTest::Test
|
|
117
117
|
assert_equal hs, obj.attr
|
118
118
|
obj.attr[:try] = 89
|
119
119
|
assert_equal 89, obj.attr[:try]
|
120
|
+
|
121
|
+
# Tests of default
|
122
|
+
str = 'abcdefghijklm'*20
|
123
|
+
obj = SubString.new str
|
124
|
+
assert_equal str.size, obj.size
|
125
|
+
assert_equal str.size, obj.subsize
|
126
|
+
assert_equal str, obj
|
127
|
+
|
128
|
+
obj = SubString.new str, attr: 6
|
129
|
+
assert_equal 6, obj.attr
|
130
|
+
|
131
|
+
obj = SubString.new str, 1
|
132
|
+
assert_equal str.size, obj.size+1
|
133
|
+
assert_equal str.size, obj.subsize+1
|
134
|
+
assert_equal str[1..-1], obj
|
135
|
+
|
136
|
+
obj = SubString.new str, -2
|
137
|
+
assert_equal 2, obj.size
|
138
|
+
assert_equal 2, obj.subsize
|
139
|
+
assert_equal str[-2..-1], obj
|
120
140
|
end
|
121
141
|
|
122
|
-
# As in README.en.rdoc
|
142
|
+
# As in README.en.rdoc (also tests of SubString() as opposed to SubString.new)
|
123
143
|
def test_sub_string03
|
124
144
|
src = "abcdef"
|
125
|
-
ss = SubString
|
145
|
+
ss = SubString(src, -4, 3, attr: (5..6)) # => Similar to "abcdef"[-4,3]
|
126
146
|
assert_operator "cde", '==', ss
|
127
147
|
assert_equal "cde", ss.to_s
|
128
148
|
assert_equal "cde3p", ss+'3p'
|
@@ -130,6 +150,7 @@ class TestUnitSubString < MiniTest::Test
|
|
130
150
|
assert_equal "Qde", ss.sub(/^./, 'Q')
|
131
151
|
assert ss.is_a?(String)
|
132
152
|
assert_equal "xy_cde", "xy_"+ss
|
153
|
+
assert_equal (5..6), ss.attr
|
133
154
|
end
|
134
155
|
end # class TestUnitSubString < MiniTest::Test
|
135
156
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sub_string
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masa Sakano
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-11-
|
11
|
+
date: 2019-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sub_object
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: plain_text
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.6'
|
27
41
|
description: " Class SubString that expresses Ruby sub-String but taking up negligible
|
28
42
|
memory space, as its instance holds the positional information only. It behaves
|
29
43
|
exactly like String (duck-typing), except destructive modification is prohibited.
|