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