aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--String.c17
-rw-r--r--String.h1
-rw-r--r--example.c2
3 files changed, 19 insertions, 1 deletions
diff --git a/String.c b/String.c
index fb003d8..7723525 100644
--- a/String.c
+++ b/String.c
@@ -240,3 +240,20 @@ S_find(String s, char c) {
return index;
}
+
+String
+S_substr(MemoryManager *mm, String s, usize start, usize length) {
+ if (NULL == s)
+ return NULL;
+
+ struct StringMeta *sm = S_metadata(s);
+
+ if (start >= sm->length || 0 == length)
+ return S_newlen(mm, 0);
+
+ String result = S_newlen(mm, length);
+ strncpy(result, s + start, length);
+ result[length] = '\0';
+
+ return result;
+}
diff --git a/String.h b/String.h
index 40d0e55..2a9ef87 100644
--- a/String.h
+++ b/String.h
@@ -30,5 +30,6 @@ Result S_prepend(String *s, const char* str);
usize S_length(String s);
usize S_find(String s, char c);
+String S_substr(MemoryManager *mm, String s, usize start, usize length);
#endif /* _STRING */
diff --git a/example.c b/example.c
index 3b1bbb6..3808acd 100644
--- a/example.c
+++ b/example.c
@@ -26,7 +26,7 @@ main() {
NULL)));
}
- printf("%s", all);
+ printf("%s|%s|\n", all, S_substr(&t, all, 23, 4));
MemoryManager T = M_std();