add pic_strcmp

This commit is contained in:
Yuichi Nishiwaki 2014-02-23 17:45:34 +09:00
parent 0fa61b6a4a
commit 9a73ba707c
2 changed files with 32 additions and 25 deletions

View File

@ -28,6 +28,7 @@ void pic_str_set(pic_state *, pic_str *, size_t, char);
pic_str *pic_strcat(pic_state *, pic_str *, pic_str *); pic_str *pic_strcat(pic_state *, pic_str *, pic_str *);
pic_str *pic_substr(pic_state *, pic_str *, size_t, size_t); pic_str *pic_substr(pic_state *, pic_str *, size_t, size_t);
int pic_strcmp(pic_state *, pic_str *, pic_str *);
pic_value pic_format(pic_state *, const char *, ...); pic_value pic_format(pic_state *, const char *, ...);
pic_value pic_vformat(pic_state *, const char *, va_list); pic_value pic_vformat(pic_state *, const char *, va_list);

View File

@ -103,6 +103,14 @@ pic_substr(pic_state *pic, pic_str *str, size_t s, size_t e)
return pic_str_new(pic, buf, len); return pic_str_new(pic, buf, len);
} }
int
pic_strcmp(pic_state *pic, pic_str *str1, pic_str *str2)
{
UNUSED(pic);
return strcmp(str1->str, str2->str);
}
pic_value pic_value
pic_vfformat(pic_state *pic, XFILE *file, const char *fmt, va_list ap) pic_vfformat(pic_state *pic, XFILE *file, const char *fmt, va_list ap)
{ {
@ -251,31 +259,29 @@ pic_str_string_set(pic_state *pic)
return pic_none_value(); return pic_none_value();
} }
#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) \
{ \ { \
size_t argc; \ size_t argc; \
pic_value *argv; \ pic_value *argv; \
size_t i; \ size_t i; \
\ \
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 (! (strcmp(pic_str_ptr(argv[i-1])->str, \ if (! (pic_strcmp(pic, pic_str_ptr(argv[i-1]), pic_str_ptr(argv[i])) op 0)) { \
pic_str_ptr(argv[i])->str) \ return pic_false_value(); \
op 0)) { \ } \
return pic_false_value(); \ } \
} \ return pic_true_value(); \
} \
return pic_true_value(); \
} }
DEFINE_STRING_CMP(eq, ==) DEFINE_STRING_CMP(eq, ==)