string.c

来自「Newlib 嵌入式 C库 标准实现代码」· C语言 代码 · 共 557 行 · 第 1/2 页

C
557
字号
  /* strstr - somewhat like strchr.  */  it = "strstr";  check(strstr("z", "abcd") == NULL); /* Not found. */  check(strstr("abx", "abcd") == NULL); /* Dead end. */  (void) strcpy(one, "abcd");  check(strstr(one,"c") == one+2); /* Basic test. */  check(strstr(one, "bc") == one+1);	/* Multichar. */  check(strstr(one,"d") == one+3); /* End of string. */  check(strstr(one,"cd") == one+2);	/* Tail of string. */  check(strstr(one,"abc") == one); /* Beginning. */  check(strstr(one,"abcd") == one);	/* Exact match. */  check(strstr(one,"de") == NULL);	/* Past end. */  check(strstr(one,"") == one); /* Finding empty. */  (void) strcpy(one, "ababa");  check(strstr(one,"ba") == one+1); /* Finding first. */  (void) strcpy(one, "");  check(strstr(one, "b") == NULL); /* Empty string. */  check(strstr(one,"") == one); /* Empty in empty string. */  (void) strcpy(one, "bcbca");  check(strstr(one,"bca") == one+2); /* False start. */  (void) strcpy(one, "bbbcabbca");  check(strstr(one,"bbca") == one+1); /* With overlap. */  /* strspn.  */  it = "strspn";  check(strspn("abcba", "abc") == 5); /* Whole string. */  check(strspn("abcba", "ab") == 2);	/* Partial. */  check(strspn("abc", "qx") == 0); /* None. */  check(strspn("", "ab") == 0); /* Null string. */  check(strspn("abc", "") == 0); /* Null search list. */  /* strcspn.  */  it = "strcspn";  check(strcspn("abcba", "qx") == 5); /* Whole string. */  check(strcspn("abcba", "cx") == 2); /* Partial. */  check(strcspn("abc", "abc") == 0);	/* None. */  check(strcspn("", "ab") == 0); /* Null string. */  check(strcspn("abc", "") == 3); /* Null search list. */  /* strtok - the hard one.  */  it = "strtok";  (void) strcpy(one, "first, second, third");  equal(strtok(one, ", "), "first");	/* Basic test. */  equal(one, "first");  equal(strtok((char *)NULL, ", "), "second");  equal(strtok((char *)NULL, ", "), "third");  check(strtok((char *)NULL, ", ") == NULL);  (void) strcpy(one, ", first, ");  equal(strtok(one, ", "), "first");	/* Extra delims, 1 tok. */  check(strtok((char *)NULL, ", ") == NULL);  (void) strcpy(one, "1a, 1b; 2a, 2b");  equal(strtok(one, ", "), "1a"); /* Changing delim lists. */  equal(strtok((char *)NULL, "; "), "1b");  equal(strtok((char *)NULL, ", "), "2a");  (void) strcpy(two, "x-y");  equal(strtok(two, "-"), "x"); /* New string before done. */  equal(strtok((char *)NULL, "-"), "y");  check(strtok((char *)NULL, "-") == NULL);  (void) strcpy(one, "a,b, c,, ,d");  equal(strtok(one, ", "), "a"); /* Different separators. */  equal(strtok((char *)NULL, ", "), "b");  equal(strtok((char *)NULL, " ,"), "c"); /* Permute list too. */  equal(strtok((char *)NULL, " ,"), "d");  check(strtok((char *)NULL, ", ") == NULL);  check(strtok((char *)NULL, ", ") == NULL); /* Persistence. */  (void) strcpy(one, ", ");  check(strtok(one, ", ") == NULL);	/* No tokens. */  (void) strcpy(one, "");  check(strtok(one, ", ") == NULL);	/* Empty string. */  (void) strcpy(one, "abc");  equal(strtok(one, ", "), "abc"); /* No delimiters. */  check(strtok((char *)NULL, ", ") == NULL);  (void) strcpy(one, "abc");  equal(strtok(one, ""), "abc"); /* Empty delimiter list. */  check(strtok((char *)NULL, "") == NULL);  (void) strcpy(one, "abcdefgh");  (void) strcpy(one, "a,b,c");  equal(strtok(one, ","), "a"); /* Basics again... */  equal(strtok((char *)NULL, ","), "b");  equal(strtok((char *)NULL, ","), "c");  check(strtok((char *)NULL, ",") == NULL);  equal(one+6, "gh");	/* Stomped past end? */  equal(one, "a");		/* Stomped old tokens? */  equal(one+2, "b");  equal(one+4, "c");  /* memcmp.  */  it = "memcmp";  check(memcmp("a", "a", 1) == 0); /* Identity. */  check(memcmp("abc", "abc", 3) == 0); /* Multicharacter. */  check(memcmp("abcd", "abce", 4) < 0); /* Honestly unequal. */  check(memcmp("abce", "abcd",4));  check(memcmp("alph", "beta", 4) < 0);  check(memcmp("abce", "abcd", 3) == 0); /* Count limited. */  check(memcmp("abc", "def", 0) == 0); /* Zero count. */  /* memcmp should test strings as unsigned */  one[0] = 0xfe;  two[0] = 0x03;  check(memcmp(one, two,1) > 0);      /* memchr.  */  it = "memchr";  check(memchr("abcd", 'z', 4) == NULL); /* Not found. */  (void) strcpy(one, "abcd");  check(memchr(one, 'c', 4) == one+2); /* Basic test. */  check(memchr(one, 'd', 4) == one+3); /* End of string. */  check(memchr(one, 'a', 4) == one);	/* Beginning. */  check(memchr(one, '\0', 5) == one+4); /* Finding NUL. */  (void) strcpy(one, "ababa");  check(memchr(one, 'b', 5) == one+1); /* Finding first. */  check(memchr(one, 'b', 0) == NULL); /* Zero count. */  check(memchr(one, 'a', 1) == one);	/* Singleton case. */  (void) strcpy(one, "a\203b");  check(memchr(one, 0203, 3) == one+1); /* Unsignedness. */  /* memcpy - need not work for overlap.  */  it = "memcpy";  check(memcpy(one, "abc", 4) == one); /* Returned value. */  equal(one, "abc");		/* Did the copy go right? */  (void) strcpy(one, "abcdefgh");  (void) memcpy(one+1, "xyz", 2);  equal(one, "axydefgh");	/* Basic test. */  (void) strcpy(one, "abc");  (void) memcpy(one, "xyz", 0);  equal(one, "abc");		/* Zero-length copy. */  (void) strcpy(one, "hi there");  (void) strcpy(two, "foo");  (void) memcpy(two, one, 9);  equal(two, "hi there");	/* Just paranoia. */  equal(one, "hi there");	/* Stomped on source? */#if 0  /* memmove - must work on overlap.  */  it = "memmove";  check(memmove(one, "abc", 4) == one); /* Returned value. */  equal(one, "abc");		/* Did the copy go right? */  (void) strcpy(one, "abcdefgh");  (void) memmove(one+1, "xyz", 2);  equal(one, "axydefgh");	/* Basic test. */  (void) strcpy(one, "abc");  (void) memmove(one, "xyz", 0);  equal(one, "abc");		/* Zero-length copy. */  (void) strcpy(one, "hi there");  (void) strcpy(two, "foo");  (void) memmove(two, one, 9);  equal(two, "hi there");	/* Just paranoia. */  equal(one, "hi there");	/* Stomped on source? */  (void) strcpy(one, "abcdefgh");  (void) memmove(one+1, one, 9);  equal(one, "aabcdefgh");	/* Overlap, right-to-left. */  (void) strcpy(one, "abcdefgh");  (void) memmove(one+1, one+2, 7);  equal(one, "acdefgh");	/* Overlap, left-to-right. */  (void) strcpy(one, "abcdefgh");  (void) memmove(one, one, 9);  equal(one, "abcdefgh");	/* 100% overlap. */#endif#if 0  /* memccpy - first test like memcpy, then the search part     The SVID, the only place where memccpy is mentioned, says     overlap might fail, so we don't try it.  Besides, it's hard     to see the rationale for a non-left-to-right memccpy.  */  it = "memccpy";  check(memccpy(one, "abc", 'q', 4) == NULL); /* Returned value. */  equal(one, "abc");		/* Did the copy go right? */  (void) strcpy(one, "abcdefgh");  (void) memccpy(one+1, "xyz", 'q', 2);  equal(one, "axydefgh");	/* Basic test. */  (void) strcpy(one, "abc");  (void) memccpy(one, "xyz", 'q', 0);  equal(one, "abc");		/* Zero-length copy. */  (void) strcpy(one, "hi there");  (void) strcpy(two, "foo");  (void) memccpy(two, one, 'q', 9);  equal(two, "hi there");	/* Just paranoia. */  equal(one, "hi there");	/* Stomped on source? */  (void) strcpy(one, "abcdefgh");  (void) strcpy(two, "horsefeathers");  check(memccpy(two, one, 'f', 9) == two+6);	/* Returned value. */  equal(one, "abcdefgh");	/* Source intact? */  equal(two, "abcdefeathers"); /* Copy correct? */  (void) strcpy(one, "abcd");  (void) strcpy(two, "bumblebee");  check(memccpy(two, one, 'a', 4) == two+1); /* First char. */  equal(two, "aumblebee");  check(memccpy(two, one, 'd', 4) == two+4); /* Last char. */  equal(two, "abcdlebee");  (void) strcpy(one, "xyz");  check(memccpy(two, one, 'x', 1) == two+1); /* Singleton. */  equal(two, "xbcdlebee");#endif  /* memset.  */  it = "memset";  (void) strcpy(one, "abcdefgh");  check(memset(one+1, 'x', 3) == one+1); /* Return value. */  equal(one, "axxxefgh");	/* Basic test. */  (void) memset(one+2, 'y', 0);  equal(one, "axxxefgh");	/* Zero-length set. */  (void) memset(one+5, 0, 1);  equal(one, "axxxe");	/* Zero fill. */  equal(one+6, "gh");	/* _AND the leftover. */  (void) memset(one+2, 010045, 1);  equal(one, "ax\045xe");	/* Unsigned char convert. */  /* bcopy - much like memcpy.     Berklix manual is silent about overlap, so don't test it.  */  it = "bcopy";  (void) bcopy("abc", one, 4);  equal(one, "abc");		/* Simple copy. */  (void) strcpy(one, "abcdefgh");  (void) bcopy("xyz", one+1, 2);  equal(one, "axydefgh");	/* Basic test. */  (void) strcpy(one, "abc");  (void) bcopy("xyz", one, 0);  equal(one, "abc");		/* Zero-length copy. */  (void) strcpy(one, "hi there");  (void) strcpy(two, "foo");  (void) bcopy(one, two, 9);  equal(two, "hi there");	/* Just paranoia. */  equal(one, "hi there");	/* Stomped on source? */  /* bzero.  */  it = "bzero";  (void) strcpy(one, "abcdef");  bzero(one+2, 2);  equal(one, "ab");		/* Basic test. */  equal(one+3, "");  equal(one+4, "ef");  (void) strcpy(one, "abcdef");  bzero(one+2, 0);  equal(one, "abcdef");	/* Zero-length copy. */  /* bcmp - somewhat like memcmp.  */  it = "bcmp";  check(bcmp("a", "a", 1) == 0); /* Identity. */  check(bcmp("abc", "abc", 3) == 0);	/* Multicharacter. */  check(bcmp("abcd", "abce", 4) != 0); /* Honestly unequal. */  check(bcmp("abce", "abcd",4));  check(bcmp("alph", "beta", 4) != 0);  check(bcmp("abce", "abcd", 3) == 0); /* Count limited. */  check(bcmp("abc", "def", 0) == 0);	/* Zero count. */#if 0  /* strerror - VERY system-dependent.  */{  extern CONST unsigned int _sys_nerr;  extern CONST char *CONST _sys_errlist[];  int f;  it = "strerror";  f = open("/", O_WRONLY);	/* Should always fail. */  check(f < 0 && errno > 0 && errno < _sys_nerr);  equal(strerror(errno), _sys_errlist[errno]);}#endif}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?