Merge upstream.

This commit is contained in:
Doug Currie 2015-12-29 11:26:55 -05:00
commit 8bcfc139f7
1 changed files with 34 additions and 47 deletions

View File

@ -486,28 +486,28 @@ pic_str_string_ref(pic_state *pic)
} }
#define DEFINE_STRING_CMP(name, op) \ #define DEFINE_STRING_CMP(name, op) \
static pic_value \ static pic_value \
pic_str_string_##name(pic_state *pic) \ pic_str_string_##name(pic_state *pic) \
{ \ { \
int argc, i; \ int argc, i; \
pic_value *argv; \ pic_value *argv; \
\ \
pic_get_args(pic, "*", &argc, &argv); \ pic_get_args(pic, "*", &argc, &argv); \
\ \
if (argc < 1 || ! pic_str_p(argv[0])) { \ if (argc < 1 || ! pic_str_p(argv[0])) { \
return pic_false_value(); \ return pic_false_value(); \
} \ } \
\ \
for (i = 1; i < argc; ++i) { \ for (i = 1; i < argc; ++i) { \
if (! pic_str_p(argv[i])) { \ if (! pic_str_p(argv[i])) { \
return pic_false_value(); \ return pic_false_value(); \
} \ } \
if (! (pic_str_cmp(pic, pic_str_ptr(argv[i-1]), pic_str_ptr(argv[i])) op 0)) { \ if (! (pic_str_cmp(pic, pic_str_ptr(argv[i-1]), pic_str_ptr(argv[i])) op 0)) { \
return pic_false_value(); \ return pic_false_value(); \
} \ } \
} \ } \
return pic_true_value(); \ return pic_true_value(); \
} }
DEFINE_STRING_CMP(eq, ==) DEFINE_STRING_CMP(eq, ==)
DEFINE_STRING_CMP(lt, <) DEFINE_STRING_CMP(lt, <)
@ -518,37 +518,24 @@ DEFINE_STRING_CMP(ge, >=)
static pic_value static pic_value
pic_str_string_copy(pic_state *pic) pic_str_string_copy(pic_state *pic)
{ {
pic_str *str; pic_str *str;
int n, start, end, len; int n, start, end, len;
n = pic_get_args(pic, "s|ii", &str, &start, &end); n = pic_get_args(pic, "s|ii", &str, &start, &end);
len = pic_str_len(str); len = pic_str_len(str);
switch (n) { switch (n) {
case 1: case 1:
start = 0; start = 0;
case 2: case 2:
end = len; end = len;
} }
#if 0 if (start < 0 || end > len || end < start)
#if 0 pic_errorf(pic, "string-copy: invalid index");
if (start < 0) start = 0; /* should an error be reported? */
if (end > len) end = len; /* should an error be reported? */
#else
if ((start < 0) || (end < 0) || (start > len) || (end > len))
pic_errorf(pic, "string-copy: invalid index");
#endif
if (end < start) /* surely this is an error!? */
pic_errorf(pic, "string-copy: start index > end index");
#else
/* simplest version to catch all cases as errors */
if ((start < 0) || (end > len) || (end < start))
pic_errorf(pic, "string-copy: invalid index");
#endif
return pic_obj_value(pic_str_sub(pic, str, start, end)); return pic_obj_value(pic_str_sub(pic, str, start, end));
} }
static pic_value static pic_value