forked from ying32/govcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdstr.go
89 lines (77 loc) · 1.82 KB
/
stdstr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//----------------------------------------
//
// Copyright © ying32. All Rights Reserved.
//
// Licensed under Apache License 2.0
//
//----------------------------------------
package api
import (
"unsafe"
)
// 字符串到UTF8指针
func StringToUTF8Ptr(s string) *uint8 {
temp := []byte(s)
utf8StrArr := make([]uint8, len(temp)+1) // +1是因为Lazarus中PChar为0结尾
copy(utf8StrArr, temp)
return &utf8StrArr[0]
}
// Go的string转换为Lazarus的string
func GoStrToDStr(s string) uintptr {
if s == "" {
return 0
}
return uintptr(unsafe.Pointer(StringToUTF8Ptr(s)))
}
// 这种跟copyStr3基本一样,只是用go来处理了
func copyStr(src uintptr, strLen int) string {
if strLen == 0 {
return ""
}
str := make([]uint8, strLen)
for i := 0; i < strLen; i++ {
str[i] = *(*uint8)(unsafe.Pointer(src + uintptr(i)))
}
return string(str)
}
type GoStringHeader struct {
Data uintptr
Len int
}
// 小点的字符适合此种方式,大了就不行了
func copyStr2(str uintptr, strLen int) string {
if strLen == 0 {
return ""
}
var ret string
head := (*GoStringHeader)(unsafe.Pointer(&ret))
head.Data = str
head.Len = strLen
return ret
}
// 最新的lz macOS下出问题了
func copyStr3(str uintptr, strLen int) string {
if strLen == 0 {
return ""
}
buffer := make([]uint8, strLen)
DMove(str, uintptr(unsafe.Pointer(&buffer[0])), strLen)
return string(buffer)
}
// Lazarus的string转换为Go的string
func DStrToGoStr(ustr uintptr) string {
l := DStrLen(ustr)
if l == 0 {
return ""
}
return copyStr(ustr, int(l))
}
func getBuff(size int32) interface{} {
return make([]uint8, size+1)
}
func getBuffPtr(buff interface{}) uintptr {
return uintptr(unsafe.Pointer(&(buff.([]uint8))[0]))
}
func getTextBuf(strBuff interface{}, Buffer *string, slen int) {
*Buffer = string((strBuff.([]uint8))[:slen])
}