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
5346bdd7
Commit
5346bdd7
authored
Jan 29, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move range aware aget to array.c from mruby-array-ext gem
parent
b69bb896
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
59 deletions
+68
-59
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+0
-32
src/array.c
src/array.c
+67
-24
src/string.c
src/string.c
+1
-3
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
5346bdd7
...
@@ -201,36 +201,4 @@ class Array
...
@@ -201,36 +201,4 @@ class Array
self
.
replace
(
result
)
self
.
replace
(
result
)
end
end
end
end
##
# call-seq:
# ary[rng] -> ary slice
#
# Remeturns a slice of +ary+ according to the Range instance +rng+.
#
# a = [ "a", "b", "c", "d", "e" ]
# a[1] => "b"
# a[1,2] => ["b", "c"]
# a[1..-2] => ["b", "c", "d"]
#
def
[]
(
idx
,
len
=
nil
)
case
idx
when
Range
if
idx
.
last
<
0
then
len
=
self
.
length
-
idx
.
first
+
idx
.
last
+
1
else
len
=
idx
.
last
-
idx
.
first
+
1
end
return
self
.
slice
(
idx
.
first
,
len
)
when
Numeric
if
len
then
return
self
.
slice
(
idx
.
to_i
,
len
.
to_i
)
else
return
self
.
slice
(
idx
.
to_i
)
end
else
self
.
slice
(
idx
)
end
end
end
end
src/array.c
View file @
5346bdd7
...
@@ -8,6 +8,7 @@
...
@@ -8,6 +8,7 @@
#include "mruby/array.h"
#include "mruby/array.h"
#include "mruby/class.h"
#include "mruby/class.h"
#include "mruby/string.h"
#include "mruby/string.h"
#include "mruby/range.h"
#include "value_array.h"
#include "value_array.h"
#define ARY_DEFAULT_LEN 4
#define ARY_DEFAULT_LEN 4
...
@@ -677,37 +678,79 @@ ary_subseq(mrb_state *mrb, struct RArray *a, mrb_int beg, mrb_int len)
...
@@ -677,37 +678,79 @@ ary_subseq(mrb_state *mrb, struct RArray *a, mrb_int beg, mrb_int len)
return
mrb_obj_value
(
b
);
return
mrb_obj_value
(
b
);
}
}
static
mrb_int
aget_index
(
mrb_state
*
mrb
,
mrb_value
index
)
{
if
(
mrb_fixnum_p
(
index
))
{
return
mrb_fixnum
(
index
);
}
else
{
mrb_int
i
;
mrb_get_args
(
mrb
,
"i"
,
&
i
);
return
i
;
}
}
/*
* call-seq:
* ary[index] -> obj or nil
* ary[start, length] -> new_ary or nil
* ary[range] -> new_ary or nil
* ary.slice(index) -> obj or nil
* ary.slice(start, length) -> new_ary or nil
* ary.slice(range) -> new_ary or nil
*
* Element Reference --- Returns the element at +index+, or returns a
* subarray starting at the +start+ index and continuing for +length+
* elements, or returns a subarray specified by +range+ of indices.
*
* Negative indices count backward from the end of the array (-1 is the last
* element). For +start+ and +range+ cases the starting index is just before
* an element. Additionally, an empty array is returned when the starting
* index for an element range is at the end of the array.
*
* Returns +nil+ if the index (or starting index) are out of range.
*
* a = [ "a", "b", "c", "d", "e" ]
* a[1] => "b"
* a[1,2] => ["b", "c"]
* a[1..-2] => ["b", "c", "d"]
*
*/
mrb_value
mrb_value
mrb_ary_aget
(
mrb_state
*
mrb
,
mrb_value
self
)
mrb_ary_aget
(
mrb_state
*
mrb
,
mrb_value
self
)
{
{
struct
RArray
*
a
=
mrb_ary_ptr
(
self
);
struct
RArray
*
a
=
mrb_ary_ptr
(
self
);
mrb_int
index
,
len
;
mrb_int
i
,
len
;
mrb_value
*
argv
;
mrb_value
index
;
int
size
;
if
(
mrb_get_args
(
mrb
,
"o|i"
,
&
index
,
&
len
)
==
1
)
{
mrb_get_args
(
mrb
,
"i*"
,
&
index
,
&
argv
,
&
size
);
switch
(
mrb_type
(
index
))
{
switch
(
size
)
{
case
MRB_TT_RANGE
:
case
0
:
len
=
a
->
len
;
return
mrb_ary_ref
(
mrb
,
self
,
index
);
if
(
mrb_range_beg_len
(
mrb
,
index
,
&
i
,
&
len
,
len
))
{
return
ary_subseq
(
mrb
,
a
,
i
,
len
);
case
1
:
}
if
(
!
mrb_fixnum_p
(
argv
[
0
]))
{
else
{
mrb_raise
(
mrb
,
E_TYPE_ERROR
,
"expected Fixnum"
);
return
mrb_nil_value
();
}
case
MRB_TT_FIXNUM
:
return
mrb_ary_ref
(
mrb
,
self
,
mrb_fixnum
(
index
));
default:
return
mrb_ary_ref
(
mrb
,
self
,
aget_index
(
mrb
,
index
));
}
}
if
(
index
<
0
)
index
+=
a
->
len
;
if
(
index
<
0
||
a
->
len
<
(
int
)
index
)
return
mrb_nil_value
();
len
=
mrb_fixnum
(
argv
[
0
]);
if
(
len
<
0
)
return
mrb_nil_value
();
if
(
a
->
len
==
(
int
)
index
)
return
mrb_ary_new
(
mrb
);
if
(
len
>
a
->
len
-
index
)
len
=
a
->
len
-
index
;
return
ary_subseq
(
mrb
,
a
,
index
,
len
);
default:
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"wrong number of arguments"
);
break
;
}
}
return
mrb_nil_value
();
/* dummy to avoid warning : not reach here */
i
=
aget_index
(
mrb
,
index
);
if
(
i
<
0
)
i
+=
a
->
len
;
if
(
i
<
0
||
a
->
len
<
(
int
)
i
)
return
mrb_nil_value
();
if
(
len
<
0
)
return
mrb_nil_value
();
if
(
a
->
len
==
(
int
)
i
)
return
mrb_ary_new
(
mrb
);
if
(
len
>
a
->
len
-
i
)
len
=
a
->
len
-
i
;
return
ary_subseq
(
mrb
,
a
,
i
,
len
);
}
}
mrb_value
mrb_value
...
...
src/string.c
View file @
5346bdd7
...
@@ -741,12 +741,10 @@ num_index:
...
@@ -741,12 +741,10 @@ num_index:
/* check if indx is Range */
/* check if indx is Range */
{
{
mrb_int
beg
,
len
;
mrb_int
beg
,
len
;
mrb_value
tmp
;
len
=
RSTRING_LEN
(
str
);
len
=
RSTRING_LEN
(
str
);
if
(
mrb_range_beg_len
(
mrb
,
indx
,
&
beg
,
&
len
,
len
))
{
if
(
mrb_range_beg_len
(
mrb
,
indx
,
&
beg
,
&
len
,
len
))
{
tmp
=
mrb_str_subseq
(
mrb
,
str
,
beg
,
len
);
return
mrb_str_subseq
(
mrb
,
str
,
beg
,
len
);
return
tmp
;
}
}
else
{
else
{
return
mrb_nil_value
();
return
mrb_nil_value
();
...
...
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