Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
070e04ea
Commit
070e04ea
authored
Sep 17, 2015
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2953 from takahashim/string_aset
support String#[pos, len]= val
parents
08f9a3ed
f1c23a0f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
105 additions
and
7 deletions
+105
-7
mrblib/string.rb
mrblib/string.rb
+42
-7
test/t/string.rb
test/t/string.rb
+63
-0
No files found.
mrblib/string.rb
View file @
070e04ea
...
...
@@ -154,13 +154,48 @@ class String
end
##
# Modify +self+ by replacing the content of +self+
# at the position +pos+ with +value+.
def
[]=
(
pos
,
value
)
pos
+=
self
.
length
if
pos
<
0
b
=
self
[
0
,
pos
]
a
=
self
[
pos
+
1
..-
1
]
self
.
replace
([
b
,
value
,
a
].
join
(
''
))
# Modify +self+ by replacing the content of +self+.
# The portion of the string affected is determined using the same criteria as +String#[]+.
def
[]=
(
*
args
)
anum
=
args
.
size
if
anum
==
2
pos
,
value
=
args
if
pos
.
kind_of?
String
posnum
=
self
.
index
(
pos
)
if
posnum
b
=
self
[
0
,
posnum
.
to_i
]
a
=
self
[(
posnum
+
pos
.
length
)
..-
1
]
self
.
replace
([
b
,
value
,
a
].
join
(
''
))
return
value
else
raise
IndexError
,
"string not matched"
end
else
pos
+=
self
.
length
if
pos
<
0
if
pos
<
0
||
pos
>
self
.
length
raise
IndexError
,
"index
#{
args
[
0
]
}
out of string"
end
b
=
self
[
0
,
pos
.
to_i
]
a
=
self
[
pos
+
1
..-
1
]
self
.
replace
([
b
,
value
,
a
].
join
(
''
))
return
value
end
elsif
anum
==
3
pos
,
len
,
value
=
args
pos
+=
self
.
length
if
pos
<
0
if
pos
<
0
||
pos
>
self
.
length
raise
IndexError
,
"index
#{
args
[
0
]
}
out of string"
end
if
len
<
0
raise
IndexError
,
"negative length
#{
len
}
"
end
b
=
self
[
0
,
pos
.
to_i
]
a
=
self
[
pos
+
len
..-
1
]
self
.
replace
([
b
,
value
,
a
].
join
(
''
))
return
value
else
raise
ArgumentError
,
"wrong number of arguments (
#{
anum
}
for 2..3)"
end
end
##
...
...
test/t/string.rb
View file @
070e04ea
...
...
@@ -122,6 +122,69 @@ assert('String#[] with Range') do
assert_equal
'bc'
,
j2
end
assert
(
'String#[]='
)
do
# length of args is 1
a
=
'abc'
a
[
0
]
=
'X'
assert_equal
'Xbc'
,
a
b
=
'abc'
b
[
-
1
]
=
'X'
assert_equal
'abX'
,
b
c
=
'abc'
assert_raise
(
IndexError
)
do
c
[
10
]
=
'X'
end
d
=
'abc'
assert_raise
(
IndexError
)
do
d
[
-
10
]
=
'X'
end
e
=
'abc'
e
[
1.1
]
=
'X'
assert_equal
'aXc'
,
e
# length of args is 2
a1
=
'abc'
assert_raise
(
IndexError
)
do
a1
[
0
,
-
1
]
=
'X'
end
b1
=
'abc'
assert_raise
(
IndexError
)
do
b1
[
10
,
0
]
=
'X'
end
c1
=
'abc'
assert_raise
(
IndexError
)
do
c1
[
-
10
,
0
]
=
'X'
end
d1
=
'abc'
d1
[
0
,
0
]
=
'X'
assert_equal
'Xabc'
,
d1
e1
=
'abc'
e1
[
1
,
3
]
=
'X'
assert_equal
'aX'
,
e1
# args is RegExp
# It will be tested in mrbgems.
# args is String
a3
=
'abc'
a3
[
'bc'
]
=
'X'
assert_equal
a3
,
'aX'
b3
=
'abc'
assert_raise
(
IndexError
)
do
b3
[
'XX'
]
=
'Y'
end
end
assert
(
'String#capitalize'
,
'15.2.10.5.7'
)
do
a
=
'abc'
a
.
capitalize
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment