Changeset 457


Ignore:
Timestamp:
Mar 6, 2008, 9:49:43 PM (17 years ago)
Author:
イグトランス (egtra)
Message:

(SPrintF.ab) FormatIntegerExにStringBuilderを引数に取る版を追加。

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Include/Classes/ActiveBasic/Strings/SPrintF.ab

    r400 r457  
    205205*/
    206206Function FormatFloatF_Core(s As String, e As Long, negative As Boolean, precision As DWord, ByRef flags As FormatFlags) As System.Text.StringBuilder
    207     FormatFloatF_Core = New System.Text.StringBuilder
     207    FormatFloatF_Core = New System.Text.StringBuilder(128)
    208208    With FormatFloatF_Core
    209209        AppendSign(FormatFloatF_Core, negative, flags)
     
    818818
    819819    Dim sb = New System.Text.StringBuilder
    820     If sb.Length = &hfeeefeee Then Debug
    821820    With sb
    822821        Dim prefixFunc = tr.Prefix
    823822        Dim prefix = prefixFunc(x, flags)
    824823        sb.Append(prefix)
    825         If sb.Length = &hfeeefeee Then Debug
    826824
    827825        Dim prefixLen = 0 As DWord
     
    830828        End If
    831829
    832         Dim buf = GC_malloc_atomic((tr.MaxSize + 1) * SizeOf (StrChar)) As *StrChar
     830'       Dim buf = GC_malloc_atomic((tr.MaxSize + 1) * SizeOf (StrChar)) As *StrChar
     831        Dim buf[MaxSizeLO] As StrChar
    833832        Dim convertFunc = tr.Convert
    834833        Dim bufStartPos = convertFunc(buf, x, flags)
    835834
    836         If sb.Length = &hfeeefeee Then Debug
    837835        Dim len = (tr.MaxSize - bufStartPos) As Long
    838836        If len < 0 Then
     
    844842
    845843        .Append(buf, bufStartPos + 1, len)
    846         If sb.Length = &hfeeefeee Then Debug
    847844
    848845        AdjustFieldWidth(sb, field, flags And (Not (Sign Or Blank)), prefixLen)
    849         If sb.Length = &hfeeefeee Then Debug
    850846    End With
    851847    FormatIntegerEx = sb.ToString()
     
    855851    End If
    856852End Function
     853Sub FormatIntegerEx(sb As System.Text.StringBuilder, ByRef tr As IntegerConvertTraits, x As QWord, d As DWord, field As DWord, flags As FormatFlags)
     854
     855    If d = DWORD_MAX Then
     856        d = 1
     857    Else
     858        '精度が指定されているとき、ゼロフラグは無視される。
     859        '仕様上、左揃えのときも無視されるが、それはAdjustFieldWidthが行ってくれる。
     860        flags And= Not Zero
     861    End If
     862
     863    Dim lastLength = sb.Length
     864
     865    With sb
     866        Dim prefixFunc = tr.Prefix
     867        Dim prefix = prefixFunc(x, flags)
     868        sb.Append(prefix)
     869
     870        Dim prefixLen = 0 As DWord
     871        If String.IsNullOrEmpty(prefix) = False Then
     872            prefixLen = prefix.Length As DWord
     873        End If
     874
     875'       Dim buf = GC_malloc_atomic((tr.MaxSize + 1) * SizeOf (StrChar)) As *StrChar
     876        Dim buf[MaxSizeLO] As StrChar
     877        Dim convertFunc = tr.Convert
     878        Dim bufStartPos = convertFunc(buf, x, flags)
     879
     880        Dim len = (tr.MaxSize - bufStartPos) As Long
     881        If len < 0 Then
     882            Debug
     883        End If
     884        If len < d Then
     885            .Append(&h30 As StrChar, d - len)
     886        End If
     887
     888        .Append(buf, bufStartPos + 1, len)
     889
     890        AdjustFieldWidth(sb, field, flags And (Not (Sign Or Blank)), prefixLen, lastLength)
     891    End With
     892    Dim t = sb.ToString()
     893   
     894    If (flags And Cap) = 0 Then
     895        Dim len = sb.Length
     896        Dim i As Long
     897        For i = lastLength To ELM(len)
     898            sb[i] = CType.ToLower(sb[i])
     899        Next
     900    End If
     901End Sub
    857902
    858903/*!
     
    864909
    865910/*!
    866 @brief  文字列をフィールド幅まで満たされるように空白などを挿入する。
     911@brief  書式化の仕上げとして、変換部分がフィールド幅まで満たされるように空白などを挿入する。
    867912@author Egtra
    868913@date   2007/10/13
     
    872917@param[in] flags    フラグ
    873918@param[in] prefixLen    (あれば)接頭辞の文字数。ゼロ埋めする際、この数だけ挿入位置を後ろにする。
     919@param[in] offset   変換した部分へのオフセット。AppendではなくInsertを行う際に用いられる。
    874920sbが"-1"のように負符号を持っている場合は、呼出元でSignフラグ(またはBlank)を立てること。
    875921*/
    876 Sub AdjustFieldWidth(sb As System.Text.StringBuilder, field As DWord, flags As FormatFlags, prefixLen = 0 As DWord)
     922Sub AdjustFieldWidth(sb As System.Text.StringBuilder, field As DWord, flags As FormatFlags, prefixLen = 0 As DWord, offset = 0 As Long)
    877923    With sb
    878         If .Length < field Then
    879             Dim embeddedSize = field - .Length
     924        Dim len = .Length - offset
     925        If len < field Then
     926            Dim embeddedSize = field - len
    880927            If flags And LeftSide Then
    881928                .Append(&h20, embeddedSize)
    882929            Else
    883                 Dim insPos As Long
    884930                If (flags And Zero) <> 0 Then
     931                    offset += prefixLen
    885932                    If (flags And Blank) Or (flags And Sign) Then
    886                         insPos++
     933                        offset++
    887934                    End If
    888                     insPos += prefixLen
    889                     .Insert(insPos, String$(embeddedSize, "0"))
     935                    .Insert(offset, String$(embeddedSize, "0"))
    890936                Else
    891                     .Insert(insPos, String$(embeddedSize, " "))
     937                    .Insert(offset, String$(embeddedSize, " "))
    892938                End If
    893939            End If
     
    10051051    End If
    10061052
    1007     s.Append(FormatIntegerEx(ByVal traits, x, precision, field, flags))
     1053'   s.Append(FormatIntegerEx(ByVal traits, x, precision, field, flags))
     1054    FormatIntegerEx(s, ByVal traits, x, precision, field, flags)
    10081055End Sub
    10091056
     
    10281075    Do
    10291076        Dim c = s[i]
    1030         If Not IsDigit(c) Then Exit Do
     1077        If Not CType.IsDigit(c) Then Exit Do
    10311078        StrToLong *= 10
    10321079        StrToLong += ((c As DWord) And &h0f) As Long
     
    10371084    End If
    10381085    p = VarPtr(s[i])
    1039 End Function
    1040 
    1041 /*!
    1042 @brief  文字が十進数字かどうか調べる。
    1043 @author Egtra
    1044 @date   2007/11/11
    1045 @param[in] c    調べる文字
    1046 @retval True    0から9の文字である
    1047 @retval False   そうでない
    1048 */
    1049 Function IsDigit(c As StrChar) As Boolean
    1050     Dim dw = (c As DWord)
    1051     IsDigit = (dw - &h30) < 10
    10521086End Function
    10531087
  • trunk/Include/Classes/ActiveBasic/Strings/Strings.ab

    r391 r457  
    6767    Return s1[i] As Long - s2[i]
    6868End Function
    69 
    70 
    7169
    7270Function ChrCmp(s1 As PCWSTR, s2 As PCWSTR, size As SIZE_T) As Long
  • trunk/Include/Classes/System/IO/TextReader.ab

    r432 r457  
    3636        ElseIf count < 0 Then
    3737        End If
     38        Read = ReadImpl(buffer, index, count)
    3839    End Function
    3940
     
    8081    End Function
    8182    /*
     83    @brief 現在位置からストリームの終わりまで読み込む。
    8284    @date 2008/02/26
    8385    @auther Egtra
     
    9395            sb.Append(ch As StrChar)
    9496        Loop
     97    End Function
     98
     99    Static Function Synchronized(reader As TextReader) As TextReader
    95100    End Function
    96101
     
    118123End Class
    119124
     125Namespace Detail
     126
     127Class SynchronizedTextReader
     128    Inherits TextReader
     129Public
     130    Sub SynchronizedTextReader(reader As TextReader)
     131        cs = New ActiveBasic.Windows.CriticalSection
     132        base = reader
     133    End Sub
     134
     135    Override Function Peek() As Long
     136'       Using lock = cs.Lock
     137            Peek = base.Peek
     138'       End Using
     139    End Function
     140
     141    Override Function Read() As Long
     142'       Using lock = cs.Lock
     143            Read = base.Read
     144'       End Using
     145    End Function
     146
     147    Override Function ReadLine() As String
     148'       Using lock = cs.Lock
     149            ReadLine = base.ReadLine
     150'       End Using
     151    End Function
     152
     153    Override Function ReadToEnd() As String
     154'       Using lock = cs.Lock
     155            ReadToEnd = base.ReadToEnd
     156'       End Using
     157    End Function
     158
     159Protected
     160    Override Sub Dispose(disposing As Boolean)
     161        Dim s = Nothing As Stream
     162        SetPointer(VarPtr(s) As *VoidPtr, InterlockedExchangePointer(ByVal VarPtr(base) As *VoidPtr, 0))
     163        If disposing Then
     164            If Not ActiveBasic.IsNothing(s) Then
     165                s.Dispose()
     166            End If
     167            cs.Dispose()
     168        End If
     169    End Sub
     170
     171    Override Function ReadImpl(buffer As *StrChar, index As Long, count As Long) As Long
     172'       Using lock = cs.Lock
     173            ReadImpl = base.ReadImpl(buffer, index, count)
     174'       End Using
     175    End Function
     176Private
     177    cs As ActiveBasic.Windows.CriticalSection
     178    base As TextReader
     179End Class
     180
     181End Namespace
     182
    120183End NameSpace
    121184End NameSpace
  • trunk/Include/Classes/System/Math.ab

    r299 r457  
    127127
    128128    Static Function Ceiling(x As Double) As Long
    129         If Floor(x) = x then
    130             Return x As Long
    131         Else
    132             Return Floor(x) + 1
     129        Ceiling = Floor(x)
     130        If Ceiling <> x Then
     131            Ceiling++
    133132        End If
    134133    End Function
     
    145144
    146145    Static Function Cosh(value As Double) As Double
    147         Dim t As Double
    148         t = Math.Exp(value)
     146        Dim t = Math.Exp(value)
    149147        return (t + 1 / t) * 0.5
    150148    End Function
     
    156154
    157155    Static Function DivRem(x As Int64, y As Int64, ByRef ret As Int64) As Int64
    158         ret = x - (x \ y) * y
    159         return x \ y
     156        DivRem = x \ y
     157        ret = x - (DivRem) * y
    160158    End Function
    161159
     
    196194        Return Int(value)
    197195    End Function
    198 
    199     'GetHashCode
    200 
    201     'GetType
    202196
    203197    Static Function IEEERemainder(x As Double, y As Double) As Double
     
    513507
    514508    Static Function Sqrt(x As Double) As Double
    515         Dim s As Double, last As Double
    516         Dim i As *Word, j As Long, jj As Long, k As Long
    517509        If x > 0 Then
    518510            If ActiveBasic.Math.IsInf(x) Then
     
    520512            Else
    521513                Sqrt = x
    522                 i = (VarPtr(Sqrt) + 6) As *Word
    523                 jj = GetWord(i)
    524                 j = jj >> 5
    525                 k = jj And &h0000001f
    526                 j = (j+ 511) << 4 + k
     514                Dim i = (VarPtr(Sqrt) + 6) As *Word
     515                Dim jj = GetWord(i) As Long
     516                Dim j = jj >> 5 As Long
     517                Dim k = (jj And &h0000001f) As Long
     518                j = (j + 511) << 4 + k
    527519                SetWord(i, j)
     520                Dim last As Double
    528521                Do
    529522                    last = Sqrt
    530                     Sqrt = (x /Sqrt + Sqrt) * 0.5
     523                    Sqrt = (x / Sqrt + Sqrt) * 0.5
    531524                Loop While Sqrt <> last
    532525            End If
  • trunk/Include/Classes/System/String.ab

    r428 r457  
    2727
    2828    Class String
    29     '   Inherits IComparable, ICloneable, IConvertible, IEnumerable
     29        Implements /*IComparable, ICloneable, IConvertible, IComparable<String>, IEnumerable, IEnumerable<StrChar>, IEquatable<String>*/
    3030
    3131        m_Length As Long
     
    3434        Sub validPointerCheck(p As VoidPtr, size = 1 As Long)
    3535            If p As ULONG_PTR < &h10000 Then
    36                 'Throw ArgumentException
    37                 Debug
     36                Throw New ArgumentException
    3837            ElseIf IsBadReadPtr(p, size As ULONG_PTR) Then
    39                 'Throw ArgumentException
    40                 Debug
     38                Throw New ArgumentException
    4139            End If
    4240        End Sub
     
    153151
    154152        Const Function Operator == (y As String) As Boolean
    155             Return String.Compare(This, y) = 0
     153            Return Compare(This, y) = 0
    156154        End Function
    157155
    158156        Const Function Operator == (y As *StrChar) As Boolean
    159             Return String.Compare(This, y) = 0
     157            Return Compare(This, y) = 0
    160158        End Function
    161159
    162160        Const Function Operator <> (y As String) As Boolean
    163             Return String.Compare(This, y) <> 0
     161            Return Compare(This, y) <> 0
    164162        End Function
    165163
    166164        Const Function Operator <> (y As *StrChar) As Boolean
    167             Return String.Compare(This, y) <> 0
     165            Return Compare(This, y) <> 0
    168166        End Function
    169167
    170168        Const Function Operator < (y As String) As Boolean
    171             Return String.Compare(This, y) < 0
     169            Return Compare(This, y) < 0
    172170        End Function
    173171
    174172        Const Function Operator < (y As *StrChar) As Boolean
    175             Return String.Compare(This, y) < 0
     173            Return Compare(This, y) < 0
    176174        End Function
    177175
    178176        Const Function Operator > (y As String) As Boolean
    179             Return String.Compare(This, y) > 0
     177            Return Compare(This, y) > 0
    180178        End Function
    181179
    182180        Const Function Operator > (y As *StrChar) As Boolean
    183             Return String.Compare(This, y) > 0
     181            Return Compare(This, y) > 0
    184182        End Function
    185183
    186184        Const Function Operator <= (y As String) As Boolean
    187             Return String.Compare(This, y) <= 0
     185            Return Compare(This, y) <= 0
    188186        End Function
    189187
    190188        Const Function Operator <= (y As *StrChar) As Boolean
    191             Return String.Compare(This, y) <= 0
     189            Return Compare(This, y) <= 0
    192190        End Function
    193191
    194192        Const Function Operator >= (y As String) As Boolean
    195             Return String.Compare(This, y) >= 0
     193            Return Compare(This, y) >= 0
    196194        End Function
    197195
    198196        Const Function Operator >= (y As *StrChar) As Boolean
    199             Return String.Compare(This, y) >= 0
     197            Return Compare(This, y) >= 0
    200198        End Function
    201199
     
    205203
    206204    Public
     205        'Compareなどで、x.Charsではなく、StrPtr(x)と書く理由は、xがNothingの場合のため。
     206
    207207        Static Function Compare(x As String, indexX As Long, y As String, indexY As Long, length As Long) As Long
    208             Return String.CompareOrdinal(x, indexX, y, indexY, length)
     208            Return CompareOrdinal(x, indexX, y, indexY, length)
    209209        End Function
    210210
    211211        Static Function CompareOrdinal(x As String, y As String) As Long
    212             Return String.CompareOrdinal(x.Chars, y.Chars)
     212            Return CompareOrdinal(StrPtr(x), StrPtr(y))
    213213        End Function
    214214
    215215        Static Function CompareOrdinal(x As String, indexX As Long, y As String, indexY As Long, length As Long) As Long
    216             Return String.CompareOrdinal(x.Chars, indexX, y.Chars, indexY, length)
     216            Return CompareOrdinal(StrPtr(x), indexX, StrPtr(y), indexY, length)
    217217        End Function
    218218    Private
    219219        Static Function Compare(x As String, y As *StrChar) As Long
    220             Return String.CompareOrdinal(x, y)
     220            Return CompareOrdinal(x, y)
    221221        End Function
    222222
    223223        Static Function CompareOrdinal(x As String, y As *StrChar) As Long
    224             Return String.CompareOrdinal(x.Chars, y)
     224            Return CompareOrdinal(StrPtr(x), y)
    225225        End Function
    226226
     
    257257        Function CompareTo(y As Object) As Long
    258258            If Not Object.Equals(This.GetType(), y.GetType()) Then
    259                 Throw New ArgumentException("String.CompareTo: An argument is out of range value.", "y")
     259                Throw New ArgumentException("String.CompareTo: The type of the argument y is not String.", "y")
    260260            End If
    261261            Return CompareTo(y As String)
     
    351351        End Function
    352352
     353        Static Function Concat(x As String, y As String, z As String) As String
     354            Dim sb = New Text.StringBuilder(removeNull(x).Length + removeNull(y).Length + removeNull(z).Length)
     355            sb.Append(x).Append(y).Append(z)
     356            Concat = sb.ToString
     357        End Function
     358
     359        Static Function Concat(x As String, y As String, z As String, w As String) As String
     360            Dim sb = New Text.StringBuilder(removeNull(x).Length + removeNull(y).Length + removeNull(z).Length + removeNull(w).Length)
     361            sb.Append(x).Append(y).Append(z).Append(w)
     362            Concat = sb.ToString
     363        End Function
     364
    353365        Static Function Concat(x As Object, y As Object) As String
    354             Return String.Concat(x.ToString, y.ToString)
     366            Return Concat(x.ToString, y.ToString)
     367        End Function
     368
     369        Static Function Concat(x As Object, y As Object, z As Object) As String
     370            Return Concat(x.ToString, y.ToString, z.ToString)
     371        End Function
     372
     373        Static Function Concat(x As Object, y As Object, z As Object, w As Object) As String
     374            Return Concat(x.ToString, y.ToString, z.ToString, w.ToString)
    355375        End Function
    356376
     
    575595            Clone = This
    576596        End Function
    577 
     597/*
     598        Function Clone() As Object
     599            Clone = This
     600        End Function
     601*/
    578602        Static Function Copy(s As String) As String
    579603            Copy = New String(s.Chars, s.m_Length)
     
    656680            End If
    657681        End Sub
     682
     683        Static Function removeNull(s As String) As String
     684            If ActiveBasic.IsNothing(s) Then
     685                removeNull = Empty
     686            Else
     687                removeNull = s
     688            End If
     689        End Function
    658690    End Class
    659691
  • trunk/Include/Classes/System/Text/StringBuilder.ab

    r435 r457  
    182182        ElseIf c > Capacity Then
    183183            Dim p = GC_malloc_atomic((c + 1) * SizeOf (StrChar)) As *StrChar
    184             If p = 0 Then
    185                 'Throw OutOfMemoryException
    186                 Debug
    187             End If
    188184            ActiveBasic.Strings.ChrCopy(p, chars, size As SIZE_T)
    189185            chars = p
     
    488484        This.size = 0
    489485        This.chars = GC_malloc_atomic((This.capacity + 1) * SizeOf (StrChar))
    490         If chars = 0 Then
    491             'Throw OutOfMemoryException
    492             Debug
    493         End If
    494 
    495486    End Sub
    496487
  • trunk/Include/api_commctrl.sbp

    r300 r457  
    997997Const MCN_SELCHANGE = MCN_FIRST + &H1
    998998
     999Declare Function _TrackMouseEvent Lib "comctl32" (ByRef EventTrack As TRACKMOUSEEVENT) As BOOL
    9991000
    10001001#endif '_INC_COMMCTRL
  • trunk/Include/basic/command.sbp

    r391 r457  
    247247    FileNumber--
    248248
    249     WriteFile(_System_hFile(FileNumber), buf, Len(buf), VarPtr(dwAccessByte), ByVal 0)
     249    WriteFile(_System_hFile(FileNumber), StrPtr(buf), Len(buf), VarPtr(dwAccessByte), ByVal 0)
    250250End Sub
    251251
  • trunk/Include/basic/function.sbp

    r400 r457  
    946946@author Egtra
    947947@date   2007/08/24
    948 @param[in]  p   COMインタフェースを指すポインタ
     948@param[in]  p   オブジェクトを指すポインタ
    949949@return Object参照型
    950950*/
  • trunk/TestCase/SimpleTestCase/SimpleTestCase.ab

    r438 r457  
    4040End Sub
    4141
    42 Print "Please enter any key"
    43 Dim a As Long
    44 Input a
     42Print "Please Enter key"
     43System.Console.ReadLine()
  • trunk/TestCase/SimpleTestCase/StringBuilderTest.ab

    r299 r457  
    99Sub TestMain()
    1010    Dim sb = New StringBuilder("Hello")
    11     UnitTest("StringBuilder - Constractor & ToString", sb.ToString = "Hello")
    12     UnitTest("StringBuilder.ToString(index, count)", sb.ToString(1, 3) = "ell")
    13     UnitTest("StringBuilder.Append", sb.Append("StringBuilder").ToString = "HelloStringBuilder")
    14     UnitTest("StringBuilder.Insert (String)", sb.Insert(5, ", ").ToString = "Hello, StringBuilder")
     11    UnitTest("StringBuilder - Constractor & ToString", sb.ToString, "Hello")
     12    UnitTest("StringBuilder.ToString(index, count)", sb.ToString(1, 3), "ell")
     13    UnitTest("StringBuilder.Append", sb.Append("StringBuilder").ToString, "HelloStringBuilder")
     14    UnitTest("StringBuilder.Insert (String)", sb.Insert(5, ", ").ToString, "Hello, StringBuilder")
    1515    sb.Length = 0
    16     UnitTest("StringBuilder.Length = 0", sb.ToString = "")
     16    UnitTest("StringBuilder.Length = 0", sb.ToString, "")
    1717    sb.Append("でてこいでてこいいけのこい")
    1818    sb.Replace("こい", "いけ").Replace("いけ", "こい")
    19     UnitTest("StringBuilder.Replace (String)", sb.ToString = "でてこいでてこいいけのこい")
     19    UnitTest("StringBuilder.Replace (String)", sb.ToString, "でてこいでてこいこいのこい")
    2020End Sub
    2121
Note: See TracChangeset for help on using the changeset viewer.