Ejemplo n.º 1
0
 char *strrchr( const char *s, int c )
#endif
/*********************************************/
{
    RISC_DATA_LOCALREF;
    UINT                *result = NULL;
    CHAR_TYPE           *result2 = NULL;
    UINT                *dw = ROUND(s); // round down to dword
    UINT                mask, dword, cdword, tmpdword;
    size_t              len = 0;
    int                 offset = OFFSET(s);
#ifdef __WIDECHAR__
    UINT                cShl16;
#else
    UINT                cShl8, cShl16, cShl24;
#endif

#ifdef __WIDECHAR__
    if( offset % 2 )
        return( __simple_wcsrchr( s, c ) );
#endif

    /*** If searching for '\0', use different technique ***/
    if( c == NULLCHAR )
        return( (CHAR_TYPE*)s + __F_NAME(strlen,wcslen)( s ) );

    /*** Initialize locals ***/
    c &= CHR1MASK;
#ifdef __WIDECHAR__
    cShl16 = c << 16;
    cdword = cShl16 | c;
#else
    cShl8 = c << 8;
    cShl16 = c << 16;
    cShl24 = c << 24;
    cdword = cShl24 | cShl16 | cShl8 | c;
#endif
    dword = *dw;

    /*** Scan any bytes up to a 4-byte alignment ***/
    if( OFFSET_GOT_NIL(dword,offset) ) { /* there is a null char in the first word */
        tmpdword = SKIP_CHRS(dword,offset/CHARSIZE) ^ cdword;
        if( GOT_NIL(tmpdword) ) {   /* c is in the first word */
#ifdef __WIDECHAR__
            if( offset == 0 ) {     /* no odd alignments */
                tmpdword = CHR1(dword);
                if( tmpdword == c ) {
                    result2 = (CHAR_TYPE*)s;
                } else if( tmpdword == 0 ) {
                    return( NULL );
                }
                len++;
            }
#else
            switch( offset ) {
              case 0:
                tmpdword = CHR1(dword);
                if( tmpdword == c ) {
                    result2 = (CHAR_TYPE*)s;
                } else if( tmpdword == 0 ) {
                    return( NULL );
                }
                len++;
                /* fall through */
              case 1:
                tmpdword = CHR2(dword);
                if( tmpdword == cShl8 ) {
                    result2 = (CHAR_TYPE*)s+len;
                } else if( tmpdword == 0 ) {
                    return( result2 );
                }
                len++;
                /* fall through */
              case 2:
                tmpdword = CHR3(dword);
                if( tmpdword == cShl16 ) {
                    return (CHAR_TYPE*)s+len;
                } else {
                    return( result2 );
                }
            }
#endif
        }
        return( result2 );
    } else {
        tmpdword = SKIP_CHRS(dword,offset/CHARSIZE) ^ cdword;
        if( GOT_NIL(tmpdword) ) { // c is in the first word
            result = dw;
            mask = SKIP_CHRS_MASKS(offset/CHARSIZE);
        }
    }

    /*** Scan in aligned 4-byte groups ***/
    for( ;; ) {
        dword = *(++dw);
        if( GOT_NIL(dword) )
            break;
        tmpdword = dword ^ cdword;
        if( GOT_NIL(tmpdword)) {
            result = dw;
            mask = SKIP_CHRS_MASKS(0);
        }

        dword = *(++dw);
        if( GOT_NIL(dword) )
            break;
        tmpdword = dword ^ cdword;
        if( GOT_NIL(tmpdword)) {
            result = dw;
            mask = SKIP_CHRS_MASKS(0);
        }
    }

    /*** Scan the last byte(s) in the string ***/

    /* we have a null char somewhere in the last dword */
#ifdef __WIDECHAR__
    if( CHR1(dword) ) { // first char in the dword is not null
        if( CHR1(dword) == c ) {
            return( (CHAR_TYPE*)dw );
        }
    }
#else
    if( CHR1(dword) ) { // first char in the dword is not null
        if( CHR2(dword) ) { // second char in the dword is not null
            if( CHR3(dword) ) { // third char in the dword is not null
                if ( ( CHR3(dword) ) == cShl16 ) {
                    return( ((char *)dw) + 2 );
                } else if ( ( CHR2(dword) ) == cShl8 ) {
                    return( ((char *)dw) + 1 );
                } else if ( ( CHR1(dword) ) == c ) {
                    return( (char *)dw );
                }
            } else {
                if ( ( CHR2(dword) ) == cShl8 ) {
                     return ((char *)dw)+1;
                } else if ( ( CHR1(dword) ) == c ) {
                     return( (char *)dw );
                }
            }
        } else {
            if ( ( CHR1(dword) ) == c ) {
                return( (char *)dw );
            }
        }
    }
#endif

    if( result != NULL ) { // we found a dword with c
        dword = *result;
        dword &= mask;
#ifdef __WIDECHAR__
        if( CHR2(dword) == cShl16 ) {
            return( ((CHAR_TYPE*)result) + 1 );
        }
#else
        if( CHR4(dword) == cShl24 ) {
            return( ((CHAR_TYPE*)result) + 3 );
        } else if( CHR3(dword) == cShl16 ) {
            return( ((CHAR_TYPE*)result) + 2 );
        } else if( CHR2(dword) == cShl8 ) {
            return( ((CHAR_TYPE*)result) + 1 );
        }
#endif
    }
    return( (CHAR_TYPE*)result );
}
Ejemplo n.º 2
0
CHAR_TYPE *__F_NAME(strnset,_wcsnset)( CHAR_TYPE *s, int c, size_t n )
/********************************************************************/
{
    RISC_DATA_LOCALREF;
    UINT                *dw = ROUND(s); /* round down to dword */
    UINT                dword, cdword, tmpdword;
    INT                 offset = OFFSET(s);

#ifdef __WIDECHAR__
    if( offset % 2 )
        return( __simple__wcsnset( s, c, n ) );
#endif

    if( n == 0 )
        return( s );    // nothing left to set

    /*** Initialize locals ***/
    c &= CHR1MASK;
#ifdef __WIDECHAR__
    cdword = (c<<16) | c;
#else
    cdword = (c<<24) | (c<<16) | (c<<8) | c;
#endif
    dword = *dw;

    /*** Set any bytes up to a 4-byte alignment ***/
    tmpdword = FRONT_CHRS(dword,offset);
#ifdef __WIDECHAR__
    switch( offset ) {
      case 0:
        if( CHR1(dword) && n ) {
            tmpdword |= CHR1(cdword);
            --n;
        } else {
            return( s );
        }
        /* fall through */
      default:              /* offset must equal 2 (no odd offsets) */
        if( CHR2(dword) && n ) {
            tmpdword |= CHR2(cdword);
            *dw = tmpdword;
            --n;
        } else {
            tmpdword |= SKIP_CHRS(dword,1);
            *dw = tmpdword;
            return( s );
        }
    }
#else
    switch( offset ) {
      case 0:
        if( CHR1(dword) && n ) {
            tmpdword |= CHR1(cdword);
            --n;
        } else {
            return( s );
        }
        /* fall through */
      case 1:
        if( CHR2(dword) && n ) {
            tmpdword |= CHR2(cdword);
            --n;
        } else {
            tmpdword |= SKIP_CHRS(dword,1);
            *dw = tmpdword;
            return( s );
        }
      case 2:
        if( CHR3(dword) && n ) {
            tmpdword |= CHR3(cdword);
            --n;
        } else {
            tmpdword |= SKIP_CHRS(dword,2);
            *dw = tmpdword;
            return( s );
        }
      default:
        if( CHR4(dword) && n ) {
            tmpdword |= CHR4(cdword);
            *dw = tmpdword;
            --n;
        } else {
            tmpdword |= SKIP_CHRS(dword,3);
            *dw = tmpdword;
            return( s );
        }
    }
#endif

    dw++;

    /*** Write in aligned 4-byte groups ***/
    for( ;; ) {
        if( n == 0 )
            return( s );    // nothing left to set
        dword = *dw;
        if( GOT_NIL(dword) )  break;
        if( n >= CHARS_PER_WORD ) {
            *dw++ = cdword;
            n -= CHARS_PER_WORD;
        } else {
            *dw = FRONT_CHRS(cdword,n) | SKIP_CHRS(dword,n);
            return( s );
        }
    }

    /*** Write in last dword ***/
#ifdef __WIDECHAR__
    if( !CHR1(dword) ) {
        return( s );
    } else {
        if( n >= 2 ) {
            *dw = SKIP_CHRS(dword,2) | FRONT_CHRS(cdword,2);
        } else if( n == 1 ) {
            *dw = SKIP_CHRS(dword,1) | FRONT_CHRS(cdword,1);
        }
        return( s );
    }
#else
    if( !CHR1(dword) ) {
        return( s );
    } else if( !CHR2(dword) ) {
        if( n >= 1 ) {
            *dw = (SKIP_CHRS(dword,1) | FRONT_CHRS(cdword,1));
        }
        return( s );
    } else if( !CHR3(dword) ) {
        if( n >= 2 ) {
            *dw = (SKIP_CHRS(dword,2) | FRONT_CHRS(cdword,2));
        } else if( n == 1 ) {
            *dw = (SKIP_CHRS(dword,1) | FRONT_CHRS(cdword,1));
        }
        return( s );
    } else {
        if( n >= 3 ) {
            *dw = (SKIP_CHRS(dword,3) | FRONT_CHRS(cdword,3));
        } else if( n == 2 ) {
            *dw = (SKIP_CHRS(dword,2) | FRONT_CHRS(cdword,2));
        } else if( n == 1 ) {
            *dw = (SKIP_CHRS(dword,1) | FRONT_CHRS(cdword,1));
        }
        return( s );
    }
#endif
}
Ejemplo n.º 3
0
CHAR_TYPE *__F_NAME(strlwr,_wcslwr)( CHAR_TYPE *s )
/*************************************************/
{
    RISC_DATA_LOCALREF;
    UINT                *dw = ROUND(s);
    UINT                dword;
    INT                 offset = OFFSET(s);
    INT                 tmpdword, tmpchr;

#ifdef __WIDECHAR__
    if( offset % 2 )
        return( __simple__wcslwr( s ) );
#endif

    /*** Initialize locals ***/
    dword = *dw;

    /*** Set any bytes up to a 4-byte alignment ***/
    tmpdword = FRONT_CHRS(dword,offset/CHARSIZE);
#ifdef __WIDECHAR__
    switch( offset ) {
    case 0:
        tmpchr = CHR1(dword);
        if( tmpchr ) {
            tmpdword |= TO_LOW_CHR1(tmpchr);
        } else {
            return( s );
        }
        /* fall through */
    default:                /* offset must equal 2 (no odd offsets) */
        tmpchr = CHR2(dword);
        if( tmpchr ) {
            tmpdword |= TO_LOW_CHR2(tmpchr);
            *dw = tmpdword;
        } else {
            *dw = tmpdword;
            return( s );
        }
    }
#else
    switch( offset ) {
    case 0:
        tmpchr = CHR1(dword);
        if( tmpchr ) {
            tmpdword |= TO_LOW_CHR1(tmpchr);
        } else {
            return( s );
        }
        /* fall through */
    case 1:
        tmpchr = CHR2(dword);
        if( tmpchr ) {
            tmpdword |= TO_LOW_CHR2(tmpchr);
        } else {
            tmpdword |= SKIP_CHRS(dword,2);
            *dw = tmpdword;
            return( s );
        }
        /* fall through */
    case 2:
        tmpchr = CHR3(dword);
        if( tmpchr ) {
            tmpdword |= TO_LOW_CHR3(tmpchr);
        } else {
            tmpdword |= SKIP_CHRS(dword,3);
            *dw = tmpdword;
            return( s );
        }
        /* fall through */
    default:
        tmpchr = CHR4(dword);
        if( tmpchr ) {
            tmpdword |= TO_LOW_CHR4(tmpchr);
            *dw = tmpdword;
        } else {
            *dw = tmpdword;
            return( s );
        }
    }
#endif

    dw++;

    /*** Write in aligned 4-byte groups ***/
    for( ;; ) {
        dword = *dw;
        if( GOT_NIL(dword) )
            break;
        tmpdword = 0;
        tmpchr = CHR1(dword);
        tmpdword |= TO_LOW_CHR1(tmpchr);
        tmpchr = CHR2(dword);
        tmpdword |= TO_LOW_CHR2(tmpchr);
#ifndef __WIDECHAR__
        tmpchr = CHR3(dword);
        tmpdword |= TO_LOW_CHR3(tmpchr);
        tmpchr = CHR4(dword);
        tmpdword |= TO_LOW_CHR4(tmpchr);
#endif
        *dw++ = tmpdword;
    }

    /*** Write in last dword ***/
#ifdef __WIDECHAR__
    if( !CHR1(dword) ) {
        return( s );
    } else {
        tmpdword = SKIP_CHRS(dword,1);
        tmpchr = CHR1(dword);
        tmpdword |= TO_LOW_CHR1(tmpchr);
        *dw = tmpdword;
        return( s );
    }
#else
    if( !CHR1(dword) ) {
        return( s );
    } else if( !CHR2(dword) ) {
        tmpdword = SKIP_CHRS(dword,1);
        tmpchr = CHR1(dword);
        tmpdword |= TO_LOW_CHR1(tmpchr);
        *dw = tmpdword;
        return( s );
    } else if( !CHR3(dword) ) {
        tmpdword = SKIP_CHRS(dword,2);
        tmpchr = CHR1(dword);
        tmpdword |= TO_LOW_CHR1(tmpchr);
        tmpchr = CHR2(dword);
        tmpdword |= TO_LOW_CHR2(tmpchr);
        *dw = tmpdword;
        return( s );
    } else {
        tmpdword = SKIP_CHRS(dword,3);
        tmpchr = CHR1(dword);
        tmpdword |= TO_LOW_CHR1(tmpchr);
        tmpchr = CHR2(dword);
        tmpdword |= TO_LOW_CHR2(tmpchr);
        tmpchr = CHR3(dword);
        tmpdword |= TO_LOW_CHR3(tmpchr);
        *dw = tmpdword;
        return( s );
    }
#endif
}
Ejemplo n.º 4
0
size_t __F_NAME(strlen,wcslen)( const CHAR_TYPE *s )
/**************************************************/
{
    RISC_DATA_LOCALREF;
    int                 offset = OFFSET(s);
    UINT                *dw = ROUND(s); /* round down to dword */
    UINT                dword;
    size_t              len = 0;

#ifdef __WIDECHAR__
    if( offset % 2 )
        return( __simple_wcslen( s ) );
#endif

    /*** Scan until s is aligned ***/
    dword = *dw++;
    if( OFFSET_GOT_NIL(dword,offset) ) {
#if USE_INT64
        switch( offset ) {
          case 0:
            if( !CHR1(dword) )  break;
            len++;
            /* fall through */
          case 1:
            if( !CHR2(dword) )  break;
            len++;
            /* fall through */
          case 2:
            if( !CHR3(dword) )  break;
            len++;
            /* fall through */
          case 3:
            if( !CHR4(dword) )  break;
            len++;
            /* fall through */
          case 4:
            if( !CHR5(dword) )  break;
            len++;
            /* fall through */
          case 5:
            if( !CHR6(dword) )  break;
            len++;
            /* fall through */
          case 6:
            if( !CHR7(dword) )  break;
            len++;
            /* fall through */
          default:
            break;
        }
#else
    #ifdef __WIDECHAR__
        switch( offset ) {
          case 0:
            if( !CHR1(dword) )  break;
            len++;
            /* fall through */
          default:              /* offset==2 (no odd offsets) */
            break;
        }
    #else
        switch( offset ) {
          case 0:
            if( !CHR1(dword) )  break;
            len++;
            /* fall through */
          case 1:
            if( !CHR2(dword) )  break;
            len++;
            /* fall through */
          case 2:
            if( !CHR3(dword) )  break;
            len++;
            /* fall through */
          default:
            break;
        }
    #endif
#endif
        return( len );
    } else {
        len += ( BYTES_PER_WORD - offset ) / CHARSIZE;
    }

    /*** Scan one word at a time until a null char is found ***/
    for( ;; ) {
        dword = *dw++;
        if( GOT_NIL(dword) )
            break;

        dword = *dw++;
        if( GOT_NIL(dword) )
            break;
    }

    /*** Locate the null char within the offending word ***/
    len = (CHAR_TYPE*)dw - s;
#if USE_INT64
    if( !CHR1(dword) ) {
        len -= 8;
    } else if( !CHR2(dword) ) {
        len -= 7;
    } else if( !CHR3(dword) ) {
        len -= 6;
    } else if( !CHR4(dword) ) {
        len -= 5;
    } else if( !CHR5(dword) ) {
        len -= 4;
    } else if( !CHR6(dword) ) {
        len -= 3;
    } else if( !CHR7(dword) ) {
        len -= 2;
    } else {
        len -= 1;
    }
#else
  #ifdef __WIDECHAR__
    if( !CHR1(dword) ) {
        len -= 2;
    } else {
        len -= 1;
    }
  #else
    if( !CHR1(dword) ) {
        len -= 4;
    } else if( !CHR2(dword) ) {
        len -= 3;
    } else if( !CHR3(dword) ) {
        len -= 2;
    } else {
        len -= 1;
    }
  #endif
#endif

    return( len );
}
Ejemplo n.º 5
0
int __F_NAME(strnicmp,_wcsnicmp)( const CHAR_TYPE *s1, const CHAR_TYPE *s2,
                                  size_t n )
/*************************************************************************/
{
    RISC_DATA_LOCALREF;
    UINT                *dw1 = ROUND(s1);   // round down to dword
    UINT                *dw2 = ROUND(s2);
    UINT                dword1, dword2;
    INT                 shr1, shr2, shl1, shl2, char1, char2;
    INT                 tmpchar, tmpdword1, tmpdword2;

#ifdef __WIDECHAR__
    if( OFFSET(s1) % 2  ||  OFFSET(s2) % 2 ) {
        return( __simple__wcsnicmp( s1, s2, n ) );
    }
#endif

    /*** Initialize locals ***/
    shr1 = OFFSET(s1) << 3; // sift right  = offset * 8
    shr2 = OFFSET(s2) << 3;
    shl1 = INT_SIZE - shr1; // shift left = 32 - shift right
    shl2 = INT_SIZE - shr2;

    if( shr1 != 0 )
        dword1 = *dw1++;

    if( shr2 != 0 )
        dword2 = *dw2++;

    /*** Scan in aligned 4-byte groups ***/
    for( ;; ) {
        if( n == 0 )
            return( 0 );    /* nothing left to compare */

        if( shr1 == 0 ) {
            tmpdword1 = *dw1++;
        } else {
            tmpdword1 = dword1 >> shr1;
            dword1 = *dw1++;
            tmpdword1 |= (dword1 << shl1);
        }

        if( shr2 == 0 ) {
            tmpdword2 = *dw2++;
        } else {
            tmpdword2 = dword2 >> shr2;
            dword2 = *dw2++;
            tmpdword2 |= (dword2 << shl2);
        }

        if( n < CHARS_PER_WORD ) { // some chars can be set to 00
            tmpdword1 = FRONT_CHRS(tmpdword1,n);
            tmpdword2 = FRONT_CHRS(tmpdword2,n);
            break;
        }

        /*** Did s1 end already? ***/
        if( GOT_NIL(tmpdword1) )
            break;
        /*** Are s1 and s2 still the same? ***/
#ifdef __WIDECHAR__
        if( tmpdword1 != tmpdword2 ) {
            char1 = CHR1(tmpdword1);
            char2 = CHR1(tmpdword2);
            tmpchar = TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2);
            if( tmpchar )
                return( tmpchar );

            char1 = CHR2(tmpdword1);
            char2 = CHR2(tmpdword2);
            tmpchar = TO_LOW_CHR2(char1) - TO_LOW_CHR2(char2);
            if( tmpchar )
                return( tmpchar );
        }
#else
        if (tmpdword1 != tmpdword2) {
            char1 = CHR1(tmpdword1);
            char2 = CHR1(tmpdword2);
            tmpchar = TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2);
            if( tmpchar )
                return( tmpchar );

            char1 = CHR2(tmpdword1);
            char2 = CHR2(tmpdword2);
            tmpchar = TO_LOW_CHR2(char1) - TO_LOW_CHR2(char2);
            if( tmpchar )
                return( tmpchar );

            char1 = CHR3(tmpdword1);
            char2 = CHR3(tmpdword2);
            tmpchar = TO_LOW_CHR3(char1) - TO_LOW_CHR3(char2);
            if( tmpchar )
                return ( tmpchar );

            char1 = CHR4(tmpdword1);
            char2 = CHR4(tmpdword2);
            tmpchar = TO_LOW_CHR4(char1) - TO_LOW_CHR4(char2);
            if( tmpchar )
                return( tmpchar );
        }
#endif

        n -= CHARS_PER_WORD;
    }

    /*** Scan the last byte(s) in the string ***/
    if( tmpdword1 == tmpdword2 )
        return( 0 );

    /* we have a null char somewhere in the last dword */
#ifdef __WIDECHAR__
    char1 = CHR1(tmpdword1);
    char2 = CHR1(tmpdword2);
    if( char1 == 0 )                                /* 1st char is null */
        return( TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2) );

    tmpchar = TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2);  /* 2nd is null */
    if( tmpchar )
        return( tmpchar );
    char1 = CHR2(tmpdword1);
    char2 = CHR2(tmpdword2);
    return( TO_LOW_CHR2(char1) - TO_LOW_CHR2(char2) );
#else
    char1 = CHR1(tmpdword1);
    char2 = CHR1(tmpdword2);
    if( char1 == 0 )                // first char in the dword is null
        return( TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2) );

    if( CHR2(tmpdword1) == 0 ) {     // second char in the dword is null
        tmpchar = TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2);
        if( tmpchar )
            return( tmpchar) ;

        char1 = CHR2(tmpdword1);
        char2 = CHR2(tmpdword2);
        return( TO_LOW_CHR2(char1) - TO_LOW_CHR2(char2) );
    }

    if( CHR3(tmpdword1) == 0 ) {     // third char in the dword is null
        tmpchar = TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2);
        if( tmpchar )
            return( tmpchar) ;

        char1 = CHR2(tmpdword1);
        char2 = CHR2(tmpdword2);
        tmpchar = TO_LOW_CHR2(char1) - TO_LOW_CHR2(char2);
        if( tmpchar )
            return( tmpchar) ;

        char1 = CHR3(tmpdword1);
        char2 = CHR3(tmpdword2);
        return( TO_LOW_CHR3(char1) - TO_LOW_CHR3(char2) );
    }

    // 4th char in the dword is null
    tmpchar = TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2);
    if( tmpchar )
        return( tmpchar );

    char1 = CHR2(tmpdword1);
    char2 = CHR2(tmpdword2);
    tmpchar = TO_LOW_CHR2(char1) - TO_LOW_CHR2(char2);
    if( tmpchar )
        return ( tmpchar );

    char1 = CHR3(tmpdword1);
    char2 = CHR3(tmpdword2);
    tmpchar = TO_LOW_CHR3(char1) - TO_LOW_CHR3(char2);
    if( tmpchar)
        return( tmpchar );

    char1 = CHR4(tmpdword1);
    char2 = CHR4(tmpdword2);
    return( TO_LOW_CHR4(char1) - TO_LOW_CHR4(char2) );
#endif
}
Ejemplo n.º 6
0
CHAR_TYPE *__F_NAME(strchr,wcschr)( const CHAR_TYPE *s, INTCHAR_TYPE c )
/**********************************************************************/
{
#if USE_INT64
    RISC_DATA_LOCALREF;
#endif
    UINT                *dw = ROUND(s); /* round down to dword */
    UINT                dword, cdword, tmpdword;
    INT                 len = 0;
    int                 offset = OFFSET(s);
#ifdef __WIDECHAR__
    UINT                cShl16;
#else
    INT                 cShl8, cShl16, cShl24;
#if USE_INT64
    INT                 cShl32, cShl40, cShl48, cShl56;
#endif
#endif

#ifdef __WIDECHAR__
    if( offset % 2 )
        return( __simple_wcschr( s, c ) );
#endif

    /*** Initialize locals ***/
    c &= CHR1MASK;
#ifdef __WIDECHAR__
    cShl16 = c << 16;
    cdword = cShl16 | c;
#else
    cShl8 = c << 8;
    cShl16 = c << 16;
    cShl24 = c << 24;
  #if USE_INT64
    cShl32 = (UINT)c << 32;
    cShl40 = (UINT)c << 40;
    cShl48 = (UINT)c << 48;
    cShl56 = (UINT)c << 56;
    cdword = cShl56 | cShl48 | cShl40 | cShl32 | cShl24 | cShl16 | cShl8 | c;
  #else
    cdword = cShl24 | cShl16 | cShl8 | c;
  #endif
#endif

    /*** Scan any bytes up to a 4-byte alignment ***/
    if( offset != 0 ) {
        dword = *dw++;
#ifdef __WIDECHAR__
        tmpdword = CHR2(dword);
        if( tmpdword == cShl16 ) {
            return( (CHAR_TYPE*)s + len );
        } else if( tmpdword == 0 ) {
            return( NULL );
        }
        len++;
#else
        switch( offset ) {
          case 1:
            tmpdword = CHR2(dword);
            if( tmpdword == cShl8 ) {
                return( (CHAR_TYPE*)s + len );
            } else if( tmpdword == 0 ) {
                return( NULL );
            }
            len++;
            /* fall through */
          case 2:
            tmpdword = CHR3(dword);
            if( tmpdword == cShl16 ) {
                return( (CHAR_TYPE*)s + len );
            } else if( tmpdword == 0 ) {
                return( NULL );
            }
            len++;
            /* fall through */
        #if USE_INT64
          case 3:
            tmpdword = CHR4(dword);
            if( tmpdword == cShl24 ) {
                return( (CHAR_TYPE*)s + len );
            } else if( tmpdword == 0 ) {
                return( NULL );
            }
            len++;
            /* fall through */
          case 4:
            tmpdword = CHR5(dword);
            if( tmpdword == cShl32 ) {
                return( (CHAR_TYPE*)s + len );
            } else if( tmpdword == 0 ) {
                return( NULL );
            }
            len++;
            /* fall through */
          case 5:
            tmpdword = CHR6(dword);
            if( tmpdword == cShl40 ) {
                return( (CHAR_TYPE*)s + len );
            } else if( tmpdword == 0 ) {
                return( NULL );
            }
            len++;
            /* fall through */
          case 6:
            tmpdword = CHR7(dword);
            if( tmpdword == cShl48 ) {
                return( (CHAR_TYPE*)s + len );
            } else if( tmpdword == 0 ) {
                return( NULL );
            }
            len++;
            /* fall through */
          default:
            tmpdword = CHR8(dword);
            if( tmpdword == cShl56 ) {
                return( (CHAR_TYPE*)s + len );
            } else if( tmpdword == 0 ) {
                return( NULL );
            }
            len++;
        #else
          default:
            tmpdword = CHR4(dword);
            if( tmpdword == cShl24 ) {
                return( (CHAR_TYPE*)s + len );
            } else if( tmpdword == 0 ) {
                return( NULL );
            }
            len++;
        #endif
        }
#endif
    }

    /*** Scan in aligned 4-byte groups ***/
    for( ;; ) {
        dword = *dw++;
        if( GOT_NIL(dword) )
            break;
        tmpdword = dword ^ cdword;
        if( GOT_NIL(tmpdword) ) {
#ifdef __WIDECHAR__
            if( !CHR1(tmpdword) )  return( (CHAR_TYPE*)s + len );
            /* otherwise */        return( (CHAR_TYPE*)s + len + 1 );
#else
            if( !CHR1(tmpdword) )  return( (char*)s + len );
            if( !CHR2(tmpdword) )  return( (char*)s + len + 1 );
            if( !CHR3(tmpdword) )  return( (char*)s + len + 2 );
            if( !CHR4(tmpdword) )  return( (char*)s + len + 3 );
    #if USE_INT64
            if( !CHR5(tmpdword) )  return( (char*)s + len + 4 );
            if( !CHR6(tmpdword) )  return( (char*)s + len + 5 );
            if( !CHR7(tmpdword) )  return( (char*)s + len + 6 );
            if( !CHR8(tmpdword) )  return( (char*)s + len + 7 );
    #endif
#endif
        }
        len += CHARS_PER_WORD;
    }

    /*** Scan the last byte(s) in the string ***/
    tmpdword = CHR1(dword);
    if( tmpdword == c ) {
        return( (CHAR_TYPE*)s + len );
    } else if( tmpdword == 0 ) {
        return( NULL );
    }

#ifdef __WIDECHAR__
    tmpdword = CHR2(dword);
    if( tmpdword == cShl16 ) {
        return( (CHAR_TYPE*)s + len + 1 );
    } else if( tmpdword == 0 ) {
        return( NULL );
    }
#else
    tmpdword = CHR2(dword);
    if( tmpdword == cShl8 ) {
        return( (CHAR_TYPE*)s + len + 1 );
    } else if( tmpdword == 0 ) {
        return( NULL );
    }

    tmpdword = CHR3(dword);
    if( tmpdword == cShl16 ) {
        return( (CHAR_TYPE*)s + len + 2 );
    } else if( tmpdword == 0 ) {
        return( NULL );
    }

    tmpdword = CHR4(dword);
    if( tmpdword == cShl24 ) {
        return( (CHAR_TYPE*)s + len + 3 );
    } else if( tmpdword == 0 ) {
        return( NULL );
    }

  #if USE_INT64
    tmpdword = CHR5(dword);
    if( tmpdword == cShl32 ) {
        return( (CHAR_TYPE*)s + len + 4 );
    } else if( tmpdword == 0 ) {
        return( NULL );
    }

    tmpdword = CHR6(dword);
    if( tmpdword == cShl40 ) {
        return( (CHAR_TYPE*)s + len + 5 );
    } else if( tmpdword == 0 ) {
        return( NULL );
    }

    tmpdword = CHR7(dword);
    if( tmpdword == cShl48 ) {
        return( (CHAR_TYPE*)s + len + 6 );
    } else if( tmpdword == 0 ) {
        return( NULL );
    }

    tmpdword = CHR8(dword);
    if( tmpdword == cShl48 ) {
        return( (CHAR_TYPE*)s + len + 7 );
    } else if( tmpdword == 0 ) {
        return( NULL );
    }
  #endif
#endif

    return( NULL );
}
Ejemplo n.º 7
0
int memicmp( const void *s1, const void *s2, size_t n )
/*****************************************************/
{
    UINT *              dw1 = ROUND(s1);        /* round down to dword */
    UINT *              dw2 = ROUND(s2);        /* round down to dword */
    UINT                dword1, dword2, tmpdword1, tmpdword2;
    int                 tmpchar, shr1, shr2, shl1, shl2;
    INT                 char1, char2;

    if( n == 0 )  return( 0 );

    /*** Initialize locals ***/
    shr1 = OFFSET(s1) << 3;             /* shift right = offset * 8 */
    shr2 = OFFSET(s2) << 3;
    shl1 = INT_SIZE - shr1;             /* shift left = 32 - shift right */
    shl2 = INT_SIZE - shr2;

    if (shr1 != 0) {
        dword1 = *dw1++;
    }
    if (shr2 != 0) {
        dword2 = *dw2++;
    }

    /*** Scan in aligned 4-byte groups ***/
    for( ;; ) {
        /*** Extract the next few characters from each string ***/
        if( shr1 == 0 ) {
            tmpdword1 = *dw1++;
        } else {
            tmpdword1 = dword1 >> shr1;
            dword1 = *dw1++;
            tmpdword1 |= (dword1 << shl1);
        }
        if( shr2 == 0 ) {
            tmpdword2 = *dw2++;
        } else {
            tmpdword2 = dword2 >> shr2;
            dword2 = *dw2++;
            tmpdword2 |= (dword2 << shl2);
        }

        if( n < BYTES_PER_WORD )  break;

        /*** Are s1 and s2 still the same? ***/
        if( tmpdword1 != tmpdword2 ) {
            char1 = CHR1(tmpdword1);
            char2 = CHR1(tmpdword2);
            tmpchar = TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2);
            if( tmpchar )  return( tmpchar );

            char1 = CHR2(tmpdword1);
            char2 = CHR2(tmpdword2);
            tmpchar = TO_LOW_CHR2(char1) - TO_LOW_CHR2(char2);
            if( tmpchar )  return( tmpchar );

            char1 = CHR3(tmpdword1);
            char2 = CHR3(tmpdword2);
            tmpchar = TO_LOW_CHR3(char1) - TO_LOW_CHR3(char2);
            if( tmpchar )  return( tmpchar );

            char1 = CHR4(tmpdword1);
            char2 = CHR4(tmpdword2);
            tmpchar = TO_LOW_CHR4(char1) - TO_LOW_CHR4(char2);
            if( tmpchar )  return ( tmpchar );
        }
        n -= BYTES_PER_WORD;
    }

    /*** Scan the last byte(s) in the buffer ***/
    if( tmpdword1 == tmpdword2 )  return( 0 );
    if( n == 1 ) {
        char1 = CHR1(tmpdword1);
        char2 = CHR1(tmpdword2);
        return( TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2) );
    } else if( n == 2 ) {
        char1 = CHR1(tmpdword1);
        char2 = CHR1(tmpdword2);
        tmpchar = TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2);
        if( tmpchar )  return( tmpchar );
        char1 = CHR2(tmpdword1);
        char2 = CHR2(tmpdword2);
        return( TO_LOW_CHR2(char1) - TO_LOW_CHR2(char2) );
    } else if( n == 3 ) {
        char1 = CHR1(tmpdword1);
        char2 = CHR1(tmpdword2);
        tmpchar = TO_LOW_CHR1(char1) - TO_LOW_CHR1(char2);
        if( tmpchar )  return( tmpchar );
        char1 = CHR2(tmpdword1);
        char2 = CHR2(tmpdword2);
        tmpchar = TO_LOW_CHR2(char1) - TO_LOW_CHR2(char2);
        if( tmpchar )  return( tmpchar );
        char1 = CHR3(tmpdword1);
        char2 = CHR3(tmpdword2);
        return( TO_LOW_CHR3(char1) - TO_LOW_CHR3(char2) );
    }

    return( 0 );
}