📄 unicodeencoding.cls
字号:
End Function
''
' Returns an array of bytes that represents this encoding.
'
' @return A byte array containg the bytes to be used to identify this encoding type.
' @remarks <p>If the encoding was created setting the <i>BigEndian</i> paramter
' to true, then this will return the Unicode byte array identifer &HFEFF otherwise
' &hFFFE for a non-bigEndian encoding. If no identifier is to be emitted, then an
' empty byte array is returned (not a null array).
'
Public Function GetPreamble() As Byte()
If mEmitBOM Then
If mIsBigEndian Then
GetPreamble = Cor.NewBytes(&HFE, &HFF)
Else
GetPreamble = Cor.NewBytes(&HFF, &HFE)
End If
Else
GetPreamble = Cor.NewBytes()
End If
End Function
''
' Decodes a set of bytes into a String.
'
' @param Bytes The set of bytes to be decoded into a string.
' @param Index The index of the first byte to be decoded.
' @param Count The number of bytes to be used in the decoding.
' @return A string containing the decoded set of bytes.
'
Public Function GetString(ByRef Bytes() As Byte, Optional ByRef Index As Variant, Optional ByRef Count As Variant) As String
Dim ElemIndex As Long
Dim ElemCount As Long
Dim Result As Long
Result = GetOptionalArrayRange(SAPtr(Bytes), Index, ElemIndex, Count, ElemCount)
If Result <> NO_ERROR Then ThrowArrayRangeException Result, "Bytes", ElemIndex, "Index", ElemCount, "Count", IsMissing(Index)
Dim Size As Long
Size = InternalGetCharCount(ElemCount)
GetString = SysAllocStringLen(0, Size)
With mCharsSA
.pvData = StrPtr(GetString)
.lLbound = 0
.cElements = Size
End With
SAPtr(mChars) = VarPtr(mCharsSA)
Call InternalGetChars(Bytes, ElemIndex, ElemCount, mChars, 0)
End Function
''
' Returns an encoder that maintains state.
'
' @return The stateful encoder.
' @remarks Since encoding Unicode characters equates to exactly 2 bytes per
' character, there is no state that is maintained between calls. This method
' functions identically to GetBytes.
'
Public Function GetEncoder() As Encoder
Dim Ret As New DefaultEncoder
Call Ret.Init(Me)
Set GetEncoder = Ret
End Function
''
' Returns a decoder that maintains state.
'
' @return The stateful decoder.
' @remarks Unlike UnicodeEncoding, the decoder maintains state between decoding calls.
' Since a single unicode character required exactly 2 bytes to be decoded, it is possible
' that an odd number of bytes may be attempted to be decoding, creating an orphaned byte
' which represents only half of a unicode character. The remaining byte is held until
' the next decoding call, and the byte is then inserted at the beginning of the next set
' of bytes to be decoded, picking up byte decoding where the orphaned byte left off.
'
Public Function GetDecoder() As Decoder
Dim Ret As New UnicodeDecoder
Call Ret.Init(mIsBigEndian)
Set GetDecoder = Ret
End Function
''
' Returns a string representation of this object instance.
'
' @return String representing this instance.
Public Function ToString() As String
ToString = Object.ToString(Me, App)
End Function
''
' Returns a boolean indicating if the value and this object
' instance are the same instance.
'
' @param value The value to compare equality to.
' @return Boolean indicating equality.
Public Function Equals(ByRef Value As Variant) As Boolean
If IsObject(Value) Then
If Value Is Nothing Then Exit Function
If TypeOf Value Is UnicodeEncoding Then
Dim en As UnicodeEncoding
Set en = Value
Equals = (en.GetHashCode = GetHashCode)
End If
End If
End Function
''
' Returns a pseudo-unique number identifying this instance.
'
' @return Pseudo-unique number identifying this instance.
'
Public Function GetHashCode() As Long
GetHashCode = mCodePage Or CLng(IIf(mIsBigEndian, &H80000000, 0)) Or CLng(IIf(mEmitBOM, &H40000000, 0))
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Friend Interface
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Friend Sub Init(ByVal BigEndian As Boolean, ByVal ByteOrderMark As Boolean)
mIsBigEndian = BigEndian
mEmitBOM = ByteOrderMark
If BigEndian Then mCodePage = 1201
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Private Helpers
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function InternalGetBytes(ByVal CharIndex As Long, ByVal CharCount As Long, ByRef Bytes() As Byte, ByVal ByteIndex As Long) As Long
If CharCount = 0 Then Exit Function
If mIsBigEndian Then
Dim i As Long
Dim Ch As Long
For i = 0 To CharCount - 1
Ch = mChars(CharIndex + i)
Bytes(ByteIndex) = (Ch \ &H100) And &HFF
Bytes(ByteIndex + 1) = Ch And &HFF
ByteIndex = ByteIndex + 2
Next i
Else
Call CopyMemory(Bytes(ByteIndex), mChars(CharIndex), CharCount * CHAR_SIZE)
End If
InternalGetBytes = CharCount * CHAR_SIZE
End Function
Private Function InternalGetChars(ByRef Bytes() As Byte, ByVal ByteIndex As Long, ByVal ByteCount As Long, ByRef Chars() As Integer, ByVal CharIndex As Long) As Long
If ByteCount = 0 Then Exit Function
If mIsBigEndian Then
Dim i As Long
Dim Ch As Long
For i = 0 To ByteCount - 1 Step 2
Ch = Bytes(ByteIndex + i) * &H100
Ch = Ch Or Bytes(ByteIndex + i + 1)
Chars(CharIndex) = AsWord(Ch)
CharIndex = CharIndex + 1
Next i
Else
If ByteCount And 1 Then ByteCount = ByteCount - 1
Call CopyMemory(Chars(CharIndex), Bytes(ByteIndex), ByteCount)
End If
InternalGetChars = InternalGetCharCount(ByteCount)
End Function
Private Function InternalGetByteCount(ByVal CharCount As Long) As Long
InternalGetByteCount = CharCount * CHAR_SIZE
End Function
Private Function InternalGetCharCount(ByVal ByteCount As Long) As Long
InternalGetCharCount = ByteCount \ CHAR_SIZE
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Class Events
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Class_InitProperties()
mCodePage = 1200
End Sub
Private Sub Class_ReadProperties(PropBag As PropertyBag)
With PropBag
Call Init(.ReadProperty("BigEndian", False), .ReadProperty("EmitBOM", True))
End With
End Sub
Private Sub Class_Terminate()
SAPtr(mChars) = 0
End Sub
Private Sub Class_WriteProperties(PropBag As PropertyBag)
With PropBag
Call .WriteProperty("BigEndian", mIsBigEndian)
Call .WriteProperty("EmitBOM", mEmitBOM)
End With
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' IObject Interface
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function IObject_Equals(Value As Variant) As Boolean
IObject_Equals = Equals(Value)
End Function
Private Function IObject_GetHashcode() As Long
IObject_GetHashcode = GetHashCode
End Function
Private Function IObject_ToString() As String
IObject_ToString = ToString
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Encoding Interface
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Property Get Encoding_BodyName() As String
Encoding_BodyName = BodyName
End Property
Private Property Get Encoding_CodePage() As Long
Encoding_CodePage = CodePage
End Property
Private Property Get Encoding_EncodingName() As String
Encoding_EncodingName = EncodingName
End Property
Private Function Encoding_Equals(Value As Variant) As Boolean
Encoding_Equals = Equals(Value)
End Function
Private Function Encoding_GetByteCount(Chars As Variant, Optional Index As Variant, Optional Count As Variant) As Long
Encoding_GetByteCount = GetByteCount(Chars, Index, Count)
End Function
Private Function Encoding_GetBytes(Chars As Variant, Optional Index As Variant, Optional Count As Variant) As Byte()
Encoding_GetBytes = GetBytes(Chars, Index, Count)
End Function
Private Function Encoding_GetBytesEx(Chars As Variant, ByVal CharIndex As Long, ByVal CharCount As Long, Bytes() As Byte, ByVal ByteIndex As Long) As Long
Encoding_GetBytesEx = GetBytesEx(Chars, CharIndex, CharCount, Bytes, ByteIndex)
End Function
Private Function Encoding_GetCharCount(Bytes() As Byte, Optional Index As Variant, Optional Count As Variant) As Long
Encoding_GetCharCount = GetCharCount(Bytes, Index, Count)
End Function
Private Function Encoding_GetChars(Bytes() As Byte, Optional Index As Variant, Optional Count As Variant) As Integer()
Encoding_GetChars = GetChars(Bytes, Index, Count)
End Function
Private Function Encoding_GetCharsEx(Bytes() As Byte, ByVal ByteIndex As Long, ByVal ByteCount As Long, Chars() As Integer, ByVal CharIndex As Long) As Long
Encoding_GetCharsEx = GetCharsEx(Bytes, ByteIndex, ByteCount, Chars, CharIndex)
End Function
Private Function Encoding_GetDecoder() As Decoder
Set Encoding_GetDecoder = GetDecoder
End Function
Private Function Encoding_GetEncoder() As Encoder
Set Encoding_GetEncoder = GetEncoder
End Function
Private Function Encoding_GetHashCode() As Long
Encoding_GetHashCode = GetHashCode
End Function
Private Function Encoding_GetMaxByteCount(ByVal CharCount As Long) As Long
Encoding_GetMaxByteCount = GetMaxByteCount(CharCount)
End Function
Private Function Encoding_GetMaxCharCount(ByVal ByteCount As Long) As Long
Encoding_GetMaxCharCount = GetMaxCharCount(ByteCount)
End Function
Private Function Encoding_GetPreamble() As Byte()
Encoding_GetPreamble = GetPreamble
End Function
Private Function Encoding_GetString(Bytes() As Byte, Optional Index As Variant, Optional Count As Variant) As String
Encoding_GetString = GetString(Bytes, Index, Count)
End Function
Private Property Get Encoding_HeaderName() As String
Encoding_HeaderName = HeaderName
End Property
Private Property Get Encoding_IsBrowserDisplay() As Boolean
Encoding_IsBrowserDisplay = IsBrowserDisplay
End Property
Private Property Get Encoding_IsBrowserSave() As Boolean
Encoding_IsBrowserSave = IsBrowserSave
End Property
Private Property Get Encoding_IsMailNewsDisplay() As Boolean
Encoding_IsMailNewsDisplay = IsMailNewsDisplay
End Property
Private Property Get Encoding_IsMailNewsSave() As Boolean
Encoding_IsMailNewsSave = IsMailNewsSave
End Property
Private Function Encoding_ToString() As String
Encoding_ToString = ToString
End Function
Private Property Get Encoding_WebName() As String
Encoding_WebName = WebName
End Property
Private Property Get Encoding_WindowsCodePage() As Long
Encoding_WindowsCodePage = WindowsCodePage
End Property
Private Property Get Encoding_IsSingleByte() As Boolean
Encoding_IsSingleByte = IsSingleByte
End Property
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -