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
014f9282
Unverified
Commit
014f9282
authored
Jun 26, 2019
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Jun 26, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4524 from dearblue/reverse-utf8
Change to UTF-8 string reversing with in place
parents
84779f01
11e09dc5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
35 deletions
+34
-35
src/string.c
src/string.c
+29
-35
test/t/string.rb
test/t/string.rb
+5
-0
No files found.
src/string.c
View file @
014f9282
...
@@ -1674,6 +1674,18 @@ mrb_ptr_to_str(mrb_state *mrb, void *p)
...
@@ -1674,6 +1674,18 @@ mrb_ptr_to_str(mrb_state *mrb, void *p)
return
mrb_obj_value
(
p_str
);
return
mrb_obj_value
(
p_str
);
}
}
static
inline
void
str_reverse
(
char
*
p
,
char
*
e
)
{
char
c
;
while
(
p
<
e
)
{
c
=
*
p
;
*
p
++
=
*
e
;
*
e
--
=
c
;
}
}
/* 15.2.10.5.30 */
/* 15.2.10.5.30 */
/*
/*
* call-seq:
* call-seq:
...
@@ -1684,53 +1696,35 @@ mrb_ptr_to_str(mrb_state *mrb, void *p)
...
@@ -1684,53 +1696,35 @@ mrb_ptr_to_str(mrb_state *mrb, void *p)
static
mrb_value
static
mrb_value
mrb_str_reverse_bang
(
mrb_state
*
mrb
,
mrb_value
str
)
mrb_str_reverse_bang
(
mrb_state
*
mrb
,
mrb_value
str
)
{
{
struct
RString
*
s
=
mrb_str_ptr
(
str
);
char
*
p
,
*
e
;
#ifdef MRB_UTF8_STRING
#ifdef MRB_UTF8_STRING
mrb_int
utf8_len
=
RSTRING_CHAR_LEN
(
str
);
mrb_int
utf8_len
=
RSTRING_CHAR_LEN
(
str
);
mrb_int
len
=
RSTRING_LEN
(
str
);
mrb_int
len
=
RSTR_LEN
(
s
);
if
(
utf8_len
==
len
)
goto
bytes
;
if
(
utf8_len
>
1
)
{
char
*
buf
;
char
*
p
,
*
e
,
*
r
;
mrb_str_modify
(
mrb
,
mrb_str_ptr
(
str
));
len
=
RSTRING_LEN
(
str
);
buf
=
(
char
*
)
mrb_malloc
(
mrb
,
(
size_t
)
len
);
p
=
buf
;
e
=
buf
+
len
;
memcpy
(
buf
,
RSTRING_PTR
(
str
),
len
);
r
=
RSTRING_PTR
(
str
)
+
len
;
if
(
utf8_len
<
2
)
return
str
;
if
(
utf8_len
<
len
)
{
mrb_str_modify
(
mrb
,
s
);
p
=
RSTR_PTR
(
s
);
e
=
p
+
RSTR_LEN
(
s
);
while
(
p
<
e
)
{
while
(
p
<
e
)
{
mrb_int
clen
=
utf8len
(
p
,
e
);
mrb_int
clen
=
utf8len
(
p
,
e
);
r
-=
clen
;
str_reverse
(
p
,
p
+
clen
-
1
);
memcpy
(
r
,
p
,
clen
);
p
+=
clen
;
p
+=
clen
;
}
}
mrb_free
(
mrb
,
buf
)
;
goto
bytes
;
}
}
return
str
;
bytes:
#endif
#endif
{
struct
RString
*
s
=
mrb_str_ptr
(
str
);
char
*
p
,
*
e
;
char
c
;
mrb_str_modify
(
mrb
,
s
);
if
(
RSTR_LEN
(
s
)
>
1
)
{
if
(
RSTR_LEN
(
s
)
>
1
)
{
mrb_str_modify
(
mrb
,
s
);
bytes:
p
=
RSTR_PTR
(
s
);
p
=
RSTR_PTR
(
s
);
e
=
p
+
RSTR_LEN
(
s
)
-
1
;
e
=
p
+
RSTR_LEN
(
s
)
-
1
;
while
(
p
<
e
)
{
str_reverse
(
p
,
e
);
c
=
*
p
;
*
p
++
=
*
e
;
*
e
--
=
c
;
}
}
}
return
str
;
return
str
;
}
}
}
/* ---------------------------------- */
/* ---------------------------------- */
...
...
test/t/string.rb
View file @
014f9282
...
@@ -479,6 +479,7 @@ assert('String#reverse(UTF-8)', '15.2.10.5.29') do
...
@@ -479,6 +479,7 @@ assert('String#reverse(UTF-8)', '15.2.10.5.29') do
assert_equal
'こんにちは世界!'
,
a
assert_equal
'こんにちは世界!'
,
a
assert_equal
'!界世はちにんこ'
,
'こんにちは世界!'
.
reverse
assert_equal
'!界世はちにんこ'
,
'こんにちは世界!'
.
reverse
assert_equal
'あ'
,
'あ'
.
reverse
end
if
UTF8STRING
end
if
UTF8STRING
assert
(
'String#reverse!'
,
'15.2.10.5.30'
)
do
assert
(
'String#reverse!'
,
'15.2.10.5.30'
)
do
...
@@ -495,6 +496,10 @@ assert('String#reverse!(UTF-8)', '15.2.10.5.30') do
...
@@ -495,6 +496,10 @@ assert('String#reverse!(UTF-8)', '15.2.10.5.30') do
assert_equal
'!界世はちにんこ'
,
a
assert_equal
'!界世はちにんこ'
,
a
assert_equal
'!界世はちにんこ'
,
'こんにちは世界!'
.
reverse!
assert_equal
'!界世はちにんこ'
,
'こんにちは世界!'
.
reverse!
b
=
'あ'
b
.
reverse!
assert_equal
'あ'
,
b
end
if
UTF8STRING
end
if
UTF8STRING
assert
(
'String#rindex'
,
'15.2.10.5.31'
)
do
assert
(
'String#rindex'
,
'15.2.10.5.31'
)
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