]> gitweb.factorcode.org Git - factor.git/blob - basis/core-foundation/strings/strings.factor
ab81087ab778042ec1735004402f2c005c10e9da
[factor.git] / basis / core-foundation / strings / strings.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3
4 USING: accessors alien alien.c-types alien.data alien.syntax
5 byte-vectors combinators.short-circuit core-foundation
6 core-foundation.arrays core-foundation.data destructors fry
7 io.encodings.string io.encodings.utf8 kernel math math.order
8 parser sequences words ;
9
10 IN: core-foundation.strings
11
12 TYPEDEF: void* CFStringRef
13
14 TYPEDEF: int CFStringEncoding
15 CONSTANT: kCFStringEncodingMacRoman 0x0
16 CONSTANT: kCFStringEncodingWindowsLatin1 0x0500
17 CONSTANT: kCFStringEncodingISOLatin1 0x0201
18 CONSTANT: kCFStringEncodingNextStepLatin 0x0B01
19 CONSTANT: kCFStringEncodingASCII 0x0600
20 CONSTANT: kCFStringEncodingUnicode 0x0100
21 CONSTANT: kCFStringEncodingUTF8 0x08000100
22 CONSTANT: kCFStringEncodingNonLossyASCII 0x0BFF
23 CONSTANT: kCFStringEncodingUTF16 0x0100
24 CONSTANT: kCFStringEncodingUTF16BE 0x10000100
25 CONSTANT: kCFStringEncodingUTF16LE 0x14000100
26 CONSTANT: kCFStringEncodingUTF32 0x0c000100
27 CONSTANT: kCFStringEncodingUTF32BE 0x18000100
28 CONSTANT: kCFStringEncodingUTF32LE 0x1c000100
29
30 FUNCTION: CFStringRef CFStringCreateWithBytes (
31     CFAllocatorRef alloc,
32     UInt8* bytes,
33     CFIndex numBytes,
34     CFStringEncoding encoding,
35     Boolean isExternalRepresentation
36 ) ;
37
38 FUNCTION: CFIndex CFStringGetLength ( CFStringRef theString ) ;
39
40 FUNCTION: void CFStringGetCharacters ( void* theString, CFIndex start, CFIndex length, void* buffer ) ;
41
42 FUNCTION: Boolean CFStringGetCString (
43     CFStringRef theString,
44     UInt8* buffer,
45     CFIndex bufferSize,
46     CFStringEncoding encoding
47 ) ;
48
49 FUNCTION: CFIndex CFStringGetBytes (
50    CFStringRef theString,
51    CFRange range,
52    CFStringEncoding encoding,
53    UInt8 lossByte,
54    Boolean isExternalRepresentation,
55    UInt8* buffer,
56    CFIndex maxBufLen,
57    CFIndex* usedBufLen
58 ) ;
59
60 FUNCTION: CFStringRef CFStringCreateWithCString (
61     CFAllocatorRef alloc,
62     UInt8* cStr,
63     CFStringEncoding encoding
64 ) ;
65
66 FUNCTION: CFStringRef CFCopyDescription ( CFTypeRef cf ) ;
67 FUNCTION: CFStringRef CFCopyTypeIDDescription ( CFTypeID type_id ) ;
68
69 : prepare-CFString ( string -- byte-array )
70     [
71         dup { [ 0x10ffff > ] [ 0xd800 0xdfff between? ] } 1||
72         [ drop 0xfffd ] when
73     ] map! utf8 encode ;
74
75 : <CFString> ( string -- alien )
76     [ f ] dip
77     prepare-CFString dup length
78     kCFStringEncodingUTF8 f
79     CFStringCreateWithBytes
80     [ "CFStringCreateWithBytes failed" throw ] unless* ;
81
82 : CF>string ( alien -- string )
83     dup CFStringGetLength
84     [ 0 swap <CFRange> kCFStringEncodingUTF8 0 f ] keep
85     4 * 1 + <byte-vector> [
86         underlying>> dup length
87         { CFIndex } [ CFStringGetBytes drop ] with-out-parameters
88     ] keep swap >>length utf8 decode ;
89
90 : CF>string-array ( alien -- seq )
91     CF>array [ CF>string ] map ;
92
93 : <CFStringArray> ( seq -- alien )
94     [ [ <CFString> &CFRelease ] map <CFArray> ] with-destructors ;
95
96 : CF>description ( cf -- description )
97     [ CFCopyDescription &CFRelease CF>string ] with-destructors ;
98 : CFType>description ( cf -- description )
99     CFGetTypeID [ CFCopyTypeIDDescription &CFRelease CF>string ] with-destructors ;
100
101 SYNTAX: CFSTRING: 
102     scan-new-word scan-object 
103     [ drop ] [ '[ _ [ _ <CFString> ] initialize-alien ] ] 2bi
104     ( -- alien ) define-declared ;