| 
									
										
										
										
											2008-01-28 19:15:21 -05:00
										 |  |  | ! Copyright (C) 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: arrays kernel kernel.private math sequences | 
					
						
							| 
									
										
										
										
											2008-12-08 14:58:57 -05:00
										 |  |  | sequences.private growable byte-arrays accessors parser | 
					
						
							| 
									
										
										
										
											2008-12-08 15:58:00 -05:00
										 |  |  | prettyprint.custom ;
 | 
					
						
							| 
									
										
										
										
											2008-01-28 19:15:21 -05:00
										 |  |  | IN: byte-vectors | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  | TUPLE: byte-vector | 
					
						
							| 
									
										
										
										
											2008-06-29 22:37:57 -04:00
										 |  |  | { underlying byte-array } | 
					
						
							|  |  |  | { length array-capacity } ;
 | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-28 19:15:21 -05:00
										 |  |  | : <byte-vector> ( n -- byte-vector )
 | 
					
						
							| 
									
										
										
										
											2008-12-05 08:28:52 -05:00
										 |  |  |     (byte-array) 0 byte-vector boa ; inline
 | 
					
						
							| 
									
										
										
										
											2008-01-28 19:15:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-20 01:18:27 -04:00
										 |  |  | : >byte-vector ( seq -- byte-vector )
 | 
					
						
							|  |  |  |     T{ byte-vector f B{ } 0 } clone-like ;
 | 
					
						
							| 
									
										
										
										
											2008-01-28 19:15:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: byte-vector like | 
					
						
							|  |  |  |     drop dup byte-vector? [ | 
					
						
							|  |  |  |         dup byte-array? | 
					
						
							| 
									
										
										
										
											2008-07-01 21:19:03 -04:00
										 |  |  |         [ dup length byte-vector boa ] [ >byte-vector ] if
 | 
					
						
							| 
									
										
										
										
											2008-01-28 19:15:21 -05:00
										 |  |  |     ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-13 13:54:58 -04:00
										 |  |  | M: byte-vector new-sequence | 
					
						
							| 
									
										
										
										
											2008-12-05 08:28:52 -05:00
										 |  |  |     drop [ (byte-array) ] [ >fixnum ] bi byte-vector boa ;
 | 
					
						
							| 
									
										
										
										
											2008-01-28 19:15:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: byte-vector equal? | 
					
						
							|  |  |  |     over byte-vector? [ sequence= ] [ 2drop f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-05 07:37:19 -05:00
										 |  |  | M: byte-array like | 
					
						
							|  |  |  |     #! If we have an byte-array, we're done. | 
					
						
							|  |  |  |     #! If we have a byte-vector, and it's at full capacity, | 
					
						
							|  |  |  |     #! we're done. Otherwise, call resize-byte-array, which is a | 
					
						
							|  |  |  |     #! relatively fast primitive. | 
					
						
							|  |  |  |     drop dup byte-array? [ | 
					
						
							|  |  |  |         dup byte-vector? [ | 
					
						
							|  |  |  |             [ length ] [ underlying>> ] bi
 | 
					
						
							|  |  |  |             2dup length eq?
 | 
					
						
							|  |  |  |             [ nip ] [ resize-byte-array ] if
 | 
					
						
							|  |  |  |         ] [ >byte-array ] if
 | 
					
						
							|  |  |  |     ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-30 02:40:22 -05:00
										 |  |  | M: byte-array new-resizable drop <byte-vector> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  | SYNTAX: BV{ \ } [ >byte-vector ] parse-literal ;
 | 
					
						
							| 
									
										
										
										
											2008-12-08 14:58:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: byte-vector pprint* pprint-object ;
 | 
					
						
							|  |  |  | M: byte-vector pprint-delims drop \ BV{ \ } ;
 | 
					
						
							|  |  |  | M: byte-vector >pprint-sequence ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-28 19:15:21 -05:00
										 |  |  | INSTANCE: byte-vector growable |