[132] | 1 | ' Classes/System/String.ab
|
---|
| 2 |
|
---|
[246] | 3 | Namespace System
|
---|
[203] | 4 |
|
---|
[246] | 5 | Class String
|
---|
[676] | 6 | Implements /*ICloneable, IConvertible, IComparable<String>, IEnumerable<Char>, IEquatable<String>*/
|
---|
[1] | 7 |
|
---|
[246] | 8 | m_Length As Long
|
---|
[468] | 9 | Chars As *Char
|
---|
[1] | 10 |
|
---|
[272] | 11 | Sub validPointerCheck(p As VoidPtr, size = 1 As Long)
|
---|
[676] | 12 | If p = 0 Then
|
---|
[457] | 13 | Throw New ArgumentException
|
---|
[272] | 14 | ElseIf IsBadReadPtr(p, size As ULONG_PTR) Then
|
---|
[457] | 15 | Throw New ArgumentException
|
---|
[272] | 16 | End If
|
---|
| 17 | End Sub
|
---|
| 18 | Public
|
---|
| 19 | Static Const Empty = New String
|
---|
| 20 |
|
---|
[246] | 21 | Sub String()
|
---|
[272] | 22 | ' Chars = 0
|
---|
| 23 | ' m_Length = 0
|
---|
[246] | 24 | End Sub
|
---|
[30] | 25 |
|
---|
[272] | 26 | Sub String(initStr As PCWSTR)
|
---|
| 27 | validPointerCheck(initStr)
|
---|
| 28 | Assign(initStr, lstrlenW(initStr))
|
---|
[246] | 29 | End Sub
|
---|
[125] | 30 |
|
---|
[272] | 31 | Sub String(initStr As PCWSTR, length As Long)
|
---|
| 32 | validPointerCheck(initStr, length)
|
---|
[246] | 33 | Assign(initStr, length)
|
---|
| 34 | End Sub
|
---|
[139] | 35 |
|
---|
[272] | 36 | Sub String(initStr As PCWSTR, start As Long, length As Long)
|
---|
[651] | 37 | If start < 0 Or length < 0 Then
|
---|
| 38 | Throw New ArgumentOutOfRangeException("start or length or both")
|
---|
[272] | 39 | End If
|
---|
[651] | 40 | validPointerCheck(VarPtr(initStr[start]), length)
|
---|
| 41 | Assign(VarPtr(initStr[start]), length)
|
---|
[246] | 42 | End Sub
|
---|
[139] | 43 |
|
---|
[272] | 44 | Sub String(initStr As PCSTR)
|
---|
| 45 | validPointerCheck(initStr)
|
---|
| 46 | Assign(initStr, lstrlenA(initStr))
|
---|
[246] | 47 | End Sub
|
---|
[49] | 48 |
|
---|
[272] | 49 | Sub String(initStr As PCSTR, length As Long)
|
---|
| 50 | validPointerCheck(initStr, length)
|
---|
| 51 | Assign(initStr, length)
|
---|
[246] | 52 | End Sub
|
---|
[121] | 53 |
|
---|
[272] | 54 | Sub String(initStr As PCSTR, start As Long, length As Long)
|
---|
[388] | 55 | If start < 0 Or length < 0 Then
|
---|
| 56 | Throw New ArgumentOutOfRangeException("String constructor: One or more arguments are out of range value.", "start or length or both")
|
---|
[272] | 57 | End If
|
---|
| 58 | validPointerCheck(initStr + start, length)
|
---|
| 59 | Assign(initStr + start, length)
|
---|
[246] | 60 | End Sub
|
---|
[383] | 61 |
|
---|
[468] | 62 | Sub String(initChar As Char, length As Long)
|
---|
[272] | 63 | AllocStringBuffer(length)
|
---|
| 64 | ActiveBasic.Strings.ChrFill(Chars, length, initChar)
|
---|
| 65 | Chars[length] = 0
|
---|
[246] | 66 | End Sub
|
---|
| 67 |
|
---|
[388] | 68 | Sub String(sb As Text.StringBuilder)
|
---|
[272] | 69 | Chars = StrPtr(sb)
|
---|
| 70 | m_Length = sb.Length
|
---|
| 71 | sb.__Stringized()
|
---|
[246] | 72 | End Sub
|
---|
[1] | 73 |
|
---|
[246] | 74 | Const Function Length() As Long
|
---|
| 75 | Return m_Length
|
---|
| 76 | End Function
|
---|
[30] | 77 |
|
---|
[468] | 78 | Function Operator() As *Char
|
---|
[246] | 79 | Return Chars
|
---|
| 80 | End Function
|
---|
[1] | 81 |
|
---|
[468] | 82 | Const Function Operator [] (n As Long) As Char
|
---|
[272] | 83 | rangeCheck(n)
|
---|
[246] | 84 | Return Chars[n]
|
---|
| 85 | End Function
|
---|
[1] | 86 |
|
---|
[272] | 87 | Const Function Operator + (y As PCSTR) As String
|
---|
[426] | 88 | If y = 0 Then
|
---|
| 89 | Return This
|
---|
| 90 | Else
|
---|
| 91 | Return Concat(y, lstrlenA(y))
|
---|
| 92 | End If
|
---|
[246] | 93 | End Function
|
---|
[139] | 94 |
|
---|
[272] | 95 | Const Function Operator + (y As PCWSTR) As String
|
---|
[426] | 96 | If y = 0 Then
|
---|
| 97 | Return This
|
---|
| 98 | Else
|
---|
| 99 | Return Concat(y, lstrlenW(y))
|
---|
| 100 | End If
|
---|
[246] | 101 | End Function
|
---|
[139] | 102 |
|
---|
[272] | 103 | Const Function Operator + (y As String) As String
|
---|
[426] | 104 | If ActiveBasic.IsNothing(y) Then
|
---|
| 105 | Return This
|
---|
| 106 | Else
|
---|
| 107 | Return Concat(y.Chars, y.m_Length)
|
---|
| 108 | End If
|
---|
[246] | 109 | End Function
|
---|
[1] | 110 |
|
---|
[272] | 111 | Const Function Operator & (y As PCSTR) As String
|
---|
| 112 | Return This + y
|
---|
[246] | 113 | End Function
|
---|
[1] | 114 |
|
---|
[272] | 115 | Const Function Operator & (y As PCWSTR) As String
|
---|
[426] | 116 | Return This + y
|
---|
[246] | 117 | End Function
|
---|
[139] | 118 |
|
---|
[272] | 119 | Const Function Operator & (y As String) As String
|
---|
[426] | 120 | Return This + y
|
---|
[246] | 121 | End Function
|
---|
[1] | 122 |
|
---|
[272] | 123 | Const Function Operator == (y As String) As Boolean
|
---|
[634] | 124 | Return CompareOrdinal(This, y) = 0
|
---|
[246] | 125 | End Function
|
---|
[1] | 126 |
|
---|
[468] | 127 | Const Function Operator == (y As *Char) As Boolean
|
---|
[634] | 128 | Return CompareOrdinal(This, y) = 0
|
---|
[246] | 129 | End Function
|
---|
[1] | 130 |
|
---|
[272] | 131 | Const Function Operator <> (y As String) As Boolean
|
---|
[634] | 132 | Return CompareOrdinal(This, y) <> 0
|
---|
[246] | 133 | End Function
|
---|
[1] | 134 |
|
---|
[468] | 135 | Const Function Operator <> (y As *Char) As Boolean
|
---|
[634] | 136 | Return CompareOrdinal(This, y) <> 0
|
---|
[246] | 137 | End Function
|
---|
[1] | 138 |
|
---|
[272] | 139 | Const Function Operator < (y As String) As Boolean
|
---|
[634] | 140 | Return CompareOrdinal(This, y) < 0
|
---|
[246] | 141 | End Function
|
---|
[1] | 142 |
|
---|
[468] | 143 | Const Function Operator < (y As *Char) As Boolean
|
---|
[634] | 144 | Return CompareOrdinal(This, y) < 0
|
---|
[246] | 145 | End Function
|
---|
[1] | 146 |
|
---|
[272] | 147 | Const Function Operator > (y As String) As Boolean
|
---|
[634] | 148 | Return CompareOrdinal(This, y) > 0
|
---|
[246] | 149 | End Function
|
---|
[1] | 150 |
|
---|
[468] | 151 | Const Function Operator > (y As *Char) As Boolean
|
---|
[634] | 152 | Return CompareOrdinal(This, y) > 0
|
---|
[246] | 153 | End Function
|
---|
[1] | 154 |
|
---|
[272] | 155 | Const Function Operator <= (y As String) As Boolean
|
---|
[634] | 156 | Return CompareOrdinal(This, y) <= 0
|
---|
[246] | 157 | End Function
|
---|
[1] | 158 |
|
---|
[468] | 159 | Const Function Operator <= (y As *Char) As Boolean
|
---|
[634] | 160 | Return CompareOrdinal(This, y) <= 0
|
---|
[246] | 161 | End Function
|
---|
[1] | 162 |
|
---|
[272] | 163 | Const Function Operator >= (y As String) As Boolean
|
---|
[634] | 164 | Return CompareOrdinal(This, y) >= 0
|
---|
[246] | 165 | End Function
|
---|
[1] | 166 |
|
---|
[468] | 167 | Const Function Operator >= (y As *Char) As Boolean
|
---|
[634] | 168 | Return CompareOrdinal(This, y) >= 0
|
---|
[246] | 169 | End Function
|
---|
[1] | 170 |
|
---|
[634] | 171 | /*!
|
---|
| 172 | @brief 単語順での文字列比較
|
---|
| 173 | @auther Egtra
|
---|
| 174 | */
|
---|
[246] | 175 | Static Function Compare(x As String, y As String) As Long
|
---|
[634] | 176 | Return Compare(x, y, False)
|
---|
[246] | 177 | End Function
|
---|
[203] | 178 |
|
---|
[634] | 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
|
---|
[457] | 184 |
|
---|
[246] | 185 | Static Function Compare(x As String, indexX As Long, y As String, indexY As Long, length As Long) As Long
|
---|
[634] | 186 | Return Compare(x, indexX, y, indexY, length, False)
|
---|
[246] | 187 | End Function
|
---|
[203] | 188 |
|
---|
[634] | 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 | */
|
---|
[246] | 206 | Static Function CompareOrdinal(x As String, y As String) As Long
|
---|
[634] | 207 | Dim lhs = removeNull(x)
|
---|
| 208 | Dim rhs = removeNull(y)
|
---|
| 209 | Return compareOrdinalImpl(lhs.Chars, lhs.Length, rhs.Chars, rhs.Length)
|
---|
[246] | 210 | End Function
|
---|
[203] | 211 |
|
---|
[246] | 212 | Static Function CompareOrdinal(x As String, indexX As Long, y As String, indexY As Long, length As Long) As Long
|
---|
[634] | 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)
|
---|
[272] | 224 | End Function
|
---|
| 225 |
|
---|
[468] | 226 | Static Function CompareOrdinal(x As String, y As *Char) As Long
|
---|
[634] | 227 | Dim lhs = removeNull(x)
|
---|
| 228 | Return compareOrdinalImpl(lhs.Chars, lhs.Length, y, lstrlen(y))
|
---|
[272] | 229 | End Function
|
---|
[634] | 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
|
---|
[203] | 236 | End If
|
---|
[634] | 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
|
---|
[246] | 248 | End Function
|
---|
[203] | 249 |
|
---|
[634] | 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)
|
---|
[272] | 252 | End Function
|
---|
[634] | 253 |
|
---|
[272] | 254 | Public
|
---|
[246] | 255 | Function CompareTo(y As String) As Long
|
---|
| 256 | Return String.Compare(This, y)
|
---|
| 257 | End Function
|
---|
[203] | 258 |
|
---|
[246] | 259 | Function CompareTo(y As Object) As Long
|
---|
[388] | 260 | If Not Object.Equals(This.GetType(), y.GetType()) Then
|
---|
[621] | 261 | Throw New ArgumentException("String.CompareTo: y is not String.", "y")
|
---|
[388] | 262 | End If
|
---|
[272] | 263 | Return CompareTo(y As String)
|
---|
[246] | 264 | End Function
|
---|
[203] | 265 |
|
---|
[308] | 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
|
---|
[478] | 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
|
---|
[308] | 275 | End If
|
---|
| 276 | Return False
|
---|
| 277 | End Function
|
---|
| 278 |
|
---|
[468] | 279 | Const Function StrPtr() As *Char
|
---|
[246] | 280 | Return Chars
|
---|
| 281 | End Function
|
---|
[272] | 282 | Private
|
---|
[1] | 283 |
|
---|
[246] | 284 | Sub Assign(text As PCSTR, textLengthA As Long)
|
---|
[497] | 285 | #ifdef UNICODE
|
---|
[246] | 286 | Dim textLengthW = MultiByteToWideChar(CP_THREAD_ACP, 0, text, textLengthA, 0, 0)
|
---|
[621] | 287 | AllocStringBuffer(textLengthW)
|
---|
| 288 | MultiByteToWideChar(CP_THREAD_ACP, 0, text, textLengthA, Chars, textLengthW)
|
---|
| 289 | Chars[textLengthW] = 0
|
---|
[497] | 290 | #else
|
---|
| 291 | AssignFromCharPtr(text, textLengthA)
|
---|
[139] | 292 | #endif
|
---|
[246] | 293 | End Sub
|
---|
[1] | 294 |
|
---|
[246] | 295 | Sub Assign(text As PCWSTR, textLengthW As Long)
|
---|
[497] | 296 | #ifdef UNICODE
|
---|
| 297 | AssignFromCharPtr(text, textLengthW)
|
---|
| 298 | #else
|
---|
[246] | 299 | Dim textLengthA = WideCharToMultiByte(CP_THREAD_ACP, 0, text, textLengthW, 0, 0, 0, 0)
|
---|
[621] | 300 | AllocStringBuffer(textLengthA)
|
---|
| 301 | WideCharToMultiByte(CP_THREAD_ACP, 0, text, textLengthW, Chars, textLengthA, 0, 0)
|
---|
| 302 | Chars[textLengthA] = 0
|
---|
[139] | 303 | #endif
|
---|
[246] | 304 | End Sub
|
---|
[139] | 305 |
|
---|
[246] | 306 | Private
|
---|
[468] | 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
|
---|
[246] | 310 | .AllocStringBuffer(text1Length + text2Length)
|
---|
[272] | 311 | ActiveBasic.Strings.ChrCopy(.Chars, text1, text1Length As SIZE_T)
|
---|
| 312 | ActiveBasic.Strings.ChrCopy(VarPtr(.Chars[text1Length]), text2, text2Length As SIZE_T)
|
---|
[246] | 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
|
---|
[497] | 318 | #ifdef UNICODE
|
---|
[246] | 319 | With Concat
|
---|
| 320 | Dim lenW = MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, 0, 0)
|
---|
[272] | 321 | Concat = New String
|
---|
[246] | 322 | .AllocStringBuffer(m_Length + lenW)
|
---|
[400] | 323 | ActiveBasic.Strings.ChrCopy(.Chars, This.Chars, m_Length As SIZE_T)
|
---|
[246] | 324 | MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, VarPtr(.Chars[m_Length]), lenW)
|
---|
| 325 | .Chars[m_Length + lenW] = 0
|
---|
| 326 | End With
|
---|
[497] | 327 | #else
|
---|
| 328 | Return ConcatChar(This.Chars, m_Length, text, len)
|
---|
[139] | 329 | #endif
|
---|
[246] | 330 | End Function
|
---|
[132] | 331 |
|
---|
[246] | 332 | Const Function Concat(text As PCWSTR, len As Long) As String
|
---|
[497] | 333 | #ifdef UNICODE
|
---|
| 334 | Return ConcatChar(This.Chars, m_Length, text, len)
|
---|
| 335 | #else
|
---|
[246] | 336 | With Concat
|
---|
[272] | 337 | Concat = New String
|
---|
[246] | 338 | Dim lenA = WideCharToMultiByte(CP_THREAD_ACP, 0, text, len, 0, 0, 0, 0)
|
---|
| 339 | .AllocStringBuffer(m_Length + lenA)
|
---|
[272] | 340 | ActiveBasic.Strings.ChrCopy(.Chars, This.Chars, m_Length As SIZE_T)
|
---|
[246] | 341 | WideCharToMultiByte(CP_THREAD_ACP, 0, text, len, VarPtr(.Chars[m_Length]), lenA, 0, 0)
|
---|
| 342 | .Chars[m_Length + lenA] = 0
|
---|
| 343 | End With
|
---|
[139] | 344 | #endif
|
---|
[246] | 345 | End Function
|
---|
[203] | 346 |
|
---|
[246] | 347 | Static Function Concat(x As String, y As String) As String
|
---|
[621] | 348 | If IsNullOrEmpty(x) Then
|
---|
[246] | 349 | Return y
|
---|
| 350 | Else
|
---|
| 351 | Return x.Concat(y.Chars, y.m_Length)
|
---|
| 352 | End If
|
---|
| 353 | End Function
|
---|
[203] | 354 |
|
---|
[457] | 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 |
|
---|
[246] | 367 | Static Function Concat(x As Object, y As Object) As String
|
---|
[457] | 368 | Return Concat(x.ToString, y.ToString)
|
---|
[246] | 369 | End Function
|
---|
[203] | 370 |
|
---|
[457] | 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 |
|
---|
[468] | 379 | Const Function Contains(c As Char) As Boolean
|
---|
[428] | 380 | Return IndexOf(c) >= 0
|
---|
| 381 | End Function
|
---|
| 382 |
|
---|
[272] | 383 | Const Function Contains(s As String) As Boolean
|
---|
[621] | 384 | If ActiveBasic.IsNothing(s) Then
|
---|
[391] | 385 | Throw New ArgumentNullException("String.Contains: An argument is null value.", "s")
|
---|
[388] | 386 | ElseIf s = "" Then
|
---|
| 387 | Return True
|
---|
| 388 | Else
|
---|
| 389 | Return IndexOf(s, 0, m_Length) >= 0
|
---|
[272] | 390 | End If
|
---|
[246] | 391 | End Function
|
---|
[1] | 392 |
|
---|
[468] | 393 | Const Function IndexOf(c As Char) As Long
|
---|
[272] | 394 | Return indexOfCore(c, 0, m_Length)
|
---|
[246] | 395 | End Function
|
---|
[1] | 396 |
|
---|
[468] | 397 | Const Function IndexOf(c As Char, start As Long) As Long
|
---|
[272] | 398 | rangeCheck(start)
|
---|
| 399 | Return indexOfCore(c, start, m_Length - start)
|
---|
[246] | 400 | End Function
|
---|
[1] | 401 |
|
---|
[468] | 402 | Const Function IndexOf(c As Char, start As Long, count As Long) As Long
|
---|
[272] | 403 | rangeCheck(start, count)
|
---|
| 404 | Return indexOfCore(c, start, count)
|
---|
[246] | 405 | End Function
|
---|
[272] | 406 | Private
|
---|
[468] | 407 | Const Function indexOfCore(c As Char, start As Long, count As Long) As Long
|
---|
[370] | 408 | indexOfCore = ActiveBasic.Strings.ChrFind(VarPtr(Chars[start]), count, c) As Long
|
---|
[272] | 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
|
---|
[1] | 417 |
|
---|
[272] | 418 | Const Function IndexOf(s As String, startIndex As Long) As Long
|
---|
| 419 | Return IndexOf(s, startIndex, m_Length - startIndex)
|
---|
| 420 | End Function
|
---|
[1] | 421 |
|
---|
[272] | 422 | Const Function IndexOf(s As String, startIndex As Long, count As Long) As Long
|
---|
| 423 | rangeCheck(startIndex, count)
|
---|
[621] | 424 | If ActiveBasic.IsNothing(s) Then
|
---|
[388] | 425 | Throw New ArgumentNullException("String.IndexOf: An argument is out of range value.", "s")
|
---|
[272] | 426 | End If
|
---|
[1] | 427 |
|
---|
[272] | 428 | Dim length = s.Length
|
---|
[246] | 429 | If length = 0 Then Return startIndex
|
---|
[1] | 430 |
|
---|
[246] | 431 | Dim i As Long, j As Long
|
---|
| 432 | For i = startIndex To startIndex + count - 1
|
---|
| 433 | For j = 0 To length - 1
|
---|
[272] | 434 | If Chars[i + j] = s[j] Then
|
---|
[246] | 435 | If j = length - 1 Then Return i
|
---|
| 436 | Else
|
---|
| 437 | Exit For
|
---|
| 438 | End If
|
---|
| 439 | Next
|
---|
[1] | 440 | Next
|
---|
[246] | 441 | Return -1
|
---|
| 442 | End Function
|
---|
[1] | 443 |
|
---|
[468] | 444 | Const Function LastIndexOf(c As Char) As Long
|
---|
[388] | 445 | Return lastIndexOf(c, m_Length - 1, m_Length)
|
---|
| 446 | End Function
|
---|
| 447 |
|
---|
[468] | 448 | Const Function LastIndexOf(c As Char, start As Long) As Long
|
---|
[388] | 449 | rangeCheck(start)
|
---|
| 450 | Return lastIndexOf(c, start, start + 1)
|
---|
| 451 | End Function
|
---|
| 452 |
|
---|
[468] | 453 | Const Function LastIndexOf(c As Char, start As Long, count As Long) As Long
|
---|
[388] | 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
|
---|
[468] | 462 | Const Function lastIndexOf(c As Char, start As Long, count As Long) As Long
|
---|
[388] | 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
|
---|
[272] | 474 | Const Function LastIndexOf(s As String) As Long
|
---|
| 475 | Return LastIndexOf(s, m_Length - 1, m_Length)
|
---|
[246] | 476 | End Function
|
---|
[1] | 477 |
|
---|
[272] | 478 | Const Function LastIndexOf(s As String, startIndex As Long) As Long
|
---|
| 479 | Return LastIndexOf(s, startIndex, startIndex + 1)
|
---|
[246] | 480 | End Function
|
---|
[1] | 481 |
|
---|
[388] | 482 | Const Function LastIndexOf(s As String, start As Long, count As Long) As Long
|
---|
[621] | 483 | If ActiveBasic.IsNothing(s) Then
|
---|
[388] | 484 | Throw New ArgumentNullException("String.LastIndexOf: An argument is out of range value.", "s")
|
---|
[272] | 485 | End If
|
---|
[1] | 486 |
|
---|
[388] | 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")
|
---|
[272] | 490 | End If
|
---|
[388] | 491 | Dim length = s.m_Length
|
---|
[246] | 492 | If length > m_Length Then Return -1
|
---|
[388] | 493 | If length = 0 Then Return start
|
---|
[1] | 494 |
|
---|
[246] | 495 | Dim i As Long, j As Long
|
---|
[388] | 496 | For i = start To start - count + 1 Step -1
|
---|
[246] | 497 | For j = length - 1 To 0 Step -1
|
---|
[272] | 498 | If Chars[i + j] = s[j] Then
|
---|
[246] | 499 | If j = 0 Then Return i
|
---|
| 500 | Else
|
---|
| 501 | Exit For
|
---|
| 502 | End If
|
---|
| 503 | Next
|
---|
[1] | 504 | Next
|
---|
[246] | 505 | Return -1
|
---|
| 506 | End Function
|
---|
[1] | 507 |
|
---|
[468] | 508 | Const Function StartsWith(c As Char) As Boolean
|
---|
[428] | 509 | Return IndexOf(c) = 0
|
---|
| 510 | End Function
|
---|
| 511 |
|
---|
[272] | 512 | Const Function StartsWith(s As String) As Boolean
|
---|
| 513 | Return IndexOf(s) = 0
|
---|
[246] | 514 | End Function
|
---|
[1] | 515 |
|
---|
[468] | 516 | Const Function EndsWith(c As Char) As Boolean
|
---|
[428] | 517 | Return LastIndexOf(c) = m_Length - 1
|
---|
| 518 | End Function
|
---|
| 519 |
|
---|
[272] | 520 | Const Function EndsWith(s As String) As Boolean
|
---|
| 521 | Return LastIndexOf(s) = m_Length - s.Length
|
---|
[246] | 522 | End Function
|
---|
[1] | 523 |
|
---|
[246] | 524 | Const Function Insert(startIndex As Long, text As String) As String
|
---|
[388] | 525 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 526 | sb.Insert(startIndex, text)
|
---|
| 527 | Return sb.ToString
|
---|
[246] | 528 | End Function
|
---|
[1] | 529 |
|
---|
[272] | 530 | Const Function Substring(startIndex As Long) As String
|
---|
| 531 | rangeCheck(startIndex)
|
---|
| 532 | Return Substring(startIndex, m_Length - startIndex)
|
---|
[246] | 533 | End Function
|
---|
[1] | 534 |
|
---|
[272] | 535 | Const Function Substring(startIndex As Long, length As Long) As String
|
---|
| 536 | rangeCheck(startIndex, length)
|
---|
| 537 | Return New String(Chars, startIndex, length)
|
---|
[246] | 538 | End Function
|
---|
[1] | 539 |
|
---|
[246] | 540 | Const Function Remove(startIndex As Long) As String
|
---|
[272] | 541 | rangeCheck(startIndex)
|
---|
| 542 | Remove = Substring(0, startIndex)
|
---|
[246] | 543 | End Function
|
---|
[1] | 544 |
|
---|
[246] | 545 | Const Function Remove(startIndex As Long, count As Long) As String
|
---|
[388] | 546 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 547 | sb.Remove(startIndex, count)
|
---|
| 548 | Remove = sb.ToString
|
---|
[246] | 549 | End Function
|
---|
[192] | 550 |
|
---|
[246] | 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
|
---|
[132] | 556 | End If
|
---|
[246] | 557 | Return True
|
---|
| 558 | End Function
|
---|
[192] | 559 |
|
---|
[468] | 560 | Const Function Replace(oldChar As Char, newChar As Char) As String
|
---|
[388] | 561 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 562 | sb.Replace(oldChar, newChar)
|
---|
| 563 | Replace = sb.ToString
|
---|
[246] | 564 | End Function
|
---|
[1] | 565 |
|
---|
[272] | 566 | Const Function Replace(oldStr As String, newStr As String) As String
|
---|
[388] | 567 | Dim sb = New Text.StringBuilder(This)
|
---|
[272] | 568 | sb.Replace(oldStr, newStr)
|
---|
| 569 | Return sb.ToString
|
---|
[246] | 570 | End Function
|
---|
[1] | 571 |
|
---|
[531] | 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 |
|
---|
[246] | 626 | Const Function ToLower() As String
|
---|
[388] | 627 | Dim sb = New Text.StringBuilder(m_Length)
|
---|
[272] | 628 | sb.Length = m_Length
|
---|
[246] | 629 | Dim i As Long
|
---|
| 630 | For i = 0 To ELM(m_Length)
|
---|
[388] | 631 | sb[i] = ActiveBasic.CType.ToLower(Chars[i])
|
---|
[246] | 632 | Next
|
---|
[272] | 633 | Return sb.ToString
|
---|
[246] | 634 | End Function
|
---|
[1] | 635 |
|
---|
[246] | 636 | Const Function ToUpper() As String
|
---|
[388] | 637 | Dim sb = New Text.StringBuilder(m_Length)
|
---|
[272] | 638 | sb.Length = m_Length
|
---|
[246] | 639 | Dim i As Long
|
---|
| 640 | For i = 0 To ELM(m_Length)
|
---|
[388] | 641 | sb[i] = ActiveBasic.CType.ToUpper(Chars[i])
|
---|
[246] | 642 | Next
|
---|
[272] | 643 | Return sb.ToString
|
---|
[246] | 644 | End Function
|
---|
[272] | 645 |
|
---|
[246] | 646 | Override Function ToString() As String
|
---|
[272] | 647 | ToString = This
|
---|
[246] | 648 | End Function
|
---|
[119] | 649 |
|
---|
[272] | 650 | Const Function Clone() As String
|
---|
| 651 | Clone = This
|
---|
| 652 | End Function
|
---|
[457] | 653 | /*
|
---|
| 654 | Function Clone() As Object
|
---|
| 655 | Clone = This
|
---|
| 656 | End Function
|
---|
| 657 | */
|
---|
[246] | 658 | Static Function Copy(s As String) As String
|
---|
[272] | 659 | Copy = New String(s.Chars, s.m_Length)
|
---|
[246] | 660 | End Function
|
---|
[132] | 661 |
|
---|
[468] | 662 | Sub CopyTo(sourceIndex As Long, destination As *Char, destinationIndex As Long, count As Long)
|
---|
[272] | 663 | ActiveBasic.Strings.ChrCopy(VarPtr(destination[destinationIndex]), VarPtr(Chars[sourceIndex]), count As SIZE_T)
|
---|
| 664 | End Sub
|
---|
| 665 |
|
---|
[246] | 666 | Override Function GetHashCode() As Long
|
---|
[497] | 667 | #ifdef UNICODE
|
---|
| 668 | Dim size = m_Length
|
---|
| 669 | #else
|
---|
[246] | 670 | Dim size = (m_Length + 1) >> 1
|
---|
[143] | 671 | #endif
|
---|
[388] | 672 | Return _System_GetHashFromWordArray(Chars As *Word, size) Xor m_Length
|
---|
[246] | 673 | End Function
|
---|
[272] | 674 |
|
---|
| 675 | Function PadLeft(total As Long) As String
|
---|
[468] | 676 | PadLeft(total, &h30 As Char)
|
---|
[272] | 677 | End Function
|
---|
| 678 |
|
---|
[468] | 679 | Function PadLeft(total As Long, c As Char) As String
|
---|
[272] | 680 | If total < 0 Then
|
---|
[388] | 681 | Throw New ArgumentOutOfRangeException("String.PadLeft: An arguments is out of range value.", "total")
|
---|
[272] | 682 | End If
|
---|
| 683 | If total >= m_Length Then
|
---|
| 684 | Return This
|
---|
| 685 | End If
|
---|
[388] | 686 | Dim sb = New Text.StringBuilder(total)
|
---|
[272] | 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
|
---|
[468] | 693 | PadRight(total, &h30 As Char)
|
---|
[272] | 694 | End Function
|
---|
| 695 |
|
---|
[468] | 696 | Function PadRight(total As Long, c As Char) As String
|
---|
[272] | 697 | If total < 0 Then
|
---|
[388] | 698 | Throw New ArgumentOutOfRangeException("String.PadRight: An arguments is out of range value.", "total")
|
---|
[272] | 699 | End If
|
---|
| 700 | If total >= m_Length Then
|
---|
| 701 | Return This
|
---|
| 702 | End If
|
---|
[388] | 703 | Dim sb = New Text.StringBuilder(total)
|
---|
[272] | 704 | sb.Append(This)
|
---|
| 705 | sb.Append(c, total - m_Length)
|
---|
| 706 | Return sb.ToString
|
---|
| 707 | End Function
|
---|
[246] | 708 | Private
|
---|
[621] | 709 | Sub AllocStringBuffer(textLength As Long)
|
---|
[246] | 710 | If textLength < 0 Then
|
---|
[621] | 711 | Throw New ArgumentException
|
---|
[1] | 712 | End If
|
---|
[621] | 713 | Chars = GC_malloc_atomic(SizeOf(Char) * (textLength + 1))
|
---|
[272] | 714 | m_Length = textLength
|
---|
[621] | 715 | End Sub
|
---|
[132] | 716 |
|
---|
[468] | 717 | Sub AssignFromCharPtr(text As *Char, textLength As Long)
|
---|
[272] | 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
|
---|
[621] | 725 | Throw New ArgumentOutOfRangeException("String: An argument is out of range value.", "index")
|
---|
[246] | 726 | End If
|
---|
| 727 | End Sub
|
---|
[383] | 728 |
|
---|
[272] | 729 | Const Sub rangeCheck(start As Long, length As Long)
|
---|
| 730 | If start < 0 Or start > This.m_Length Or length < 0 Then
|
---|
[388] | 731 | Throw New ArgumentOutOfRangeException("String: One or more arguments are out of range value.", "start or length or both")
|
---|
[272] | 732 | End If
|
---|
| 733 | End Sub
|
---|
[457] | 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
|
---|
[246] | 742 | End Class
|
---|
| 743 |
|
---|
[531] | 744 | Enum StringSplitOptions
|
---|
| 745 | None
|
---|
| 746 | RemoveEmptyEntries
|
---|
| 747 | End Enum
|
---|
| 748 |
|
---|
[246] | 749 | End Namespace
|
---|