diff options
| author | Syndamia <kamen@syndamia.com> | 2026-04-29 18:26:15 +0300 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2026-05-12 07:08:56 +0300 |
| commit | 8636d3558523fe30a2be84549c2b6eb5e125e97c (patch) | |
| tree | c004b54e73d1584fb9f4c17caffbd1534855ab6c | |
| parent | 5d3ee795417ad5e0cad8e05a829b143cfb9652e6 (diff) | |
| download | foollib-8636d3558523fe30a2be84549c2b6eb5e125e97c.tar foollib-8636d3558523fe30a2be84549c2b6eb5e125e97c.tar.gz foollib-8636d3558523fe30a2be84549c2b6eb5e125e97c.zip | |
feat(String): Implement S_substr function
| -rw-r--r-- | String.c | 17 | ||||
| -rw-r--r-- | String.h | 1 | ||||
| -rw-r--r-- | example.c | 2 |
3 files changed, 19 insertions, 1 deletions
@@ -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; +} @@ -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 */ @@ -26,7 +26,7 @@ main() { NULL))); } - printf("%s", all); + printf("%s|%s|\n", all, S_substr(&t, all, 23, 4)); MemoryManager T = M_std(); |
