Skip to content

Commit a2ff3c4

Browse files
committed
Add String.byteslice with range arguments
1 parent 2166a15 commit a2ff3c4

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

lib/backports/3.3.0/string.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'backports/tools/require_relative_dir'
2+
3+
Backports.require_relative_dir
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
unless ("".bytesplice(0..0, "", 0..0) rescue false)
2+
require 'backports/tools/alias_method_chain'
3+
4+
# Original API
5+
# * bytesplice(index, length, str) -> string
6+
# * bytesplice(range, str) -> string
7+
8+
# Newly supported API:
9+
# * bytesplice(index, length, str, str_index, str_length) -> string
10+
# * bytesplice(range, str, str_range) -> string
11+
12+
class String
13+
def bytesplice_with_range_replacement_support(*args)
14+
case args.size
15+
when 5
16+
index, length, str, str_index, str_length = *args
17+
18+
str = str.byteslice(str_index, str_length) || (raise IndexError, "oops") # if str_index < -str.length
19+
bytesplice_without_range_replacement_support(index, length, str)
20+
when 3
21+
if args[2].is_a?(Range)
22+
str = args[1].byteslice(args[2]) || (raise RangeError, "oops")
23+
bytesplice_without_range_replacement_support(args[0], str)
24+
else
25+
bytesplice_without_range_replacement_support(*args)
26+
end
27+
else
28+
bytesplice_without_range_replacement_support(*args)
29+
end
30+
end
31+
Backports.alias_method_chain self, :bytesplice, :range_replacement_support
32+
end
33+
end

test/bytesplice_test.rb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
require './test/test_helper'
2+
require 'backports/3.3.0/string/bytesplice'
3+
4+
class BytespliceTest < Test::Unit::TestCase
5+
def test_bytesplice
6+
assert_bytesplice_raise(IndexError, S("hello"), -6, 0, "bye")
7+
assert_bytesplice_result("byehello", S("hello"), -5, 0, "bye")
8+
assert_bytesplice_result("byehello", S("hello"), 0, 0, "bye")
9+
assert_bytesplice_result("byeello", S("hello"), 0, 1, "bye")
10+
assert_bytesplice_result("bye", S("hello"), 0, 5, "bye")
11+
assert_bytesplice_result("bye", S("hello"), 0, 6, "bye")
12+
13+
assert_bytesplice_raise(IndexError, S("hello"), -5, 0, "bye", -4, 0)
14+
assert_bytesplice_result("byehello", S("hello"), 0, 0, "bye", 0, 3)
15+
assert_bytesplice_result("yehello", S("hello"), 0, 0, "bye", 1, 3)
16+
assert_bytesplice_result("yehello", S("hello"), 0, 0, "bye", 1, 2)
17+
assert_bytesplice_result("ehello", S("hello"), 0, 0, "bye", 2, 1)
18+
assert_bytesplice_result("hello", S("hello"), 0, 0, "bye", 3, 0)
19+
assert_bytesplice_result("hello", s = S("hello"), 0, 5, s, 0, 5)
20+
assert_bytesplice_result("elloo", s = S("hello"), 0, 4, s, 1, 4)
21+
assert_bytesplice_result("llolo", s = S("hello"), 0, 3, s, 2, 3)
22+
assert_bytesplice_result("lollo", s = S("hello"), 0, 2, s, 3, 2)
23+
assert_bytesplice_result("oello", s = S("hello"), 0, 1, s, 4, 1)
24+
assert_bytesplice_result("hhell", s = S("hello"), 1, 4, s, 0, 4)
25+
assert_bytesplice_result("hehel", s = S("hello"), 2, 3, s, 0, 3)
26+
assert_bytesplice_result("helhe", s = S("hello"), 3, 2, s, 0, 2)
27+
assert_bytesplice_result("hellh", s = S("hello"), 4, 1, s, 0, 1)
28+
29+
assert_bytesplice_raise(RangeError, S("hello"), -6...-6, "bye")
30+
assert_bytesplice_result("byehello", S("hello"), -5...-5, "bye")
31+
assert_bytesplice_result("byehello", S("hello"), 0...0, "bye")
32+
assert_bytesplice_result("byeello", S("hello"), 0..0, "bye")
33+
assert_bytesplice_result("byeello", S("hello"), 0...1, "bye")
34+
assert_bytesplice_result("byello", S("hello"), 0..1, "bye")
35+
assert_bytesplice_result("bye", S("hello"), 0..-1, "bye")
36+
assert_bytesplice_result("bye", S("hello"), 0...5, "bye")
37+
assert_bytesplice_result("bye", S("hello"), 0...6, "bye")
38+
assert_bytesplice_result("llolo", s = S("hello"), 0..2, s, 2..4)
39+
40+
assert_bytesplice_raise(RangeError, S("hello"), -5...-5, "bye", -6...-6)
41+
assert_bytesplice_result("byehello", S("hello"), -5...-5, "bye", 0..-1)
42+
assert_bytesplice_result("byehello", S("hello"), 0...0, "bye", 0..-1)
43+
assert_bytesplice_result("bhello", S("hello"), 0...0, "bye", 0..0)
44+
assert_bytesplice_result("byhello", S("hello"), 0...0, "bye", 0..1)
45+
assert_bytesplice_result("byehello", S("hello"), 0...0, "bye", 0..2)
46+
assert_bytesplice_result("yehello", S("hello"), 0...0, "bye", 1..2)
47+
48+
assert_bytesplice_raise(TypeError, S("hello"), 0, "bye")
49+
50+
assert_bytesplice_raise(IndexError, S("こんにちは"), -16, 0, "bye")
51+
assert_bytesplice_result("byeこんにちは", S("こんにちは"), -15, 0, "bye")
52+
assert_bytesplice_result("byeこんにちは", S("こんにちは"), 0, 0, "bye")
53+
assert_bytesplice_raise(IndexError, S("こんにちは"), 1, 0, "bye")
54+
assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 1, "bye")
55+
assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 2, "bye")
56+
assert_bytesplice_result("byeんにちは", S("こんにちは"), 0, 3, "bye")
57+
assert_bytesplice_result("こんにちはbye", S("こんにちは"), 15, 0, "bye")
58+
59+
assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 0, "さようなら", -16, 0)
60+
assert_bytesplice_result("こんにちはさようなら", S("こんにちは"), 15, 0, "さようなら", 0, 15)
61+
assert_bytesplice_result("さようなら", S("こんにちは"), 0, 15, "さようなら", 0, 15)
62+
assert_bytesplice_result("さんにちは", S("こんにちは"), 0, 3, "さようなら", 0, 3)
63+
assert_bytesplice_result("さようちは", S("こんにちは"), 0, 9, "さようなら", 0, 9)
64+
assert_bytesplice_result("ようなちは", S("こんにちは"), 0, 9, "さようなら", 3, 9)
65+
assert_bytesplice_result("ようちは", S("こんにちは"), 0, 9, "さようなら", 3, 6)
66+
assert_bytesplice_result("ようならちは", S("こんにちは"), 0, 9, "さようなら", 3, 12)
67+
assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 15, "さようなら", -16, 0)
68+
# assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 15, "さようなら", 1, 0)
69+
# assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 15, "さようなら", 2, 0)
70+
# assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 15, "さようなら", 0, 1)
71+
# assert_bytesplice_raise(IndexError, S("こんにちは"), 0, 15, "さようなら", 0, 2)
72+
assert_bytesplice_result("にちはちは", s = S("こんにちは"), 0, 9, s, 6, 9)
73+
74+
assert_bytesplice_result("", S(""), 0, 0, "")
75+
assert_bytesplice_result("xxx", S(""), 0, 0, "xxx")
76+
77+
assert_bytesplice_raise(ArgumentError, S("hello"), 0, 5, "bye", 0)
78+
assert_bytesplice_raise(ArgumentError, S("hello"), 0, 5, "bye", 0..-1)
79+
assert_bytesplice_raise(ArgumentError, S("hello"), 0..-1, "bye", 0, 3)
80+
end
81+
82+
private
83+
84+
def assert_bytesplice_result(expected, s, *args)
85+
assert_equal(expected, s.send(:bytesplice, *args))
86+
assert_equal(expected, s)
87+
end
88+
89+
def assert_bytesplice_raise(e, s, *args)
90+
assert_raise(e) { s.send(:bytesplice, *args) }
91+
end
92+
93+
def S(*args, **kw)
94+
String.new(*args, **kw)
95+
end
96+
97+
end

0 commit comments

Comments
 (0)