pike.git/
src/
builtin.cmod
Branch:
Tag:
Non-build tags
All tags
No tags
2014-08-27
2014-08-27 17:54:10 by Stephen R. van den Berg <srb@cuci.nl>
5f922f3c373815c10e25a6e45e47ead0094def95 (
74
lines) (+
42
/-
32
)
[
Show
|
Annotate
]
Branch:
8.0
Use
[..]
lfun instead.
3071:
push_undefined(); }
-
/*! @decl int
|Buffer
`[](int index
, int|void end_or_none
)
+
/*! @decl int `[](int index)
*/
-
PIKEFUN int
|Buffer
`[](int index
, int|void end_or_none
)
+
PIKEFUN int `[](int index)
{
-
struct
Buffer_struct *str = THIS;
-
struct
pike_string *s =
str
->str.s;
-
unsigned len = s->len
, shift = s->size_shift
;
-
void *p = s->str;
+
struct pike_string *s =
THIS
->str.s;
+
unsigned len = s->len;
-
if (args==1) {
-
pop_n_elems(args);
+
if (index<0) index += len; if (index<0 || index>=len) index_error("Buffer->`[]", Pike_sp, args, NULL, Pike_sp, "Index %"PRINTPIKEINT"d is out of array range 0..%d,\n", index, len-1);
-
push_int (
generic_extract(
p
, shift, index)
)
;
-
}
else
{
-
INT
_
TYPE
end
=
end_
or
_
none
->
u
.
integer
;
+
RETURN
generic_extract(
s->str
,
s->size_
shift, index);
+
}
+
+
/*!
@decl
Buffer `[..](int start, int start_type, int end, int end_type)
+
*/
+
PIKEFUN
Buffer `[..](int start, int start
_
type,
int
end
,
int
end_
type)
+
{
+
struct Buffer
_
struct *str = THIS;
+
struct pike_string *s = str
->
str
.
s
;
+
unsigned len = s->len, shift = s->size_shift;
+
void *p;
+
struct object *res; struct Buffer_struct *str2; pop_n_elems(args);
-
+
if (start_type == INDEX_FROM_END)
+
start = len-1-start;
+
if (end_type != INDEX_FROM_BEG)
+
end = len-1-end;
res = fast_clone_object( Buffer_program ); str2 = OBJ2_BUFFER( res );
-
if (
index
< 0)
-
index
= 0;
+
if (
start
< 0)
+
start
= 0;
if (end >= len) end = len-1;
-
str2->initial = end -=
index
-1;
+
str2->initial = end -=
start
-1;
init_string_builder_alloc (&str2->str, end, shift);
-
memcpy ((s = str2->str.s)->str, (char*)p+(
index
<<shift), end<<shift);
+
p
=
s->str;
+
memcpy ((s = str2->str.s)->str, (char*)p+(
start
<<shift), end<<shift);
s->str[end<<shift] = 0; // Ensure NUL-termination s->len = end; if( (Pike_fp->current_object->flags & OBJECT_CLEAR_ON_EXIT) ) res->flags |= OBJECT_CLEAR_ON_EXIT; push_object (res); }
-
}
+
/*! @decl int `[]=(int index, int ch) */