Main Page | Modules | File List | File Members

string.c

00001 /* ndk - [ string.c ]
00002  *
00003  * Contains functions similar to the Ansi C
00004  * implementations in memory.h and string.h
00005  * Many of these have been borrowed from
00006  * the NetBSD C Library.  Not only is development
00007  * quicker when you can rely on these open source
00008  * projects, it's also incredibly stable (how many
00009  * thousands of users do you think have "beta
00010  * tested" those NetBSD routines?)
00011  *
00012  * Of course, some of these routines may have to
00013  * be replaced if I go with a non-GPL license. I
00014  * haven't really decided upon licensing yet...
00015  *
00016  * Please see:
00017  *   http://www.ajk.tele.fi/libc/code.html [ NetBSD C Library Site ]
00018  *
00019  * (c)2002 dcipher / neuraldk
00020  *           www.neuraldk.org
00021  */
00022 
00023 #include "string.h"
00024 
00025 /*
00026  * sizeof(word) MUST BE A POWER OF TWO
00027  * SO THAT wmask BELOW IS ALL ONES
00028  */
00029 typedef long word;              /* "word" used for optimal copy speed */
00030 
00031 #define wsize   sizeof(word)
00032 #define wmask   (wsize - 1)
00033 
00034 
00035 int strncmp(char *s1, char *s2, long n) {
00036 
00037   if(n == 0)
00038     return 0;
00039 
00040   do {
00041     if (*s1 != *s2++)
00042       return (*(unsigned char *)s1 -
00043               *(unsigned char *)--s2);
00044 
00045     if (*s1++ == 0) break;
00046   } while (--n != 0);
00047 
00048   return 0;
00049 }
00050 
00051 
00052 int strcmp(char *s1, char *s2) {
00053   while (*s1 == *s2++)
00054     if (*s1++ == 0)
00055       return 0;
00056 
00057   return (*(char *)s1 - *(char *)--s2);
00058 }
00059 
00060 
00061 void *memcpy(void *dst0, void *src0, long length) {
00062 //        void *dst0;
00063 //        const void *src0;
00064 //        register size_t length;
00065 
00066   char *dst = dst0;
00067   const char *src = src0;
00068   long t;
00069 
00070   if (length == 0 || dst == src)          /* nothing to do */
00071     goto done;
00072 
00073         /*
00074          * Macros: loop-t-times; and loop-t-times, t>0
00075          */
00076 #define TLOOP(s) if (t) TLOOP1(s)
00077 #define TLOOP1(s) do { s; } while (--t)
00078 
00079   if ((unsigned long)dst < (unsigned long)src) {
00080     /*
00081      * Copy forward.
00082      */
00083     t = (long)src;  /* only need low bits */
00084     if ((t | (long)dst) & wmask) {
00085       /*
00086        * Try to align operands.  This cannot be done
00087        * unless the low bits match.
00088        */
00089       if ((t ^ (long)dst) & wmask || length < wsize)
00090         t = length;
00091       else
00092         t = wsize - (t & wmask);
00093       length -= t;
00094       TLOOP1(*dst++ = *src++);
00095     }
00096     /*
00097      * Copy whole words, then mop up any trailing bytes.
00098      */
00099     t = length / wsize;
00100     TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
00101     t = length & wmask;
00102     TLOOP(*dst++ = *src++);
00103   } else {
00104     /*
00105      * Copy backwards.  Otherwise essentially the same.
00106      * Alignment works as before, except that it takes
00107      * (t&wmask) bytes to align, not wsize-(t&wmask).
00108      */
00109     src += length;
00110     dst += length;
00111     t = (long)src;
00112     if ((t | (long)dst) & wmask) {
00113       if ((t ^ (long)dst) & wmask || length <= wsize)
00114         t = length;
00115       else
00116         t &= wmask;
00117       length -= t;
00118       TLOOP1(*--dst = *--src);
00119     }
00120     t = length / wsize;
00121     TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
00122     t = length & wmask;
00123     TLOOP(*--dst = *--src);
00124   }
00125 done:
00126   return (dst0);
00127 }
00128 
00129 
00130 void *memset(void *dst, int c, int n) {
00131   if (n != 0) {
00132     char *d = dst;
00133 
00134     do {
00135       *d++ = c;
00136     } while (--n != 0);
00137   }
00138   return (dst);
00139 }

Generated on Sun Nov 21 18:26:11 2004 for ndk by doxygen 1.3.2