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
97283faa
Commit
97283faa
authored
Jul 15, 2016
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Jul 15, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3176 from ksss/string-insert
String#insert should be destructive
parents
72b28f1e
d83d9cd1
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
8 deletions
+11
-8
mrbgems/mruby-string-ext/mrblib/string.rb
mrbgems/mruby-string-ext/mrblib/string.rb
+7
-8
mrbgems/mruby-string-ext/test/string.rb
mrbgems/mruby-string-ext/test/string.rb
+4
-0
No files found.
mrbgems/mruby-string-ext/mrblib/string.rb
View file @
97283faa
...
@@ -254,14 +254,13 @@ class String
...
@@ -254,14 +254,13 @@ class String
# "abcd".insert(-1, 'X') #=> "abcdX"
# "abcd".insert(-1, 'X') #=> "abcdX"
#
#
def
insert
(
idx
,
str
)
def
insert
(
idx
,
str
)
pos
=
idx
.
to_i
if
idx
==
-
1
pos
+=
self
.
size
+
1
if
pos
<
0
return
self
<<
str
elsif
idx
<
0
raise
IndexError
,
"index
#{
idx
.
to_i
}
out of string"
if
pos
<
0
||
pos
>
self
.
size
idx
+=
1
end
return
self
+
str
if
pos
==
-
1
self
[
idx
,
0
]
=
str
return
str
+
self
if
pos
==
0
self
return
self
[
0
..
pos
-
1
]
+
str
+
self
[
pos
..-
1
]
end
end
##
##
...
...
mrbgems/mruby-string-ext/test/string.rb
View file @
97283faa
...
@@ -406,6 +406,10 @@ assert('String#insert') do
...
@@ -406,6 +406,10 @@ assert('String#insert') do
assert_equal
"abcdX"
,
"abcd"
.
insert
(
-
1
,
'X'
)
assert_equal
"abcdX"
,
"abcd"
.
insert
(
-
1
,
'X'
)
assert_raise
(
IndexError
)
{
"abcd"
.
insert
(
5
,
'X'
)
}
assert_raise
(
IndexError
)
{
"abcd"
.
insert
(
5
,
'X'
)
}
assert_raise
(
IndexError
)
{
"abcd"
.
insert
(
-
6
,
'X'
)
}
assert_raise
(
IndexError
)
{
"abcd"
.
insert
(
-
6
,
'X'
)
}
a
=
"abcd"
a
.
insert
(
0
,
'X'
)
assert_equal
"Xabcd"
,
a
end
end
assert
(
'String#prepend'
)
do
assert
(
'String#prepend'
)
do
...
...
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