#Include ONCE "../Inc/Translate.bi"

Constructor EncodeUTFHTML()
	
	Dim As Short shTemp = &h400
	
	For j As Integer = 0 To 1
		
		For i As Integer = 0 To &h3f
			sArrayUTFHTML(i+j*&h40).sUTFHTML = "%d" & j & "%" & Hex(&h80+i)
			sArrayUTFHTML(i+j*&h40).sUTF = "d" & j & LCase(Hex(&h80+i))
			sArrayUTFHTML(i+j*&h40).sSimvol = UTFTOASCII(Cast(WString Ptr,@shTemp))
			shTemp += 1
		Next
		
	Next
	
	sArrayUTFHTML(&h80).sUTF = "e28496"
	sArrayUTFHTML(&h80).sUTFHTML = "%e2%84%96"
	sArrayUTFHTML(&h80).sSimvol = "¹"
	
End Constructor

Function EncodeUTFHTML.GetUtfHtmlSimvols(sSimvol As String) As String
	
	Dim As String sTempSimvol,sResult
	
	For i As Integer = 1 To Len(sSimvol)
		
		sTempSimvol = Mid(sSimvol,i,1)
		Dim bResult As BOOL
		
		If sTempSimvol <> "?" Then
			For k As Integer = 0 To &h80
				If sTempSimvol = sArrayUTFHTML(k).sSimvol Then
					sResult &= sArrayUTFHTML(k).sUTFHTML
					bResult = TRUE
					Exit For
				EndIf
			Next
		EndIf
		
		If bResult = FALSE Then
			If Asc(sTempSimvol) = 10 Then
				sResult &= "%0A"
			ElseIf sTempSimvol = "?" Then
				sResult &= " ?"
			ElseIf sTempSimvol = "&" Then
				sResult &= "%26"
			ElseIf sTempSimvol = "#" Then
				sResult &= "%23"							
			Else
				sResult &= sTempSimvol
			EndIf
		EndIf
		
	Next
	
	Return sResult
	
End Function

Function EncodeUTFHTML.GetSimvols(sUTF As String) As String
	
	Dim As Integer iPos,iPos2
	iPos = InStr(sUTF,"5B22")
	iPos2 = InStr(sUTF,"225D")
	
	If iPos And iPos2 Then
		sUTF = Mid(sUTF,iPos+4,iPos2-4-iPos)
	Else
		Function = "Error"
	EndIf

	CharLower(sUTF)
	
	Dim As String sTempSimvol,sResult
	For i As Integer = 1 To Len(sUTF)
		
		sTempSimvol = Mid(sUTF,i,4)
		Dim bResult As BOOL

		For k As Integer = 0 To &h79
			If sTempSimvol = sArrayUTFHTML(k).sUTF Then
				sResult &= sArrayUTFHTML(k).sSimvol
				i+=3
				bResult = TRUE
				Exit For
			EndIf
		Next

		If bResult = FALSE Then
			Var sTemp = Chr(Val("&h" & Mid(sTempSimvol,1,2)))
			If sTemp = "\" And Chr(Val("&h" & Mid(sTempSimvol,3,2)))="n" Then
				sResult &=!"\r\n"
				i+=3
			Else
				sResult &= sTemp
				i+=1
			EndIf
		EndIf

	Next

	Return ReplaceString(sResult,sArrayUTFHTML(&h80).sUTF,"¹")
	
End Function
