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: memmem.S,v 1.1.2.1 2007/03/31 13:23:16 dmix Exp $ */
33
/** \ingroup avr_string
34
\fn void *memmem(const void *s1, size_t len1, const void *s2, size_t len2)
36
The memmem() function finds the start of the first occurrence of the
37
substring \p s2 of length \p len2 in the memory area \p s1 of length
40
\return The memmem() function returns a pointer to the beginning of
41
the substring, or \c NULL if the substring is not found. If \p len2
42
is zero, the function returns \p s1. */
44
/** \ingroup avr_pgmspace
45
\fn void *memmem_P(const void *s1, size_t len1, PGM_VOID_P s2, size_t len2)
47
The memmem_P() function is similar to memmem() except that \p s2 is
48
pointer to a string in program space. */
51
#if !defined(__DOXYGEN__)
64
#define beg2 r17 /* begin of s2: s2[0] */
65
#define c1 r16 /* char from s1[] */
66
#define c2 r0 /* char from s2[]: tuned for classic lpm instr. */
69
# define memmem memmem_P
76
cp len2_lo, __zero_reg__
77
cpc len2_hi, __zero_reg__
78
breq .L_ret ; s2[] is empty
83
add len2_lo, s2_lo ; len2 = &(s2[len2])
85
add len1_lo, s1_lo ; len1 = &(s1[len1])
89
LOAD beg2, Z+ ; beg2 = s2[0]
90
X_movw s2_lo, ZL ; save: address of s2[1]
92
1: X_movw XL, s1_lo ; goto to begin of s1[]
94
2: cp XL, len1_lo ; find first char that is matched
101
X_movw s1_lo, XL ; store address
104
3: cp ZL, len2_lo ; compare strings
106
brsh .L_match ; end of s2[] --> OK
109
brsh .L_nomatch ; s1[] tail is too short
120
sbiw s1_lo, 1 ; restore after post-increment
127
#endif /* not __DOXYGEN__ */