Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
string conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nyuichi committed Sep 10, 2014
1 parent 8f93ddd commit c6b46ae
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,50 @@ pic_str_string_fill_ip(pic_state *pic)
return pic_none_value();
}

static pic_value
pic_str_list_to_string(pic_state *pic)
{
pic_str *str;
pic_value list, e;
int i = 0;

pic_get_args(pic, "o", &list);

str = pic_str_new_fill(pic, pic_length(pic, list), ' ');

pic_for_each (e, list) {
pic_assert_type(pic, e, char);

pic_str_set(pic, str, i++, pic_char(e));
}

return pic_obj_value(str);
}

static pic_value
pic_str_string_to_list(pic_state *pic)
{
pic_str *str;
pic_value list;
int n, start, end, i;

n = pic_get_args(pic, "s|ii", &str, &start, &end);

switch (n) {
case 1:
start = 0;
case 2:
end = pic_strlen(str);
}

list = pic_nil_value();

for (i = start; i < end; ++i) {
pic_push(pic, pic_char_value(pic_str_ref(pic, str, i)), list);
}
return pic_reverse(pic, list);
}

void
pic_init_str(pic_state *pic)
{
Expand All @@ -421,6 +465,8 @@ pic_init_str(pic_state *pic)
pic_defun(pic, "string-copy!", pic_str_string_copy_ip);
pic_defun(pic, "string-append", pic_str_string_append);
pic_defun(pic, "string-fill!", pic_str_string_fill_ip);
pic_defun(pic, "list->string", pic_str_list_to_string);
pic_defun(pic, "string->list", pic_str_string_to_list);

pic_defun(pic, "string=?", pic_str_string_eq);
pic_defun(pic, "string<?", pic_str_string_lt);
Expand Down

0 comments on commit c6b46ae

Please sign in to comment.