1
0
mirror of https://github.com/adtools/clib2.git synced 2025-12-08 14:59:05 +00:00
Files
amiga-clib2/library/string_memchr.c
Olaf Barthel 1550c6fef3 - When using the wildcard expansion code for command line
parameters (which is by default linked in with libunix.a),
  regular expressions can no longer prompt dos.library requesters
  to appear. However, to be on the safe side, if you are expecting
  to pass regular expressions on the command line, do not use
  the wildcard expansion code such as by overriding the library
  symbols with dummy functions such as are used in the file
  "stdlib_wildcard_expand.c".

- Added a new variable '__open_locale' which can be used to
  restrict all library functions to use the "C" language locale
  rather than the current system locale settings. In addition
  to that, two new functions __locale_exit() and __locale_init()
  can be used to close and (re-)open the system locale at a
  later time.

- Local ("static") functions are now identified by the STATIC
  qualifier. This was done in preparation for changes that will
  deal with global and local data and the issue of thread safety.


git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14839 87f5fb63-7c3d-0410-a384-fd976d0f7a62
2005-02-25 10:14:22 +00:00

220 lines
5.1 KiB
C

/*
* $Id: string_memchr.c,v 1.5 2005-02-25 10:14:21 obarthel Exp $
*
* :ts=4
*
* Portable ISO 'C' (1994) runtime library for the Amiga computer
* Copyright (c) 2002-2005 by Olaf Barthel <olsen@sourcery.han.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of Olaf Barthel nor the names of contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _STDLIB_NULL_POINTER_CHECK_H
#include "stdlib_null_pointer_check.h"
#endif /* _STDLIB_NULL_POINTER_CHECK_H */
/****************************************************************************/
#ifndef _STRING_HEADERS_H
#include "string_headers.h"
#endif /* _STRING_HEADERS_H */
/****************************************************************************/
/* Check if one of the four bytes which make up a long word is zero. */
#define LONG_CONTAINS_ZERO_OCTET(x) (((x) + 0xfefefeff) & ~((x) | 0x7f7f7f7f))
/****************************************************************************/
INLINE STATIC void *
__memchr(const unsigned char * m,unsigned char val,size_t len)
{
void * result = NULL;
assert( m != NULL && len > 0 );
/* The setup below is intended to speed up searching in larger
* memory blocks. This can be very elaborate and should not be
* done unless a payoff can be expected.
*/
if(len > 4 * sizeof(long))
{
/* Try to align the memory block to an even address. */
if(IS_UNALIGNED(m))
{
len--;
if((*m) == val)
{
result = (void *)m;
goto out;
}
m++;
}
/* Try to align the memory block to an address which is
* a multiple of a long word.
*/
if(len >= sizeof(short) && IS_SHORT_ALIGNED(m))
{
len--;
if((*m) == val)
{
result = (void *)m;
goto out;
}
m++;
len--;
if((*m) == val)
{
result = (void *)m;
goto out;
}
m++;
}
/* If the memory block is aligned to an address from which
* data can be read one long word at a time, perform the
* search in this manner.
*/
if(len >= sizeof(long) && IS_LONG_ALIGNED(m))
{
const unsigned long * _m = (const unsigned long *)m;
unsigned long _val = val;
unsigned long x;
/* Build a long word which contains the byte value to
* find, repeated four times.
*/
_val |= (_val << 8);
_val |= (_val << 16);
do
{
/* Technically, what we want to achieve is to look
* at a single long word and be able to tell whether
* it contains the value we are looking for in one
* of the octets which it consists of. This is
* achieved by an XOR operation which sets those
* octets to zero which match the search value. The
* result of this operation is then tested to see
* whether it contains any zero octets.
*/
x = (*_m) ^ _val;
if(LONG_CONTAINS_ZERO_OCTET(x))
{
/* We got what we wanted. Now figure out which byte
* would match the value we were looking for.
*/
m = (const unsigned char *)_m;
goto out;
}
_m++;
len -= sizeof(long);
}
while(len >= sizeof(long));
m = (const unsigned char *)_m;
}
}
out:
/* If there are bytes left in need of comparison, take
* care of them here. This also includes 'aborted'
* comparison attempts from above.
*/
while(len-- > 0)
{
if((*m) == val)
{
result = (void *)m;
break;
}
m++;
}
return(result);
}
/****************************************************************************/
void *
memchr(const void * ptr, int val, size_t len)
{
const unsigned char * m = ptr;
void * result = NULL;
assert( ptr != NULL );
assert( (int)len >= 0 );
#if defined(CHECK_FOR_NULL_POINTERS)
{
if(ptr == NULL)
{
__set_errno(EFAULT);
goto out;
}
}
#endif /* CHECK_FOR_NULL_POINTERS */
if(len > 0)
{
#if 0
{
val &= 255;
do
{
if((*m) == val)
{
result = (void *)m;
break;
}
m++;
}
while(--len > 0);
}
#else
{
result = __memchr(m,(unsigned char)(val & 255),len);
}
#endif
}
out:
return(result);
}