gonzui


Format: Advanced Search

mtkernel_3/kernel/tstdlib/string.cbare sourcepermlink (0.01 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    micro T-Kernel 3.00.00
    4:  *
    5:  *    Copyright (C) 2006-2019 by Ken Sakamura.
    6:  *    This software is distributed under the T-License 2.1.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by TRON Forum(http://www.tron.org) at 2019/12/11.
   10:  *
   11:  *----------------------------------------------------------------------
   12:  */
   13: 
   14: /*
   15:  *      string.c
   16:  *      T-Kernel standard library 
   17:  */
   18: 
   19: #include <tk/tkernel.h>
   20: #include "kernel.h"
   21: 
   22: /*** binary operation ***/
   23: /* memset : fill memory area */
   24: void* knl_memset( void *s, int c, SZ n )
   25: {
   26:         register unsigned char *cp, cval;
   27:         register unsigned long *lp, lval;
   28: 
   29:         cp = (unsigned char *)s;
   30:         cval = (unsigned char)c;
   31:         
   32:         if (n < 8) {
   33:                 while (n-- > 0) {
   34:                         *cp++ = cval;
   35:                 }
   36:                 return s;
   37:         }
   38: 
   39:         while ((long)cp % 4) {
   40:                 --n;
   41:                 *cp++ = cval;
   42:         }
   43: 
   44:         lp = (unsigned long *)cp;
   45:         lval = (unsigned long)cval |
   46:                 (unsigned long)cval << 8 |
   47:                 (unsigned long)cval << 16 |
   48:                 (unsigned long)cval << 24;
   49: 
   50:         while (n >= 4) {
   51:                 *lp++ = lval;
   52:                 n -= 4;
   53:         }
   54: 
   55:         cp = (unsigned char *)lp;
   56:         while (n) {
   57:                 *cp++ = cval;
   58:                 --n;
   59:         }
   60: 
   61:         return s;
   62: }
   63: 
   64: /* memcpy : copy memory */
   65: void* knl_memcpy( void *dst, const void *src, SZ n )
   66: {
   67:         register unsigned char *cdst, *csrc;
   68: 
   69:         cdst = (unsigned char *)dst;
   70:         csrc = (unsigned char *)src;
   71:         while (n-- > 0) {
   72:                 *cdst++ = *csrc++;
   73:         }
   74: 
   75:         return dst;
   76: }
   77: 
   78: /* strlen : get text string length */
   79: SZ knl_strlen( const char *s )
   80: {
   81:         register char *cp;
   82: 
   83:         cp = (char *)s;
   84:         while (*cp) {
   85:                 ++cp;
   86:         }
   87:         return (SZ)(cp - s);
   88: }
   89: 
   90: /* strcpy : copy text string */
   91: char* knl_strcpy( char *dst, const char *src )
   92: {
   93:         register char *cp;
   94: 
   95:         cp = dst;
   96:         do {
   97:                 *cp++ = *src;
   98:         } while (*src++);
   99: 
  100:         return dst;
  101: }
  102: 
  103: char* knl_strncpy( char *dst, const char *src, SZ n )
  104: {
  105:         register char *cp;
  106: 
  107:         cp = dst;
  108:         do {
  109:                 if (n-- <= 0) {
  110:                         return dst;
  111:                 }
  112:                 *cp++ = *src;
  113:         } while (*src++);
  114: 
  115:         while (n-- > 0) {
  116:                 *cp++ = 0;
  117:         }
  118: 
  119:         return dst;
  120: }
  121: 
  122: /* strcmp : perform text string comparison */
  123: int knl_strcmp( const char *s1, const char *s2 )
  124: {
  125:         register int result;
  126: 
  127:         while (*s1) {
  128:                 result = (unsigned char)*s1++ - (unsigned char)*s2++;
  129:                 if (result) {
  130:                         return result;
  131:                 }
  132:         }
  133: 
  134:         return (unsigned char)*s1 - (unsigned char)*s2;
  135: }
  136: 
  137: /* strcat : perform text string concatenation */
  138: char* knl_strcat( char *dst, const char *src )
  139: {
  140:         register char *cp;
  141: 
  142:         cp = dst;
  143:         while (*cp) {
  144:                 ++cp;
  145:         }
  146: 
  147:         while (*src) {
  148:                 *cp++ = *src++;
  149:         }
  150:         *cp = '\0';
  151: 
  152:         return dst;
  153: }