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