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
631de65e
Commit
631de65e
authored
Mar 20, 2017
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Mar 20, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3527 from ksss/string-gsub
Update String#gsub and String#gsub!
parents
dcf6a413
7a74a617
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
19 deletions
+31
-19
mrblib/string.rb
mrblib/string.rb
+26
-19
test/t/string.rb
test/t/string.rb
+5
-0
No files found.
mrblib/string.rb
View file @
631de65e
...
...
@@ -58,28 +58,34 @@ class String
#
# ISO 15.2.10.5.18
def
gsub
(
*
args
,
&
block
)
if
args
.
size
==
2
pattern
,
replace
=
*
args
plen
=
pattern
.
length
return
to_enum
(
:gsub
,
*
args
)
if
args
.
length
==
1
&&
!
block
raise
ArgumentError
,
"wrong number of arguments"
unless
(
1
..
2
).
include?
(
args
.
length
)
pattern
,
replace
=
*
args
plen
=
pattern
.
length
if
args
.
length
==
2
&&
block
block
=
nil
end
if
!
replace
.
nil?
||
!
block
replace
=
replace
.
to_str
offset
=
0
result
=
[]
while
found
=
index
(
pattern
,
offset
)
result
<<
self
[
offset
,
found
-
offset
]
offset
=
found
+
plen
result
<<
replace
.
__sub_replace
(
self
[
0
,
found
],
pattern
,
self
[
offset
..-
1
]
||
""
)
if
plen
==
0
result
<<
self
[
offset
,
1
]
offset
+=
1
end
end
offset
=
0
result
=
[]
while
found
=
index
(
pattern
,
offset
)
result
<<
self
[
offset
,
found
-
offset
]
offset
=
found
+
plen
result
<<
if
block
block
.
call
(
pattern
).
to_s
else
replace
.
__sub_replace
(
self
[
0
,
found
],
pattern
,
self
[
offset
..-
1
]
||
""
)
end
if
plen
==
0
result
<<
self
[
offset
,
1
]
offset
+=
1
end
result
<<
self
[
offset
..-
1
]
if
offset
<
length
result
.
join
elsif
args
.
size
==
1
&&
block
split
(
args
[
0
],
-
1
).
join
(
block
.
call
(
args
[
0
]))
else
raise
ArgumentError
,
"wrong number of arguments"
end
result
<<
self
[
offset
..-
1
]
if
offset
<
length
result
.
join
end
##
...
...
@@ -91,6 +97,7 @@ class String
# ISO 15.2.10.5.19
def
gsub!
(
*
args
,
&
block
)
raise
RuntimeError
,
"can't modify frozen String"
if
frozen?
return
to_enum
(
:gsub!
,
*
args
)
if
args
.
length
==
1
&&
!
block
str
=
self
.
gsub
(
*
args
,
&
block
)
return
nil
if
str
==
self
self
.
replace
(
str
)
...
...
test/t/string.rb
View file @
631de65e
...
...
@@ -373,6 +373,11 @@ assert('String#gsub', '15.2.10.5.18') do
assert_equal
(
'A'
,
'a'
.
gsub
(
'a'
){
|
w
|
w
.
capitalize
})
assert_equal
(
"<a><><>"
,
'a'
.
gsub
(
'a'
,
'<\0><\1><\2>'
))
assert_equal
(
".h.e.l.l.o."
,
"hello"
.
gsub
(
""
,
"."
))
a
=
[]
assert_equal
(
".h.e.l.l.o."
,
"hello"
.
gsub
(
""
)
{
|
i
|
a
<<
i
;
"."
})
assert_equal
([
""
,
""
,
""
,
""
,
""
,
""
],
a
)
assert_raise
(
ArgumentError
)
{
""
.
gsub
}
assert_raise
(
ArgumentError
)
{
""
.
gsub
(
""
,
""
,
""
)
}
end
assert
(
'String#gsub with backslash'
)
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