[272] | 1 | 'Classes/ActiveBasic/Strings/Strings.ab
|
---|
| 2 |
|
---|
| 3 | #ifndef __ACTIVEBASIC_STRINGS_STRINGS_AB__
|
---|
| 4 | #define __ACTIVEBASIC_STRINGS_STRINGS_AB__
|
---|
| 5 |
|
---|
| 6 | #require <Classes/System/Math.ab>
|
---|
| 7 | #require <Classes/System/Collections/ArrayList.ab>
|
---|
| 8 |
|
---|
| 9 | Namespace ActiveBasic
|
---|
| 10 | Namespace Strings
|
---|
| 11 |
|
---|
| 12 | Sub ChrFill(p As PWSTR, n As SIZE_T, c As WCHAR)
|
---|
| 13 | Dim i As SIZE_T
|
---|
| 14 | For i = 0 To ELM(n)
|
---|
| 15 | p[i] = c
|
---|
| 16 | Next
|
---|
| 17 | End Sub
|
---|
| 18 |
|
---|
| 19 | Sub ChrFill(p As PSTR, n As SIZE_T, c As SByte)
|
---|
| 20 | Dim i As SIZE_T
|
---|
| 21 | For i = 0 To ELM(n)
|
---|
| 22 | p[i] = c
|
---|
| 23 | Next
|
---|
| 24 | End Sub
|
---|
| 25 |
|
---|
| 26 | Function ChrCopy(dst As PCWSTR, src As PCWSTR, size As SIZE_T) As PCWSTR
|
---|
| 27 | memcpy(dst, src, size * SizeOf (WCHAR))
|
---|
| 28 | Return dst
|
---|
| 29 | End Function
|
---|
| 30 |
|
---|
| 31 | Function ChrCopy(dst As PCSTR, src As PCSTR, size As SIZE_T) As PCSTR
|
---|
| 32 | memcpy(dst, src, size)
|
---|
| 33 | Return dst
|
---|
| 34 | End Function
|
---|
| 35 |
|
---|
| 36 | Function ChrMove(dst As PCWSTR, src As PCWSTR, size As SIZE_T) As PCWSTR
|
---|
| 37 | MoveMemory(dst, src, size * SizeOf (WCHAR))
|
---|
| 38 | Return dst
|
---|
| 39 | End Function
|
---|
| 40 |
|
---|
| 41 | Function ChrMove(dst As PCSTR, src As PCSTR, size As SIZE_T) As PCSTR
|
---|
| 42 | MoveMemory(dst, src, size)
|
---|
| 43 | Return dst
|
---|
| 44 | End Function
|
---|
| 45 |
|
---|
| 46 | Function StrCmp(s1 As PCWSTR, s2 As PCWSTR) As Long
|
---|
| 47 | Dim i = 0 As SIZE_T
|
---|
| 48 | While s1[i] = s2[i]
|
---|
| 49 | If s1[i] = 0 Then
|
---|
| 50 | Exit While
|
---|
| 51 | End If
|
---|
| 52 | i++
|
---|
| 53 | Wend
|
---|
| 54 | Return s1[i] As Long - s2[i]
|
---|
| 55 | End Function
|
---|
| 56 |
|
---|
| 57 | Function StrCmp(s1 As PCSTR, s2 As PCSTR) As Long
|
---|
| 58 | Dim i = 0 As SIZE_T
|
---|
| 59 | While s1[i] = s2[i]
|
---|
| 60 | If s1[i] = 0 Then
|
---|
| 61 | Exit While
|
---|
| 62 | End If
|
---|
| 63 | i++
|
---|
| 64 | Wend
|
---|
| 65 | Return s1[i] As Long - s2[i]
|
---|
| 66 | End Function
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | Function ChrCmp(s1 As PCWSTR, s2 As PCWSTR, size As SIZE_T) As Long
|
---|
| 71 | Dim i = 0 As SIZE_T
|
---|
| 72 | While i <> size 'Forではsize = 0のときにまずい
|
---|
| 73 | ChrCmp = s1[i] As Long - s2[i]
|
---|
| 74 | If ChrCmp <> 0 Then
|
---|
| 75 | Exit Function
|
---|
| 76 | End If
|
---|
| 77 | i++
|
---|
| 78 | Wend
|
---|
| 79 | End Function
|
---|
| 80 |
|
---|
| 81 | Function ChrCmp(s1 As PCSTR, s2 As PCSTR, size As SIZE_T) As Long
|
---|
| 82 | Dim i = 0 As SIZE_T
|
---|
| 83 | While i <> size
|
---|
| 84 | ChrCmp = s1[i] As Long - s2[i]
|
---|
| 85 | If ChrCmp <> 0 Then
|
---|
| 86 | Exit Function
|
---|
| 87 | End If
|
---|
| 88 | i++
|
---|
| 89 | Wend
|
---|
| 90 | End Function
|
---|
| 91 |
|
---|
| 92 | Function ChrCmp(s1 As PCWSTR, size1 As SIZE_T, s2 As PCWSTR, size2 As SIZE_T) As Long
|
---|
| 93 | ChrCmp = ChrCmp(s1, s2, System.Math.Min(size1, size2))
|
---|
| 94 | If ChrCmp = 0 Then
|
---|
[370] | 95 | ChrCmp = ( size1 - size2 ) As Long
|
---|
[272] | 96 | End If
|
---|
| 97 | End Function
|
---|
| 98 |
|
---|
| 99 | Function ChrCmp(s1 As PCSTR, size1 As SIZE_T, s2 As PCSTR, size2 As SIZE_T) As Long
|
---|
| 100 | ChrCmp = ChrCmp(s1, s2, System.Math.Min(size1, size2))
|
---|
| 101 | If ChrCmp = 0 Then
|
---|
[370] | 102 | ChrCmp = ( size1 - size2 ) As Long
|
---|
[272] | 103 | End If
|
---|
| 104 | End Function
|
---|
| 105 |
|
---|
| 106 | Function ChrPBrk(str As PCWSTR, cStr As SIZE_T, chars As PCWSTR, cChars As SIZE_T) As SIZE_T
|
---|
| 107 | Dim i As SIZE_T
|
---|
| 108 | For i = 0 To ELM(cStr)
|
---|
| 109 | If ChrFind(chars, cChars, str[i]) <> -1 Then
|
---|
| 110 | Return i
|
---|
| 111 | End If
|
---|
| 112 | Next
|
---|
| 113 | Return -1 As SIZE_T
|
---|
| 114 | End Function
|
---|
| 115 |
|
---|
| 116 | Function ChrPBrk(str As PCSTR, cStr As SIZE_T, Chars As PCSTR, cChars As SIZE_T) As SIZE_T
|
---|
| 117 | Dim i As SIZE_T
|
---|
| 118 | For i = 0 To ELM(cStr)
|
---|
| 119 | If ChrFind(Chars, cChars, str[i]) <> -1 Then
|
---|
| 120 | Return i
|
---|
| 121 | End If
|
---|
| 122 | Next
|
---|
| 123 | Return -1 As SIZE_T
|
---|
| 124 | End Function
|
---|
| 125 |
|
---|
| 126 | Function ChrFind(s As PCWSTR, size As SIZE_T, c As WCHAR) As SIZE_T
|
---|
| 127 | Dim i As SIZE_T
|
---|
| 128 | For i = 0 To ELM(size)
|
---|
| 129 | If s[i] = c Then
|
---|
| 130 | Return i
|
---|
| 131 | End If
|
---|
| 132 | Next
|
---|
| 133 | Return -1 As SIZE_T
|
---|
| 134 | End Function
|
---|
| 135 |
|
---|
| 136 | Function ChrFind(s As PCSTR, size As SIZE_T, c As CHAR) As SIZE_T
|
---|
| 137 | Dim i As SIZE_T
|
---|
| 138 | For i = 0 To ELM(size)
|
---|
| 139 | If s[i] = c Then
|
---|
| 140 | Return i
|
---|
| 141 | End If
|
---|
| 142 | Next
|
---|
| 143 | Return -1 As SIZE_T
|
---|
| 144 | End Function
|
---|
| 145 |
|
---|
| 146 | Function ChrFind(s1 As PCWSTR, len1 As SIZE_T, s2 As PCWSTR, len2 As SIZE_T) As SIZE_T
|
---|
| 147 | If len2 = 0 Then
|
---|
| 148 | 'ChrFind = 0
|
---|
| 149 | Exit Function
|
---|
| 150 | End If
|
---|
| 151 | Do
|
---|
| 152 | Dim prev = ChrFind
|
---|
| 153 | ChrFind = ChrFind(VarPtr(s1[prev]), (len1 - prev) As SIZE_T, s2[0])
|
---|
| 154 | If ChrFind = -1 As SIZE_T Then
|
---|
| 155 | Exit Function
|
---|
| 156 | End If
|
---|
| 157 | ChrFind += prev
|
---|
| 158 |
|
---|
| 159 | If ChrCmp(VarPtr(s1[ChrFind]), s2, len2) = 0 Then
|
---|
| 160 | Exit Function
|
---|
| 161 | End If
|
---|
| 162 | ChrFind++
|
---|
| 163 | If ChrFind = len1 Then
|
---|
| 164 | ChrFind = -1
|
---|
| 165 | Exit Function
|
---|
| 166 | End If
|
---|
| 167 | Loop
|
---|
| 168 | End Function
|
---|
| 169 |
|
---|
| 170 | Function ChrFind(s1 As PCSTR, len1 As SIZE_T, s2 As PCSTR, len2 As SIZE_T) As SIZE_T
|
---|
| 171 | If len2 = 0 Then
|
---|
| 172 | 'ChrFind = 0
|
---|
| 173 | Exit Function
|
---|
| 174 | End If
|
---|
| 175 | Do
|
---|
| 176 | Dim prev = ChrFind
|
---|
| 177 | ChrFind = ChrFind(VarPtr(s1[prev]), (len1 - prev) As SIZE_T, s2[0])
|
---|
| 178 | If ChrFind = -1 As SIZE_T Then
|
---|
| 179 | Exit Function
|
---|
| 180 | End If
|
---|
| 181 | ChrFind += prev
|
---|
| 182 |
|
---|
| 183 | If ChrCmp(VarPtr(s1[ChrFind]), s2, len2) = 0 Then
|
---|
| 184 | Exit Function
|
---|
| 185 | End If
|
---|
| 186 | ChrFind++
|
---|
| 187 | If ChrFind = len1 Then
|
---|
| 188 | ChrFind = -1
|
---|
| 189 | Exit Function
|
---|
| 190 | End If
|
---|
| 191 | Loop
|
---|
| 192 | End Function
|
---|
| 193 |
|
---|
| 194 | Namespace Detail
|
---|
[355] | 195 | Function Split(s As String, c As StrChar) As System.Collections.ArrayList
|
---|
| 196 | Split = New System.Collections.ArrayList
|
---|
[272] | 197 |
|
---|
| 198 | Dim last = 0 As Long
|
---|
| 199 | Do
|
---|
| 200 | Dim i = s.IndexOf(c, last)
|
---|
| 201 | If i < 0 Then
|
---|
| 202 | Split.Add(s.Substring(last, s.Length - last))
|
---|
| 203 | Exit Function
|
---|
| 204 | End If
|
---|
| 205 | Split.Add(s.Substring(last, i - last))
|
---|
| 206 | last = i + 1
|
---|
| 207 | If last > s.Length Then
|
---|
| 208 | Split.Add(System.String.Empty)
|
---|
| 209 | End If
|
---|
| 210 | Loop
|
---|
| 211 | End Function
|
---|
| 212 | End Namespace 'Detail
|
---|
| 213 |
|
---|
| 214 | End Namespace 'Strings
|
---|
| 215 | End Namespace 'ActiveBasic
|
---|
| 216 |
|
---|
| 217 | #endif '__ACTIVEBASIC_STRINGS_STRINGS_AB__
|
---|