comparison packages/language/c/libc/current/src/string/strchr.cxx @ 0:3111d98ba7b3 ecos-v1_1-release

Initial commit of eCos version 1.1
author jlarmour
date Tue, 11 May 1999 11:16:07 +0000
parents
children 443894e2e912
comparison
equal deleted inserted replaced
-1:000000000000 0:3111d98ba7b3
1 //===========================================================================
2 //
3 // strchr.cxx
4 //
5 // ANSI standard strchr() routine
6 //
7 //===========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Cygnus eCos Public License
12 // Version 1.0 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://sourceware.cygnus.com/ecos
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Cygnus Operating System, released
22 // September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Cygnus. Portions created
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved.
26 // -------------------------------------------
27 //
28 //####COPYRIGHTEND####
29 //===========================================================================
30 //#####DESCRIPTIONBEGIN####
31 //
32 // Author(s): jlarmour
33 // Contributors: jlarmour@cygnus.co.uk
34 // Date: 1998-02-13
35 // Purpose:
36 // Description:
37 // Usage:
38 //
39 //####DESCRIPTIONEND####
40 //
41 //===========================================================================
42
43 // CONFIGURATION
44
45 #include <pkgconf/libc.h> // Configuration header
46
47 // Include the C library?
48 #ifdef CYGPKG_LIBC
49
50 // INCLUDES
51
52 #include <cyg/infra/cyg_type.h> // Common type definitions
53 #include <cyg/infra/cyg_trac.h> // Tracing support
54 #include <cyg/infra/cyg_ass.h> // Assertion support
55 #include <string.h> // Header for this file
56 #include <stddef.h> // Compiler definitions such as size_t, NULL etc.
57 #include "clibincl/stringsupp.hxx" // Useful string function support and
58 // prototypes
59
60 // EXPORTED SYMBOLS
61
62 externC char *
63 strchr( const char *s, int c ) CYGPRI_LIBC_WEAK_ALIAS("_strchr");
64
65 // MACROS
66
67 // DETECTCHAR returns nonzero if X contains the byte used
68 // to fill MASK.
69 #define DETECTCHAR(X,MASK) (CYG_LIBC_STR_DETECTNULL( (X) ^ (MASK) ))
70
71 // FUNCTIONS
72
73 char *
74 _strchr( const char *s, int c )
75 {
76 CYG_REPORT_FUNCNAMETYPE( "_strchr", "returning %08x" );
77 CYG_REPORT_FUNCARG2( "s=%08x, c=%d", s, c );
78
79 CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
80
81 #if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__)
82 char charc = c;
83
84 while (*s && *s != charc)
85 {
86 s++;
87 }
88
89 if (*s != charc)
90 {
91 s = NULL;
92 }
93
94 CYG_REPORT_RETVAL( s );
95
96 return (char *) s;
97 #else
98 char charc = c;
99 CYG_WORD mask;
100 CYG_WORD j;
101 CYG_WORD *aligned_addr;
102
103 if (CYG_LIBC_STR_UNALIGNED (s))
104 {
105 while (*s && *s != charc)
106 s++;
107 if (*s != charc)
108 s = NULL;
109
110 CYG_REPORT_RETVAL( s );
111
112 return (char *) s;
113 }
114
115 for (j = 0, mask = 0; j < sizeof(mask); j++)
116 mask = (mask << 8) + charc;
117
118 aligned_addr = (CYG_WORD *)s;
119 while (!CYG_LIBC_STR_DETECTNULL (*aligned_addr) &&
120 !DETECTCHAR (*aligned_addr, mask))
121 aligned_addr++;
122
123 // The block of bytes currently pointed to by aligned_addr
124 // contains either a null or the target char, or both. We
125 // catch it using the bytewise search.
126
127 s = (char *)aligned_addr;
128 while (*s && *s != charc)
129 s++;
130 if (*s != charc)
131 s = NULL;
132
133 CYG_REPORT_RETVAL( s );
134
135 return (char *)s;
136 #endif // not defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) ||
137 // defined(__OPTIMIZE_SIZE__)
138 } // _strchr()
139
140 #endif // ifdef CYGPKG_LIBC
141
142 // EOF strchr.cxx