1 | #Region "Microsoft.VisualBasic::39b819ac12a3387042ea974b2b086de0, Microsoft.VisualBasic.Core\Language\Language\C\CString.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Module CString |
35 | ' |
36 | ' Function: ChangeCharacter, Decode, IsXDigit, StrChr, StrRChr |
37 | ' StrStr, StrTok |
38 | ' Structure __tokensHelper |
39 | ' |
40 | ' Function: StrTok |
41 | ' |
42 | ' |
43 | ' |
44 | ' |
45 | ' /********************************************************************************/ |
46 | |
47 | #End Region |
48 | |
49 | '---------------------------------------------------------------------------------------- |
50 | ' Copyright © 2006 - 2012 Tangible Software Solutions Inc. |
51 | ' This class can be used by anyone provided that the copyright notice remains intact. |
52 | ' ' This class provides the ability to simulate various classic C string functions |
53 | ' which don't have exact equivalents in the .NET Framework. |
54 | '---------------------------------------------------------------------------------------- |
55 | |
56 | Imports System.Runtime.CompilerServices |
57 | |
58 | Namespace Language.C |
59 | |
60 | ''' <summary> |
61 | ''' This class provides the ability to simulate various classic C string functions |
62 | ''' which don't have exact equivalents in the .NET Framework. |
63 | ''' </summary> |
64 | Public Module CString |
65 | |
66 | <Extension> Public Function Decode(s As String) As String |
67 | If s.StringEmpty Then |
68 | Return s |
69 | Else |
70 | s = s.Replace("\U", "\u").Replace("\A", "\a") |
71 | End If |
72 | |
73 | Try |
74 | ' Hex Unicode \u0000 |
75 | Do |
76 | Dim i = s.IndexOf("\u") |
77 | |
78 | If i = -1 Then |
79 | Exit Do |
80 | End If |
81 | |
82 | Dim u = s.Substring(i, 6) |
83 | Dim n = Convert.ToInt16(u.Replace("\u", ""), 16) |
84 | |
85 | s = s.Replace(u, ChrW(n)) |
86 | Loop |
87 | |
88 | ' Decimal ASCII \a000 |
89 | Do |
90 | Dim i = s.IndexOf("\a") |
91 | |
92 | If i = -1 Then |
93 | Exit Do |
94 | End If |
95 | |
96 | Dim a = s.Substring(i, 5) |
97 | Dim n = CByte(a.Replace("\a", "")) |
98 | |
99 | s = s.Replace(a, Chr(n)) |
100 | Loop |
101 | |
102 | Catch ex As Exception |
103 | Throw New Exception("bad format") |
104 | End Try |
105 | |
106 | Return s |
107 | End Function |
108 | |
109 | ''' <summary> |
110 | ''' This method allows replacing a single character in a string, to help convert |
111 | ''' C++ code where a single character in a character array is replaced. |
112 | ''' </summary> |
113 | ''' <param name="sourcestring"></param> |
114 | ''' <param name="charindex"></param> |
115 | ''' <param name="changechar"></param> |
116 | ''' <returns></returns> |
117 | ''' |
118 | <Extension> |
119 | Public Function ChangeCharacter(sourcestring As String, charindex As Integer, changechar As Char) As String |
120 | Return (If(charindex > 0, sourcestring.Substring(0, charindex), "")) & changechar.ToString() & (If(charindex < sourcestring.Length - 1, sourcestring.Substring(charindex + 1), "")) |
121 | End Function |
122 | |
123 | ''' <summary> |
124 | ''' This method simulates the classic C string function 'isxdigit' (and 'iswxdigit'). |
125 | ''' </summary> |
126 | ''' <param name="character"></param> |
127 | ''' <returns></returns> |
128 | ''' |
129 | <Extension> |
130 | Public Function IsXDigit(character As Char) As Boolean |
131 | If Char.IsDigit(character) Then |
132 | Return True |
133 | ElseIf "ABCDEFabcdef".IndexOf(character) > -1 Then |
134 | Return True |
135 | Else |
136 | Return False |
137 | End If |
138 | End Function |
139 | |
140 | ''' <summary> |
141 | ''' This method simulates the classic C string function 'strchr' (and 'wcschr'). |
142 | ''' </summary> |
143 | ''' <param name="stringtosearch"></param> |
144 | ''' <param name="chartofind"></param> |
145 | ''' <returns></returns> |
146 | ''' |
147 | <Extension> |
148 | Public Function StrChr(stringtosearch As String, chartofind As Char) As String |
149 | Dim index As Integer = stringtosearch.IndexOf(chartofind) |
150 | If index > -1 Then |
151 | Return stringtosearch.Substring(index) |
152 | Else |
153 | Return Nothing |
154 | End If |
155 | End Function |
156 | |
157 | ''' <summary> |
158 | ''' This method simulates the classic C string function 'strrchr' (and 'wcsrchr'). |
159 | ''' </summary> |
160 | ''' <param name="stringtosearch"></param> |
161 | ''' <param name="chartofind"></param> |
162 | ''' <returns></returns> |
163 | ''' |
164 | <Extension> |
165 | Public Function StrRChr(stringtosearch As String, chartofind As Char) As String |
166 | Dim index As Integer = stringtosearch.LastIndexOf(chartofind) |
167 | If index > -1 Then |
168 | Return stringtosearch.Substring(index) |
169 | Else |
170 | Return Nothing |
171 | End If |
172 | End Function |
173 | |
174 | ''' <summary> |
175 | ''' This method simulates the classic C string function 'strstr' (and 'wcsstr'). |
176 | ''' </summary> |
177 | ''' <param name="stringtosearch"></param> |
178 | ''' <param name="stringtofind"></param> |
179 | ''' <returns></returns> |
180 | ''' |
181 | <Extension> |
182 | Public Function StrStr(stringtosearch As String, stringtofind As String) As String |
183 | Dim index As Integer = stringtosearch.IndexOf(stringtofind) |
184 | If index > -1 Then |
185 | Return stringtosearch.Substring(index) |
186 | Else |
187 | Return Nothing |
188 | End If |
189 | End Function |
190 | |
191 | Private Structure __tokensHelper |
192 | Private activestring As String |
193 | Private activeposition As Integer |
194 | |
195 | ''' <summary> |
196 | ''' This method simulates the classic C string function 'strtok' (and 'wcstok'). |
197 | ''' Note that the .NET string 'Split' method cannot be used to simulate 'strtok' since |
198 | ''' it doesn't allow changing the delimiters between each token retrieval. |
199 | ''' </summary> |
200 | ''' <param name="stringtotokenize"></param> |
201 | ''' <param name="delimiters"></param> |
202 | ''' <returns></returns> |
203 | Public Function StrTok(stringtotokenize As String, delimiters As String) As String |
204 | If stringtotokenize IsNot Nothing Then |
205 | activestring = stringtotokenize |
206 | activeposition = -1 |
207 | End If |
208 | |
209 | 'the stringtotokenize was never set: |
210 | If activestring Is Nothing Then |
211 | Return Nothing |
212 | End If |
213 | |
214 | 'all tokens have already been extracted: |
215 | If activeposition = activestring.Length Then |
216 | Return Nothing |
217 | End If |
218 | |
219 | 'bypass delimiters: |
220 | activeposition += 1 |
221 | While activeposition < activestring.Length AndAlso delimiters.IndexOf(activestring(activeposition)) > -1 |
222 | activeposition += 1 |
223 | End While |
224 | |
225 | 'only delimiters were left, so return null: |
226 | If activeposition = activestring.Length Then |
227 | Return Nothing |
228 | End If |
229 | |
230 | 'get starting position of string to return: |
231 | Dim startingposition As Integer = activeposition |
232 | |
233 | 'read until next delimiter: |
234 | Do |
235 | activeposition += 1 |
236 | Loop While activeposition < activestring.Length AndAlso delimiters.IndexOf(activestring(activeposition)) = -1 |
237 | |
238 | Return activestring.Substring(startingposition, activeposition - startingposition) |
239 | End Function |
240 | End Structure |
241 | |
242 | ''' <summary> |
243 | ''' This method simulates the classic C string function 'strtok' (and 'wcstok'). |
244 | ''' Note that the .NET string 'Split' method cannot be used to simulate 'strtok' since |
245 | ''' it doesn't allow changing the delimiters between each token retrieval. |
246 | ''' </summary> |
247 | ''' <param name="stringtotokenize"></param> |
248 | ''' <param name="delimiters"></param> |
249 | ''' <returns></returns> |
250 | <Extension> |
251 | Public Function StrTok(stringtotokenize As String, delimiters As String) As String |
252 | Return New __tokensHelper().StrTok(stringtotokenize, delimiters) |
253 | End Function |
254 | End Module |
255 | End Namespace |