summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/sail.c18
-rw-r--r--lib/sail.h1
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/sail.c b/lib/sail.c
index 9b7a1696..94cd5c2c 100644
--- a/lib/sail.c
+++ b/lib/sail.c
@@ -151,7 +151,25 @@ void string_drop(sail_string *dst, sail_string s, sail_int ns)
*dst = realloc(*dst, (len - n) + 1);
memcpy(*dst, s + n, len - n);
(*dst)[len - n] = '\0';
+ } else {
+ *dst = realloc(*dst, 1);
+ **dst = '\0';
+ }
+}
+
+void string_take(sail_string *dst, sail_string s, sail_int ns)
+{
+ size_t len = strlen(s);
+ mach_int n = CREATE_OF(mach_int, sail_int)(ns);
+ mach_int to_copy;
+ if (len <= n) {
+ to_copy = len;
+ } else {
+ to_copy = n;
}
+ *dst = realloc(*dst, to_copy + 1);
+ memcpy(*dst, s, to_copy);
+ *dst[to_copy] = '\0';
}
/* ***** Sail integers ***** */
diff --git a/lib/sail.h b/lib/sail.h
index 598ac67d..4ccd8b93 100644
--- a/lib/sail.h
+++ b/lib/sail.h
@@ -327,6 +327,7 @@ void random_real(real *rop, unit);
void string_length(sail_int *len, sail_string s);
void string_drop(sail_string *dst, sail_string s, sail_int len);
+void string_take(sail_string *dst, sail_string s, sail_int len);
/* ***** Printing ***** */