[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
|
---|
[478] | 258 | If Not ActiveBasic.IsNothing(s) Then
|
---|
| 259 | If Object.Equals(This.GetType(), s.GetType()) Then
|
---|
| 260 | Return This.Equals(s As String)
|
---|
| 261 | End If
|
---|
[308] | 262 | End If
|
---|
| 263 | Return False
|
---|
| 264 | End Function
|
---|
| 265 |
|
---|
[468] | 266 | Const Function StrPtr() As *Char
|
---|
[246] | 267 | Return Chars
|
---|
| 268 | End Function
|
---|
[272] | 269 | Private
|
---|
[1] | 270 |
|
---|
[246] | 271 | Sub Assign(text As PCSTR, textLengthA As Long)
|
---|
[175] | 272 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
[468] | 273 | AssignFromCharPtr(text, textLengthA)
|
---|
[139] | 274 | #else
|
---|
[246] | 275 | Dim textLengthW = MultiByteToWideChar(CP_THREAD_ACP, 0, text, textLengthA, 0, 0)
|
---|
| 276 | If AllocStringBuffer(textLengthW) <> 0 Then
|
---|
| 277 | MultiByteToWideChar(CP_THREAD_ACP, 0, text, textLengthA, Chars, textLengthW)
|
---|
| 278 | Chars[textLengthW] = 0
|
---|
| 279 | End If
|
---|
[139] | 280 | #endif
|
---|
[246] | 281 | End Sub
|
---|
[1] | 282 |
|
---|
[246] | 283 | Sub Assign(text As PCWSTR, textLengthW As Long)
|
---|
[139] | 284 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
[246] | 285 | Dim textLengthA = WideCharToMultiByte(CP_THREAD_ACP, 0, text, textLengthW, 0, 0, 0, 0)
|
---|
| 286 | If AllocStringBuffer(textLengthA) <> 0 Then
|
---|
| 287 | WideCharToMultiByte(CP_THREAD_ACP, 0, text, textLengthW, Chars, textLengthA, 0, 0)
|
---|
| 288 | Chars[textLengthA] = 0
|
---|
| 289 | End If
|
---|
[139] | 290 | #else
|
---|
[468] | 291 | AssignFromCharPtr(text, textLengthW)
|
---|
[139] | 292 | #endif
|
---|
[246] | 293 | End Sub
|
---|
[139] | 294 |
|
---|
[246] | 295 | Private
|
---|
[468] | 296 | Static Function ConcatChar(text1 As *Char, text1Length As Long, text2 As *Char, text2Length As Long) As String
|
---|
| 297 | ConcatChar = New String()
|
---|
| 298 | With ConcatChar
|
---|
[246] | 299 | .AllocStringBuffer(text1Length + text2Length)
|
---|
[272] | 300 | ActiveBasic.Strings.ChrCopy(.Chars, text1, text1Length As SIZE_T)
|
---|
| 301 | ActiveBasic.Strings.ChrCopy(VarPtr(.Chars[text1Length]), text2, text2Length As SIZE_T)
|
---|
[246] | 302 | .Chars[text1Length + text2Length] = 0
|
---|
| 303 | End With
|
---|
| 304 | End Function
|
---|
| 305 | Public
|
---|
| 306 | Const Function Concat(text As PCSTR, len As Long) As String
|
---|
[139] | 307 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
[468] | 308 | Return ConcatChar(This.Chars, m_Length, text, len)
|
---|
[139] | 309 | #else
|
---|
[246] | 310 | With Concat
|
---|
| 311 | Dim lenW = MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, 0, 0)
|
---|
[272] | 312 | Concat = New String
|
---|
[246] | 313 | .AllocStringBuffer(m_Length + lenW)
|
---|
[400] | 314 | ActiveBasic.Strings.ChrCopy(.Chars, This.Chars, m_Length As SIZE_T)
|
---|
[246] | 315 | MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, VarPtr(.Chars[m_Length]), lenW)
|
---|
| 316 | .Chars[m_Length + lenW] = 0
|
---|
| 317 | End With
|
---|
[139] | 318 | #endif
|
---|
[246] | 319 | End Function
|
---|
[132] | 320 |
|
---|
[246] | 321 | Const Function Concat(text As PCWSTR, len As Long) As String
|
---|
[139] | 322 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
[246] | 323 | With Concat
|
---|
[272] | 324 | Concat = New String
|
---|
[246] | 325 | Dim lenA = WideCharToMultiByte(CP_THREAD_ACP, 0, text, len, 0, 0, 0, 0)
|
---|
| 326 | .AllocStringBuffer(m_Length + lenA)
|
---|
[272] | 327 | ActiveBasic.Strings.ChrCopy(.Chars, This.Chars, m_Length As SIZE_T)
|
---|
[246] | 328 | WideCharToMultiByte(CP_THREAD_ACP, 0, text, len, VarPtr(.Chars[m_Length]), lenA, 0, 0)
|
---|
| 329 | .Chars[m_Length + lenA] = 0
|
---|
| 330 | End With
|
---|
[139] | 331 | #else
|
---|
[468] | 332 | Return ConcatChar(This.Chars, m_Length, text, len)
|
---|
[139] | 333 | #endif
|
---|
[246] | 334 | End Function
|
---|
[203] | 335 |
|
---|
[246] | 336 | Static Function Concat(x As String, y As String) As String
|
---|
| 337 | If String.IsNullOrEmpty(x) Then
|
---|
| 338 | Return y
|
---|
| 339 | Else
|
---|
| 340 | Return x.Concat(y.Chars, y.m_Length)
|
---|
| 341 | End If
|
---|
| 342 | End Function
|
---|
[203] | 343 |
|
---|
[457] | 344 | Static Function Concat(x As String, y As String, z As String) As String
|
---|
| 345 | Dim sb = New Text.StringBuilder(removeNull(x).Length + removeNull(y).Length + removeNull(z).Length)
|
---|
| 346 | sb.Append(x).Append(y).Append(z)
|
---|
| 347 | Concat = sb.ToString
|
---|
| 348 | End Function
|
---|
| 349 |
|
---|
| 350 | Static Function Concat(x As String, y As String, z As String, w As String) As String
|
---|
| 351 | Dim sb = New Text.StringBuilder(removeNull(x).Length + removeNull(y).Length + removeNull(z).Length + removeNull(w).Length)
|
---|
| 352 | sb.Append(x).Append(y).Append(z).Append(w)
|
---|
| 353 | Concat = sb.ToString
|
---|
| 354 | End Function
|
---|
| 355 |
|
---|
[246] | 356 | Static Function Concat(x As Object, y As Object) As String
|
---|
[457] | 357 | Return Concat(x.ToString, y.ToString)
|
---|
[246] | 358 | End Function
|
---|
[203] | 359 |
|
---|
[457] | 360 | Static Function Concat(x As Object, y As Object, z As Object) As String
|
---|
| 361 | Return Concat(x.ToString, y.ToString, z.ToString)
|
---|
| 362 | End Function
|
---|
| 363 |
|
---|
| 364 | Static Function Concat(x As Object, y As Object, z As Object, w As Object) As String
|
---|
| 365 | Return Concat(x.ToString, y.ToString, z.ToString, w.ToString)
|
---|
| 366 | End Function
|
---|
| 367 |
|
---|
[468] | 368 | Const Function Contains(c As Char) As Boolean
|
---|
[428] | 369 | Return IndexOf(c) >= 0
|
---|
| 370 | End Function
|
---|
| 371 |
|
---|
[272] | 372 | Const Function Contains(s As String) As Boolean
|
---|
| 373 | If Object.ReferenceEquals(s, Nothing) Then
|
---|
[391] | 374 | Throw New ArgumentNullException("String.Contains: An argument is null value.", "s")
|
---|
[388] | 375 | ElseIf s = "" Then
|
---|
| 376 | Return True
|
---|
| 377 | Else
|
---|
| 378 | Return IndexOf(s, 0, m_Length) >= 0
|
---|
[272] | 379 | End If
|
---|
[246] | 380 | End Function
|
---|
[1] | 381 |
|
---|
[468] | 382 | Const Function IndexOf(c As Char) As Long
|
---|
[272] | 383 | Return indexOfCore(c, 0, m_Length)
|
---|
[246] | 384 | End Function
|
---|
[1] | 385 |
|
---|
[468] | 386 | Const Function IndexOf(c As Char, start As Long) As Long
|
---|
[272] | 387 | rangeCheck(start)
|
---|
| 388 | Return indexOfCore(c, start, m_Length - start)
|
---|
[246] | 389 | End Function
|
---|
[1] | 390 |
|
---|
[468] | 391 | Const Function IndexOf(c As Char, start As Long, count As Long) As Long
|
---|
[272] | 392 | rangeCheck(start, count)
|
---|
| 393 | Return indexOfCore(c, start, count)
|
---|
[246] | 394 | End Function
|
---|
[272] | 395 | Private
|
---|
[468] | 396 | Const Function indexOfCore(c As Char, start As Long, count As Long) As Long
|
---|
[370] | 397 | indexOfCore = ActiveBasic.Strings.ChrFind(VarPtr(Chars[start]), count, c) As Long
|
---|
[272] | 398 | If indexOfCore <> -1 Then
|
---|
| 399 | indexOfCore += start
|
---|
| 400 | End If
|
---|
| 401 | End Function
|
---|
| 402 | Public
|
---|
| 403 | Const Function IndexOf(s As String) As Long
|
---|
| 404 | Return IndexOf(s, 0, m_Length)
|
---|
| 405 | End Function
|
---|
[1] | 406 |
|
---|
[272] | 407 | Const Function IndexOf(s As String, startIndex As Long) As Long
|
---|
| 408 | Return IndexOf(s, startIndex, m_Length - startIndex)
|
---|
| 409 | End Function
|
---|
[1] | 410 |
|
---|
[272] | 411 | Const Function IndexOf(s As String, startIndex As Long, count As Long) As Long
|
---|
| 412 | rangeCheck(startIndex, count)
|
---|
| 413 | If Object.ReferenceEquals(s, Nothing) Then
|
---|
[388] | 414 | Throw New ArgumentNullException("String.IndexOf: An argument is out of range value.", "s")
|
---|
[272] | 415 | End If
|
---|
[1] | 416 |
|
---|
[272] | 417 | Dim length = s.Length
|
---|
[246] | 418 | If length = 0 Then Return startIndex
|
---|
[1] | 419 |
|
---|
[246] | 420 | Dim i As Long, j As Long
|
---|
| 421 | For i = startIndex To startIndex + count - 1
|
---|
| 422 | For j = 0 To length - 1
|
---|
[272] | 423 | If Chars[i + j] = s[j] Then
|
---|
[246] | 424 | If j = length - 1 Then Return i
|
---|
| 425 | Else
|
---|
| 426 | Exit For
|
---|
| 427 | End If
|
---|
| 428 | Next
|
---|
[1] | 429 | Next
|
---|
[246] | 430 | Return -1
|
---|
| 431 | End Function
|
---|
[1] | 432 |
|
---|
[468] | 433 | Const Function LastIndexOf(c As Char) As Long
|
---|
[388] | 434 | Return lastIndexOf(c, m_Length - 1, m_Length)
|
---|
| 435 | End Function
|
---|
| 436 |
|
---|
[468] | 437 | Const Function LastIndexOf(c As Char, start As Long) As Long
|
---|
[388] | 438 | rangeCheck(start)
|
---|
| 439 | Return lastIndexOf(c, start, start + 1)
|
---|
| 440 | End Function
|
---|
| 441 |
|
---|
[468] | 442 | Const Function LastIndexOf(c As Char, start As Long, count As Long) As Long
|
---|
[388] | 443 | rangeCheck(start)
|
---|
| 444 | Dim lastFindPos = start - (count - 1)
|
---|
| 445 | If Not (m_Length > lastFindPos And lastFindPos >= 0) Then
|
---|
| 446 | Throw New ArgumentOutOfRangeException("String.LastIndexOf: An argument is out of range value.", "count")
|
---|
| 447 | End If
|
---|
| 448 | Return lastIndexOf(c, start, count)
|
---|
| 449 | End Function
|
---|
| 450 | Private
|
---|
[468] | 451 | Const Function lastIndexOf(c As Char, start As Long, count As Long) As Long
|
---|
[388] | 452 | Dim lastFindPos = start - (count - 1)
|
---|
| 453 | Dim i As Long
|
---|
| 454 | For i = start To lastFindPos Step -1
|
---|
| 455 | If Chars[i] = c Then
|
---|
| 456 | Return i
|
---|
| 457 | End If
|
---|
| 458 | Next
|
---|
| 459 | Return -1
|
---|
| 460 | End Function
|
---|
| 461 |
|
---|
| 462 | Public
|
---|
[272] | 463 | Const Function LastIndexOf(s As String) As Long
|
---|
| 464 | Return LastIndexOf(s, m_Length - 1, m_Length)
|
---|
[246] | 465 | End Function
|
---|
[1] | 466 |
|
---|
[272] | 467 | Const Function LastIndexOf(s As String, startIndex As Long) As Long
|
---|
| 468 | Return LastIndexOf(s, startIndex, startIndex + 1)
|
---|
[246] | 469 | End Function
|
---|
[1] | 470 |
|
---|
[388] | 471 | Const Function LastIndexOf(s As String, start As Long, count As Long) As Long
|
---|
[272] | 472 | If Object.ReferenceEquals(s, Nothing) Then
|
---|
[388] | 473 | Throw New ArgumentNullException("String.LastIndexOf: An argument is out of range value.", "s")
|
---|
[272] | 474 | End If
|
---|
[1] | 475 |
|
---|
[388] | 476 | If start < 0 Or start > m_Length - 1 Or _
|
---|
| 477 | count < 0 Or count > start + 2 Then
|
---|
| 478 | Throw New ArgumentOutOfRangeException("String.LastIndexOf: One or more arguments are out of range value.", "start or count or both")
|
---|
[272] | 479 | End If
|
---|
[388] | 480 | Dim length = s.m_Length
|
---|
[246] | 481 | If length > m_Length Then Return -1
|
---|
[388] | 482 | If length = 0 Then Return start
|
---|
[1] | 483 |
|
---|
[246] | 484 | Dim i As Long, j As Long
|
---|
[388] | 485 | For i = start To start - count + 1 Step -1
|
---|
[246] | 486 | For j = length - 1 To 0 Step -1
|
---|
[272] | 487 | If Chars[i + j] = s[j] Then
|
---|
[246] | 488 | If j = 0 Then Return i
|
---|
| 489 | Else
|
---|
| 490 | Exit For
|
---|
| 491 | End If
|
---|
| 492 | Next
|
---|
[1] | 493 | Next
|
---|
[246] | 494 | Return -1
|
---|
| 495 | End Function
|
---|
[1] | 496 |
|
---|
[468] | 497 | Const Function StartsWith(c As Char) As Boolean
|
---|
[428] | 498 | Return IndexOf(c) = 0
|
---|
| 499 | End Function
|
---|
| 500 |
|
---|
[272] | 501 | Const Function StartsWith(s As String) As Boolean
|
---|
| 502 | Return IndexOf(s) = 0
|
---|
[246] | 503 | End Function
|
---|
[1] | 504 |
|
---|
[468] | 505 | Const Function EndsWith(c As Char) As Boolean
|
---|
[428] | 506 | Return LastIndexOf(c) = m_Length - 1
|
---|
| 507 | End Function
|
---|
| 508 |
|
---|
[272] | 509 | Const Function EndsWith(s As String) As Boolean
|
---|
| 510 | Return LastIndexOf(s) = m_Length - s.Length
|
---|
[246] | 511 | End Function
|
---|
[1] | 512 |
|
---|
[246] | 513 | Const Function Insert(startIndex As Long, text As String) As String
|
---|
[388] | 514 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 515 | sb.Insert(startIndex, text)
|
---|
| 516 | Return sb.ToString
|
---|
[246] | 517 | End Function
|
---|
[1] | 518 |
|
---|
[272] | 519 | Const Function Substring(startIndex As Long) As String
|
---|
| 520 | rangeCheck(startIndex)
|
---|
| 521 | Return Substring(startIndex, m_Length - startIndex)
|
---|
[246] | 522 | End Function
|
---|
[1] | 523 |
|
---|
[272] | 524 | Const Function Substring(startIndex As Long, length As Long) As String
|
---|
| 525 | rangeCheck(startIndex, length)
|
---|
| 526 | Return New String(Chars, startIndex, length)
|
---|
[246] | 527 | End Function
|
---|
[1] | 528 |
|
---|
[246] | 529 | Const Function Remove(startIndex As Long) As String
|
---|
[272] | 530 | rangeCheck(startIndex)
|
---|
| 531 | Remove = Substring(0, startIndex)
|
---|
[246] | 532 | End Function
|
---|
[1] | 533 |
|
---|
[246] | 534 | Const Function Remove(startIndex As Long, count As Long) As String
|
---|
[388] | 535 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 536 | sb.Remove(startIndex, count)
|
---|
| 537 | Remove = sb.ToString
|
---|
[246] | 538 | End Function
|
---|
[192] | 539 |
|
---|
[246] | 540 | Static Function IsNullOrEmpty(s As String) As Boolean
|
---|
| 541 | If Not Object.ReferenceEquals(s, Nothing) Then
|
---|
| 542 | If s.m_Length > 0 Then
|
---|
| 543 | Return False
|
---|
| 544 | End If
|
---|
[132] | 545 | End If
|
---|
[246] | 546 | Return True
|
---|
| 547 | End Function
|
---|
[192] | 548 |
|
---|
[468] | 549 | Const Function Replace(oldChar As Char, newChar As Char) As String
|
---|
[388] | 550 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 551 | sb.Replace(oldChar, newChar)
|
---|
| 552 | Replace = sb.ToString
|
---|
[246] | 553 | End Function
|
---|
[1] | 554 |
|
---|
[272] | 555 | Const Function Replace(oldStr As String, newStr As String) As String
|
---|
[388] | 556 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 557 | sb.Replace(oldStr, newStr)
|
---|
| 558 | Return sb.ToString
|
---|
[246] | 559 | End Function
|
---|
[1] | 560 |
|
---|
[246] | 561 | Const Function ToLower() As String
|
---|
[388] | 562 | Dim sb = New Text.StringBuilder(m_Length)
|
---|
[272] | 563 | sb.Length = m_Length
|
---|
[246] | 564 | Dim i As Long
|
---|
| 565 | For i = 0 To ELM(m_Length)
|
---|
[388] | 566 | sb[i] = ActiveBasic.CType.ToLower(Chars[i])
|
---|
[246] | 567 | Next
|
---|
[272] | 568 | Return sb.ToString
|
---|
[246] | 569 | End Function
|
---|
[1] | 570 |
|
---|
[246] | 571 | Const Function ToUpper() As String
|
---|
[388] | 572 | Dim sb = New Text.StringBuilder(m_Length)
|
---|
[272] | 573 | sb.Length = m_Length
|
---|
[246] | 574 | Dim i As Long
|
---|
| 575 | For i = 0 To ELM(m_Length)
|
---|
[388] | 576 | sb[i] = ActiveBasic.CType.ToUpper(Chars[i])
|
---|
[246] | 577 | Next
|
---|
[272] | 578 | Return sb.ToString
|
---|
[246] | 579 | End Function
|
---|
[272] | 580 |
|
---|
[246] | 581 | Override Function ToString() As String
|
---|
[272] | 582 | ToString = This
|
---|
[246] | 583 | End Function
|
---|
[119] | 584 |
|
---|
[272] | 585 | Const Function Clone() As String
|
---|
| 586 | Clone = This
|
---|
| 587 | End Function
|
---|
[457] | 588 | /*
|
---|
| 589 | Function Clone() As Object
|
---|
| 590 | Clone = This
|
---|
| 591 | End Function
|
---|
| 592 | */
|
---|
[246] | 593 | Static Function Copy(s As String) As String
|
---|
[272] | 594 | Copy = New String(s.Chars, s.m_Length)
|
---|
[246] | 595 | End Function
|
---|
[132] | 596 |
|
---|
[468] | 597 | Sub CopyTo(sourceIndex As Long, destination As *Char, destinationIndex As Long, count As Long)
|
---|
[272] | 598 | ActiveBasic.Strings.ChrCopy(VarPtr(destination[destinationIndex]), VarPtr(Chars[sourceIndex]), count As SIZE_T)
|
---|
| 599 | End Sub
|
---|
| 600 |
|
---|
[246] | 601 | Override Function GetHashCode() As Long
|
---|
[143] | 602 | #ifdef __STRING_IS_NOT_UNICODE
|
---|
[246] | 603 | Dim size = (m_Length + 1) >> 1
|
---|
[143] | 604 | #else
|
---|
[246] | 605 | Dim size = m_Length
|
---|
[143] | 606 | #endif
|
---|
[388] | 607 | Return _System_GetHashFromWordArray(Chars As *Word, size) Xor m_Length
|
---|
[246] | 608 | End Function
|
---|
[272] | 609 |
|
---|
| 610 | Function PadLeft(total As Long) As String
|
---|
[468] | 611 | PadLeft(total, &h30 As Char)
|
---|
[272] | 612 | End Function
|
---|
| 613 |
|
---|
[468] | 614 | Function PadLeft(total As Long, c As Char) As String
|
---|
[272] | 615 | If total < 0 Then
|
---|
[388] | 616 | Throw New ArgumentOutOfRangeException("String.PadLeft: An arguments is out of range value.", "total")
|
---|
[272] | 617 | End If
|
---|
| 618 | If total >= m_Length Then
|
---|
| 619 | Return This
|
---|
| 620 | End If
|
---|
[388] | 621 | Dim sb = New Text.StringBuilder(total)
|
---|
[272] | 622 | sb.Append(c, total - m_Length)
|
---|
| 623 | sb.Append(This)
|
---|
| 624 | Return sb.ToString
|
---|
| 625 | End Function
|
---|
| 626 |
|
---|
| 627 | Function PadRight(total As Long) As String
|
---|
[468] | 628 | PadRight(total, &h30 As Char)
|
---|
[272] | 629 | End Function
|
---|
| 630 |
|
---|
[468] | 631 | Function PadRight(total As Long, c As Char) As String
|
---|
[272] | 632 | If total < 0 Then
|
---|
[388] | 633 | Throw New ArgumentOutOfRangeException("String.PadRight: An arguments is out of range value.", "total")
|
---|
[272] | 634 | End If
|
---|
| 635 | If total >= m_Length Then
|
---|
| 636 | Return This
|
---|
| 637 | End If
|
---|
[388] | 638 | Dim sb = New Text.StringBuilder(total)
|
---|
[272] | 639 | sb.Append(This)
|
---|
| 640 | sb.Append(c, total - m_Length)
|
---|
| 641 | Return sb.ToString
|
---|
| 642 | End Function
|
---|
[246] | 643 | Private
|
---|
[468] | 644 | Function AllocStringBuffer(textLength As Long) As *Char
|
---|
[246] | 645 | If textLength < 0 Then
|
---|
| 646 | Return 0
|
---|
[1] | 647 | End If
|
---|
[468] | 648 | AllocStringBuffer = GC_malloc_atomic(SizeOf(Char) * (textLength + 1))
|
---|
[272] | 649 | If AllocStringBuffer = 0 Then
|
---|
| 650 | 'Throw New OutOfMemoryException
|
---|
[246] | 651 | End If
|
---|
[272] | 652 | m_Length = textLength
|
---|
| 653 | Chars = AllocStringBuffer
|
---|
[246] | 654 | End Function
|
---|
[132] | 655 |
|
---|
[468] | 656 | Sub AssignFromCharPtr(text As *Char, textLength As Long)
|
---|
[272] | 657 | AllocStringBuffer(textLength)
|
---|
| 658 | ActiveBasic.Strings.ChrCopy(Chars, text, textLength As SIZE_T)
|
---|
| 659 | Chars[m_Length] = 0
|
---|
| 660 | End Sub
|
---|
| 661 |
|
---|
| 662 | Const Sub rangeCheck(index As Long)
|
---|
| 663 | If index < 0 Or index > m_Length Then
|
---|
[388] | 664 | Throw New ArgumentOutOfRangeException("String: An arguments is out of range value.", "index")
|
---|
[246] | 665 | End If
|
---|
| 666 | End Sub
|
---|
[383] | 667 |
|
---|
[272] | 668 | Const Sub rangeCheck(start As Long, length As Long)
|
---|
| 669 | If start < 0 Or start > This.m_Length Or length < 0 Then
|
---|
[388] | 670 | Throw New ArgumentOutOfRangeException("String: One or more arguments are out of range value.", "start or length or both")
|
---|
[272] | 671 | End If
|
---|
| 672 | End Sub
|
---|
[457] | 673 |
|
---|
| 674 | Static Function removeNull(s As String) As String
|
---|
| 675 | If ActiveBasic.IsNothing(s) Then
|
---|
| 676 | removeNull = Empty
|
---|
| 677 | Else
|
---|
| 678 | removeNull = s
|
---|
| 679 | End If
|
---|
| 680 | End Function
|
---|
[246] | 681 | End Class
|
---|
| 682 |
|
---|
| 683 | End Namespace
|
---|