'===========================================================================
' Subject: STRING DE/COMPRESSION FUNCTIONS    Date: 12-16-97 (09:53)       
'  Author: Alexander Podkolzin                Code: PB                     
'  Origin: app@ns.sbank.e-burg.su           Packet: ALGOR.ABC
'===========================================================================
'---------------------------------------------------------------------------
' Simple string de/compression functions. Are usefull, if string
' consists of sets of equivalent chars. Use it as you want.
'---------------------------- Demo programme -------------------------------
 Defint a - z
 Cls
 Shared Flag$, Delta%
 Flag$ = Chr$(254)      ' 255 is better, but it's invisiable :)
 Delta% = 31            ' We need it, if do not want special chars

 Print "Example:"
 FulLine$ = "1111111111111111111111111111111111Hello!111122222"
 Print "Full line    ="; FulLine$       Tab(65) "Len ="; Len(FulLine$)
 CompLine$ = fCompressLine(FulLine$)
 Print "Compressed   ="; CompLine$      Tab(65) "Len ="; Len(CompLine$)
 FulLine$ = fDecompressLine(CompLine$)
 Print "Decompressed ="; FulLine$       Tab(65) "Len ="; Len(FulLine$)
 Print
 Print "Exlanations for our example:"
 Print Mid$(CompLine$,1,1); " <--- Compression flag,"
 Print Mid$(CompLine$,2,1); " <--- Number of chars in set = Chr$(Delta% + Length of set); "
 Print Mid$(CompLine$,3,1); " <--- Represents set of equivalent chars."
 Print
 Print "WARNING: (Delta% + Length of set)  has to be less then 256,"
 Print "         or else Chr$() will cause Err.5 !

 Print
 Print "Next example:"
 FulLine$ = "QQQQQQQQQQQQQQQQWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWEEEEEEEEEEEEEEEEEEEEEEEEEEEEEZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
 Print "Full line:"
 Print FulLine$
 CompLine$ = fCompressLine(FulLine$)
 Print "Compressed:"
 Print CompLine$
 FulLine$ = fDecompressLine(CompLine$)
 Print "Decompressed:"
 Print FulLine$
 Print "Thank you!";
 End

'----------------------------------------------------------------------------
 Function fCompressLine$(FulLine$)

'   Flag$ = Chr$(254)
'   Delta% = 31
   MaxLen% = Asc(Flag$) - Delta%
   nr% = 3                        ' Len of mimimal set = 4
   ns% = 1
   CompLine$ = ""
   While ns% <= Len(FulLine$)
     s$ = Mid$(FulLine$, ns%, 1)
     n% = fCoins(FulLine$, ns%)
     If n% > MaxLen% Then
       n% = MaxLen%
     End If
     If n% > nr% Then
       CompLine$ = CompLine$ + Flag$ + Chr$(n% + Delta%) + s$
     Else
       CompLine$ = CompLine$ + Mid$(FulLine$, ns%, n%)
     End If
     Incr ns%, n%
   Wend
   Function = CompLine$

 End Function
'----------------------------------------------------------------------------
 Function fDecompressLine$(CompLine$)

'   Flag$ = Chr$(254)
'   Delta% = 31
   ns% = 1
   FulLine$ = ""
   While ns% <= Len(CompLine$)
     s$ = Mid$(CompLine$, ns%, 1)
     If s$ = Flag$ Then
       n% = Asc(Mid$(CompLine$, ns% + 1, 1)) - (Delta% + 1)
       s$ = Mid$(CompLine$, ns% + 2, 1)
       FulLine$ = FulLine$ + String$(n%, s$)
       Incr ns%, 2
     Else
       FulLine$ = FulLine$ + Mid$(CompLine$, ns%, 1)
       Incr ns%
     End If
   Wend
   Function = FulLine$

 End Function
'----------------------------------------------------------------------------
 Function fCoins%(s$, n%)         ' Counts lengths of sets

   i% = 1
   c$ = Mid$(s$, n%, 1)
   While Mid$(s$, n% + i%, 1) = c$
     Incr i%
   Wend
   Function = i%

 End Function
'----------------------------------------------------------------------------
