Fix <CFString> and CF>string to allow null bytes
parent
fe7c2fecbe
commit
c6415c7b7d
|
@ -4,4 +4,4 @@ USING: tools.test core-foundation.attributed-strings
|
||||||
core-foundation ;
|
core-foundation ;
|
||||||
IN: core-foundation.attributed-strings.tests
|
IN: core-foundation.attributed-strings.tests
|
||||||
|
|
||||||
[ ] [ "Hello world" { } <CFAttributedString> CFRelease ] unit-test
|
[ ] [ "Hello world" H{ } <CFAttributedString> CFRelease ] unit-test
|
|
@ -12,7 +12,7 @@ FUNCTION: CFAttributedStringRef CFAttributedStringCreate (
|
||||||
CFDictionaryRef attributes
|
CFDictionaryRef attributes
|
||||||
) ;
|
) ;
|
||||||
|
|
||||||
: <CFAttributedString> ( string alist -- alien )
|
: <CFAttributedString> ( string assoc -- alien )
|
||||||
[
|
[
|
||||||
[ >cf &CFRelease ] bi@
|
[ >cf &CFRelease ] bi@
|
||||||
[ kCFAllocatorDefault ] 2dip CFAttributedStringCreate
|
[ kCFAllocatorDefault ] 2dip CFAttributedStringCreate
|
||||||
|
|
|
@ -1,21 +1,34 @@
|
||||||
! Copyright (C) 2006, 2008 Slava Pestov
|
! Copyright (C) 2006, 2008 Slava Pestov
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: alien.syntax alien.destructors accessors kernel ;
|
USING: alien.syntax alien.c-types alien.destructors accessors kernel ;
|
||||||
IN: core-foundation
|
IN: core-foundation
|
||||||
|
|
||||||
TYPEDEF: void* CFTypeRef
|
TYPEDEF: void* CFTypeRef
|
||||||
|
|
||||||
TYPEDEF: void* CFAllocatorRef
|
TYPEDEF: void* CFAllocatorRef
|
||||||
: kCFAllocatorDefault f ; inline
|
CONSTANT: kCFAllocatorDefault f
|
||||||
|
|
||||||
TYPEDEF: bool Boolean
|
TYPEDEF: bool Boolean
|
||||||
TYPEDEF: long CFIndex
|
TYPEDEF: long CFIndex
|
||||||
|
TYPEDEF: char UInt8
|
||||||
TYPEDEF: int SInt32
|
TYPEDEF: int SInt32
|
||||||
TYPEDEF: uint UInt32
|
TYPEDEF: uint UInt32
|
||||||
TYPEDEF: ulong CFTypeID
|
TYPEDEF: ulong CFTypeID
|
||||||
TYPEDEF: UInt32 CFOptionFlags
|
TYPEDEF: UInt32 CFOptionFlags
|
||||||
TYPEDEF: void* CFUUIDRef
|
TYPEDEF: void* CFUUIDRef
|
||||||
|
|
||||||
|
ALIAS: <CFIndex> <long>
|
||||||
|
ALIAS: *CFIndex *long
|
||||||
|
|
||||||
|
C-STRUCT: CFRange
|
||||||
|
{ "CFIndex" "location" }
|
||||||
|
{ "CFIndex" "length" } ;
|
||||||
|
|
||||||
|
: <CFRange> ( location length -- range )
|
||||||
|
"CFRange" <c-object>
|
||||||
|
[ set-CFRange-length ] keep
|
||||||
|
[ set-CFRange-location ] keep ;
|
||||||
|
|
||||||
FUNCTION: CFTypeRef CFRetain ( CFTypeRef cf ) ;
|
FUNCTION: CFTypeRef CFRetain ( CFTypeRef cf ) ;
|
||||||
|
|
||||||
FUNCTION: void CFRelease ( CFTypeRef cf ) ;
|
FUNCTION: void CFRelease ( CFTypeRef cf ) ;
|
||||||
|
|
|
@ -7,3 +7,5 @@ IN: core-foundation
|
||||||
[ "Hello" ] [ "Hello" <CFString> [ CF>string ] [ CFRelease ] bi ] unit-test
|
[ "Hello" ] [ "Hello" <CFString> [ CF>string ] [ CFRelease ] bi ] unit-test
|
||||||
[ "Hello\u003456" ] [ "Hello\u003456" <CFString> [ CF>string ] [ CFRelease ] bi ] unit-test
|
[ "Hello\u003456" ] [ "Hello\u003456" <CFString> [ CF>string ] [ CFRelease ] bi ] unit-test
|
||||||
[ "Hello\u013456" ] [ "Hello\u013456" <CFString> [ CF>string ] [ CFRelease ] bi ] unit-test
|
[ "Hello\u013456" ] [ "Hello\u013456" <CFString> [ CF>string ] [ CFRelease ] bi ] unit-test
|
||||||
|
[ ] [ "\0" <CFString> CFRelease ] unit-test
|
||||||
|
[ "\0" ] [ "\0" <CFString> [ CF>string ] [ CFRelease ] bi ] unit-test
|
|
@ -1,8 +1,8 @@
|
||||||
! Copyright (C) 2008, 2009 Slava Pestov.
|
! Copyright (C) 2008, 2009 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: alien.syntax alien.strings kernel sequences byte-arrays
|
USING: alien.syntax alien.strings io.encodings.string kernel
|
||||||
io.encodings.utf8 math core-foundation core-foundation.arrays
|
sequences byte-arrays io.encodings.utf8 math core-foundation
|
||||||
destructors ;
|
core-foundation.arrays destructors ;
|
||||||
IN: core-foundation.strings
|
IN: core-foundation.strings
|
||||||
|
|
||||||
TYPEDEF: void* CFStringRef
|
TYPEDEF: void* CFStringRef
|
||||||
|
@ -42,6 +42,17 @@ FUNCTION: Boolean CFStringGetCString (
|
||||||
CFStringEncoding encoding
|
CFStringEncoding encoding
|
||||||
) ;
|
) ;
|
||||||
|
|
||||||
|
FUNCTION: CFIndex CFStringGetBytes (
|
||||||
|
CFStringRef theString,
|
||||||
|
CFRange range,
|
||||||
|
CFStringEncoding encoding,
|
||||||
|
UInt8 lossByte,
|
||||||
|
Boolean isExternalRepresentation,
|
||||||
|
UInt8* buffer,
|
||||||
|
CFIndex maxBufLen,
|
||||||
|
CFIndex* usedBufLen
|
||||||
|
) ;
|
||||||
|
|
||||||
FUNCTION: CFStringRef CFStringCreateWithCString (
|
FUNCTION: CFStringRef CFStringCreateWithCString (
|
||||||
CFAllocatorRef alloc,
|
CFAllocatorRef alloc,
|
||||||
char* cStr,
|
char* cStr,
|
||||||
|
@ -49,16 +60,14 @@ FUNCTION: CFStringRef CFStringCreateWithCString (
|
||||||
) ;
|
) ;
|
||||||
|
|
||||||
: <CFString> ( string -- alien )
|
: <CFString> ( string -- alien )
|
||||||
f swap utf8 string>alien kCFStringEncodingUTF8 CFStringCreateWithCString
|
[ f ] dip utf8 encode dup length kCFStringEncodingUTF8 f CFStringCreateWithBytes
|
||||||
[ "CFStringCreateWithCString failed" throw ] unless* ;
|
[ "CFStringCreateWithBytes failed" throw ] unless* ;
|
||||||
|
|
||||||
: CF>string ( alien -- string )
|
: CF>string ( alien -- string )
|
||||||
dup CFStringGetLength 4 * 1 + <byte-array> [
|
dup CFStringGetLength
|
||||||
dup length
|
[ 0 swap <CFRange> kCFStringEncodingUTF8 0 f ] keep
|
||||||
kCFStringEncodingUTF8
|
4 * 1 + <byte-array> [ dup length 0 <CFIndex> [ CFStringGetBytes drop ] keep ] keep
|
||||||
CFStringGetCString
|
swap *CFIndex head-slice utf8 decode ;
|
||||||
[ "CFStringGetCString failed" throw ] unless
|
|
||||||
] keep utf8 alien>string ;
|
|
||||||
|
|
||||||
: CF>string-array ( alien -- seq )
|
: CF>string-array ( alien -- seq )
|
||||||
CF>array [ CF>string ] map ;
|
CF>array [ CF>string ] map ;
|
||||||
|
|
Loading…
Reference in New Issue