Sub main()
Dim Plaindata() As String
Dim Plength As Integer, Compresseddata(20) As String, Clength As Integer
Dim Str As String
Str = "ABCDEFABCDABCDEF"
ReDim Plaindata(0 To Len(Str) - 1)
For i = 1 To UBound(Plaindata) + 1
Plaindata(i - 1) = Mid(Str, i, 1)
' Debug.Print "Plaindata("; i - 1; ")="; Plaindata(i - 1)
Next i
Plength = Len(Str)
Call Compress(Plaindata, Plength, Compresseddata, Clength)
For i = 1 To UBound(Compresseddata)
Debug.Print Compresseddata(i - 1)
Next i
End Sub
Sub Compress(ByRef Plaindata() As String, ByVal Plength As Integer, ByRef Compresseddata() As String, ByVal Clength As Integer)
Dim Esym As String
Dim Pindex As Integer, Cindex As Integer
Dim Maxfitnum As Integer, Maxdistance As Integer, Distance As Integer, Fitnum As Integer
Dim ABCD As String
ABCD = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
If Plength >= 1 Then
Esym = "$"
Pindex = 0
Cindex = 0
Do While (Pindex < Plength) And (Pindex < 4)
Compresseddata(Cindex) = Plaindata(Pindex)
Debug.Print "Pindex="; Pindex, "Plaindata()="; Plaindata(Pindex), "cindex="; Cindex, "Compresseddata()="; Compresseddata(Cindex)
Cindex = Cindex + 1
Pindex = Pindex + 1
Loop
Do While (Pindex < Plength)
Maxfitnum = -1
Maxdistance = -1
Distance = 4
Do While ((Distance <= 26) And (Pindex - Distance >= 0))
Fitnum = 0
Do While ((Fitnum < Distance) And ((Pindex + Fitnum) < Plength))
If Plaindata(Pindex + Fitnum) <> Plaindata(Pindex + Fitnum - Distance) Then
Debug.Print "Pindex="; Pindex, "Fitnum="; Fitnum, "Plaindata()="; Plaindata(Pindex + Fitnum), "Distance="; Distance, ";Plaindata()="; Plaindata(Pindex + Fitnum - Distance)
Exit Do
End If
Debug.Print "Pindex="; Pindex, "Fitnum="; Fitnum, "Plaindata()="; Plaindata(Pindex + Fitnum), "Distance="; Distance, "Plaindata()="; Plaindata(Pindex + Fitnum - Distance)
Fitnum = Fitnum + 1
Loop
If (Fitnum >= 4) And (Maxfitnum < Fitnum) Then
Maxfitnum = Fitnum
Maxdistance = Distance
End If
Distance = Distance + 1
Loop
If Maxfitnum = -1 Then
Compresseddata(Cindex) = Plaindata(Pindex)
Debug.Print "Pindex="; Pindex, "Plaindata()="; Plaindata(Pindex), "cindex="; Cindex, "Compresseddata()="; Compresseddata(Cindex)
Cindex = Cindex + 1
Pindex = Pindex + 1
Else
Compresseddata(Cindex) = Esym
Compresseddata(Cindex + 1) = Mid(ABCD, Maxdistance, 1)
Compresseddata(Cindex + 2) = Mid(ABCD, Maxfitnum, 1)
Debug.Print "Pindex="; Pindex, "Plaindata()="; Plaindata(Pindex), "cindex="; Cindex, "Compresseddata()="; Compresseddata(Cindex); Compresseddata(Cindex + 1); Compresseddata(Cindex + 2)
Cindex = Cindex + 3
Pindex = Pindex + Maxfitnum
End If
Loop
Clength = Cindex
End If
End Sub
実行結果
Pindex= 0 Plaindata()=A cindex= 0 Compresseddata()=A
Pindex= 1 Plaindata()=B cindex= 1 Compresseddata()=B
Pindex= 2 Plaindata()=C cindex= 2 Compresseddata()=C
Pindex= 3 Plaindata()=D cindex= 3 Compresseddata()=D
Pindex= 4 Fitnum= 0 Plaindata()=E Distance= 4 ;Plaindata()=A
Pindex= 4 Plaindata()=E cindex= 4 Compresseddata()=E
Pindex= 5 Fitnum= 0 Plaindata()=F Distance= 4 ;Plaindata()=B
Pindex= 5 Fitnum= 0 Plaindata()=F Distance= 5 ;Plaindata()=A
Pindex= 5 Plaindata()=F cindex= 5 Compresseddata()=F
Pindex= 6 Fitnum= 0 Plaindata()=A Distance= 4 ;Plaindata()=C
Pindex= 6 Fitnum= 0 Plaindata()=A Distance= 5 ;Plaindata()=B
Pindex= 6 Fitnum= 0 Plaindata()=A Distance= 6 Plaindata()=A
Pindex= 6 Fitnum= 1 Plaindata()=B Distance= 6 Plaindata()=B
Pindex= 6 Fitnum= 2 Plaindata()=C Distance= 6 Plaindata()=C
Pindex= 6 Fitnum= 3 Plaindata()=D Distance= 6 Plaindata()=D
Pindex= 6 Fitnum= 4 Plaindata()=A Distance= 6 ;Plaindata()=E
Pindex= 6 Plaindata()=A cindex= 6 Compresseddata()=$FD
Pindex= 10 Fitnum= 0 Plaindata()=A Distance= 4 Plaindata()=A
Pindex= 10 Fitnum= 1 Plaindata()=B Distance= 4 Plaindata()=B
Pindex= 10 Fitnum= 2 Plaindata()=C Distance= 4 Plaindata()=C
Pindex= 10 Fitnum= 3 Plaindata()=D Distance= 4 Plaindata()=D
Pindex= 10 Fitnum= 0 Plaindata()=A Distance= 5 ;Plaindata()=F
Pindex= 10 Fitnum= 0 Plaindata()=A Distance= 6 ;Plaindata()=E
Pindex= 10 Fitnum= 0 Plaindata()=A Distance= 7 ;Plaindata()=D
Pindex= 10 Fitnum= 0 Plaindata()=A Distance= 8 ;Plaindata()=C
Pindex= 10 Fitnum= 0 Plaindata()=A Distance= 9 ;Plaindata()=B
Pindex= 10 Fitnum= 0 Plaindata()=A Distance= 10 Plaindata()=A
Pindex= 10 Fitnum= 1 Plaindata()=B Distance= 10 Plaindata()=B
Pindex= 10 Fitnum= 2 Plaindata()=C Distance= 10 Plaindata()=C
Pindex= 10 Fitnum= 3 Plaindata()=D Distance= 10 Plaindata()=D
Pindex= 10 Fitnum= 4 Plaindata()=E Distance= 10 Plaindata()=E
Pindex= 10 Fitnum= 5 Plaindata()=F Distance= 10 Plaindata()=F
Pindex= 10 Plaindata()=A cindex= 9 Compresseddata()=$JF
A
B
C
D
E
F
$
F
D
$
J
F