commit e1508acb17079f241b5fe54eeaf0f55c99cc8c3d
parent f90a7baa42f04fd1f5539efe7d33c66e34110f55
Author: Andres Navarro <canavarro82@gmail.com>
Date: Wed, 23 Mar 2011 02:06:34 -0300
Added substring to the ground environment.
Diffstat:
3 files changed, 37 insertions(+), 3 deletions(-)
diff --git a/src/kground.c b/src/kground.c
@@ -611,7 +611,7 @@ void kinit_ground_env(klisp_State *K)
/* TODO */
/* 13.2.5? substring */
- /* TODO */
+ add_applicative(K, ground_env, "substring", substring, 0);
/* 13.2.6? string-append */
/* TODO */
diff --git a/src/kgstrings.c b/src/kgstrings.c
@@ -113,7 +113,41 @@ void string_setS(klisp_State *K, TValue *xparams, TValue ptree, TValue denv)
/* TODO */
/* 13.2.5? substring */
-/* TODO */
+void substring(klisp_State *K, TValue *xparams, TValue ptree, TValue denv)
+{
+ UNUSED(xparams);
+ UNUSED(denv);
+ bind_3tp(K, "substring", ptree, "string", ttisstring, str,
+ "finite integer", ttisfixint, tv_start,
+ "finite integer", ttisfixint, tv_end);
+
+ int32_t start = ivalue(tv_start);
+ int32_t end = ivalue(tv_end);
+
+ if (start < 0 || start >= kstring_size(str)) {
+ /* TODO show index */
+ klispE_throw(K, "substring: start index out of bounds");
+ return;
+ } else if (end < 0 || end > kstring_size(str)) { /* end can be = size */
+ /* TODO show index */
+ klispE_throw(K, "substring: end index out of bounds");
+ return;
+ } else if (start > end) {
+ /* TODO show indexes */
+ klispE_throw(K, "substring: end index is greater than start index");
+ return;
+ }
+
+ int32_t size = end - start;
+ TValue new_str;
+ /* the if isn't strictly necessary but it's clearer this way */
+ if (size == 0) {
+ new_str = K->empty_string;
+ } else {
+ new_str = kstring_new(K, kstring_buf(str)+start, size);
+ }
+ kapply_cc(K, new_str);
+}
/* 13.2.6? string-append */
/* TODO */
diff --git a/src/kgstrings.h b/src/kgstrings.h
@@ -47,7 +47,7 @@ void string_setS (klisp_State *K, TValue *xparams, TValue ptree, TValue denv);
/* TODO */
/* 13.2.5? substring */
-/* TODO */
+void substring(klisp_State *K, TValue *xparams, TValue ptree, TValue denv);
/* 13.2.6? string-append */
/* TODO */