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
8b92ab5c
Commit
8b92ab5c
authored
Apr 19, 2018
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
CRuby2.6 stops deprecating `String#lines` with a block.
`String#lines` (with a block) is now implemented in Ruby.
parent
c79e81eb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
35 deletions
+47
-35
mrbgems/mruby-string-ext/mrblib/string.rb
mrbgems/mruby-string-ext/mrblib/string.rb
+21
-0
mrbgems/mruby-string-ext/src/string.c
mrbgems/mruby-string-ext/src/string.c
+26
-35
No files found.
mrbgems/mruby-string-ext/mrblib/string.rb
View file @
8b92ab5c
...
...
@@ -365,4 +365,25 @@ class String
self
[
0
,
0
]
=
arg
self
end
##
# call-seq:
# string.lines -> array of string
# string.lines {|s| block} -> array of string
#
# Returns strings per line;
#
# a = "abc\ndef"
# a.lines #=> ["abc\n", "def"]
#
# If a block is given, it works the same as <code>each_line</code>.
def
lines
(
&
blk
)
lines
=
self
.
__lines
if
blk
lines
.
each
do
|
line
|
blk
.
call
(
line
)
end
end
lines
end
end
mrbgems/mruby-string-ext/src/string.c
View file @
8b92ab5c
...
...
@@ -307,40 +307,6 @@ mrb_fixnum_chr(mrb_state *mrb, mrb_value num)
#endif
}
/*
* call-seq:
* string.lines -> array of string
*
* Returns strings per line;
*
* a = "abc\ndef"
* a.lines #=> ["abc\n", "def"]
*/
static
mrb_value
mrb_str_lines
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_value
result
;
int
ai
;
mrb_int
len
;
char
*
b
=
RSTRING_PTR
(
self
);
char
*
p
=
b
,
*
t
;
char
*
e
=
b
+
RSTRING_LEN
(
self
);
mrb_get_args
(
mrb
,
""
);
result
=
mrb_ary_new
(
mrb
);
ai
=
mrb_gc_arena_save
(
mrb
);
while
(
p
<
e
)
{
t
=
p
;
while
(
p
<
e
&&
*
p
!=
'\n'
)
p
++
;
if
(
*
p
==
'\n'
)
p
++
;
len
=
(
mrb_int
)
(
p
-
t
);
mrb_ary_push
(
mrb
,
result
,
mrb_str_new
(
mrb
,
t
,
len
));
mrb_gc_arena_restore
(
mrb
,
ai
);
}
return
result
;
}
/*
* call-seq:
* string.succ -> string
...
...
@@ -745,6 +711,31 @@ mrb_str_del_suffix(mrb_state *mrb, mrb_value self)
return
mrb_str_substr
(
mrb
,
self
,
0
,
slen
-
plen
);
}
static
mrb_value
mrb_str_lines
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_value
result
;
int
ai
;
mrb_int
len
;
char
*
b
=
RSTRING_PTR
(
self
);
char
*
p
=
b
,
*
t
;
char
*
e
=
b
+
RSTRING_LEN
(
self
);
mrb_get_args
(
mrb
,
""
);
result
=
mrb_ary_new
(
mrb
);
ai
=
mrb_gc_arena_save
(
mrb
);
while
(
p
<
e
)
{
t
=
p
;
while
(
p
<
e
&&
*
p
!=
'\n'
)
p
++
;
if
(
*
p
==
'\n'
)
p
++
;
len
=
(
mrb_int
)
(
p
-
t
);
mrb_ary_push
(
mrb
,
result
,
mrb_str_new
(
mrb
,
t
,
len
));
mrb_gc_arena_restore
(
mrb
,
ai
);
}
return
result
;
}
void
mrb_mruby_string_ext_gem_init
(
mrb_state
*
mrb
)
{
...
...
@@ -763,7 +754,6 @@ mrb_mruby_string_ext_gem_init(mrb_state* mrb)
mrb_define_method
(
mrb
,
s
,
"hex"
,
mrb_str_hex
,
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
,
"lines"
,
mrb_str_lines
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"succ"
,
mrb_str_succ
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"succ!"
,
mrb_str_succ_bang
,
MRB_ARGS_NONE
());
mrb_alias_method
(
mrb
,
s
,
mrb_intern_lit
(
mrb
,
"next"
),
mrb_intern_lit
(
mrb
,
"succ"
));
...
...
@@ -775,6 +765,7 @@ mrb_mruby_string_ext_gem_init(mrb_state* mrb)
mrb_define_method
(
mrb
,
s
,
"delete_suffix!"
,
mrb_str_del_suffix_bang
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
s
,
"delete_suffix"
,
mrb_str_del_suffix
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
s
,
"__lines"
,
mrb_str_lines
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
mrb
->
fixnum_class
,
"chr"
,
mrb_fixnum_chr
,
MRB_ARGS_NONE
());
}
...
...
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