Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / obp / fm / lib / parses1.fth
CommitLineData
920dae64
AT
1\ ========== Copyright Header Begin ==========================================
2\
3\ Hypervisor Software File: parses1.fth
4\
5\ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
6\
7\ - Do no alter or remove copyright notices
8\
9\ - Redistribution and use of this software in source and binary forms, with
10\ or without modification, are permitted provided that the following
11\ conditions are met:
12\
13\ - Redistribution of source code must retain the above copyright notice,
14\ this list of conditions and the following disclaimer.
15\
16\ - Redistribution in binary form must reproduce the above copyright notice,
17\ this list of conditions and the following disclaimer in the
18\ documentation and/or other materials provided with the distribution.
19\
20\ Neither the name of Sun Microsystems, Inc. or the names of contributors
21\ may be used to endorse or promote products derived from this software
22\ without specific prior written permission.
23\
24\ This software is provided "AS IS," without a warranty of any kind.
25\ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
26\ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
27\ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
28\ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
29\ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
30\ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
31\ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
32\ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
33\ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
34\ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
35\ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
36\
37\ You acknowledge that this software is not designed, licensed or
38\ intended for use in the design, construction, operation or maintenance of
39\ any nuclear facility.
40\
41\ ========== Copyright Header End ============================================
42id: @(#)parses1.fth 2.9 07/01/22
43purpose:
44copyright: Copyright 1985-1990 Bradley Forthware
45copyright: Copyright 2007 Sun Microsystems, Inc. All rights reserved.
46copyright: Use is subject to license terms.
47
48
49
50headers
51: +string ( adr len -- adr len+1 ) 1+ ;
52: -string ( adr len -- adr+1 len-1 ) swap 1+ swap 1- ;
53
54\ Splits a string into two halves before the first occurrence of
55\ a delimiter character.
56\ adra,lena is the string including and after the delimiter
57\ adrb,lenb is the string before the delimiter
58\ lena = 0 if there was no delimiter
59
60: split-before ( adr len delim -- adra lena adrb lenb )
61 split-string 2swap
62;
63alias $split left-parse-string
64
65: cindex ( adr len char -- [ index true ] | false )
66 false swap 2swap bounds ?do ( false char )
67 dup i c@ = if nip i true rot leave then
68 loop ( false char | index true char )
69 drop
70;
71
72\ Splits a string into two halves after the last occurrence of
73\ a delimiter character.
74\ adra,lena is the string after the delimiter
75\ adrb,lenb is the string before and including the delimiter
76\ lena = 0 if there was no delimiter
77
78\ adra,lena is the string after the delimiter
79\ adrb,lenb is the string before and including the delimiter
80\ lena = 0 if there was no delimiter
81
82: split-after ( adr len char -- adra lena adrb lenb )
83 >r 2dup + 0 ( adrb lenb adra 0 )
84
85 \ Throughout the loop, we maintain both substrings. Each time through,
86 \ we add a character to the "after" string and remove it from the "before".
87 \ The loop terminates when either the "before" string is empty or the
88 \ desired character is found
89
90 begin 2 pick while ( adrb lenb adra lena )
91 over 1- c@ r@ = if \ Found it ( adrb lenb adra lena )
92 r> drop 2swap exit ( adrb lenb adra lena )
93 then
94 2swap 1- 2swap swap 1- swap 1+ ( adrb lenb adra lena )
95 repeat ( adrb lenb adr1 len1 )
96
97 \ Character not found. lenb is 0.
98 r> drop 2swap
99;
100
101\ Count # of words in the specified string
102\ Example:
103\ " " --> 0
104\ " ab cdef" --> 2
105\ " ab cdef " --> 2
106: count-words ( adr len -- n )
107 0 >r ( ) ( r: count )
108 begin ( ) ( r: count )
109 -leading bl left-parse-string nip 0> ( remain$ len>0? ) ( r: count )
110 while ( remain$ ) ( r: count )
111 r> 1+ >r ( remain$ ) ( r: count )
112 repeat ( adr 0 ) ( r: count )
113 2drop r> ( count )
114;
115
116headers
117