aboutsummaryrefslogtreecommitdiffstats
path: root/strncmove.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--strncmove.c304
1 files changed, 297 insertions, 7 deletions
diff --git a/strncmove.c b/strncmove.c
index ce53ed5..25f83bc 100644
--- a/strncmove.c
+++ b/strncmove.c
@@ -4,20 +4,31 @@
char *
-libsimple_strncmove(char *d, const char *s, int c_, size_t n) /* TODO test, man */
+libsimple_strncmove(char *d, const char *s, int c_, size_t n) /* TODO man */
{
- char c = (char)c_, *p;
- if (d < s) {
+ char c = (char)c_, *p, *end = &d[n];
+ if (d <= s) {
do {
- if ((*d++ = *s) == c)
+ if (!n--)
+ break;
+ if ((*d++ = *s) == c) {
+ if (n)
+ *d = '\0';
return d;
- } while (*s++ && n--);
+ }
+ } while (*s++);
} else {
for (p = *(char **)(void *)&s; n; n--, p++) {
- if (*p == c || *p) {
+ if (*p == c || !*p) {
n = (size_t)(p - s);
p = &d[n];
- do { d[n] = s[n]; } while (n--);
+ for (;; n--) {
+ d[n] = s[n];
+ if (!n)
+ break;
+ }
+ if (&p[1] != end && *p)
+ p[1] = '\0';
return *p == c ? &p[1] : NULL;
}
}
@@ -36,6 +47,285 @@ libsimple_strncmove(char *d, const char *s, int c_, size_t n) /* TODO test, man
int
main(void)
{
+ char buf[1024];
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], '\0', 1024) == &buf[5 + 6]);
+ assert(!strcmp(buf, "-----hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'o', 1024) == &buf[5 + 5]);
+ assert(!strcmp(buf, "-----hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'l', 1024) == &buf[5 + 3]);
+ assert(!strcmp(buf, "-----hel"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'x', 1024) == NULL);
+ assert(!strcmp(buf, "-----hello"));
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], '\0', 1024) == &buf[3 + 6]);
+ assert(!strcmp(buf, "---hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'o', 1024) == &buf[3 + 5]);
+ assert(!strcmp(buf, "---hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'l', 1024) == &buf[3 + 3]);
+ assert(!strcmp(buf, "---hel"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'x', 1024) == NULL);
+ assert(!strcmp(buf, "---hello"));
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], '\0', 1024) == &buf[8 + 6]);
+ assert(!strcmp(buf, "-----helhello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'o', 1024) == &buf[8 + 5]);
+ assert(!strcmp(buf, "-----helhello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'l', 1024) == &buf[8 + 3]);
+ assert(!strcmp(buf, "-----helhel"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'x', 1024) == NULL);
+ assert(!strcmp(buf, "-----helhello"));
+
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], '\0', 6) == &buf[5 + 6]);
+ assert(!strcmp(buf, "-----hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'o', 6) == &buf[5 + 5]);
+ assert(!strcmp(buf, "-----hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'l', 6) == &buf[5 + 3]);
+ assert(!strcmp(buf, "-----hel"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'x', 6) == NULL);
+ assert(!strcmp(buf, "-----hello"));
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], '\0', 6) == &buf[3 + 6]);
+ assert(!strcmp(buf, "---hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'o', 6) == &buf[3 + 5]);
+ assert(!strcmp(buf, "---hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'l', 6) == &buf[3 + 3]);
+ assert(!strcmp(buf, "---hel"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'x', 6) == NULL);
+ assert(!strcmp(buf, "---hello"));
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], '\0', 6) == &buf[8 + 6]);
+ assert(!strcmp(buf, "-----helhello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'o', 6) == &buf[8 + 5]);
+ assert(!strcmp(buf, "-----helhello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'l', 6) == &buf[8 + 3]);
+ assert(!strcmp(buf, "-----helhel"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'x', 6) == NULL);
+ assert(!strcmp(buf, "-----helhello"));
+
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ stpcpy(&buf[5], "hello")[0] = '-';
+ assert(libsimple_strncmove(&buf[5], &buf[5], '\0', 5) == NULL);
+ assert(!strncmp(buf, "-----hello-", 11));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ stpcpy(&buf[5], "hello")[0] = '-';
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'o', 5) == &buf[5 + 5]);
+ assert(!strncmp(buf, "-----hello-", 11));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ stpcpy(&buf[5], "hello")[0] = '-';
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'l', 5) == &buf[5 + 3]);
+ assert(!strcmp(buf, "-----hel"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ stpcpy(&buf[5], "hello")[0] = '-';
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'x', 5) == NULL);
+ assert(!strncmp(buf, "-----hello-", 11));
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], '\0', 5) == NULL);
+ assert(!strcmp(buf, "-----hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'o', 5) == &buf[5 + 5]);
+ assert(!strcmp(buf, "-----hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'l', 5) == &buf[5 + 3]);
+ assert(!strcmp(buf, "-----hel"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'x', 5) == NULL);
+ assert(!strcmp(buf, "-----hello"));
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], '\0', 5) == NULL);
+ assert(!strcmp(buf, "---hellolo"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'o', 5) == &buf[3 + 5]);
+ assert(!strcmp(buf, "---hellolo"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'l', 5) == &buf[3 + 3]);
+ assert(!strcmp(buf, "---hel"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'x', 5) == NULL);
+ assert(!strcmp(buf, "---hellolo"));
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], '\0', 5) == NULL);
+ assert(!strncmp(buf, "-----helhello-", 14));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'o', 5) == &buf[8 + 5]);
+ assert(!strncmp(buf, "-----helhello-", 14));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'l', 5) == &buf[8 + 3]);
+ assert(!strcmp(buf, "-----helhel"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'x', 5) == NULL);
+ assert(!strncmp(buf, "-----helhello-", 14));
+
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], '\0', 3) == NULL);
+ assert(!strcmp(buf, "-----hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'o', 3) == NULL);
+ assert(!strcmp(buf, "-----hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'l', 3) == &buf[5 + 3]);
+ assert(!strcmp(buf, "-----hello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[5], &buf[5], 'x', 3) == NULL);
+ assert(!strcmp(buf, "-----hello"));
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], '\0', 3) == NULL);
+ assert(!strcmp(buf, "---helello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'o', 3) == NULL);
+ assert(!strcmp(buf, "---helello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'l', 3) == &buf[3 + 3]);
+ assert(!strcmp(buf, "---helello"));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[3], &buf[5], 'x', 3) == NULL);
+ assert(!strcmp(buf, "---helello"));
+
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], '\0', 3) == NULL);
+ assert(!strncmp(buf, "-----helhel-", 12));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'o', 3) == NULL);
+ assert(!strncmp(buf, "-----helhel-", 12));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'l', 3) == &buf[8 + 3]);
+ assert(!strncmp(buf, "-----helhel-", 12));
+
+ memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
+ strcpy(&buf[5], "hello");
+ assert(libsimple_strncmove(&buf[8], &buf[5], 'x', 3) == NULL);
+ assert(!strncmp(buf, "-----helhel-", 12));
+
+
return 0;
}