| 
									
										
										
										
											2008-03-06 01:23:38 -05:00
										 |  |  | ! Copyright (C) 2008 Daniel Ehrenberg. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2013-11-24 19:08:26 -05:00
										 |  |  | USING: accessors byte-arrays byte-vectors io io.encodings | 
					
						
							|  |  |  | io.streams.byte-array io.streams.string kernel locals | 
					
						
							|  |  |  | sbufs sequences io.private io.encodings.ascii | 
					
						
							|  |  |  | io.encodings.binary io.encodings.private io.encodings.utf8 ;
 | 
					
						
							| 
									
										
										
										
											2008-03-05 18:41:25 -05:00
										 |  |  | IN: io.encodings.string | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 00:12:57 -04:00
										 |  |  | :: decode ( byte-array encoding -- string )
 | 
					
						
							| 
									
										
										
										
											2011-10-31 14:07:17 -04:00
										 |  |  |     encoding binary eq? [ byte-array ] [ | 
					
						
							| 
									
										
										
										
											2013-11-24 19:08:26 -05:00
										 |  |  |         byte-array byte-array? encoding ascii eq? and [ | 
					
						
							|  |  |  |             byte-array byte-array>string-fast | 
					
						
							|  |  |  |         ] [ | 
					
						
							|  |  |  |             byte-array encoding <byte-reader> :> reader | 
					
						
							| 
									
										
										
										
											2013-11-24 19:22:29 -05:00
										 |  |  |             byte-array length encoding guess-decoded-length <sbuf> :> buf | 
					
						
							| 
									
										
										
										
											2013-11-24 19:08:26 -05:00
										 |  |  |             [ reader stream-read1 dup ] [ buf push ] while drop
 | 
					
						
							| 
									
										
										
										
											2013-11-24 19:22:29 -05:00
										 |  |  |             buf "" like
 | 
					
						
							| 
									
										
										
										
											2013-11-24 19:08:26 -05:00
										 |  |  |         ] if
 | 
					
						
							| 
									
										
										
										
											2011-10-31 14:07:17 -04:00
										 |  |  |     ] if ; inline
 | 
					
						
							| 
									
										
										
										
											2008-03-05 20:12:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 00:12:57 -04:00
										 |  |  | :: encode ( string encoding -- byte-array )
 | 
					
						
							| 
									
										
										
										
											2011-10-31 14:07:17 -04:00
										 |  |  |     encoding binary eq? [ string ] [ | 
					
						
							| 
									
										
										
										
											2013-11-24 19:08:26 -05:00
										 |  |  |         string aux>> not encoding { ascii utf8 } member-eq? and [ | 
					
						
							|  |  |  |             string string>byte-array-fast | 
					
						
							|  |  |  |         ] [ | 
					
						
							|  |  |  |             string length encoding guess-encoded-length <byte-vector> :> vec | 
					
						
							|  |  |  |             string vec encoding <encoder> stream-write
 | 
					
						
							|  |  |  |             vec B{ } like
 | 
					
						
							|  |  |  |         ] if
 | 
					
						
							| 
									
										
										
										
											2011-10-31 14:07:17 -04:00
										 |  |  |     ] if ; inline
 |