| 
									
										
										
										
											2010-05-23 03:07:47 -04:00
										 |  |  | ! Copyright (C) 2008, 2010 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2013-03-29 14:31:39 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | USING: accessors alien alien.c-types alien.data alien.syntax | 
					
						
							|  |  |  | byte-vectors combinators.short-circuit core-foundation | 
					
						
							| 
									
										
										
										
											2012-06-01 20:56:34 -04:00
										 |  |  | core-foundation.arrays core-foundation.data destructors fry | 
					
						
							|  |  |  | io.encodings.string io.encodings.utf8 kernel math math.order | 
					
						
							|  |  |  | parser sequences words ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | IN: core-foundation.strings | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TYPEDEF: void* CFStringRef | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TYPEDEF: int CFStringEncoding | 
					
						
							| 
									
										
										
										
											2011-11-23 21:49:33 -05:00
										 |  |  | CONSTANT: kCFStringEncodingMacRoman 0x0
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingWindowsLatin1 0x0500
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingISOLatin1 0x0201
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingNextStepLatin 0x0B01
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingASCII 0x0600
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingUnicode 0x0100
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingUTF8 0x08000100
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingNonLossyASCII 0x0BFF
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingUTF16 0x0100
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingUTF16BE 0x10000100
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingUTF16LE 0x14000100
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingUTF32 0x0c000100
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingUTF32BE 0x18000100
 | 
					
						
							|  |  |  | CONSTANT: kCFStringEncodingUTF32LE 0x1c000100
 | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | FUNCTION: CFStringRef CFStringCreateWithBytes (
 | 
					
						
							| 
									
										
										
										
											2008-12-18 19:09:22 -05:00
										 |  |  |     CFAllocatorRef alloc, | 
					
						
							|  |  |  |     UInt8* bytes, | 
					
						
							|  |  |  |     CFIndex numBytes, | 
					
						
							|  |  |  |     CFStringEncoding encoding, | 
					
						
							|  |  |  |     Boolean isExternalRepresentation | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | FUNCTION: CFIndex CFStringGetLength ( CFStringRef theString ) | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | FUNCTION: void CFStringGetCharacters ( void* theString, CFIndex start, CFIndex length, void* buffer ) | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | FUNCTION: Boolean CFStringGetCString (
 | 
					
						
							| 
									
										
										
										
											2008-12-18 19:09:22 -05:00
										 |  |  |     CFStringRef theString, | 
					
						
							| 
									
										
										
										
											2010-02-23 15:53:09 -05:00
										 |  |  |     UInt8* buffer, | 
					
						
							| 
									
										
										
										
											2008-12-18 19:09:22 -05:00
										 |  |  |     CFIndex bufferSize, | 
					
						
							|  |  |  |     CFStringEncoding encoding | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-25 20:02:59 -05:00
										 |  |  | FUNCTION: CFIndex CFStringGetBytes (
 | 
					
						
							|  |  |  |    CFStringRef theString, | 
					
						
							|  |  |  |    CFRange range, | 
					
						
							|  |  |  |    CFStringEncoding encoding, | 
					
						
							|  |  |  |    UInt8 lossByte, | 
					
						
							|  |  |  |    Boolean isExternalRepresentation, | 
					
						
							|  |  |  |    UInt8* buffer, | 
					
						
							|  |  |  |    CFIndex maxBufLen, | 
					
						
							|  |  |  |    CFIndex* usedBufLen | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2009-01-25 20:02:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | FUNCTION: CFStringRef CFStringCreateWithCString (
 | 
					
						
							| 
									
										
										
										
											2008-12-18 19:09:22 -05:00
										 |  |  |     CFAllocatorRef alloc, | 
					
						
							| 
									
										
										
										
											2010-02-23 15:53:09 -05:00
										 |  |  |     UInt8* cStr, | 
					
						
							| 
									
										
										
										
											2008-12-18 19:09:22 -05:00
										 |  |  |     CFStringEncoding encoding | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | FUNCTION: CFStringRef CFCopyDescription ( CFTypeRef cf ) | 
					
						
							|  |  |  | FUNCTION: CFStringRef CFCopyTypeIDDescription ( CFTypeID type_id ) | 
					
						
							| 
									
										
										
										
											2011-07-29 16:24:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-02 14:44:44 -05:00
										 |  |  | : prepare-CFString ( string -- byte-array )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2012-06-01 20:56:34 -04:00
										 |  |  |         dup { [ 0x10ffff > ] [ 0xd800 0xdfff between? ] } 1|| | 
					
						
							| 
									
										
										
										
											2011-11-23 21:49:33 -05:00
										 |  |  |         [ drop 0xfffd ] when
 | 
					
						
							| 
									
										
										
										
											2012-07-18 01:54:08 -04:00
										 |  |  |     ] map! utf8 encode ;
 | 
					
						
							| 
									
										
										
										
											2009-02-02 14:44:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | : <CFString> ( string -- alien )
 | 
					
						
							| 
									
										
										
										
											2009-02-02 14:44:44 -05:00
										 |  |  |     [ f ] dip
 | 
					
						
							|  |  |  |     prepare-CFString dup length
 | 
					
						
							|  |  |  |     kCFStringEncodingUTF8 f
 | 
					
						
							|  |  |  |     CFStringCreateWithBytes | 
					
						
							| 
									
										
										
										
											2009-01-25 20:02:59 -05:00
										 |  |  |     [ "CFStringCreateWithBytes failed" throw ] unless* ;
 | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : CF>string ( alien -- string )
 | 
					
						
							| 
									
										
										
										
											2009-01-25 20:02:59 -05:00
										 |  |  |     dup CFStringGetLength | 
					
						
							|  |  |  |     [ 0 swap <CFRange> kCFStringEncodingUTF8 0 f ] keep
 | 
					
						
							| 
									
										
										
										
											2013-03-29 14:31:39 -04:00
										 |  |  |     4 * 1 + <byte-vector> [ | 
					
						
							|  |  |  |         underlying>> dup length
 | 
					
						
							| 
									
										
										
										
											2010-07-16 17:32:05 -04:00
										 |  |  |         { CFIndex } [ CFStringGetBytes drop ] with-out-parameters | 
					
						
							| 
									
										
										
										
											2013-03-29 14:31:39 -04:00
										 |  |  |     ] keep swap >>length utf8 decode ;
 | 
					
						
							| 
									
										
										
										
											2008-12-12 01:33:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : CF>string-array ( alien -- seq )
 | 
					
						
							|  |  |  |     CF>array [ CF>string ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <CFStringArray> ( seq -- alien )
 | 
					
						
							| 
									
										
										
										
											2009-01-19 17:29:52 -05:00
										 |  |  |     [ [ <CFString> &CFRelease ] map <CFArray> ] with-destructors ;
 | 
					
						
							| 
									
										
										
										
											2009-09-12 20:43:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-29 16:24:10 -04:00
										 |  |  | : CF>description ( cf -- description )
 | 
					
						
							|  |  |  |     [ CFCopyDescription &CFRelease CF>string ] with-destructors ;
 | 
					
						
							|  |  |  | : CFType>description ( cf -- description )
 | 
					
						
							|  |  |  |     CFGetTypeID [ CFCopyTypeIDDescription &CFRelease CF>string ] with-destructors ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  | SYNTAX: CFSTRING: | 
					
						
							|  |  |  |     scan-new-word scan-object | 
					
						
							| 
									
										
										
										
											2009-09-12 20:43:23 -04:00
										 |  |  |     [ drop ] [ '[ _ [ _ <CFString> ] initialize-alien ] ] 2bi
 | 
					
						
							| 
									
										
										
										
											2011-10-18 16:18:42 -04:00
										 |  |  |     ( -- alien ) define-declared ;
 |