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
2ad5d4f5
Unverified
Commit
2ad5d4f5
authored
Apr 28, 2021
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
string.c: add a new method `String#center`.
parent
e5e5acef
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
4 deletions
+32
-4
mrbgems/mruby-string-ext/mrblib/string.rb
mrbgems/mruby-string-ext/mrblib/string.rb
+24
-4
mrbgems/mruby-string-ext/test/string.rb
mrbgems/mruby-string-ext/test/string.rb
+8
-0
No files found.
mrbgems/mruby-string-ext/mrblib/string.rb
View file @
2ad5d4f5
...
...
@@ -275,8 +275,8 @@ class String
def
ljust
(
idx
,
padstr
=
' '
)
raise
ArgumentError
,
'zero width padding'
if
padstr
==
''
return
self
if
idx
<=
self
.
size
pad_repetitions
=
(
idx
/
padstr
.
length
).
ceil
padding
=
(
padstr
*
pad_repetitions
)[
0
...
(
idx
-
self
.
length
)
]
pad_repetitions
=
idx
/
padstr
.
size
padding
=
(
padstr
*
pad_repetitions
)[
0
,
idx
-
self
.
size
]
self
+
padding
end
...
...
@@ -294,11 +294,31 @@ class String
def
rjust
(
idx
,
padstr
=
' '
)
raise
ArgumentError
,
'zero width padding'
if
padstr
==
''
return
self
if
idx
<=
self
.
size
pad_repetitions
=
(
idx
/
padstr
.
length
).
ceil
padding
=
(
padstr
*
pad_repetitions
)[
0
...
(
idx
-
self
.
length
)
]
pad_repetitions
=
idx
/
padstr
.
size
padding
=
(
padstr
*
pad_repetitions
)[
0
,
idx
-
self
.
size
]
padding
+
self
end
##
# call-seq:
# str.center(width, padstr=' ') -> new_str
#
# Centers +str+ in +width+. If +width+ is greater than the length of +str+,
# returns a new String of length +width+ with +str+ centered and padded with
# +padstr+; otherwise, returns +str+.
#
# "hello".center(4) #=> "hello"
# "hello".center(20) #=> " hello "
# "hello".center(20, '123') #=> "1231231hello12312312"
def
center
(
width
,
padstr
=
' '
)
raise
ArgumentError
,
'zero width padding'
if
padstr
==
''
return
self
if
width
<=
self
.
size
width
-=
self
.
size
pad1
=
width
/
2
pad2
=
width
-
pad1
(
padstr
*
pad1
)[
0
,
pad1
]
+
self
+
(
padstr
*
pad2
)[
0
,
pad2
]
end
def
chars
(
&
block
)
if
block_given?
self
.
split
(
''
).
each
do
|
i
|
...
...
mrbgems/mruby-string-ext/test/string.rb
View file @
2ad5d4f5
...
...
@@ -474,6 +474,14 @@ assert('String#rjust') do
assert_equal
"hello"
,
"hello"
.
rjust
(
-
3
)
end
assert
(
'String#center'
)
do
assert_equal
"hello"
,
"hello"
.
center
(
4
)
assert_equal
" hello "
,
"hello"
.
center
(
20
)
assert_equal
20
,
"hello"
.
center
(
20
).
length
assert_equal
"1231231hello12312312"
,
"hello"
.
center
(
20
,
'123'
)
assert_equal
"hello"
,
"hello"
.
center
(
-
3
)
end
if
UTF8STRING
assert
(
'String#ljust with UTF8'
)
do
assert_equal
"helloん "
,
"helloん"
.
ljust
(
20
)
...
...
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