[132] | 1 | ' Classes/System/String.ab
|
---|
| 2 |
|
---|
| 3 | #require <basic/function.sbp>
|
---|
[272] | 4 | #require <Classes/System/Text/StringBuilder.ab>
|
---|
| 5 | #require <Classes/ActiveBasic/Strings/Strings.ab>
|
---|
[132] | 6 |
|
---|
[139] | 7 | #ifdef __STRING_IS_NOT_ALWAYS_UNICODE
|
---|
[383] | 8 |
|
---|
| 9 | #ifndef UNICODE
|
---|
[139] | 10 | TypeDef StrChar = Char
|
---|
| 11 | #define __STRING_IS_NOT_UNICODE
|
---|
| 12 | #endif
|
---|
[383] | 13 |
|
---|
| 14 | #endif
|
---|
| 15 |
|
---|
| 16 | #ifndef __STRING_IS_NOT_UNICODE
|
---|
| 17 | TypeDef StrChar = WCHAR
|
---|
| 18 |
|
---|
| 19 | #ifdef UNICODE
|
---|
| 20 | #define __STRING_IS_UNICODE
|
---|
[139] | 21 | #else
|
---|
[142] | 22 | #define __STRING_UNICODE_WINDOWS_ANSI
|
---|
[139] | 23 | #endif
|
---|
[142] | 24 | #endif
|
---|
[139] | 25 |
|
---|
[246] | 26 | Namespace System
|
---|
[203] | 27 |
|
---|
[246] | 28 | Class String
|
---|
| 29 | ' Inherits IComparable, ICloneable, IConvertible, IEnumerable
|
---|
[1] | 30 |
|
---|
[246] | 31 | m_Length As Long
|
---|
| 32 | Chars As *StrChar
|
---|
[1] | 33 |
|
---|
[272] | 34 | Sub validPointerCheck(p As VoidPtr, size = 1 As Long)
|
---|
| 35 | If p As ULONG_PTR < &h10000 Then
|
---|
| 36 | 'Throw ArgumentException
|
---|
| 37 | Debug
|
---|
| 38 | ElseIf IsBadReadPtr(p, size As ULONG_PTR) Then
|
---|
| 39 | 'Throw ArgumentException
|
---|
| 40 | Debug
|
---|
| 41 | End If
|
---|
| 42 | End Sub
|
---|
| 43 | Public
|
---|
| 44 | Static Const Empty = New String
|
---|
| 45 |
|
---|
[246] | 46 | Sub String()
|
---|
[272] | 47 | ' Chars = 0
|
---|
| 48 | ' m_Length = 0
|
---|
[246] | 49 | End Sub
|
---|
[30] | 50 |
|
---|
[272] | 51 | Sub String(initStr As PCWSTR)
|
---|
| 52 | validPointerCheck(initStr)
|
---|
| 53 | Assign(initStr, lstrlenW(initStr))
|
---|
[246] | 54 | End Sub
|
---|
[125] | 55 |
|
---|
[272] | 56 | Sub String(initStr As PCWSTR, length As Long)
|
---|
| 57 | validPointerCheck(initStr, length)
|
---|
[246] | 58 | Assign(initStr, length)
|
---|
| 59 | End Sub
|
---|
[139] | 60 |
|
---|
[272] | 61 | Sub String(initStr As PCWSTR, start As Long, length As Long)
|
---|
| 62 | If start < 0 Or length Or start + length < 0 Then
|
---|
[388] | 63 | Throw New ArgumentOutOfRangeException("String constractor: One or more arguments are out of range value.", "start or length or both")
|
---|
[272] | 64 | End If
|
---|
| 65 | validPointerCheck(initStr + start, length)
|
---|
| 66 | Assign(initStr + start, length)
|
---|
[246] | 67 | End Sub
|
---|
[139] | 68 |
|
---|
[272] | 69 | Sub String(initStr As PCSTR)
|
---|
| 70 | validPointerCheck(initStr)
|
---|
| 71 | Assign(initStr, lstrlenA(initStr))
|
---|
[246] | 72 | End Sub
|
---|
[49] | 73 |
|
---|
[272] | 74 | Sub String(initStr As PCSTR, length As Long)
|
---|
| 75 | validPointerCheck(initStr, length)
|
---|
| 76 | Assign(initStr, length)
|
---|
[246] | 77 | End Sub
|
---|
[121] | 78 |
|
---|
[272] | 79 | Sub String(initStr As PCSTR, start As Long, length As Long)
|
---|
[388] | 80 | If start < 0 Or length < 0 Then
|
---|
| 81 | Throw New ArgumentOutOfRangeException("String constructor: One or more arguments are out of range value.", "start or length or both")
|
---|
[272] | 82 | End If
|
---|
| 83 | validPointerCheck(initStr + start, length)
|
---|
| 84 | Assign(initStr + start, length)
|
---|
[246] | 85 | End Sub
|
---|
[383] | 86 |
|
---|
[272] | 87 | Sub String(initStr As String)
|
---|
| 88 | If Not String.IsNullOrEmpty(initStr) Then
|
---|
| 89 | Assign(initStr.Chars, initStr.m_Length)
|
---|
| 90 | End If
|
---|
| 91 | End Sub
|
---|
[1] | 92 |
|
---|
[246] | 93 | Sub String(initChar As StrChar, length As Long)
|
---|
[272] | 94 | AllocStringBuffer(length)
|
---|
| 95 | ActiveBasic.Strings.ChrFill(Chars, length, initChar)
|
---|
| 96 | Chars[length] = 0
|
---|
[246] | 97 | End Sub
|
---|
| 98 |
|
---|
[388] | 99 | Sub String(sb As Text.StringBuilder)
|
---|
[272] | 100 | Chars = StrPtr(sb)
|
---|
| 101 | m_Length = sb.Length
|
---|
| 102 | sb.__Stringized()
|
---|
[246] | 103 | End Sub
|
---|
[1] | 104 |
|
---|
[246] | 105 | Const Function Length() As Long
|
---|
| 106 | Return m_Length
|
---|
| 107 | End Function
|
---|
[30] | 108 |
|
---|
[246] | 109 | Function Operator() As *StrChar
|
---|
| 110 | Return Chars
|
---|
| 111 | End Function
|
---|
[1] | 112 |
|
---|
[246] | 113 | Const Function Operator [] (n As Long) As StrChar
|
---|
[272] | 114 | rangeCheck(n)
|
---|
[246] | 115 | Return Chars[n]
|
---|
| 116 | End Function
|
---|
[1] | 117 |
|
---|
[272] | 118 | Const Function Operator + (y As PCSTR) As String
|
---|
[426] | 119 | If y = 0 Then
|
---|
| 120 | Return This
|
---|
| 121 | Else
|
---|
| 122 | Return Concat(y, lstrlenA(y))
|
---|
| 123 | End If
|
---|
[246] | 124 | End Function
|
---|
[139] | 125 |
|
---|
[272] | 126 | Const Function Operator + (y As PCWSTR) As String
|
---|
[426] | 127 | If y = 0 Then
|
---|
| 128 | Return This
|
---|
| 129 | Else
|
---|
| 130 | Return Concat(y, lstrlenW(y))
|
---|
| 131 | End If
|
---|
[246] | 132 | End Function
|
---|
[139] | 133 |
|
---|
[272] | 134 | Const Function Operator + (y As String) As String
|
---|
[426] | 135 | If ActiveBasic.IsNothing(y) Then
|
---|
| 136 | Return This
|
---|
| 137 | Else
|
---|
| 138 | Return Concat(y.Chars, y.m_Length)
|
---|
| 139 | End If
|
---|
[246] | 140 | End Function
|
---|
[1] | 141 |
|
---|
[272] | 142 | Const Function Operator & (y As PCSTR) As String
|
---|
| 143 | Return This + y
|
---|
[246] | 144 | End Function
|
---|
[1] | 145 |
|
---|
[272] | 146 | Const Function Operator & (y As PCWSTR) As String
|
---|
[426] | 147 | Return This + y
|
---|
[246] | 148 | End Function
|
---|
[139] | 149 |
|
---|
[272] | 150 | Const Function Operator & (y As String) As String
|
---|
[426] | 151 | Return This + y
|
---|
[246] | 152 | End Function
|
---|
[1] | 153 |
|
---|
[272] | 154 | Const Function Operator == (y As String) As Boolean
|
---|
| 155 | Return String.Compare(This, y) = 0
|
---|
[246] | 156 | End Function
|
---|
[1] | 157 |
|
---|
[272] | 158 | Const Function Operator == (y As *StrChar) As Boolean
|
---|
| 159 | Return String.Compare(This, y) = 0
|
---|
[246] | 160 | End Function
|
---|
[1] | 161 |
|
---|
[272] | 162 | Const Function Operator <> (y As String) As Boolean
|
---|
| 163 | Return String.Compare(This, y) <> 0
|
---|
[246] | 164 | End Function
|
---|
[1] | 165 |
|
---|
[272] | 166 | Const Function Operator <> (y As *StrChar) As Boolean
|
---|
| 167 | Return String.Compare(This, y) <> 0
|
---|
[246] | 168 | End Function
|
---|
[1] | 169 |
|
---|
[272] | 170 | Const Function Operator < (y As String) As Boolean
|
---|
| 171 | Return String.Compare(This, y) < 0
|
---|
[246] | 172 | End Function
|
---|
[1] | 173 |
|
---|
[272] | 174 | Const Function Operator < (y As *StrChar) As Boolean
|
---|
| 175 | Return String.Compare(This, y) < 0
|
---|
[246] | 176 | End Function
|
---|
[1] | 177 |
|
---|
[272] | 178 | Const Function Operator > (y As String) As Boolean
|
---|
| 179 | Return String.Compare(This, y) > 0
|
---|
[246] | 180 | End Function
|
---|
[1] | 181 |
|
---|
[272] | 182 | Const Function Operator > (y As *StrChar) As Boolean
|
---|
| 183 | Return String.Compare(This, y) > 0
|
---|
[246] | 184 | End Function
|
---|
[1] | 185 |
|
---|
[272] | 186 | Const Function Operator <= (y As String) As Boolean
|
---|
| 187 | Return String.Compare(This, y) <= 0
|
---|
[246] | 188 | End Function
|
---|
[1] | 189 |
|
---|
[272] | 190 | Const Function Operator <= (y As *StrChar) As Boolean
|
---|
| 191 | Return String.Compare(This, y) <= 0
|
---|
[246] | 192 | End Function
|
---|
[1] | 193 |
|
---|
[272] | 194 | Const Function Operator >= (y As String) As Boolean
|
---|
| 195 | Return String.Compare(This, y) >= 0
|
---|
[246] | 196 | End Function
|
---|
[1] | 197 |
|
---|
[272] | 198 | Const Function Operator >= (y As *StrChar) As Boolean
|
---|
| 199 | Return String.Compare(This, y) >= 0
|
---|
[246] | 200 | End Function
|
---|
[1] | 201 |
|
---|
[246] | 202 | Static Function Compare(x As String, y As String) As Long
|
---|
| 203 | Return CompareOrdinal(x, y)
|
---|
| 204 | End Function
|
---|
[203] | 205 |
|
---|
[272] | 206 | Public
|
---|
[246] | 207 | Static Function Compare(x As String, indexX As Long, y As String, indexY As Long, length As Long) As Long
|
---|
[272] | 208 | Return String.CompareOrdinal(x, indexX, y, indexY, length)
|
---|
[246] | 209 | End Function
|
---|
[203] | 210 |
|
---|
[246] | 211 | Static Function CompareOrdinal(x As String, y As String) As Long
|
---|
[272] | 212 | Return String.CompareOrdinal(x.Chars, y.Chars)
|
---|
[246] | 213 | End Function
|
---|
[203] | 214 |
|
---|
[246] | 215 | Static Function CompareOrdinal(x As String, indexX As Long, y As String, indexY As Long, length As Long) As Long
|
---|
[272] | 216 | Return String.CompareOrdinal(x.Chars, indexX, y.Chars, indexY, length)
|
---|
| 217 | End Function
|
---|
| 218 | Private
|
---|
| 219 | Static Function Compare(x As String, y As *StrChar) As Long
|
---|
| 220 | Return String.CompareOrdinal(x, y)
|
---|
| 221 | End Function
|
---|
| 222 |
|
---|
| 223 | Static Function CompareOrdinal(x As String, y As *StrChar) As Long
|
---|
| 224 | Return String.CompareOrdinal(x.Chars, y)
|
---|
| 225 | End Function
|
---|
| 226 |
|
---|
| 227 | Static Function CompareOrdinal(x As *StrChar, y As *StrChar) As Long
|
---|
| 228 | If x = 0 Then
|
---|
| 229 | If y = 0 Then
|
---|
[246] | 230 | Return 0
|
---|
| 231 | Else
|
---|
| 232 | Return -1
|
---|
| 233 | End If
|
---|
[272] | 234 | ElseIf y = 0 Then
|
---|
[246] | 235 | Return 1
|
---|
[203] | 236 | End If
|
---|
[272] | 237 | Return ActiveBasic.Strings.StrCmp(x, y)
|
---|
[246] | 238 | End Function
|
---|
[203] | 239 |
|
---|
[272] | 240 | Static Function CompareOrdinal(x As *StrChar, indexX As Long, y As *StrChar, indexY As Long, length As Long) As Long
|
---|
| 241 | If x = 0 Then
|
---|
| 242 | If y = 0 Then
|
---|
| 243 | Return 0
|
---|
| 244 | Else
|
---|
| 245 | Return -1
|
---|
| 246 | End If
|
---|
| 247 | ElseIf y = 0 Then
|
---|
| 248 | Return 1
|
---|
| 249 | End If
|
---|
| 250 | Return ActiveBasic.Strings.ChrCmp(VarPtr(x[indexX]), VarPtr(y[indexY]), length As SIZE_T)
|
---|
| 251 | End Function
|
---|
| 252 | Public
|
---|
[246] | 253 | Function CompareTo(y As String) As Long
|
---|
| 254 | Return String.Compare(This, y)
|
---|
| 255 | End Function
|
---|
[203] | 256 |
|
---|
[246] | 257 | Function CompareTo(y As Object) As Long
|
---|
[388] | 258 | If Not Object.Equals(This.GetType(), y.GetType()) Then
|
---|
| 259 | Throw New ArgumentException("String.CompareTo: An argument is out of range value.", "y")
|
---|
| 260 | End If
|
---|
[272] | 261 | Return CompareTo(y As String)
|
---|
[246] | 262 | End Function
|
---|
[203] | 263 |
|
---|
[308] | 264 | Function Equals(s As String) As Boolean
|
---|
| 265 | Return This = s
|
---|
| 266 | End Function
|
---|
| 267 |
|
---|
| 268 | Override Function Equals(s As Object) As Boolean
|
---|
[366] | 269 | If Object.Equals( This.GetType(), s.GetType() ) Then
|
---|
[308] | 270 | Return This.Equals(s As String)
|
---|
| 271 | End If
|
---|
| 272 | Return False
|
---|
| 273 | End Function
|
---|
| 274 |
|
---|
[246] | 275 | Const Function StrPtr() As *StrChar
|
---|
| 276 | Return Chars
|
---|
| 277 | End Function
|
---|
[272] | 278 | Private
|
---|
[1] | 279 |
|
---|
[246] | 280 | Sub Assign(text As PCSTR, textLengthA As Long)
|
---|
[175] | 281 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
[246] | 282 | AssignFromStrChar(text, textLengthA)
|
---|
[139] | 283 | #else
|
---|
[246] | 284 | Dim textLengthW = MultiByteToWideChar(CP_THREAD_ACP, 0, text, textLengthA, 0, 0)
|
---|
| 285 | If AllocStringBuffer(textLengthW) <> 0 Then
|
---|
| 286 | MultiByteToWideChar(CP_THREAD_ACP, 0, text, textLengthA, Chars, textLengthW)
|
---|
| 287 | Chars[textLengthW] = 0
|
---|
| 288 | End If
|
---|
[139] | 289 | #endif
|
---|
[246] | 290 | End Sub
|
---|
[1] | 291 |
|
---|
[246] | 292 | Sub Assign(text As PCWSTR, textLengthW As Long)
|
---|
[139] | 293 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
[246] | 294 | Dim textLengthA = WideCharToMultiByte(CP_THREAD_ACP, 0, text, textLengthW, 0, 0, 0, 0)
|
---|
| 295 | If AllocStringBuffer(textLengthA) <> 0 Then
|
---|
| 296 | WideCharToMultiByte(CP_THREAD_ACP, 0, text, textLengthW, Chars, textLengthA, 0, 0)
|
---|
| 297 | Chars[textLengthA] = 0
|
---|
| 298 | End If
|
---|
[139] | 299 | #else
|
---|
[246] | 300 | AssignFromStrChar(text, textLengthW)
|
---|
[139] | 301 | #endif
|
---|
[246] | 302 | End Sub
|
---|
[139] | 303 |
|
---|
[246] | 304 | Private
|
---|
| 305 | Static Function ConcatStrChar(text1 As *StrChar, text1Length As Long, text2 As *StrChar, text2Length As Long) As String
|
---|
| 306 | ConcatStrChar = New String()
|
---|
| 307 | With ConcatStrChar
|
---|
| 308 | .AllocStringBuffer(text1Length + text2Length)
|
---|
[272] | 309 | ActiveBasic.Strings.ChrCopy(.Chars, text1, text1Length As SIZE_T)
|
---|
| 310 | ActiveBasic.Strings.ChrCopy(VarPtr(.Chars[text1Length]), text2, text2Length As SIZE_T)
|
---|
[246] | 311 | .Chars[text1Length + text2Length] = 0
|
---|
| 312 | End With
|
---|
| 313 | End Function
|
---|
| 314 | Public
|
---|
| 315 | Const Function Concat(text As PCSTR, len As Long) As String
|
---|
[139] | 316 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
[246] | 317 | Return ConcatStrChar(This.Chars, m_Length, text, len)
|
---|
[139] | 318 | #else
|
---|
[246] | 319 | With Concat
|
---|
| 320 | Dim lenW = MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, 0, 0)
|
---|
[272] | 321 | Concat = New String
|
---|
[246] | 322 | .AllocStringBuffer(m_Length + lenW)
|
---|
[400] | 323 | ActiveBasic.Strings.ChrCopy(.Chars, This.Chars, m_Length As SIZE_T)
|
---|
[246] | 324 | MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, VarPtr(.Chars[m_Length]), lenW)
|
---|
| 325 | .Chars[m_Length + lenW] = 0
|
---|
| 326 | End With
|
---|
[139] | 327 | #endif
|
---|
[246] | 328 | End Function
|
---|
[132] | 329 |
|
---|
[246] | 330 | Const Function Concat(text As PCWSTR, len As Long) As String
|
---|
[139] | 331 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
[246] | 332 | With Concat
|
---|
[272] | 333 | Concat = New String
|
---|
[246] | 334 | Dim lenA = WideCharToMultiByte(CP_THREAD_ACP, 0, text, len, 0, 0, 0, 0)
|
---|
| 335 | .AllocStringBuffer(m_Length + lenA)
|
---|
[272] | 336 | ActiveBasic.Strings.ChrCopy(.Chars, This.Chars, m_Length As SIZE_T)
|
---|
[246] | 337 | WideCharToMultiByte(CP_THREAD_ACP, 0, text, len, VarPtr(.Chars[m_Length]), lenA, 0, 0)
|
---|
| 338 | .Chars[m_Length + lenA] = 0
|
---|
| 339 | End With
|
---|
[139] | 340 | #else
|
---|
[246] | 341 | Return ConcatStrChar(This.Chars, m_Length, text, len)
|
---|
[139] | 342 | #endif
|
---|
[246] | 343 | End Function
|
---|
[203] | 344 |
|
---|
[246] | 345 | Static Function Concat(x As String, y As String) As String
|
---|
| 346 | If String.IsNullOrEmpty(x) Then
|
---|
| 347 | Return y
|
---|
| 348 | Else
|
---|
| 349 | Return x.Concat(y.Chars, y.m_Length)
|
---|
| 350 | End If
|
---|
| 351 | End Function
|
---|
[203] | 352 |
|
---|
[246] | 353 | Static Function Concat(x As Object, y As Object) As String
|
---|
| 354 | Return String.Concat(x.ToString, y.ToString)
|
---|
| 355 | End Function
|
---|
[203] | 356 |
|
---|
[428] | 357 | Const Function Contains(c As StrChar) As Boolean
|
---|
| 358 | Return IndexOf(c) >= 0
|
---|
| 359 | End Function
|
---|
| 360 |
|
---|
[272] | 361 | Const Function Contains(s As String) As Boolean
|
---|
| 362 | If Object.ReferenceEquals(s, Nothing) Then
|
---|
[391] | 363 | Throw New ArgumentNullException("String.Contains: An argument is null value.", "s")
|
---|
[388] | 364 | ElseIf s = "" Then
|
---|
| 365 | Return True
|
---|
| 366 | Else
|
---|
| 367 | Return IndexOf(s, 0, m_Length) >= 0
|
---|
[272] | 368 | End If
|
---|
[246] | 369 | End Function
|
---|
[1] | 370 |
|
---|
[272] | 371 | Const Function IndexOf(c As StrChar) As Long
|
---|
| 372 | Return indexOfCore(c, 0, m_Length)
|
---|
[246] | 373 | End Function
|
---|
[1] | 374 |
|
---|
[272] | 375 | Const Function IndexOf(c As StrChar, start As Long) As Long
|
---|
| 376 | rangeCheck(start)
|
---|
| 377 | Return indexOfCore(c, start, m_Length - start)
|
---|
[246] | 378 | End Function
|
---|
[1] | 379 |
|
---|
[272] | 380 | Const Function IndexOf(c As StrChar, start As Long, count As Long) As Long
|
---|
| 381 | rangeCheck(start, count)
|
---|
| 382 | Return indexOfCore(c, start, count)
|
---|
[246] | 383 | End Function
|
---|
[272] | 384 | Private
|
---|
| 385 | Const Function indexOfCore(c As StrChar, start As Long, count As Long) As Long
|
---|
[370] | 386 | indexOfCore = ActiveBasic.Strings.ChrFind(VarPtr(Chars[start]), count, c) As Long
|
---|
[272] | 387 | If indexOfCore <> -1 Then
|
---|
| 388 | indexOfCore += start
|
---|
| 389 | End If
|
---|
| 390 | End Function
|
---|
| 391 | Public
|
---|
| 392 | Const Function IndexOf(s As String) As Long
|
---|
| 393 | Return IndexOf(s, 0, m_Length)
|
---|
| 394 | End Function
|
---|
[1] | 395 |
|
---|
[272] | 396 | Const Function IndexOf(s As String, startIndex As Long) As Long
|
---|
| 397 | Return IndexOf(s, startIndex, m_Length - startIndex)
|
---|
| 398 | End Function
|
---|
[1] | 399 |
|
---|
[272] | 400 | Const Function IndexOf(s As String, startIndex As Long, count As Long) As Long
|
---|
| 401 | rangeCheck(startIndex, count)
|
---|
| 402 | If Object.ReferenceEquals(s, Nothing) Then
|
---|
[388] | 403 | Throw New ArgumentNullException("String.IndexOf: An argument is out of range value.", "s")
|
---|
[272] | 404 | End If
|
---|
[1] | 405 |
|
---|
[272] | 406 | Dim length = s.Length
|
---|
[246] | 407 | If length = 0 Then Return startIndex
|
---|
[1] | 408 |
|
---|
[246] | 409 | Dim i As Long, j As Long
|
---|
| 410 | For i = startIndex To startIndex + count - 1
|
---|
| 411 | For j = 0 To length - 1
|
---|
[272] | 412 | If Chars[i + j] = s[j] Then
|
---|
[246] | 413 | If j = length - 1 Then Return i
|
---|
| 414 | Else
|
---|
| 415 | Exit For
|
---|
| 416 | End If
|
---|
| 417 | Next
|
---|
[1] | 418 | Next
|
---|
[246] | 419 | Return -1
|
---|
| 420 | End Function
|
---|
[1] | 421 |
|
---|
[388] | 422 | Const Function LastIndexOf(c As StrChar) As Long
|
---|
| 423 | Return lastIndexOf(c, m_Length - 1, m_Length)
|
---|
| 424 | End Function
|
---|
| 425 |
|
---|
| 426 | Const Function LastIndexOf(c As StrChar, start As Long) As Long
|
---|
| 427 | rangeCheck(start)
|
---|
| 428 | Return lastIndexOf(c, start, start + 1)
|
---|
| 429 | End Function
|
---|
| 430 |
|
---|
| 431 | Const Function LastIndexOf(c As StrChar, start As Long, count As Long) As Long
|
---|
| 432 | rangeCheck(start)
|
---|
| 433 | Dim lastFindPos = start - (count - 1)
|
---|
| 434 | If Not (m_Length > lastFindPos And lastFindPos >= 0) Then
|
---|
| 435 | Throw New ArgumentOutOfRangeException("String.LastIndexOf: An argument is out of range value.", "count")
|
---|
| 436 | End If
|
---|
| 437 | Return lastIndexOf(c, start, count)
|
---|
| 438 | End Function
|
---|
| 439 | Private
|
---|
| 440 | Const Function lastIndexOf(c As StrChar, start As Long, count As Long) As Long
|
---|
| 441 | Dim lastFindPos = start - (count - 1)
|
---|
| 442 | Dim i As Long
|
---|
| 443 | For i = start To lastFindPos Step -1
|
---|
| 444 | If Chars[i] = c Then
|
---|
| 445 | Return i
|
---|
| 446 | End If
|
---|
| 447 | Next
|
---|
| 448 | Return -1
|
---|
| 449 | End Function
|
---|
| 450 |
|
---|
| 451 | Public
|
---|
[272] | 452 | Const Function LastIndexOf(s As String) As Long
|
---|
| 453 | Return LastIndexOf(s, m_Length - 1, m_Length)
|
---|
[246] | 454 | End Function
|
---|
[1] | 455 |
|
---|
[272] | 456 | Const Function LastIndexOf(s As String, startIndex As Long) As Long
|
---|
| 457 | Return LastIndexOf(s, startIndex, startIndex + 1)
|
---|
[246] | 458 | End Function
|
---|
[1] | 459 |
|
---|
[388] | 460 | Const Function LastIndexOf(s As String, start As Long, count As Long) As Long
|
---|
[272] | 461 | If Object.ReferenceEquals(s, Nothing) Then
|
---|
[388] | 462 | Throw New ArgumentNullException("String.LastIndexOf: An argument is out of range value.", "s")
|
---|
[272] | 463 | End If
|
---|
[1] | 464 |
|
---|
[388] | 465 | If start < 0 Or start > m_Length - 1 Or _
|
---|
| 466 | count < 0 Or count > start + 2 Then
|
---|
| 467 | Throw New ArgumentOutOfRangeException("String.LastIndexOf: One or more arguments are out of range value.", "start or count or both")
|
---|
[272] | 468 | End If
|
---|
[388] | 469 | Dim length = s.m_Length
|
---|
[246] | 470 | If length > m_Length Then Return -1
|
---|
[388] | 471 | If length = 0 Then Return start
|
---|
[1] | 472 |
|
---|
[246] | 473 | Dim i As Long, j As Long
|
---|
[388] | 474 | For i = start To start - count + 1 Step -1
|
---|
[246] | 475 | For j = length - 1 To 0 Step -1
|
---|
[272] | 476 | If Chars[i + j] = s[j] Then
|
---|
[246] | 477 | If j = 0 Then Return i
|
---|
| 478 | Else
|
---|
| 479 | Exit For
|
---|
| 480 | End If
|
---|
| 481 | Next
|
---|
[1] | 482 | Next
|
---|
[246] | 483 | Return -1
|
---|
| 484 | End Function
|
---|
[1] | 485 |
|
---|
[428] | 486 | Const Function StartsWith(c As StrChar) As Boolean
|
---|
| 487 | Return IndexOf(c) = 0
|
---|
| 488 | End Function
|
---|
| 489 |
|
---|
[272] | 490 | Const Function StartsWith(s As String) As Boolean
|
---|
| 491 | Return IndexOf(s) = 0
|
---|
[246] | 492 | End Function
|
---|
[1] | 493 |
|
---|
[428] | 494 | Const Function EndsWith(c As StrChar) As Boolean
|
---|
| 495 | Return LastIndexOf(c) = m_Length - 1
|
---|
| 496 | End Function
|
---|
| 497 |
|
---|
[272] | 498 | Const Function EndsWith(s As String) As Boolean
|
---|
| 499 | Return LastIndexOf(s) = m_Length - s.Length
|
---|
[246] | 500 | End Function
|
---|
[1] | 501 |
|
---|
[246] | 502 | Const Function Insert(startIndex As Long, text As String) As String
|
---|
[388] | 503 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 504 | sb.Insert(startIndex, text)
|
---|
| 505 | Return sb.ToString
|
---|
[246] | 506 | End Function
|
---|
[1] | 507 |
|
---|
[272] | 508 | Const Function Substring(startIndex As Long) As String
|
---|
| 509 | rangeCheck(startIndex)
|
---|
| 510 | Return Substring(startIndex, m_Length - startIndex)
|
---|
[246] | 511 | End Function
|
---|
[1] | 512 |
|
---|
[272] | 513 | Const Function Substring(startIndex As Long, length As Long) As String
|
---|
| 514 | rangeCheck(startIndex, length)
|
---|
| 515 | Return New String(Chars, startIndex, length)
|
---|
[246] | 516 | End Function
|
---|
[1] | 517 |
|
---|
[246] | 518 | Const Function Remove(startIndex As Long) As String
|
---|
[272] | 519 | rangeCheck(startIndex)
|
---|
| 520 | Remove = Substring(0, startIndex)
|
---|
[246] | 521 | End Function
|
---|
[1] | 522 |
|
---|
[246] | 523 | Const Function Remove(startIndex As Long, count As Long) As String
|
---|
[388] | 524 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 525 | sb.Remove(startIndex, count)
|
---|
| 526 | Remove = sb.ToString
|
---|
[246] | 527 | End Function
|
---|
[192] | 528 |
|
---|
[246] | 529 | Static Function IsNullOrEmpty(s As String) As Boolean
|
---|
| 530 | If Not Object.ReferenceEquals(s, Nothing) Then
|
---|
| 531 | If s.m_Length > 0 Then
|
---|
| 532 | Return False
|
---|
| 533 | End If
|
---|
[132] | 534 | End If
|
---|
[246] | 535 | Return True
|
---|
| 536 | End Function
|
---|
[192] | 537 |
|
---|
[246] | 538 | Const Function Replace(oldChar As StrChar, newChar As StrChar) As String
|
---|
[388] | 539 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 540 | sb.Replace(oldChar, newChar)
|
---|
| 541 | Replace = sb.ToString
|
---|
[246] | 542 | End Function
|
---|
[1] | 543 |
|
---|
[272] | 544 | Const Function Replace(oldStr As String, newStr As String) As String
|
---|
[388] | 545 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 546 | sb.Replace(oldStr, newStr)
|
---|
| 547 | Return sb.ToString
|
---|
[246] | 548 | End Function
|
---|
[1] | 549 |
|
---|
[246] | 550 | Const Function ToLower() As String
|
---|
[388] | 551 | Dim sb = New Text.StringBuilder(m_Length)
|
---|
[272] | 552 | sb.Length = m_Length
|
---|
[246] | 553 | Dim i As Long
|
---|
| 554 | For i = 0 To ELM(m_Length)
|
---|
[388] | 555 | sb[i] = ActiveBasic.CType.ToLower(Chars[i])
|
---|
[246] | 556 | Next
|
---|
[272] | 557 | Return sb.ToString
|
---|
[246] | 558 | End Function
|
---|
[1] | 559 |
|
---|
[246] | 560 | Const Function ToUpper() As String
|
---|
[388] | 561 | Dim sb = New Text.StringBuilder(m_Length)
|
---|
[272] | 562 | sb.Length = m_Length
|
---|
[246] | 563 | Dim i As Long
|
---|
| 564 | For i = 0 To ELM(m_Length)
|
---|
[388] | 565 | sb[i] = ActiveBasic.CType.ToUpper(Chars[i])
|
---|
[246] | 566 | Next
|
---|
[272] | 567 | Return sb.ToString
|
---|
[246] | 568 | End Function
|
---|
[272] | 569 |
|
---|
[246] | 570 | Override Function ToString() As String
|
---|
[272] | 571 | ToString = This
|
---|
[246] | 572 | End Function
|
---|
[119] | 573 |
|
---|
[272] | 574 | Const Function Clone() As String
|
---|
| 575 | Clone = This
|
---|
| 576 | End Function
|
---|
[285] | 577 |
|
---|
[246] | 578 | Static Function Copy(s As String) As String
|
---|
[272] | 579 | Copy = New String(s.Chars, s.m_Length)
|
---|
[246] | 580 | End Function
|
---|
[132] | 581 |
|
---|
[272] | 582 | Sub CopyTo(sourceIndex As Long, destination As *StrChar, destinationIndex As Long, count As Long)
|
---|
| 583 | ActiveBasic.Strings.ChrCopy(VarPtr(destination[destinationIndex]), VarPtr(Chars[sourceIndex]), count As SIZE_T)
|
---|
| 584 | End Sub
|
---|
| 585 |
|
---|
[246] | 586 | Override Function GetHashCode() As Long
|
---|
[143] | 587 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
[246] | 588 | Dim size = (m_Length + 1) >> 1
|
---|
[143] | 589 | #else
|
---|
[246] | 590 | Dim size = m_Length
|
---|
[143] | 591 | #endif
|
---|
[388] | 592 | Return _System_GetHashFromWordArray(Chars As *Word, size) Xor m_Length
|
---|
[246] | 593 | End Function
|
---|
[272] | 594 |
|
---|
| 595 | Function PadLeft(total As Long) As String
|
---|
| 596 | PadLeft(total, &h30 As StrChar)
|
---|
| 597 | End Function
|
---|
| 598 |
|
---|
| 599 | Function PadLeft(total As Long, c As StrChar) As String
|
---|
| 600 | If total < 0 Then
|
---|
[388] | 601 | Throw New ArgumentOutOfRangeException("String.PadLeft: An arguments is out of range value.", "total")
|
---|
[272] | 602 | End If
|
---|
| 603 | If total >= m_Length Then
|
---|
| 604 | Return This
|
---|
| 605 | End If
|
---|
[388] | 606 | Dim sb = New Text.StringBuilder(total)
|
---|
[272] | 607 | sb.Append(c, total - m_Length)
|
---|
| 608 | sb.Append(This)
|
---|
| 609 | Return sb.ToString
|
---|
| 610 | End Function
|
---|
| 611 |
|
---|
| 612 | Function PadRight(total As Long) As String
|
---|
[285] | 613 | PadRight(total, &h30 As StrChar)
|
---|
[272] | 614 | End Function
|
---|
| 615 |
|
---|
| 616 | Function PadRight(total As Long, c As StrChar) As String
|
---|
| 617 | If total < 0 Then
|
---|
[388] | 618 | Throw New ArgumentOutOfRangeException("String.PadRight: An arguments is out of range value.", "total")
|
---|
[272] | 619 | End If
|
---|
| 620 | If total >= m_Length Then
|
---|
| 621 | Return This
|
---|
| 622 | End If
|
---|
[388] | 623 | Dim sb = New Text.StringBuilder(total)
|
---|
[272] | 624 | sb.Append(This)
|
---|
| 625 | sb.Append(c, total - m_Length)
|
---|
| 626 | Return sb.ToString
|
---|
| 627 | End Function
|
---|
[246] | 628 | Private
|
---|
| 629 | Function AllocStringBuffer(textLength As Long) As *StrChar
|
---|
| 630 | If textLength < 0 Then
|
---|
| 631 | Return 0
|
---|
[1] | 632 | End If
|
---|
[272] | 633 | AllocStringBuffer = GC_malloc_atomic(SizeOf(StrChar) * (textLength + 1))
|
---|
| 634 | If AllocStringBuffer = 0 Then
|
---|
| 635 | 'Throw New OutOfMemoryException
|
---|
[246] | 636 | End If
|
---|
[272] | 637 | m_Length = textLength
|
---|
| 638 | Chars = AllocStringBuffer
|
---|
[246] | 639 | End Function
|
---|
[132] | 640 |
|
---|
[246] | 641 | Sub AssignFromStrChar(text As *StrChar, textLength As Long)
|
---|
[272] | 642 | AllocStringBuffer(textLength)
|
---|
| 643 | ActiveBasic.Strings.ChrCopy(Chars, text, textLength As SIZE_T)
|
---|
| 644 | Chars[m_Length] = 0
|
---|
| 645 | End Sub
|
---|
| 646 |
|
---|
| 647 | Const Sub rangeCheck(index As Long)
|
---|
| 648 | If index < 0 Or index > m_Length Then
|
---|
[388] | 649 | Throw New ArgumentOutOfRangeException("String: An arguments is out of range value.", "index")
|
---|
[246] | 650 | End If
|
---|
| 651 | End Sub
|
---|
[383] | 652 |
|
---|
[272] | 653 | Const Sub rangeCheck(start As Long, length As Long)
|
---|
| 654 | If start < 0 Or start > This.m_Length Or length < 0 Then
|
---|
[388] | 655 | Throw New ArgumentOutOfRangeException("String: One or more arguments are out of range value.", "start or length or both")
|
---|
[272] | 656 | End If
|
---|
| 657 | End Sub
|
---|
[246] | 658 | End Class
|
---|
| 659 |
|
---|
| 660 | End Namespace
|
---|