File: strsep.S

package info (click to toggle)
avr-libc 1%3A1.4.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,324 kB
  • ctags: 25,560
  • sloc: ansic: 31,105; asm: 5,266; sh: 3,525; makefile: 1,837; pascal: 558; python: 45
file content (108 lines) | stat: -rwxr-xr-x 3,730 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Copyright (c) 2003, Reiner Patommel
   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.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the copyright holders 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.

 */

/* $Id: strsep.S,v 1.5 2005/11/02 22:08:03 aesok Exp $ */

 /** \ingroup avr_string
     \fn char *strsep(char **string, const char *delim)
     \brief Parse a string into tokens.
 
     The strsep() function locates, in the string referenced by *string,
     the first occurrence of any character in the string delim (or the
     terminating '\\0' character) and replaces it with a '\\0'.  The location
     of the next character after the delimiter character (or NULL, if the
     end of the string was reached) is stored in *string. An ``empty''
     field, i.e. one caused by two adjacent delimiter characters, can be
     detected by comparing the location referenced by the pointer returned
     in *string to '\\0'.
   
     \returns The strtok_r() function returns a pointer to the original
     value of *string.  If *stringp is initially NULL, strsep() returns NULL.
 */
     
#if !defined(__DOXYGEN__)

#include "macros.inc"

#define p_str_hi	r25
#define p_str_lo	r24
#define del_hi		r23
#define del_lo		r22
#define str_hi		r21
#define str_lo		r20
#define str_c		r19
#define del_c		r18

	.text
	.global _U(strsep)
	.type	_U(strsep),@function

_U(strsep):				; Check on NULL pointers
	X_movw	XL, p_str_lo
	LD	str_lo, X+
	LD	str_hi, X		; str = *p_str
	CP	str_lo, __zero_reg__
	CPC	str_hi, __zero_reg__	; str == NULL ?
	BRNE	.L_str_scan_init
	CLR	p_str_lo
	CLR	p_str_hi
	RET				; return(NULL)

.L_str_scan_init:			; scan string
	X_movw	XL, str_lo		; X = str

.L_scan_str_loop:			; next str char, 1st del char
	LD	str_c, X+		; str_c = *str++
	X_movw	ZL, del_lo		; Z = del

.L_scan_del_loop:
	LD	del_c, Z+		; del_c = *Z
	CP	del_c, str_c		; if (del_c == str_c)
	BRNE	3f
	TST	str_c			; end of str ?
	BRNE	1f
	CLR	XL
	CLR	XH			; str = NULL
	RJMP	2f
1:	ST	-X, __zero_reg__	; str[-1] = \0
	ADIW	XL, 1			; undo auto decrement
2:	X_movw	ZL, p_str_lo
	ST	Z+, XL
	ST	Z, XH			; *string = str
	X_movw	p_str_lo, str_lo 
	RET				; return(token)
3:	TST	del_c			; end of del string ?
	BRNE	.L_scan_del_loop	; next del char, same str_char
	RJMP	.L_scan_str_loop	; next str char, 1st del char

.L_strsep_end:
	.size	_U(strsep), .L_strsep_end - _U(strsep)

#endif /* not __DOXYGEN__ */