Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: strings.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 ============================================ | |
42 | id: @(#)strings.fth 1.4 03/09/18 | |
43 | purpose: | |
44 | copyright: Copyright 2001-2003 Sun Microsystems, Inc. All Rights Reserved | |
45 | copyright: Use is subject to license terms. | |
46 | ||
47 | headerless | |
48 | ||
49 | : upper ( adr len -- ) bounds ?do i dup c@ upc swap c! loop ; | |
50 | : lower ( adr len -- ) bounds ?do i dup c@ lcc swap c! loop ; | |
51 | ||
52 | : printable? ( n -- flag ) | |
53 | dup bl h# 7f within swap h# 80 h# ff between or | |
54 | ; | |
55 | ||
56 | : white-space? ( n -- flag ) \ true is n is non-printable? or a blank | |
57 | dup printable? 0= swap bl = or | |
58 | ; | |
59 | ||
60 | : -leading ( adr len -- adr' len' ) | |
61 | begin dup while ( adr' len' ) | |
62 | over c@ white-space? 0= if exit then | |
63 | swap 1+ swap 1- | |
64 | repeat | |
65 | ; | |
66 | ||
67 | : -trailing (s adr len -- adr len' ) | |
68 | dup 0 ?do 2dup + 1- c@ white-space? 0= ?leave 1- loop | |
69 | ; | |
70 | ||
71 | : $= ( adr1 len1 adr2 len2 -- same? ) | |
72 | rot tuck <> if 3drop false exit then ( adr1 adr2 len1 ) | |
73 | comp 0= | |
74 | ; | |
75 | ||
76 | : cstrlen ( c-string -- length ) | |
77 | dup begin dup c@ while ca1+ repeat swap - | |
78 | ; | |
79 | ||
80 | \ Split (non-destructive) a string after the last occurrence of the delimiter | |
81 | \ adra,lena is the string after the delimiter | |
82 | \ adrb,lenb is the string before and including the delimiter | |
83 | \ lenb = 0 if there was no delimiter | |
84 | ||
85 | : split-after ( adr len delimiter -- adra lena adrb lenb ) | |
86 | >r 2dup + 0 ( adrb lenb adra 0 ) | |
87 | ||
88 | begin 2 pick while ( adrb lenb adra lena ) | |
89 | over 1- c@ r@ = if \ Found it ( adrb lenb adra lena ) | |
90 | r> drop 2swap exit ( adra lena adrb lenb ) | |
91 | then | |
92 | 2swap 1- 2swap swap 1- swap 1+ ( adrb lenb adra lena ) | |
93 | repeat ( adrb lenb adra lena ) | |
94 | ||
95 | \ Character not found | |
96 | r> drop 2swap ( adra lena adrb lenb ) | |
97 | ; | |
98 | ||
99 | \ Split (non-destructive) a string before first occurence of the delimiter | |
100 | \ adra,lena is the string including and after the delimiter | |
101 | \ adrb,lenb is the string before the delimiter | |
102 | \ lena = 0 if there was no delimiter | |
103 | ||
104 | : split-before ( adr len delimiter -- adra lena adrb lenb ) | |
105 | >r over 0 2swap ( adrb lenb adra lena ) | |
106 | begin dup while ( adrb lenb adra lena ) | |
107 | over c@ r@ = if ( adrb lenb adra lena ) | |
108 | r> drop 2swap exit ( adra lena adrb lenb ) | |
109 | then | |
110 | 1- swap 1+ swap 2swap 1+ 2swap ( adrb lenb adra lena ) | |
111 | repeat ( adrb lenb adra lena ) | |
112 | ||
113 | \ Character not found. lena is 0 | |
114 | r> drop 2swap ( adra lena adrb lenb ) | |
115 | ; | |
116 | ||
117 | \ Concatenate 2 forth strings | |
118 | : $strcat ( src$ dest$ -- dest+src$ ) | |
119 | rot ( src dest dlen slen ) | |
120 | 2dup + >r ( src dest dlen slen ) ( r: tlen ) | |
121 | -rot ( src slen dest dlen ) ( r: tlen ) | |
122 | over >r ( src slen dest dlen ) ( r: tlen dest ) | |
123 | ca+ ( src slen dest+dlen ) ( r: tlen dest ) | |
124 | swap cmove ( ) ( r: tlen dest ) | |
125 | r> r> ( dest tlen ) | |
126 | 2dup ca+ 0 swap c! ( dest+src$ ) | |
127 | ; | |
128 | ||
129 | \ Concatenate a string with a packed string | |
130 | : $cat ( adr len pstr -- ) | |
131 | >r r@ count nip ( addr len len' ) ( r: pstr ) | |
132 | d# 255 swap - min ( addr len' ) ( r: pstr ) | |
133 | r@ count + ( adr len end-adr ) ( r: pstr ) | |
134 | swap dup >r ( adr endadr len ) ( r: pstr len ) | |
135 | cmove r> r> ( len pstr ) | |
136 | dup c@ rot + swap c! | |
137 | ; |