func NewCFString(s string) C.CFStringRef {
	s_ := C.CString(s)
	defer C.free(unsafe.Pointer(s_))
	retval := C.CFStringCreateWithBytes(
		C.CFAllocatorRef(nil), (*C.UInt8)(unsafe.Pointer(s_)), C.CFIndex(len(s)), C.kCFStringEncodingUTF8, C.Boolean(0),
	)
	return retval
}
示例#2
0
文件: darwin.go 项目: ChongFeng/beats
func (c *osxCollator) init(locale string) {
	l := C.CFStringCreateWithBytes(
		nil,
		osxUInt8P([]byte(locale)),
		C.CFIndex(len(locale)),
		C.kCFStringEncodingUTF8,
		C.Boolean(0),
	)
	c.loc = C.CFLocaleCreate(nil, l)
}
示例#3
0
// Converts a go string to a C.CFStringRef. The content of the string is copied
// by the function so if the Go string gets garbage collected the returned object
// is still valid.
// The program needs to call C.CFRelease on the returned C.CFStringRef when it
// doesn't need it anymore to avoid any memory leak.
func GoStringToCFString(s string) C.CFStringRef {
	h := (*reflect.StringHeader)(unsafe.Pointer(&s))
	return C.CFStringCreateWithBytes(
		nil,
		(*C.UInt8)(unsafe.Pointer(h.Data)),
		C.CFIndex(len(s)),
		C.kCFStringEncodingUTF8,
		0,
	)
}
示例#4
0
// The returned CFStringRef, if non-nil, must be released via CFRelease.
func _UTF8StringToCFString(s string) (C.CFStringRef, error) {
	if !utf8.ValidString(s) {
		return nil, errors.New("invalid UTF-8 string")
	}

	bytes := []byte(s)
	var p *C.UInt8
	if len(bytes) > 0 {
		p = (*C.UInt8)(&bytes[0])
	}
	return C.CFStringCreateWithBytes(nil, p, C.CFIndex(len(s)), C.kCFStringEncodingUTF8, C.false), nil
}
示例#5
0
// StringToCFString will return a CFStringRef and if non-nil, must be released with
// Release(ref).
func StringToCFString(s string) (C.CFStringRef, error) {
	if !utf8.ValidString(s) {
		return nil, errors.New("Invalid UTF-8 string")
	}
	if uint64(len(s)) > math.MaxUint32 {
		return nil, errors.New("String is too large")
	}

	bytes := []byte(s)
	var p *C.UInt8
	if len(bytes) > 0 {
		p = (*C.UInt8)(&bytes[0])
	}
	return C.CFStringCreateWithBytes(nil, p, C.CFIndex(len(s)), C.kCFStringEncodingUTF8, C.false), nil
}
示例#6
0
// ===== CFString =====
// convertStringToCFString may return nil if the input string is not a valid UTF-8 string
func convertStringToCFString(str string) C.CFStringRef {
	var bytes *C.UInt8
	var byteCount C.CFIndex
	if len(str) > 0 {
		// check the string for invalid encodings
		// We could use unicode.ValidString() but we also want to count the desired buffer size
		// and there's no sense in iterating the string more than we have to
		var errorCount int
		for i, r := range str {
			if r == utf8.RuneError {
				// This may be a valid value in the string. Re-decode it
				_, size := utf8.DecodeRuneInString(str[i:])
				if size == 1 {
					errorCount++
				}
			}
		}
		if errorCount == 0 {
			// go through unsafe to get the string bytes directly without the copy
			header := (*reflect.StringHeader)(unsafe.Pointer(&str))
			bytes = (*C.UInt8)(unsafe.Pointer(header.Data))
			byteCount = C.CFIndex(header.Len)
		} else {
			// our desired buffer is the length of s, minus the invalid bytes, plus the
			// replacement bytes.
			buf := make([]byte, len(str)+(errorCount*(runeErrorLen-1)))
			i := 0
			for _, r := range str {
				i += utf8.EncodeRune(buf[i:], r)
			}
			bytes = (*C.UInt8)(unsafe.Pointer(&buf[0]))
			byteCount = C.CFIndex(len(buf))
		}
	}
	return C.CFStringCreateWithBytes(nil, bytes, byteCount, C.kCFStringEncodingUTF8, C.false)
}
示例#7
0
文件: hid_darwin.go 项目: tam7t/hid
func cfstring(s string) C.CFStringRef {
	n := C.CFIndex(len(s))
	return C.CFStringCreateWithBytes(nil, *(**C.UInt8)(unsafe.Pointer(&s)), n, C.kCFStringEncodingUTF8, 0)
}
示例#8
0
func NSString(inString string) Object {
	l := C.CFIndex(len(inString))
	ret := C.CFStringCreateWithBytes(nil, *(**C.UInt8)(unsafe.Pointer(&inString)),
		l, C.kCFStringEncodingUTF8, 0)
	return Object(unsafe.Pointer(ret))
}