summaryrefslogtreecommitdiff
path: root/src/str.c
diff options
context:
space:
mode:
authorMichael Jennings <mej@kainx.org>2003-12-13 03:02:19 +0000
committerMichael Jennings <mej@kainx.org>2003-12-13 03:02:19 +0000
commitb81d14cb6ce9177405622e36917601d788d14ad5 (patch)
tree749bfbac8cbcc1c4e8a4f00a591c9f12094a8ef7 /src/str.c
parent451d74239aa66a49802a7036988d304fc3a9f72b (diff)
downloadlibast-b81d14cb6ce9177405622e36917601d788d14ad5.tar.gz
Fri Dec 12 22:01:42 2003 Michael Jennings (mej)
Finished up namespace cleanups. SVN revision: 8107
Diffstat (limited to 'src/str.c')
-rw-r--r--src/str.c31
1 files changed, 27 insertions, 4 deletions
diff --git a/src/str.c b/src/str.c
index 568e285..6789ba7 100644
--- a/src/str.c
+++ b/src/str.c
@@ -392,19 +392,42 @@ spif_str_find_from_ptr(spif_str_t self, spif_charptr_t other)
spif_str_t
spif_str_substr(spif_str_t self, spif_int32_t idx, spif_int32_t cnt)
{
+ spif_int32_t start_pos, char_count;
+
ASSERT_RVAL(!SPIF_STR_ISNULL(self), SPIF_NULL_TYPE(str));
- return spif_str_new_from_buff(SPIF_STR_STR(self) + ((idx < 0) ? (self->len) : (0)) + idx, cnt);
+ if (idx < 0) {
+ start_pos = self->len + idx;
+ } else {
+ start_pos = idx;
+ }
+ if (cnt <= 0) {
+ char_count = self->len - start_pos + cnt;
+ } else {
+ char_count = cnt;
+ }
+ return spif_str_new_from_buff(SPIF_STR_STR(self) + start_pos, char_count);
}
spif_charptr_t
spif_str_substr_to_ptr(spif_str_t self, spif_int32_t idx, spif_int32_t cnt)
{
spif_charptr_t newstr;
+ spif_int32_t start_pos, char_count;
ASSERT_RVAL(!SPIF_STR_ISNULL(self), SPIF_NULL_TYPE(charptr));
- newstr = SPIF_CAST(charptr) MALLOC(cnt + 1);
- memcpy(newstr, SPIF_STR_STR(self) + ((idx < 0) ? (self->len) : (0)) + idx, cnt);
- newstr[cnt] = 0;
+ if (idx < 0) {
+ start_pos = self->len + idx;
+ } else {
+ start_pos = idx;
+ }
+ if (cnt <= 0) {
+ char_count = self->len - start_pos + cnt;
+ } else {
+ char_count = cnt;
+ }
+ newstr = SPIF_CAST(charptr) MALLOC(char_count + 1);
+ memcpy(newstr, SPIF_STR_STR(self) + start_pos, char_count);
+ newstr[char_count] = 0;
return newstr;
}