pike.git/
src/
builtin_functions.c
Branch:
Tag:
Non-build tags
All tags
No tags
2000-04-12
2000-04-12 15:14:35 by Henrik Grubbström (Grubba) <grubba@grubba.org>
a4f17fb7b85a082bc64589531b574ca84178302d (
63
lines) (+
62
/-
1
)
[
Show
|
Annotate
]
Branch:
7.9
Added has_prefix().
Rev: src/builtin_functions.c:1.255
5:
\*/ /**/ #include "global.h"
-
RCSID("$Id: builtin_functions.c,v 1.
254
2000/04/
08
19
:
27
:
40
grubba Exp $");
+
RCSID("$Id: builtin_functions.c,v 1.
255
2000/04/
12
15
:
14
:
35
grubba Exp $");
#include "interpret.h" #include "svalue.h" #include "pike_macros.h"
463:
} }
+
/* int has_prefix(string a, string prefix) */
+
void f_has_prefix(INT32 args)
+
{
+
struct pike_string *a, *b;
+
+
get_all_args("has_prefix", args, "%W%W", &a, &b);
+
+
/* First handle some common special cases. */
+
if ((b->len > a->len) || (b->size_shift > a->size_shift)) {
+
pop_n_elems(args);
+
push_int(0);
+
return;
+
}
+
+
/* Trivial cases. */
+
if ((a == b)||(!b->len)) {
+
pop_n_elems(args);
+
push_int(1);
+
return;
+
}
+
+
if (a->size_shift == b->size_shift) {
+
int res = !MEMCMP(a->str, b->str, b->len << b->size_shift);
+
pop_n_elems(args);
+
push_int(res);
+
return;
+
}
+
+
/* At this point a->size_shift > b->size_shift */
+
#define TWO_SHIFTS(S1, S2) ((S1)|((S2)<<2))
+
switch(TWO_SHIFTS(a->size_shift, b->size_shift)) {
+
#define CASE_SHIFT(S1, S2) \
+
case TWO_SHIFTS(S1, S2): \
+
{ \
+
PIKE_CONCAT(p_wchar,S1) *s1 = PIKE_CONCAT(STR,S1)(a); \
+
PIKE_CONCAT(p_wchar,S2) *s2 = PIKE_CONCAT(STR,S2)(b); \
+
int len = b->len; \
+
while(len-- && (s1[len] == s2[len])) \
+
; \
+
pop_n_elems(args); \
+
push_int(len == -1); \
+
return; \
+
} \
+
break
+
+
CASE_SHIFT(1,0);
+
CASE_SHIFT(2,0);
+
CASE_SHIFT(2,1);
+
default:
+
error("has_index(): Unexpected string shift combination: a:%d, b:%d!\n",
+
a->size_shift, b->size_shift);
+
break;
+
}
+
#undef CASE_SHIFT
+
#undef TWO_SHIFTS
+
}
+
void f_has_index(INT32 args) { int t = 0;
5880:
tFunc( tOr(tMapping,tArray) tMix tOr(tVoid,tMix), tZero)))), 0);
+
ADD_EFUN2("has_prefix", f_has_prefix, tFunc(tStr tStr,tInt01),
+
OPT_TRY_OPTIMIZE, 0, 0);
+
ADD_EFUN("has_index",f_has_index, tOr5(tFunc(tStr tIntPos, tInt), tFunc(tArray tIntPos, tInt),