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