📄 stringbuilder.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "StringBuilder"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2004 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: StringBuilder
'
''
' Allows easy manipulation, concatenation and removal of strings and characters.
'
' @remarks
' This class can be used to quickly minpulate strings. It allows for easy concatenation,
' removal and modification of the current underlying string value.
'
' @see Constructors
' @see NumberFormatInfo
' @see DateTimeFormatInfo
'
Option Explicit
Implements IObject
Private Const DEF_FILLCHAR As Integer = vbSpace
Private Const DEF_CAPACITY As Long = 512
Private Const OPEN_BRACE As Long = 123
Private Const CLOSE_BRACE As Long = 125
' module wide scope
Private mStringPtr As Long
Private mStringSA As SafeArray1d
Private mString() As Integer
Private mCapacity As Long
Private mLength As Long
Private mFillChar As Integer
' used by Replace
Private OldStr() As Integer
Private BufStr() As Integer
Private OldStrSA As SafeArray1d
Private BufStrSA As SafeArray1d
' used by AppendFormat
Private mChars As WordBuffer
''
' Appends a string to the current string value.
'
' @param Value Value to be appended.
' @param StartIndex A zero-based index of the first character to start appending from in the string.
' @param Count The number of characters from the string to append.
' @return This instance of StringBuilder.
' @remarks This version of Append attempts to convert to the value using the
' <b>Convert.ToString</b> method. This will allow for objects that impelement
' <b>IObject</b> or <b>IFormattable</b> to be converted to a string value.
'
Public Function Append(ByRef Value As Variant, Optional ByRef StartIndex As Variant, Optional ByRef Count As Variant) As StringBuilder
Set Append = AppendString(Convert.ToString(Value), StartIndex, Count)
End Function
''
' Appends a character a specified number of times to the end of string value.
'
' @param char The AscW character code to append.
' @param count The number of times to append the character.
' @return This instance of StringBuilder.
' @remarks The character is a unicode value from -32767 to 65535. For values that
' are greater than 32767 (&H7FFF), 65536 (&H10000) is subtracted from it to map the
' character to a 16-bit Integer without overflowing.
'
Public Function AppendChar(ByVal Char As Long, Optional ByVal Count As Long = 1) As StringBuilder
If Count < 0 Then _
Throw Cor.NewArgumentOutOfRangeException("Count cannot be negative.", "Count", Count)
If Char < -&H7FFF& Or Char > &HFFFF& Then _
Throw Cor.NewArgumentOutOfRangeException("Char must be between -32767 and 65535 inclusively.", "Char", Char)
If Char > &H7FFF& Then Char = Char - &H10000
If mLength + Count > mCapacity Then Call EnsureCapacity(mLength + Count)
' appending 1 character at a time happens 99%
' of the time, so optimize for it.
If Count = 1 Then
mString(mLength) = Char
Else
Call Fill(mLength, Char, Count)
End If
mLength = mLength + Count
Set AppendChar = Me
End Function
''
' Appends an array of characters to the end of the string value.
'
' @param Chars The array of characters to be appended.
' @param startindex The index from Chars to start appending.
' @param count The number of characters to append.
' @return This instance of StringBuilder.
'
Public Function AppendChars(ByRef Chars() As Integer, Optional ByRef StartIndex As Variant, Optional ByRef Count As Variant) As StringBuilder
Dim ElemCount As Long
Dim ElemIndex As Long
Dim Result As Long
Result = GetOptionalArrayRange(SAPtr(Chars), StartIndex, ElemIndex, Count, ElemCount)
If Result <> NO_ERROR Then Call ThrowArrayRangeException(Result, "Chars", ElemIndex, "StartIndex", ElemCount, "Count", IsMissing(StartIndex))
If ElemCount = 0 Then
Set AppendChars = Me
Exit Function
End If
If mLength + ElemCount > mCapacity Then Call EnsureCapacity(mLength + ElemCount)
Call CopyMemory(mString(mLength), Chars(ElemIndex), ElemCount * 2)
mLength = mLength + ElemCount
Set AppendChars = Me
End Function
''
' Appends a string with formatted arguments.
'
' @param format The string to append that contains formatting information.
' @param args The arguments to be formatted into the string.
' @return This instance of StringBuilder.
' @remarks
' <p>The format string can embed indexes into the args parameter to indicate where
' a value should be placed. The way to indicate an index is through an argument index enclosed
' in braces {N}. N is the parameter index beginning with zero.</p>
' <br><p>Some arguments may handle additional formatting commands. Please refer to their
' documentation for more information.</p>
' @see NumberFormatInfo
' @see DateTimeFormatInfo
' @include "..\..\Includes\StringBuilder.AppendFormat.txt"
Public Function AppendFormat(ByRef Format As String, ParamArray args() As Variant) As StringBuilder
Dim vArgs() As Variant
Call Helper.Swap4(ByVal ArrPtr(vArgs), ByVal Helper.DerefEBP(16))
Call InternalAppendFormat(Nothing, Format, vArgs)
Set AppendFormat = Me
End Function
''
' Appends a string with formatted arguments using the supplied provider.
'
' @param provider A custom formatting object that formats the arguments.
' @param Format The string to append that contains formatting information.
' @param args The arguments to be formatted into the string.
' @return This instance of StringBuilder.
' @remarks
' <p>The format string can embed indexes into the args parameter to indicate where
' a value should be placed. The way to indicate an index is through an argument index enclosed
' in braces {N}. N is the parameter index beginning with zero.</p>
' <br><p>Some arguments may handle additional formatting commands. Please refer to their
' documentation for more information.</p>
' <br><p>The supplied provider can format specific arguments unknown to the system. It can also
' delegate formatting of system-known arguments to another StringBuilder (usually cString.Format)
' to provide default formatting of known types and format only specific types.
' @see NumberFormatInfo
' @see DateTimeFormatInfo
'
Public Function AppendFormatEx(ByVal Provider As IFormatProvider, ByRef Format As String, ParamArray args() As Variant) As StringBuilder
Dim vArgs() As Variant
Call Helper.Swap4(ByVal ArrPtr(vArgs), ByVal Helper.DerefEBP(20))
Call InternalAppendFormat(Provider, Format, vArgs)
Set AppendFormatEx = Me
End Function
''
' Appends a string to the current string value.
'
' @param s String to be appended.
' @param StartIndex A zero-based index of the first character to start appending from in the string.
' @param Count The number of characters from the string to append.
' @return A reference to this instance of StringBuilder.
' @remarks<p>This version of append is designed for fast concatentation
' of the supplied string value. The entire string is appended.
' <p>This function does not return a reference to the instance of StringBuilder
' in order to keep the overhead cost down on the function call.</p>
'
Public Function AppendString(ByRef s As String, Optional ByRef StartIndex As Variant, Optional ByRef Count As Variant) As StringBuilder
Dim ElemIndex As Long
Dim ElemCount As Long
Dim Result As Long
Result = GetOptionalListRange(Len(s), StartIndex, ElemIndex, Count, ElemCount)
If Result <> NO_ERROR Then Call ThrowListRangeException(Result, ElemIndex, "StartIndex", ElemCount, "Count", IsMissing(StartIndex))
If mLength + ElemCount > mCapacity Then Call EnsureCapacity(mLength + ElemCount)
Call CopyMemory(ByVal mStringPtr + mLength * 2, ByVal StrPtr(s) + ElemIndex * 2, ElemCount * 2)
mLength = mLength + ElemCount
Set AppendString = Me
End Function
''
' Appends the supplied string and a Carriage-Return/Linefeed to the end of the StringBuilder.
'
' @param s The string to append to the builder, followed by a Carraige-Return/Linefeed.
' @return A reference to this instance of StringBuilder.
'
Public Function AppendLine(Optional ByVal s As String) As StringBuilder
Call AppendQuick(s & vbCrLf)
Set AppendLine = Me
End Function
''
' This function is designed to append strings quickly.
'
' @param s The string value to be appended.
' @remarks This method appends the entire string to the end of the
' StringBuilder. There is no return reference to the StringBuilder.
'
Public Sub AppendQuick(ByRef s As String)
Dim ElemCount As Long
ElemCount = Len(s)
If ElemCount = 0 Then Exit Sub
If mLength + ElemCount > mCapacity Then Call EnsureCapacity(mLength + ElemCount)
Call CopyMemory(ByVal mStringPtr + mLength * 2, ByVal StrPtr(s), ElemCount * 2)
mLength = mLength + ElemCount
End Sub
''
' Copies a set of characters from the StringBuilder into a character array.
'
' @param sourceindex The starting index in the StringBuilder to be copying from (0-based).
' @param Destination The character array to copy to.
' @param destinationindex The start index to begin placing characters in to.
' @param Count The number of characters to copy.
'
Public Sub CopyTo(ByVal SourceIndex As Long, ByRef Destination() As Integer, ByVal DestinationIndex As Long, ByVal Count As Long)
Dim Result As Long
Result = VerifyArrayRange(SAPtr(Destination), DestinationIndex, Count)
If Result <> NO_ERROR Then _
ThrowArrayRangeException Result, "Destination", DestinationIndex, "DestinationIndex", Count, "Count"
Result = VerifyListRange(mLength, SourceIndex, Count)
If Result <> NO_ERROR Then Call ThrowListRangeException(Result, SourceIndex, "SourceIndex", Count, "Count")
If Count > 0 Then Call CopyMemory(Destination(DestinationIndex), ByVal mStringPtr + SourceIndex * 2, Count * 2)
End Sub
''
' Returns the buffer size of the string value.
'
' @return Value indicating the amount of memory allocated in characters.
'
Public Property Get Capacity() As Long
Capacity = mCapacity
End Property
''
' Sets the amount of memory allocated in characters.
'
' @param RHS The number of characters to allocate.
'
Public Property Let Capacity(ByVal RHS As Long)
If RHS < mLength Then _
Throw Cor.NewArgumentOutOfRangeException("Cannot set capacity less than the length of the current instance.", "Capacity", RHS)
Dim Ptr As Long
Ptr = CoTaskMemAlloc(RHS * 2)
If Ptr = vbNullPtr Then Throw New OutOfMemoryException
Call CopyMemory(ByVal Ptr, ByVal mStringPtr, mLength * 2)
Call CoTaskMemFree(mStringPtr)
mStringPtr = Ptr
mStringSA.pvData = Ptr
mCapacity = RHS
End Property
''
' Returns a character from the string value.
'
' @param Index The index from the string value to return the character.
' @return The character in the string at the specified index.
' @remarks Index is zero-base.
'
Public Property Get Chars(ByVal Index As Long) As Integer
Attribute Chars.VB_UserMemId = 0
If Index < 0 Or Index >= mLength Then _
Throw Cor.NewIndexOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_Index))
Chars = mString(Index)
End Property
''
' Sets a character in the string value.
'
' @param index The index into the string value to set the character.
' @param char The character to set.
' @remarks index is zero-based.
'
Public Property Let Chars(ByVal Index As Long, ByVal Char As Integer)
If Index < 0 Or Index >= mLength Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_Index), "index", Index)
mString(Index) = Char
End Property
''
' Ensures that the internal buffer has atleast the requested amount.
'
' @param requiredCapacity The minimum amount of characters to be allocated.
' @return The capacity after the call is complete.
'
Public Function EnsureCapacity(ByVal RequiredCapacity As Long) As Long
If RequiredCapacity <= mCapacity Then
EnsureCapacity = mCapacity
Exit Function
End If
Dim NewCapacity As Long
NewCapacity = mCapacity * 2
If RequiredCapacity > NewCapacity Then NewCapacity = RequiredCapacity
Capacity = NewCapacity
EnsureCapacity = NewCapacity
End Function
''
' Returns a boolean indicating if the value and this object
' instance are the same instance.
'
' @param value The value to test equality on.
' @return Boolean indicating equality.
'
Public Function Equals(ByRef Value As Variant) As Boolean
Equals = Object.Equals(Me, Value)
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -