[272] | 1 | 'Classes/System/Text/StringBuilder.ab
|
---|
| 2 |
|
---|
| 3 | #require <Classes/ActiveBasic/Strings/Strings.ab>
|
---|
| 4 |
|
---|
| 5 | Namespace System
|
---|
| 6 | Namespace Text
|
---|
| 7 |
|
---|
| 8 | Class StringBuilder
|
---|
| 9 | 'Inherits ISerializable
|
---|
| 10 | Public
|
---|
| 11 | Sub StringBuilder()
|
---|
| 12 | initialize(1024)
|
---|
| 13 | End Sub
|
---|
| 14 |
|
---|
| 15 | Sub StringBuilder(capacity As Long)
|
---|
| 16 | initialize(capacity)
|
---|
| 17 | End Sub
|
---|
| 18 |
|
---|
| 19 | Sub StringBuilder(s As String)
|
---|
| 20 | initialize(s, 0, s.Length, s.Length * 2)
|
---|
| 21 | End Sub
|
---|
| 22 |
|
---|
| 23 | Sub StringBuilder(capacity As Long, maxCapacity As Long)
|
---|
| 24 | initialize(capacity, maxCapacity)
|
---|
| 25 | End Sub
|
---|
| 26 |
|
---|
| 27 | Sub StringBuilder(s As String, capacity As Long)
|
---|
| 28 | initialize(s, 0, s.Length, capacity)
|
---|
| 29 | End Sub
|
---|
| 30 |
|
---|
| 31 | Sub StringBuilder(s As String, startIndex As Long, length As Long, capacity As Long)
|
---|
| 32 | initialize(s, startIndex, length, capacity)
|
---|
| 33 | End Sub
|
---|
| 34 |
|
---|
| 35 | 'Methods
|
---|
| 36 |
|
---|
| 37 | Function Append(x As Boolean) As StringBuilder
|
---|
| 38 | Append(Str$(x))
|
---|
| 39 | Return This
|
---|
| 40 | End Function
|
---|
| 41 |
|
---|
[468] | 42 | Function Append(x As Char) As StringBuilder
|
---|
[272] | 43 | EnsureCapacity(size + 1)
|
---|
| 44 | separateBuffer()
|
---|
| 45 | chars[size] = x
|
---|
| 46 | size++
|
---|
| 47 | Return This
|
---|
| 48 | End Function
|
---|
[468] | 49 | #ifdef UNICODE
|
---|
[272] | 50 | Function Append(x As SByte) As StringBuilder
|
---|
[468] | 51 | ActiveBasic.Strings.Detail.FormatIntegerD(This, x, DWORD_MAX, 0, 0)
|
---|
[272] | 52 | Return This
|
---|
| 53 | End Function
|
---|
| 54 | #endif
|
---|
| 55 | Function Append(x As Byte) As StringBuilder
|
---|
[468] | 56 | ActiveBasic.Strings.Detail.FormatIntegerU(This, x, DWORD_MAX, 0, 0)
|
---|
[272] | 57 | Return This
|
---|
| 58 | End Function
|
---|
| 59 |
|
---|
| 60 | Function Append(x As Integer) As StringBuilder
|
---|
[468] | 61 | ActiveBasic.Strings.Detail.FormatIntegerD(This, x, DWORD_MAX, 0, 0)
|
---|
[272] | 62 | Return This
|
---|
| 63 | End Function
|
---|
[468] | 64 | #ifndef UNICODE
|
---|
| 65 | Function Append(x As Word) As StringBuilder
|
---|
| 66 | ActiveBasic.Strings.Detail.FormatIntegerU(This, x, DWORD_MAX, 0, 0)
|
---|
| 67 | Return This
|
---|
| 68 | End Function
|
---|
| 69 | #endif
|
---|
[272] | 70 | Function Append(x As Long) As StringBuilder
|
---|
[468] | 71 | ActiveBasic.Strings.Detail.FormatIntegerD(This, x, DWORD_MAX, 0, 0)
|
---|
[272] | 72 | Return This
|
---|
| 73 | End Function
|
---|
| 74 |
|
---|
| 75 | Function Append(x As DWord) As StringBuilder
|
---|
[468] | 76 | ActiveBasic.Strings.Detail.FormatIntegerU(This, x, DWORD_MAX, 0, 0)
|
---|
[272] | 77 | Return This
|
---|
| 78 | End Function
|
---|
| 79 |
|
---|
| 80 | Function Append(x As Int64) As StringBuilder
|
---|
[468] | 81 | ActiveBasic.Strings.Detail.FormatIntegerLD(This, x, DWORD_MAX, 0, 0)
|
---|
[272] | 82 | Return This
|
---|
| 83 | End Function
|
---|
| 84 |
|
---|
| 85 | Function Append(x As QWord) As StringBuilder
|
---|
[468] | 86 | ActiveBasic.Strings.Detail.FormatIntegerLU(This, x, DWORD_MAX, 0, 0)
|
---|
[272] | 87 | Return This
|
---|
| 88 | End Function
|
---|
| 89 |
|
---|
| 90 | Function Append(x As Single) As StringBuilder
|
---|
[468] | 91 | ActiveBasic.Strings.Detail.FormatFloatG(This, x, DWORD_MAX, 0, 0)
|
---|
[272] | 92 | Return This
|
---|
| 93 | End Function
|
---|
| 94 |
|
---|
| 95 | Function Append(x As Double) As StringBuilder
|
---|
[468] | 96 | ActiveBasic.Strings.Detail.FormatFloatG(This, x, DWORD_MAX, 0, 0)
|
---|
[272] | 97 | Return This
|
---|
| 98 | End Function
|
---|
| 99 |
|
---|
| 100 | Function Append(x As Object) As StringBuilder
|
---|
| 101 | Append(x.ToString)
|
---|
| 102 | Return This
|
---|
| 103 | End Function
|
---|
| 104 |
|
---|
[468] | 105 | Function Append(c As Char, n As Long) As StringBuilder
|
---|
[272] | 106 | EnsureCapacity(size + n)
|
---|
| 107 | ActiveBasic.Strings.ChrFill(VarPtr(chars[size]), n As SIZE_T, c)
|
---|
| 108 | size += n
|
---|
| 109 | Return This
|
---|
| 110 | End Function
|
---|
| 111 |
|
---|
| 112 | Function Append(s As String) As StringBuilder
|
---|
| 113 | If Not String.IsNullOrEmpty(s) Then
|
---|
| 114 | appendCore(s, 0, s.Length)
|
---|
| 115 | End If
|
---|
| 116 | Return This
|
---|
| 117 | End Function
|
---|
| 118 |
|
---|
| 119 | Function Append(s As String, startIndex As Long, count As Long) As StringBuilder
|
---|
[385] | 120 | Return Append(StrPtr(s), startIndex, count)
|
---|
[272] | 121 | End Function
|
---|
| 122 |
|
---|
[468] | 123 | Function Append(s As *Char, startIndex As Long, count As Long) As StringBuilder
|
---|
[272] | 124 | If s = 0 Then
|
---|
| 125 | If startIndex = 0 And count = 0 Then
|
---|
| 126 | Return This
|
---|
| 127 | Else
|
---|
[388] | 128 | Throw New ArgumentNullException("StringBuilder.Append: An argument is null", "s")
|
---|
[272] | 129 | End If
|
---|
| 130 | ElseIf startIndex < 0 Or count < 0 Then
|
---|
[388] | 131 | Throw New ArgumentOutOfRangeException("StringBuilder.Append: One or more arguments are out of range value.", "startIndex or count or both")
|
---|
[272] | 132 | End If
|
---|
| 133 | appendCore(s, startIndex, count)
|
---|
| 134 | Return This
|
---|
| 135 | End Function
|
---|
| 136 | Private
|
---|
[468] | 137 | Sub appendCore(s As *Char, start As Long, count As Long)
|
---|
[272] | 138 | EnsureCapacity(size + count)
|
---|
| 139 | separateBuffer()
|
---|
| 140 | ActiveBasic.Strings.ChrCopy(VarPtr(chars[size]), VarPtr(s[start]), count As SIZE_T)
|
---|
| 141 | size += count
|
---|
| 142 | End Sub
|
---|
| 143 |
|
---|
| 144 | Sub appendCore(s As String, start As Long, count As Long)
|
---|
| 145 | EnsureCapacity(size + count)
|
---|
| 146 | separateBuffer()
|
---|
| 147 | s.CopyTo(start, chars, size, count)
|
---|
| 148 | size += count
|
---|
| 149 | End Sub
|
---|
| 150 | Public
|
---|
| 151 | 'AppendFormat
|
---|
| 152 |
|
---|
| 153 | Function AppendLine() As StringBuilder
|
---|
| 154 | separateBuffer()
|
---|
| 155 | Append(Environment.NewLine)
|
---|
| 156 | Return This
|
---|
| 157 | End Function
|
---|
| 158 |
|
---|
| 159 | Function AppendLine(s As String) As StringBuilder
|
---|
| 160 | EnsureCapacity(Capacity + s.Length + Environment.NewLine.Length)
|
---|
| 161 | separateBuffer()
|
---|
| 162 | Append(s)
|
---|
| 163 | Append(Environment.NewLine)
|
---|
| 164 | Return This
|
---|
| 165 | End Function
|
---|
| 166 |
|
---|
[468] | 167 | Const Sub CopyTo(sourceIndex As Long, ByRef dest[] As Char, destIndex As Long, count As Long)
|
---|
[272] | 168 | If dest = 0 Then
|
---|
[388] | 169 | Throw New ArgumentNullException("StringBuilder.CopyTo: An argument is null", "sourceIndex")
|
---|
[272] | 170 | ElseIf size < sourceIndex + count Or sourceIndex < 0 Or destIndex < 0 Or count < 0 Then
|
---|
[388] | 171 | Throw New ArgumentOutOfRangeException("StringBuilder.CopyTo: One or more arguments are out of range value.", "startIndex or count or both")
|
---|
[272] | 172 | End If
|
---|
| 173 |
|
---|
[468] | 174 | memcpy(VarPtr(dest[destIndex]), VarPtr(chars[sourceIndex]), count * SizeOf (Char))
|
---|
[272] | 175 | End Sub
|
---|
| 176 |
|
---|
| 177 | Function EnsureCapacity(c As Long) As Long
|
---|
| 178 | If c < 0 Or c > MaxCapacity Then
|
---|
[388] | 179 | Throw New ArgumentOutOfRangeException("StringBuilder.Append: An argument is out of range value.", "c")
|
---|
[272] | 180 | ElseIf c > Capacity Then
|
---|
[468] | 181 | Dim p = GC_malloc_atomic((c + 1) * SizeOf (Char)) As *Char
|
---|
[272] | 182 | ActiveBasic.Strings.ChrCopy(p, chars, size As SIZE_T)
|
---|
| 183 | chars = p
|
---|
| 184 | capacity = c
|
---|
| 185 | stringized = False
|
---|
| 186 | End If
|
---|
| 187 | End Function
|
---|
| 188 |
|
---|
| 189 | 'Override Function Equals(o As Object) As Boolean
|
---|
| 190 |
|
---|
| 191 | Const Function Equals(s As StringBuilder) As Boolean
|
---|
| 192 | Return ActiveBasic.Strings.StrCmp(chars, s.chars) = 0 _
|
---|
| 193 | And capacity = s.capacity _
|
---|
| 194 | And maxCapacity = s.maxCapacity
|
---|
| 195 | End Function
|
---|
| 196 |
|
---|
| 197 | Override Function GetHashCode() As Long
|
---|
| 198 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
| 199 | Dim n = (size + 1) >> 1
|
---|
| 200 | #else
|
---|
| 201 | Dim n = size
|
---|
| 202 | #endif
|
---|
| 203 | Return _System_GetHashFromWordArray(chars As *Word, n As SIZE_T) Xor capacity Xor maxCapacity
|
---|
| 204 | End Function
|
---|
| 205 |
|
---|
| 206 | Function Insert(i As Long, x As Boolean) As StringBuilder
|
---|
| 207 | rangeCheck(i)
|
---|
| 208 | insertCore(i, Str$(x))
|
---|
| 209 | Return This
|
---|
| 210 | End Function
|
---|
| 211 |
|
---|
[468] | 212 | Function Insert(i As Long, x As Char) As StringBuilder
|
---|
[272] | 213 | Insert(i, VarPtr(x), 0, 1)
|
---|
| 214 | Return This
|
---|
| 215 | End Function
|
---|
| 216 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
| 217 | Function Insert(i As Long, x As Word) As StringBuilder
|
---|
| 218 | rangeCheck(i)
|
---|
| 219 | insertCore(i, Str$(x As DWord))
|
---|
| 220 | Return This
|
---|
| 221 | End Function
|
---|
| 222 | #else
|
---|
| 223 | Function Insert(i As Long, x As SByte) As StringBuilder
|
---|
| 224 | rangeCheck(i)
|
---|
| 225 | insertCore(i, Str$(x As Long))
|
---|
| 226 | Return This
|
---|
| 227 | End Function
|
---|
| 228 | #endif
|
---|
| 229 |
|
---|
| 230 | Function Insert(i As Long, x As Byte) As StringBuilder
|
---|
| 231 | rangeCheck(i)
|
---|
| 232 | insertCore(i, Str$(x))
|
---|
| 233 | Return This
|
---|
| 234 | End Function
|
---|
| 235 |
|
---|
| 236 | Function Insert(i As Long, x As Integer) As StringBuilder
|
---|
| 237 | rangeCheck(i)
|
---|
| 238 | insertCore(i, Str$(x))
|
---|
| 239 | Return This
|
---|
| 240 | End Function
|
---|
| 241 |
|
---|
| 242 | Function Insert(i As Long, x As Long) As StringBuilder
|
---|
| 243 | rangeCheck(i)
|
---|
| 244 | insertCore(i, Str$(x))
|
---|
| 245 | Return This
|
---|
| 246 | End Function
|
---|
| 247 |
|
---|
| 248 | Function Insert(i As Long, x As DWord) As StringBuilder
|
---|
| 249 | rangeCheck(i)
|
---|
| 250 | insertCore(i, Str$(x))
|
---|
| 251 | Return This
|
---|
| 252 | End Function
|
---|
| 253 |
|
---|
| 254 | Function Insert(i As Long, x As Int64) As StringBuilder
|
---|
| 255 | rangeCheck(i)
|
---|
| 256 | insertCore(i, Str$(x))
|
---|
| 257 | Return This
|
---|
| 258 | End Function
|
---|
| 259 |
|
---|
| 260 | Function Insert(i As Long, x As Single) As StringBuilder
|
---|
| 261 | rangeCheck(i)
|
---|
| 262 | insertCore(i, Str$(x))
|
---|
| 263 | Return This
|
---|
| 264 | End Function
|
---|
| 265 |
|
---|
| 266 | Function Insert(i As Long, x As Double) As StringBuilder
|
---|
| 267 | rangeCheck(i)
|
---|
| 268 | insertCore(i, Str$(x))
|
---|
| 269 | Return This
|
---|
| 270 | End Function
|
---|
| 271 |
|
---|
| 272 | Function Insert(i As Long, s As String) As StringBuilder
|
---|
| 273 | rangeCheck(i)
|
---|
| 274 | insertCore(i, s)
|
---|
| 275 | Return This
|
---|
| 276 | End Function
|
---|
| 277 |
|
---|
| 278 | Function Insert(i As Long, o As Object) As StringBuilder
|
---|
| 279 | rangeCheck(i)
|
---|
| 280 | insertCore(i, o.ToString)
|
---|
| 281 | Return This
|
---|
| 282 | End Function
|
---|
| 283 |
|
---|
| 284 | Function Insert(index As Long, x As String, n As Long) As StringBuilder
|
---|
| 285 | rangeCheck(index)
|
---|
| 286 | If n < 0 Then
|
---|
[388] | 287 | Throw New ArgumentOutOfRangeException("StringBuilder.Insert: An argument is out of range value.", "n")
|
---|
[272] | 288 | End If
|
---|
| 289 | Dim len = x.Length
|
---|
| 290 | Dim lenTotal = len * n
|
---|
| 291 | Dim newSize = size + lenTotal
|
---|
| 292 | EnsureCapacity(newSize)
|
---|
| 293 | separateBuffer()
|
---|
[288] | 294 |
|
---|
[272] | 295 | Dim i As Long
|
---|
| 296 | For i = 0 To ELM(n)
|
---|
| 297 | x.CopyTo(0, chars, size + i * len, len)
|
---|
| 298 | Next
|
---|
| 299 | size = newSize
|
---|
| 300 | Return This
|
---|
| 301 | End Function
|
---|
| 302 |
|
---|
[468] | 303 | Function Insert(i As Long, x As *Char, index As Long, count As Long) As StringBuilder
|
---|
[272] | 304 | rangeCheck(i)
|
---|
| 305 | If x = 0 Then
|
---|
[388] | 306 | Throw New ArgumentNullException("StringBuilder.Insert: An argument is null", "x")
|
---|
[272] | 307 | ElseIf index < 0 Or count < 0 Then
|
---|
[388] | 308 | Throw New ArgumentOutOfRangeException("StringBuilder.Append: One or more arguments are out of range value.", "index or count or both")
|
---|
[272] | 309 | End If
|
---|
| 310 |
|
---|
| 311 | Dim newSize = size + count
|
---|
| 312 | EnsureCapacity(newSize)
|
---|
| 313 | separateBuffer()
|
---|
| 314 | ActiveBasic.Strings.ChrMove(VarPtr(chars[i + count]), VarPtr(chars[i]), (size - i) As SIZE_T)
|
---|
| 315 | ActiveBasic.Strings.ChrCopy(VarPtr(chars[i]), VarPtr(x[index]), count As SIZE_T)
|
---|
| 316 | size = newSize
|
---|
| 317 | Return This
|
---|
| 318 | End Function
|
---|
| 319 |
|
---|
| 320 | Private
|
---|
| 321 | Sub insertCore(i As Long, s As String)
|
---|
| 322 | Dim newSize = size + s.Length
|
---|
| 323 | EnsureCapacity(newSize)
|
---|
| 324 | separateBuffer()
|
---|
| 325 | ActiveBasic.Strings.ChrMove(VarPtr(chars[i + s.Length]), VarPtr(chars[i]), (size - i) As SIZE_T)
|
---|
| 326 | s.CopyTo(0, chars, i, s.Length)
|
---|
| 327 | size = newSize
|
---|
| 328 | End Sub
|
---|
| 329 | Public
|
---|
| 330 |
|
---|
| 331 | Function Remove(startIndex As Long, length As Long) As StringBuilder
|
---|
| 332 | rangeCheck(startIndex, length)
|
---|
| 333 | separateBuffer()
|
---|
| 334 |
|
---|
| 335 | Dim moveStart = startIndex + length
|
---|
| 336 | ActiveBasic.Strings.ChrMove(
|
---|
| 337 | VarPtr(chars[startIndex]), VarPtr(chars[moveStart]), (size - moveStart) As SIZE_T)
|
---|
| 338 | size -= length
|
---|
| 339 | Return This
|
---|
| 340 | End Function
|
---|
| 341 |
|
---|
[468] | 342 | Function Replace(oldChar As Char, newChar As Char) As StringBuilder
|
---|
[272] | 343 | replaceCore(oldChar, newChar, 0, size)
|
---|
| 344 | Return This
|
---|
| 345 | End Function
|
---|
| 346 |
|
---|
| 347 | Function Replace(oldStr As String, newStr As String) As StringBuilder
|
---|
| 348 | replaceCore(oldStr, newStr, 0, size)
|
---|
| 349 | Return This
|
---|
| 350 | End Function
|
---|
| 351 |
|
---|
[468] | 352 | Function Replace(oldChar As Char, newChar As Char, startIndex As Long, count As Long) As StringBuilder
|
---|
[272] | 353 | rangeCheck(startIndex, count)
|
---|
| 354 | replaceCore(oldChar, newChar, startIndex, count)
|
---|
| 355 | Return This
|
---|
| 356 | End Function
|
---|
| 357 |
|
---|
| 358 | Function Replace(oldStr As String, newStr As String, startIndex As Long, count As Long) As StringBuilder
|
---|
| 359 | rangeCheck(startIndex, count)
|
---|
| 360 | replaceCore(oldStr, newStr, startIndex, count)
|
---|
| 361 | Return This
|
---|
| 362 | End Function
|
---|
| 363 | Private
|
---|
[468] | 364 | Sub replaceCore(oldChar As Char, newChar As Char, start As Long, count As Long)
|
---|
[272] | 365 | separateBuffer()
|
---|
| 366 | Dim i As Long
|
---|
| 367 | Dim last = ELM(start + count)
|
---|
| 368 | For i = start To last
|
---|
| 369 | If chars[i] = oldChar Then
|
---|
| 370 | chars[i] = newChar
|
---|
| 371 | End If
|
---|
| 372 | Next
|
---|
| 373 | End Sub
|
---|
| 374 |
|
---|
| 375 | Sub replaceCore(oldStr As String, newStr As String, start As Long, count As Long)
|
---|
[385] | 376 | If ActiveBasic.IsNothing(oldStr) Then
|
---|
[388] | 377 | Throw New ArgumentNullException("StringBuilder.Replace: An argument is null", "oldStr")
|
---|
[272] | 378 | ElseIf oldStr.Length = 0 Then
|
---|
[385] | 379 | Throw New ArgumentException("StringBuilder.Replace: The argument 'oldStr' is empty string. ", "oldStr")
|
---|
[272] | 380 | End If
|
---|
| 381 |
|
---|
| 382 | Dim s = New StringBuilder(capacity, maxCapacity)
|
---|
[435] | 383 | Dim curPos = start
|
---|
| 384 | Dim last = start + count
|
---|
[272] | 385 | Do
|
---|
[468] | 386 | Dim nextPos = ActiveBasic.Strings.ChrFind(VarPtr(chars[curPos]) As *Char, size As SIZE_T, StrPtr(oldStr), oldStr.Length As SIZE_T) As Long
|
---|
[435] | 387 | If nextPos = -1 As SIZE_T Or curPos > last Then
|
---|
| 388 | s.appendCore(chars, curPos, size - curPos)
|
---|
| 389 | Exit Do
|
---|
[272] | 390 | End If
|
---|
| 391 |
|
---|
[385] | 392 | s.appendCore(chars, curPos, nextPos As Long)
|
---|
[272] | 393 | s.Append(newStr)
|
---|
[385] | 394 | curPos += nextPos As Long + oldStr.Length
|
---|
[272] | 395 | Loop
|
---|
| 396 | chars = s.chars
|
---|
| 397 | size = s.size
|
---|
| 398 | End Sub
|
---|
| 399 |
|
---|
| 400 | Public
|
---|
| 401 | Override Function ToString() As String
|
---|
| 402 | chars[size] = 0
|
---|
| 403 | Return New String(This)
|
---|
| 404 | End Function
|
---|
| 405 |
|
---|
| 406 | Const Function ToString(startIndex As Long, length As Long) As String
|
---|
| 407 | rangeCheck(startIndex, length)
|
---|
| 408 | Return New String(chars, startIndex, length)
|
---|
| 409 | End Function
|
---|
| 410 |
|
---|
[468] | 411 | Const Function Operator [](i As Long) As Char
|
---|
[272] | 412 | Return Chars[i]
|
---|
| 413 | End Function
|
---|
| 414 |
|
---|
[468] | 415 | Sub Operator []=(i As Long, c As Char)
|
---|
[272] | 416 | Chars[i] = c
|
---|
| 417 | End Sub
|
---|
| 418 |
|
---|
| 419 | 'Properties
|
---|
| 420 | Const Function Capacity() As Long
|
---|
| 421 | Return capacity
|
---|
| 422 | End Function
|
---|
| 423 |
|
---|
| 424 | Sub Capacity(c As Long)
|
---|
| 425 | If c < size Or c > MaxCapacity Then 'sizeとの比較でcが負の場合も対応
|
---|
[388] | 426 | Throw New ArgumentOutOfRangeException("StringBuilder.Capacity: An argument is out of range value.", "c")
|
---|
[272] | 427 | End If
|
---|
| 428 | EnsureCapacity(c)
|
---|
| 429 | End Sub
|
---|
| 430 |
|
---|
[468] | 431 | Const Function Chars(i As Long) As Char
|
---|
[272] | 432 | If i >= Length Or i < 0 Then
|
---|
[388] | 433 | Throw New IndexOutOfRangeException("StringBuilder.Chars: The index argument 'i' is out of range value.")
|
---|
[272] | 434 | End If
|
---|
| 435 | Return chars[i]
|
---|
| 436 | End Function
|
---|
| 437 |
|
---|
[468] | 438 | Sub Chars(i As Long, c As Char)
|
---|
[272] | 439 | If i >= Length Or i < 0 Then
|
---|
[388] | 440 | Throw New ArgumentOutOfRangeException("StringBuilder.Chars: An argument is out of range value.", "i")
|
---|
[272] | 441 | End If
|
---|
| 442 | chars[i] = c
|
---|
| 443 | End Sub
|
---|
| 444 |
|
---|
| 445 | Const Function Length() As Long
|
---|
| 446 | Return size
|
---|
| 447 | End Function
|
---|
| 448 |
|
---|
| 449 | Sub Length(i As Long)
|
---|
| 450 | EnsureCapacity(i) 'iが適切な値かどうかの確認はこの中で行う
|
---|
| 451 | If size < i Then
|
---|
[468] | 452 | ActiveBasic.Strings.ChrFill(VarPtr(chars[size]), (i - size + 1) As SIZE_T, 0 As Char)
|
---|
[272] | 453 | End If
|
---|
| 454 | size = i
|
---|
| 455 | End Sub
|
---|
| 456 |
|
---|
| 457 | Const Function MaxCapacity() As Long
|
---|
| 458 | Return maxCapacity
|
---|
| 459 | End Function
|
---|
| 460 |
|
---|
[468] | 461 | Function __Chars() As *Char
|
---|
[272] | 462 | Return chars
|
---|
| 463 | End Function
|
---|
| 464 |
|
---|
| 465 | Sub __Stringized()
|
---|
| 466 | stringized = True
|
---|
| 467 | End Sub
|
---|
| 468 |
|
---|
| 469 | Private
|
---|
| 470 | Sub initialize(capacity As Long, maxCapacity = LONG_MAX As Long)
|
---|
| 471 | If capacity < 0 Or maxCapacity < 1 Or maxCapacity < capacity Then
|
---|
[388] | 472 | Throw New ArgumentOutOfRangeException("StringBuilder constructor: One or more arguments are out of range value.", "capacity or maxCapacity or both")
|
---|
[272] | 473 | End If
|
---|
| 474 |
|
---|
| 475 | If capacity = 0 Then
|
---|
| 476 | This.capacity = 1024
|
---|
| 477 | Else
|
---|
| 478 | This.capacity = capacity
|
---|
| 479 | End If
|
---|
| 480 |
|
---|
| 481 | This.maxCapacity = maxCapacity
|
---|
| 482 | This.size = 0
|
---|
[468] | 483 | This.chars = GC_malloc_atomic((This.capacity + 1) * SizeOf (Char))
|
---|
[272] | 484 | End Sub
|
---|
| 485 |
|
---|
| 486 | Sub initialize(s As String, startIndex As Long, length As Long, capacity As Long, maxCapacity = LONG_MAX As Long)
|
---|
| 487 | StringBuilder.rangeCheck2(s.Length, startIndex, length)
|
---|
| 488 |
|
---|
| 489 | initialize(Math.Max(capacity, length), maxCapacity)
|
---|
| 490 |
|
---|
| 491 | If Not String.IsNullOrEmpty(s) Then
|
---|
| 492 | s.CopyTo(startIndex, chars, 0, length)
|
---|
[383] | 493 | size = length
|
---|
[272] | 494 | End If
|
---|
| 495 | End Sub
|
---|
| 496 |
|
---|
| 497 | Sub rangeCheck(index As Long)
|
---|
| 498 | If index < 0 Or index > size Then
|
---|
[388] | 499 | Throw New ArgumentOutOfRangeException("StringBuilder: Index argument is out of range value.")
|
---|
[272] | 500 | End If
|
---|
| 501 | End Sub
|
---|
| 502 |
|
---|
| 503 | Sub rangeCheck(startIndex As Long, count As Long)
|
---|
| 504 | StringBuilder.rangeCheck2(size, startIndex, count)
|
---|
| 505 | End Sub
|
---|
| 506 |
|
---|
| 507 | Static Sub rangeCheck2(length As Long, startIndex As Long, count As Long)
|
---|
| 508 | 'length < 0は判定に入っていないことに注意
|
---|
| 509 | If startIndex < 0 Or count < 0 Or startIndex + count > length Then
|
---|
[388] | 510 | Throw New ArgumentOutOfRangeException("StringBuilder: One or more arguments are out of range value.", "startIndex or count or both")
|
---|
[272] | 511 | End If
|
---|
| 512 | End Sub
|
---|
| 513 |
|
---|
| 514 | Sub separateBuffer()
|
---|
| 515 | If stringized Then
|
---|
[468] | 516 | Dim newChars = GC_malloc_atomic(SizeOf (Char) * capacity) As *Char
|
---|
[272] | 517 | ActiveBasic.Strings.ChrCopy(newChars, chars, capacity As SIZE_T)
|
---|
| 518 | chars = newChars
|
---|
| 519 | stringized = False
|
---|
| 520 | End If
|
---|
| 521 | End Sub
|
---|
| 522 |
|
---|
[468] | 523 | chars As *Char
|
---|
[272] | 524 | maxCapacity As Long
|
---|
| 525 | capacity As Long
|
---|
| 526 | size As Long
|
---|
| 527 | stringized As Boolean
|
---|
| 528 | End Class
|
---|
| 529 |
|
---|
| 530 | End Namespace 'Text
|
---|
| 531 | End Namespace 'System
|
---|
| 532 |
|
---|
| 533 | '暫定
|
---|
[468] | 534 | Function StrPtr(sb As System.Text.StringBuilder) As *Char
|
---|
[272] | 535 | Return sb.__Chars
|
---|
| 536 | End Function
|
---|