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
ee8875b1
Commit
ee8875b1
authored
Jan 07, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1651 from h2so5/string-reverse-utf8
add String#reverse, String#reverse! for UTF-8
parents
b147970b
7fc9264b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
0 deletions
+50
-0
mrbgems/mruby-string-utf8/src/string.c
mrbgems/mruby-string-utf8/src/string.c
+34
-0
mrbgems/mruby-string-utf8/test/string.rb
mrbgems/mruby-string-utf8/test/string.rb
+16
-0
No files found.
mrbgems/mruby-string-utf8/src/string.c
View file @
ee8875b1
...
...
@@ -247,6 +247,38 @@ mrb_str_aref_m(mrb_state *mrb, mrb_value str)
return
mrb_str_aref
(
mrb
,
str
,
a1
);
}
static
mrb_value
mrb_str_reverse_bang
(
mrb_state
*
mrb
,
mrb_value
str
)
{
int
utf8_len
=
mrb_utf8_strlen
(
str
);
if
(
utf8_len
>
1
)
{
int
len
=
RSTRING_LEN
(
str
);
char
*
buf
=
(
char
*
)
mrb_malloc
(
mrb
,
len
);
unsigned
char
*
p
=
(
unsigned
char
*
)
buf
;
unsigned
char
*
e
=
(
unsigned
char
*
)
buf
+
len
;
unsigned
char
*
r
=
(
unsigned
char
*
)
RSTRING_END
(
str
);
memcpy
(
buf
,
RSTRING_PTR
(
str
),
len
);
mrb_str_modify
(
mrb
,
mrb_str_ptr
(
str
));
while
(
p
<
e
)
{
int
clen
=
utf8len
(
p
);
r
-=
clen
;
memcpy
(
r
,
p
,
clen
);
p
+=
clen
;
}
mrb_free
(
mrb
,
buf
);
}
return
str
;
}
static
mrb_value
mrb_str_reverse
(
mrb_state
*
mrb
,
mrb_value
str
)
{
return
mrb_str_reverse_bang
(
mrb
,
mrb_str_dup
(
mrb
,
str
));
}
void
mrb_mruby_string_utf8_gem_init
(
mrb_state
*
mrb
)
{
...
...
@@ -255,6 +287,8 @@ mrb_mruby_string_utf8_gem_init(mrb_state* mrb)
mrb_define_method
(
mrb
,
s
,
"size"
,
mrb_str_size
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"[]"
,
mrb_str_aref_m
,
MRB_ARGS_ANY
());
mrb_define_method
(
mrb
,
s
,
"slice"
,
mrb_str_aref_m
,
MRB_ARGS_ANY
());
mrb_define_method
(
mrb
,
s
,
"reverse"
,
mrb_str_reverse
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"reverse!"
,
mrb_str_reverse_bang
,
MRB_ARGS_NONE
());
}
void
...
...
mrbgems/mruby-string-utf8/test/string.rb
View file @
ee8875b1
...
...
@@ -9,3 +9,19 @@ assert('String#[]') do
assert_equal
"んに"
,
"こんにちわ世界"
[
1
,
2
]
assert_equal
"世"
,
"こんにちわ世界"
[
"世"
]
end
assert
(
'String#reverse'
,
'15.2.10.5.29'
)
do
a
=
'こんにちわ世界!'
a
.
reverse
assert_equal
'こんにちわ世界!'
,
a
assert_equal
'!界世わちにんこ'
,
'こんにちわ世界!'
.
reverse
end
assert
(
'String#reverse!'
,
'15.2.10.5.30'
)
do
a
=
'こんにちわ世界!'
a
.
reverse!
assert_equal
'!界世わちにんこ'
,
a
assert_equal
'!界世わちにんこ'
,
'こんにちわ世界!'
.
reverse!
end
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