-
Notifications
You must be signed in to change notification settings - Fork 71
/
class_WSBuffer.ahk
60 lines (47 loc) · 1.48 KB
/
class_WSBuffer.ahk
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
; Autohotkey-Websocket-Client
class Buffer {
__New(len) {
this.SetCapacity("buffer", len)
this.length := len
this.index := 0
}
FromString(str, encoding = "UTF-8") {
length := Buffer.GetStrSize(str, encoding)
buffer := new Buffer(length)
buffer.WriteStr(str)
return buffer
}
GetStrSize(str, encoding = "UTF-8") {
encodingSize := ((encoding="utf-16" || encoding="cp1200") ? 2 : 1)
; length of string, minus null char
return StrPut(str, encoding) - encodingSize
}
WriteStr(str, encoding := "UTF-8") {
length := this.GetStrSize(str, encoding)
VarSetCapacity(text, length)
StrPut(str, &text, encoding)
this.Write(&text, length)
return length
}
; data is a pointer to the data
Write(data, length) {
if (this.index + length > this.length) {
this.SetCapacity("buffer", this.index + length)
}
p := this.GetPointer()
DllCall("RtlMoveMemory", "ptr", p + this.index, "ptr", data, "uint", length)
this.index += length
}
Append(ByRef buffer) {
destP := this.GetPointer()
sourceP := buffer.GetPointer()
DllCall("RtlMoveMemory", "ptr", destP + this.length, "ptr", sourceP, "uint", buffer.length)
this.length += buffer.length
}
GetPointer() {
return this.GetAddress("buffer")
}
Done() {
this.SetCapacity("buffer", this.length)
}
}