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
6459a984
Commit
6459a984
authored
Jun 07, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move String#clear to mruby-string-ext; ref #2370
parent
76012a88
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
53 additions
and
53 deletions
+53
-53
mrbgems/mruby-string-ext/src/string.c
mrbgems/mruby-string-ext/src/string.c
+32
-0
mrbgems/mruby-string-ext/test/string.rb
mrbgems/mruby-string-ext/test/string.rb
+21
-0
src/string.c
src/string.c
+0
-32
test/t/string.rb
test/t/string.rb
+0
-21
No files found.
mrbgems/mruby-string-ext/src/string.c
View file @
6459a984
...
@@ -239,6 +239,37 @@ mrb_str_lines(mrb_state *mrb, mrb_value self)
...
@@ -239,6 +239,37 @@ mrb_str_lines(mrb_state *mrb, mrb_value self)
return
result
;
return
result
;
}
}
/*
* call-seq:
* string.clear -> string
*
* Makes string empty.
*
* a = "abcde"
* a.clear #=> ""
*/
static
mrb_value
mrb_str_clear
(
mrb_state
*
mrb
,
mrb_value
str
)
{
struct
RString
*
s
=
mrb_str_ptr
(
str
);
if
(
!
RSTR_SHARED_P
(
s
)
&&
!
RSTR_EMBED_P
(
s
))
{
if
(
s
->
flags
&
MRB_STR_NOFREE
)
{
s
->
flags
&=
~
MRB_STR_NOFREE
;
}
else
{
mrb_free
(
mrb
,
s
->
as
.
heap
.
ptr
);
}
s
->
as
.
heap
.
ptr
=
0
;
s
->
as
.
heap
.
len
=
0
;
}
RSTR_UNSET_SHARED_FLAG
(
s
);
RSTR_SET_EMBED_FLAG
(
s
);
RSTR_SET_EMBED_LEN
(
s
,
0
);
RSTRING_PTR
(
str
)[
0
]
=
'\0'
;
return
str
;
}
void
void
mrb_mruby_string_ext_gem_init
(
mrb_state
*
mrb
)
mrb_mruby_string_ext_gem_init
(
mrb_state
*
mrb
)
{
{
...
@@ -256,6 +287,7 @@ mrb_mruby_string_ext_gem_init(mrb_state* mrb)
...
@@ -256,6 +287,7 @@ mrb_mruby_string_ext_gem_init(mrb_state* mrb)
mrb_define_method
(
mrb
,
s
,
"oct"
,
mrb_str_oct
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"oct"
,
mrb_str_oct
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"chr"
,
mrb_str_chr
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"chr"
,
mrb_str_chr
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"lines"
,
mrb_str_lines
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"lines"
,
mrb_str_lines
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"clear"
,
mrb_str_clear
,
MRB_ARGS_NONE
());
}
}
void
void
...
...
mrbgems/mruby-string-ext/test/string.rb
View file @
6459a984
...
@@ -172,3 +172,24 @@ assert('String#lines') do
...
@@ -172,3 +172,24 @@ assert('String#lines') do
assert_equal
[
"
\n
"
,
"
\n
"
,
"
\n
"
],
"
\n\n\n
"
.
lines
assert_equal
[
"
\n
"
,
"
\n
"
,
"
\n
"
],
"
\n\n\n
"
.
lines
assert_equal
[],
""
.
lines
assert_equal
[],
""
.
lines
end
end
assert
(
'String#clear'
)
do
# embed string
s
=
"foo"
assert_equal
(
""
,
s
.
clear
)
assert_equal
(
""
,
s
)
# not embed string and not shared string
s
=
"foo"
*
100
a
=
s
assert_equal
(
""
,
s
.
clear
)
assert_equal
(
""
,
s
)
assert_equal
(
""
,
a
)
# shared string
s
=
"foo"
*
100
a
=
s
[
10
,
90
]
# create shared string
assert_equal
(
""
,
s
.
clear
)
# clear
assert_equal
(
""
,
s
)
# s is cleared
assert_not_equal
(
""
,
a
)
# a should not be affected
end
src/string.c
View file @
6459a984
...
@@ -2497,37 +2497,6 @@ mrb_str_bytes(mrb_state *mrb, mrb_value str)
...
@@ -2497,37 +2497,6 @@ mrb_str_bytes(mrb_state *mrb, mrb_value str)
return
a
;
return
a
;
}
}
/*
* call-seq:
* string.clear -> string
*
* Makes string empty.
*
* a = "abcde"
* a.clear #=> ""
*/
static
mrb_value
mrb_str_clear
(
mrb_state
*
mrb
,
mrb_value
str
)
{
struct
RString
*
s
=
mrb_str_ptr
(
str
);
if
(
!
RSTR_SHARED_P
(
s
)
&&
!
RSTR_EMBED_P
(
s
))
{
if
(
s
->
flags
&
MRB_STR_NOFREE
)
{
s
->
flags
&=
~
MRB_STR_NOFREE
;
}
else
{
mrb_free
(
mrb
,
s
->
as
.
heap
.
ptr
);
}
s
->
as
.
heap
.
ptr
=
0
;
s
->
as
.
heap
.
len
=
0
;
}
RSTR_UNSET_SHARED_FLAG
(
s
);
RSTR_SET_EMBED_FLAG
(
s
);
RSTR_SET_EMBED_LEN
(
s
,
0
);
RSTRING_PTR
(
str
)[
0
]
=
'\0'
;
return
str
;
}
/* ---------------------------*/
/* ---------------------------*/
void
void
mrb_init_string
(
mrb_state
*
mrb
)
mrb_init_string
(
mrb_state
*
mrb
)
...
@@ -2579,5 +2548,4 @@ mrb_init_string(mrb_state *mrb)
...
@@ -2579,5 +2548,4 @@ mrb_init_string(mrb_state *mrb)
mrb_define_method
(
mrb
,
s
,
"upcase!"
,
mrb_str_upcase_bang
,
MRB_ARGS_REQ
(
1
));
/* 15.2.10.5.43 */
mrb_define_method
(
mrb
,
s
,
"upcase!"
,
mrb_str_upcase_bang
,
MRB_ARGS_REQ
(
1
));
/* 15.2.10.5.43 */
mrb_define_method
(
mrb
,
s
,
"inspect"
,
mrb_str_inspect
,
MRB_ARGS_NONE
());
/* 15.2.10.5.46(x) */
mrb_define_method
(
mrb
,
s
,
"inspect"
,
mrb_str_inspect
,
MRB_ARGS_NONE
());
/* 15.2.10.5.46(x) */
mrb_define_method
(
mrb
,
s
,
"bytes"
,
mrb_str_bytes
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"bytes"
,
mrb_str_bytes
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"clear"
,
mrb_str_clear
,
MRB_ARGS_NONE
());
}
}
test/t/string.rb
View file @
6459a984
...
@@ -515,24 +515,3 @@ assert('String#each_byte') do
...
@@ -515,24 +515,3 @@ assert('String#each_byte') do
assert_equal
bytes1
,
bytes2
assert_equal
bytes1
,
bytes2
end
end
assert
(
'String#clear'
)
do
# embed string
s
=
"foo"
assert_equal
(
""
,
s
.
clear
)
assert_equal
(
""
,
s
)
# not embed string and not shared string
s
=
"foo"
*
100
a
=
s
assert_equal
(
""
,
s
.
clear
)
assert_equal
(
""
,
s
)
assert_equal
(
""
,
a
)
# shared string
s
=
"foo"
*
100
a
=
s
[
10
,
90
]
# create shared string
assert_equal
(
""
,
s
.
clear
)
# clear
assert_equal
(
""
,
s
)
# s is cleared
assert_not_equal
(
""
,
a
)
# a should not be affected
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