1
/* Copyright (c) 2007 Dmitry Xmelkov
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are met:
7
* Redistributions of source code must retain the above copyright
8
notice, this list of conditions and the following disclaimer.
9
* Redistributions in binary form must reproduce the above copyright
10
notice, this list of conditions and the following disclaimer in
11
the documentation and/or other materials provided with the
13
* Neither the name of the copyright holders nor the names of
14
contributors may be used to endorse or promote products derived
15
from this software without specific prior written permission.
17
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
POSSIBILITY OF SUCH DAMAGE. */
29
/* $Id: strcasestr.S,v 1.2.2.1 2007/03/31 13:23:16 dmix Exp $ */
33
/** \ingroup avr_string
34
\fn char *strcasestr(const char *s1, const char *s2)
36
The strcasestr() function finds the first occurrence of the
37
substring \p s2 in the string \p s1. This is like strstr(), except
38
that it ignores case of alphabetic symbols in searching for the
39
substring. (Glibc, GNU extension.)
41
\return The strcasestr() function returns a pointer to the beginning
42
of the substring, or \c NULL if the substring is not found. If \p s2
43
points to a string of zero length, the function returns \p s1. */
45
/** \ingroup avr_pgmspace
46
\fn char *strcasestr_P(const char *s1, PGM_P s2)
48
This funtion is similar to strcasestr() except that \p s2 is pointer
49
to a string in program space. */
51
#if !defined(__DOXYGEN__)
60
#define beg2 r21 /* begin of s2 (cashed): s2[0] */
61
#define ctmp r20 /* scratch arg of .Lcmp() */
62
#define csvd r0 /* second arg of .Lcmp(), nochanged */
65
# define strcasestr strcasestr_P
66
# define LOAD X_lpm /* may scratch r0 */
74
tst beg2 ; is str2 empty?
75
breq .L_ret ; return original string (req'd by standard)
76
X_movw s2_lo, ZL ; save: address of second s2 byte
79
mov csvd, beg2 ; Find first char
83
breq .L_nomatch ; end of s1
87
X_movw s1_lo, XL ; store return value (decrement is needed)
89
3: LOAD csvd, Z+ ; compare strings (csvd is r0)
91
breq .L_match ; end of s2
94
cpse ctmp, __zero_reg__ ; now Z==0
96
; (ctmp==0 && Z==0) || - end of s1
97
; (ctmp!=0 && Z==0) || - chars are not equal
98
; (Z==1) - chars are equal
101
X_movw ZL, s2_lo ; restore s2+1
102
; (ctmp==0 && Z==0) || - end of s2
103
; (ctmp!=0 && Z==0) - chars are not equal
104
cpse ctmp, __zero_reg__ ; if equal, then tail(s1) is less
105
rjmp 1b ; then strlen(s2)
111
sbiw s1_lo, 1 ; restore after post-increment
115
/* Compare 2 bytes ignoring a case of symbols.
117
Return: if (bytes are equal) Z==1, else (Z==0 && ctmp != 0)
120
.if ('a'-'A' - 0x20) | ('Z'-'A' - 25) | ('z'-'a' - 25)
123
.Lcmp: eor ctmp, csvd
124
breq 1f ; OK, bytes are equal
126
brne 1f ; bytes are different more than alpha case
127
; now caseless comparison
128
or ctmp, csvd ; ctmp = tolower(csvd)
129
subi ctmp, -(255 - 'z') ; shift a..z to 230..255
130
subi ctmp, 255 - ('z' - 'a')
131
brlo 1f ; branch, if not an alpha
136
#endif /* not __DOXYGEN__ */