megadrive_test1

annotate src/libc/string.c @ 7:8253942b0a1a

in the middle of something
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 19 Feb 2017 14:00:26 +0200
parents
children
rev   line source
nuclear@6 1 #include <string.h>
nuclear@6 2
nuclear@6 3 void *memset(void *s, int c, size_t n)
nuclear@6 4 {
nuclear@6 5 char *ptr = s;
nuclear@6 6 while(n--) {
nuclear@6 7 *ptr++ = c;
nuclear@6 8 }
nuclear@6 9 return s;
nuclear@6 10 }
nuclear@6 11
nuclear@6 12 void *memcpy(void *dest, const void *src, size_t n)
nuclear@6 13 {
nuclear@6 14 char *dptr = dest;
nuclear@6 15 const char *sptr = src;
nuclear@6 16
nuclear@6 17 while(n--) {
nuclear@6 18 *dptr++ = *sptr++;
nuclear@6 19 }
nuclear@6 20 return dest;
nuclear@6 21 }
nuclear@6 22
nuclear@6 23 void *memmove(void *dest, const void *src, size_t n)
nuclear@6 24 {
nuclear@6 25 int i;
nuclear@6 26 char *dptr;
nuclear@6 27 const char *sptr;
nuclear@6 28
nuclear@6 29 if(dest <= src) {
nuclear@6 30 /* forward copy */
nuclear@6 31 dptr = dest;
nuclear@6 32 sptr = src;
nuclear@6 33 for(i=0; i<n; i++) {
nuclear@6 34 *dptr++ = *sptr++;
nuclear@6 35 }
nuclear@6 36 } else {
nuclear@6 37 /* backwards copy */
nuclear@6 38 dptr = (char*)dest + n - 1;
nuclear@6 39 sptr = (char*)src + n - 1;
nuclear@6 40 for(i=0; i<n; i++) {
nuclear@6 41 *dptr-- = *sptr--;
nuclear@6 42 }
nuclear@6 43 }
nuclear@6 44
nuclear@6 45 return dest;
nuclear@6 46 }
nuclear@6 47
nuclear@6 48 size_t strlen(const char *s)
nuclear@6 49 {
nuclear@6 50 size_t len = 0;
nuclear@6 51 while(*s++) len++;
nuclear@6 52 return len;
nuclear@6 53 }
nuclear@6 54
nuclear@6 55 char *strchr(const char *s, int c)
nuclear@6 56 {
nuclear@6 57 while(*s) {
nuclear@6 58 if(*s == c) {
nuclear@6 59 return (char*)s;
nuclear@6 60 }
nuclear@6 61 s++;
nuclear@6 62 }
nuclear@6 63 return 0;
nuclear@6 64 }
nuclear@6 65
nuclear@6 66 char *strrchr(const char *s, int c)
nuclear@6 67 {
nuclear@6 68 const char *ptr = s;
nuclear@6 69
nuclear@6 70 /* find the end */
nuclear@6 71 while(*ptr) ptr++;
nuclear@6 72
nuclear@6 73 /* go back checking for c */
nuclear@6 74 while(--ptr >= s) {
nuclear@6 75 if(*ptr == c) {
nuclear@6 76 return (char*)ptr;
nuclear@6 77 }
nuclear@6 78 }
nuclear@6 79 return 0;
nuclear@6 80 }
nuclear@6 81
nuclear@6 82 char *strstr(const char *str, const char *substr)
nuclear@6 83 {
nuclear@6 84 while(*str) {
nuclear@6 85 const char *s1 = str;
nuclear@6 86 const char *s2 = substr;
nuclear@6 87
nuclear@6 88 while(*s1 && *s1 == *s2) {
nuclear@6 89 s1++;
nuclear@6 90 s2++;
nuclear@6 91 }
nuclear@6 92 if(!*s2) {
nuclear@6 93 return (char*)str;
nuclear@6 94 }
nuclear@6 95 str++;
nuclear@6 96 }
nuclear@6 97 return 0;
nuclear@6 98 }
nuclear@6 99
nuclear@6 100 int strcmp(const char *s1, const char *s2)
nuclear@6 101 {
nuclear@6 102 while(*s1 && *s1 == *s2) {
nuclear@6 103 s1++;
nuclear@6 104 s2++;
nuclear@6 105 }
nuclear@6 106 return *s1 - *s2;
nuclear@6 107 }