VB的字符串缓冲类,为了回答一个问题匆忙写的,HoHo
VB的字符串缓冲类,为了回答一个问题匆忙写的,HoHo Option Explicit
Private mlngCapacity As Long '缓冲区的当前容量,可用于插入新的字符的存储空间
Private mlngLength As Long '字符个数
Private mlngEOA As Long '数组第一个空元素的索引
Private mabyt() As Byte
Private mlngUb As Long '数组上界
Private Sub Class_Initialize()
mlngCapacity = 2048
ReDim mabyt(4095)
End Sub
Private Sub Class_Terminate()
Erase mabyt
End Sub
Public Sub append(ByVal str As String)
Dim abyt() As Byte '添加的字符串转为Byte数组
Dim lngUb As Long '添加的字符串转为Byte数组的上界
Dim lngLength As Long '添加的字符串的长度
Dim i As Long
abyt = str
lngUb = UBound(abyt)
lngLength = (lngUb + 1) \ 2
Do While lngLength > mlngCapacity '插入新的字符超出缓冲区容量
mlngUb = mlngUb + 4096
ReDim mabyt(mlngUb) '缓冲区扩容
mlngCapacity = mlngCapacity + 2048
Loop
For i = 0 To lngUb
mabyt(mlngEOA + i) = abyt(i)
Next
mlngEOA = mlngEOA + lngUb + 1
mlngCapacity = mlngCapacity - lngLength
mlngLength = mlngLength + lngLength
Erase abyt
End Sub
Public Property Get length() As Long
length = mlngLength
End Property
Public Property Get capacity() As Long
capacity = mlngCapacity
End Property
Public Function toString() As String
ReDim Preserve mabyt(mlngEOA - 1)
toString = mabyt
ReDim Preserve mabyt(mlngUb)
End Function