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