~lattis/muon

1c1ab113103e1a2ebcb7edf3a47f826428f8e124 — Stone Tickle a month ago d37055a
extract string.split()
M include/lang/object.h => include/lang/object.h +1 -4
@@ 2,13 2,12 @@
#define MUON_LANG_OBJECT_H

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#include "compilers.h"
#include "iterator.h"
#include "lang/string.h"
#include "lang/types.h"

enum obj_type {
	obj_any, // used for argument type checking


@@ 47,8 46,6 @@ enum obj_type {
	ARG_TYPE_ARRAY_OF = 1 << 20,
};

typedef uint32_t obj;

enum tgt_type {
	tgt_executable,
	tgt_library

M include/lang/string.h => include/lang/string.h +3 -4
@@ 1,8 1,7 @@
#ifndef MUON_LANG_STRING_H
#define MUON_LANG_STRING_H

#include <stdbool.h>
#include <stdint.h>
#include "lang/types.h"

struct workspace;



@@ 20,8 19,6 @@ struct str {

_Static_assert(sizeof(struct str) == 16, "");

typedef uint32_t str;

bool wk_str_unescape(char *buf, uint32_t len, const struct str *ss, uint32_t *r);

bool wk_str_has_null(const struct str *ss);


@@ 45,4 42,6 @@ bool wk_str_startswith(const struct str *ss, const struct str *pre);
str wk_strcat(struct workspace *wk, str s1, str s2);

bool wk_str_to_i(const struct str *ss, int64_t *res);

obj wk_str_split(struct workspace *wk, const struct str *ss, const struct str *split);
#endif

A include/lang/types.h => include/lang/types.h +8 -0
@@ 0,0 1,8 @@
#ifndef MUON_LANG_TYPES_H
#define MUON_LANG_TYPES_H
#include <stdbool.h>
#include <stdint.h>

typedef uint32_t obj;
typedef uint32_t str;
#endif

M src/functions/string.c => src/functions/string.c +3 -24
@@ 238,32 238,11 @@ func_split(struct workspace *wk, uint32_t rcvr, uint32_t args_node, uint32_t *ob
		return false;
	}

	uint32_t i, start = 0, s_id;
	const struct str *split = ao[0].set ? get_str(wk, ao[0].val) : &WKSTR(" "),
			 *ss = get_str(wk, rcvr);

	const struct str *split = ao[0].set ? get_str(wk, ao[0].val) : &WKSTR(" ");

	make_obj(wk, obj, obj_array);

	const struct str* ss = get_str(wk, rcvr);
	for (i = 0; i < ss->len; ++i) {
		struct str slice = { .s = &ss->s[i], .len = ss->len - i };

		if (wk_str_startswith(&slice, split)) {
			make_obj(wk, &s_id, obj_string)->dat.str =
				wk_str_pushn(wk, &ss->s[start], i - start);

			obj_array_push(wk, *obj, s_id);

			start = i + split->len;
			i += split->len - 1;
		}
	}

	make_obj(wk, &s_id, obj_string)->dat.str =
		wk_str_pushn(wk, &ss->s[start], i - start);

	obj_array_push(wk, *obj, s_id);

	*obj = wk_str_split(wk, ss, split);
	return true;
}


M src/lang/string.c => src/lang/string.c +29 -0
@@ 299,3 299,32 @@ wk_str_to_i(const struct str *ss, int64_t *res)

	return true;
}

obj
wk_str_split(struct workspace *wk, const struct str *ss, const struct str *split)
{
	obj res;
	make_obj(wk, &res, obj_array);

	uint32_t i, start = 0;
	obj s;

	for (i = 0; i < ss->len; ++i) {
		struct str slice = { .s = &ss->s[i], .len = ss->len - i };

		if (wk_str_startswith(&slice, split)) {
			make_obj(wk, &s, obj_string)->dat.str = wk_str_pushn(wk, &ss->s[start], i - start);

			obj_array_push(wk, res, s);

			start = i + split->len;
			i += split->len - 1;
		}
	}

	make_obj(wk, &s, obj_string)->dat.str =
		wk_str_pushn(wk, &ss->s[start], i - start);

	obj_array_push(wk, res, s);
	return res;
}