source: trunk/Include/system/string.sbp @ 395

Last change on this file since 395 was 395, checked in by イグトランス (egtra), 16 years ago

ToXXStrの効率化のため、GetStrの戻り値なし版、GetStrNTの追加。
Stringを引数に取る変換関数で、Nothingが渡された場合の対応を追加。

File size: 9.7 KB
Line 
1'string.sbp
2'文字列変数の操作用
3
4#ifndef _INC_BASIC_STRING
5#define _INC_BASIC_STRING
6
7#require <Classes/System/String.ab>
8#require <Classes/System/Text/StringBuilder.ab>
9
10Function StrPtr(s As String) As *StrChar
11    If Not ActiveBasic.IsNothing(s) Then
12        StrPtr = s.StrPtr
13    End If
14End Function
15'StringBuilder版はClasses/System/Text/StringBuilder.abに定義されている
16
17Function ZeroString(length As Long) As System.Text.StringBuilder
18    ZeroString = New System.Text.StringBuilder
19    ZeroString.Length = length
20End Function
21
22Function MakeStr(psz As PSTR) As String
23    Return New String(psz)
24End Function
25
26Function MakeStr(psz As PWSTR) As String
27    Return New String(psz)
28End Function
29
30Dim _System_AllocForConvertedString As *Function(size As SIZE_T) As VoidPtr
31_System_AllocForConvertedString = AddressOf (GC_malloc_atomic)
32
33Namespace Detail
34    Function GetWCStr(mbsSrc As PSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
35        Dim lenWCS = MultiByteToWideChar(CP_THREAD_ACP, 0, mbsSrc, (len As DWord) As Long, 0, 0)
36        wcsDst = _System_AllocForConvertedString(SizeOf (WCHAR) * (lenWCS + 1)) As PWSTR
37        GetWCStr = MultiByteToWideChar(CP_THREAD_ACP, 0, mbsSrc, (len As DWord) As Long, wcsDst, lenWCS)
38        wcsDst[GetWCStr] = 0
39    End Function
40
41    Function GetWCStr(wcsSrc As PWSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
42        wcsDst = wcsSrc
43        GetWCStr = len
44    End Function
45
46    Function GetMBStr(wcsSrc As PWSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
47        Dim lenMBS = WideCharToMultiByte(CP_THREAD_ACP, 0, wcsSrc, (len As DWord) As Long, 0, 0, 0, 0)
48        mbsDst = _System_AllocForConvertedString(SizeOf (SByte) * (lenMBS + 1)) As PSTR
49        GetMBStr = WideCharToMultiByte(CP_THREAD_ACP, 0, wcsSrc, (wcsSrc As DWord) As Long, mbsDst, lenMBS, 0, 0) As SIZE_T
50        mbsDst[GetMBStr] = 0
51    End Function
52
53    Function GetMBStr(mbsSrc As PSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
54        mbsDst = mbsSrc
55        GetMBStr = len
56    End Function
57End Namespace
58
59/*
60変換の組み合わせは、
61入力引数: wcsz, wcs + len, mbsz, mbs + len, str
62出力関数: wcs(z)出力GetStr, mbs(z)出力GetStr,
63          wcs(z)出力GetStrNT, mbs(z)出力GetStrNT,
64          GetWCStr, GetMBStr, GetTCStr, GetSCStr,
65          ToWCStr, ToMBStr, ToTCStr, ToSCStr
66で、5 * 12 = 60通り。
67*/
68
69Function GetStr(mbszSrc As PSTR, ByRef wcsDst As PWSTR) As SIZE_T
70    If mbszSrc = 0 Then
71        wcsDst = 0
72        Return 0
73    Else
74        Return Detail.GetWCStr(mbszSrc, lstrlenA(mbszSrc) As SIZE_T, wcsDst)
75    End If
76End Function
77
78Function GetStr(mbsSrc As PSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
79    If mbsSrc = 0 Then
80        wcsDst = 0
81        Return 0
82    Else
83        Return Detail.GetWCStr(mbsSrc, len, wcsDst)
84    End If
85End Function
86
87Function GetStr(wcszSrc As PWSTR, ByRef wcsDst As PWSTR) As SIZE_T
88    If wcszSrc = 0 Then
89        wcsDst = 0
90        Return 0
91    Else
92        wcsDst = wcszSrc
93        Return lstrlenW(wcszSrc) As SIZE_T
94    End If
95End Function
96
97Function GetStr(wcsSrc As PWSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
98    If wcsSrc = 0 Then
99        wcsDst = 0
100        Return 0
101    Else
102        wcsDst = wcsSrc
103        Return len
104    End If
105End Function
106
107Function GetStr(wcszSrc As PWSTR, ByRef mbsDst As PSTR) As SIZE_T
108    If wcszSrc = 0 Then
109        mbsDst = 0
110        Return 0
111    Else
112        Return Detail.GetMBStr(wcszSrc, lstrlenW(wcszSrc) As SIZE_T, mbsDst)
113    End If
114End Function
115
116Function GetStr(wcsSrc As PWSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
117    If wcsSrc = 0 Then
118        mbsDst = 0
119        Return 0
120    Else
121        Return Detail.GetMBStr(wcsSrc, len As SIZE_T, mbsDst)
122    End If
123End Function
124
125Function GetStr(mbszSrc As PSTR, ByRef mbsDst As PSTR) As SIZE_T
126    If mbszSrc = 0 Then
127        mbsDst = 0
128        Return 0
129    Else
130        mbsDst = mbszSrc
131        Return lstrlenA(mbszSrc) As SIZE_T
132    End If
133End Function
134
135Function GetStr(mbsSrc As PSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
136    If mbsSrc = 0 Then
137        mbsDst = 0
138        Return len
139    Else
140        mbsDst = mbsSrc
141        Return 0
142    End If
143End Function
144
145Function GetStr(strSrc As String, ByRef wcsDst As PWSTR) As SIZE_T
146    If ActiveBasic.IsNothing(strSrc) Then
147        wcsDst = 0
148        Return 0
149    Else
150        Return Detail.GetWCStr(strSrc.StrPtr, strSrc.Length As SIZE_T, wcsDst)
151    End If
152End Function
153
154Function GetStr(strSrc As String, ByRef mbsDst As PSTR) As SIZE_T
155    If ActiveBasic.IsNothing(strSrc) Then
156        mbsDst = 0
157        Return 0
158    Else
159        Return Detail.GetMBStr(strSrc.StrPtr, strSrc.Length As SIZE_T, mbsDst)
160    End If
161End Function
162
163Sub GetStrNT(mbszSrc As PSTR, ByRef mbszDst As PSTR)
164    mbszDst = mbszSrc
165End Sub
166
167Sub GetStrNT(mbsSrc As PSTR, len As SIZE_T, ByRef mbszDst As PSTR)
168    mbszDst = mbsSrc
169End Sub
170
171Sub GetStrNT(mbszSrc As PSTR, ByRef wcszDst As PWSTR)
172    GetStr(mbszSrc, wcszDst)
173End Sub
174
175Sub GetStrNT(mbsSrc As PSTR, len As SIZE_T, ByRef wcszDst As PWSTR)
176    GetStr(mbsSrc, len, wcszDst)
177End Sub
178
179Sub GetStrNT(wcszSrc As PWSTR, ByRef mbszDst As PSTR)
180    GetStr(wcszSrc, mbszDst)
181End Sub
182
183Sub GetStrNT(wcsSrc As PWSTR, len As SIZE_T, ByRef mbszDst As PSTR)
184    GetStr(wcsSrc, len, mbszDst)
185End Sub
186
187Sub GetStrNT(wcszSrc As PWSTR, ByRef wcszDst As PWSTR)
188    wcszDst = wcszSrc
189End Sub
190
191Sub GetStrNT(wcsSrc As PWSTR, len As SIZE_T, ByRef wcszDst As PWSTR)
192    wcszDst = wcsSrc
193End Sub
194
195Sub GetStrNT(strSrc As String, ByRef mbszDst As PSTR)
196    GetStr(strSrc, mbszDst)
197End Sub
198
199Sub GetStrNT(strSrc As String, ByRef wcszDst As PWSTR)
200    GetStr(strSrc, wcszDst)
201End Sub
202
203Function GetWCStr(mbszSrc As PSTR, ByRef wcsDst As PWSTR) As SIZE_T
204    Return GetStr(mbszSrc, wcsDst)
205End Function
206
207Function GetWCStr(mbsSrc As PSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
208    Return GetStr(mbsSrc, len, wcsDst)
209End Function
210
211Function GetWCStr(wcszSrc As PWSTR, ByRef wcsDst As PWSTR) As SIZE_T
212    Return GetStr(wcszSrc, wcsDst)
213End Function
214
215Function GetWCStr(wcsSrc As PWSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
216    Return GetStr(wcsSrc, len, wcsDst)
217End Function
218
219Function GetWCStr(strSrc As String, ByRef wcsDst As PWSTR) As SIZE_T
220    Return GetStr(strSrc.StrPtr, strSrc.Length As SIZE_T, wcsDst)
221End Function
222
223Function GetMBStr(mbszSrc As PWSTR, ByRef mbsDst As PSTR) As SIZE_T
224    Return GetStr(mbszSrc, mbsDst)
225End Function
226
227Function GetMBStr(wcsSrc As PWSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
228    Return GetStr(wcsSrc, len, mbsDst)
229End Function
230
231Function GetMBStr(mbszSrc As PSTR, ByRef mbsDst As PSTR) As SIZE_T
232    Return GetStr(mbszSrc, mbsDst)
233End Function
234
235Function GetMBStr(mbsSrc As PSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
236    Return GetStr(mbsSrc, len, mbsDst)
237End Function
238
239Function GetMBStr(strSrc As String, ByRef mbsDst As PSTR) As SIZE_T
240    Return GetStr(strSrc.StrPtr, strSrc.Length As SIZE_T, mbsDst)
241End Function
242
243Function GetTCStr(mbszSrc As PSTR, ByRef tcsDst As PCTSTR) As SIZE_T
244    Return GetStr(mbszSrc, tcsDst)
245End Function
246
247Function GetTCStr(mbsSrc As PSTR, len As SIZE_T, ByRef tcsDst As PCTSTR) As SIZE_T
248    Return GetStr(mbsSrc, len, tcsDst)
249End Function
250
251Function GetTCStr(wcszSrc As PWSTR, ByRef tcsDst As PCTSTR) As SIZE_T
252    Return GetStr(wcszSrc, tcsDst)
253End Function
254
255Function GetTCStr(wcsSrc As PWSTR, len As SIZE_T, ByRef tcsDst As PCTSTR) As SIZE_T
256    Return GetStr(wcsSrc, len, tcsDst)
257End Function
258
259Function GetTCStr(strSrc As String, ByRef tcsDst As PCTSTR) As SIZE_T
260    Return GetStr(strSrc.StrPtr, strSrc.Length As SIZE_T, tcsDst)
261End Function
262
263Function GetSCStr(mbszSrc As PSTR, ByRef ssDst As *StrChar) As SIZE_T
264    Return GetStr(mbszSrc, ssDst)
265End Function
266
267Function GetSCStr(mbsSrc As PSTR, len As SIZE_T, ByRef ssDst As *StrChar) As SIZE_T
268    Return GetStr(mbsSrc, len, ssDst)
269End Function
270
271Function GetSCStr(wcszSrc As PWSTR, ByRef ssDst As *StrChar) As SIZE_T
272    Return GetStr(wcszSrc, ssDst)
273End Function
274
275Function GetSCStr(wcsSrc As PWSTR, len As SIZE_T, ByRef ssDst As *StrChar) As SIZE_T
276    Return GetStr(wcsSrc, len, ssDst)
277End Function
278
279Function GetSCStr(strSrc As String, ByRef ssDst As *StrChar) As SIZE_T
280    Return GetStr(strSrc.StrPtr, strSrc.Length As SIZE_T, ssDst)
281End Function
282
283Function ToWCStr(mbsz As PSTR) As PWSTR
284    GetStrNT(mbsz, ToWCStr)
285End Function
286
287Function ToWCStr(mbs As PSTR, len As SIZE_T) As PWSTR
288    GetStrNT(mbs, len, ToWCStr)
289End Function
290
291Function ToWCStr(wcsz As PWSTR) As PWSTR
292    ToWCStr = wcsz
293End Function
294
295Function ToWCStr(wcs As PWSTR, len As SIZE_T) As PWSTR
296    ToWCStr = wcs
297End Function
298
299Function ToWCStr(s As String) As PWSTR
300    GetStrNT(s, ToWCStr)
301End Function
302
303Function ToMBStr(mbsz As PSTR) As PSTR
304    ToMBStr = mbsz
305End Function
306
307Function ToMBStr(mbs As PSTR, len As SIZE_T) As PSTR
308    ToMBStr = mbs
309End Function
310
311Function ToMBStr(wcsz As PWSTR) As PSTR
312    GetStrNT(wcsz, ToMBStr)
313End Function
314
315Function ToMBStr(wcs As PWSTR, len As SIZE_T) As PSTR
316    GetStrNT(wcs, len, ToMBStr)
317End Function
318
319Function ToMBStr(s As String) As PSTR
320    GetStrNT(s, ToMBStr)
321End Function
322
323Function ToTCStr(mbsz As PSTR) As PCTSTR
324    GetStrNT(mbsz, ToTCStr)
325End Function
326
327Function ToTCStr(mbs As PSTR, len As SIZE_T) As PCTSTR
328    GetStrNT(mbs, len, ToTCStr)
329End Function
330
331Function ToTCStr(wcsz As PWSTR) As PCTSTR
332    GetStrNT(wcsz, ToTCStr)
333End Function
334
335Function ToTCStr(wcs As PWSTR, len As SIZE_T) As PCTSTR
336    GetStrNT(wcs, len, ToTCStr)
337End Function
338
339Function ToTCStr(s As String) As PCTSTR
340    GetStrNT(s, ToTCStr)
341End Function
342
343Function ToSCStr(mbsz As PSTR) As *StrChar
344    GetStrNT(mbsz, ToSCStr)
345End Function
346
347Function ToSCStr(mbs As PSTR, len As SIZE_T) As *StrChar
348    GetStrNT(mbs, len, ToSCStr)
349End Function
350
351Function ToSCStr(wcsz As PWSTR) As *StrChar
352    GetStrNT(wcsz, ToSCStr)
353End Function
354
355Function ToSCStr(wcs As PWSTR, len As SIZE_T) As *StrChar
356    GetStrNT(wcs, len, ToSCStr)
357End Function
358
359Function ToSCStr(s As String) As *StrChar
360    ToSCStr = StrPtr(s)
361End Function
362
363#ifdef __STRING_IS_NOT_UNICODE
364Typedef BoxedStrChar = System.SByte
365#else
366TypeDef BoxedStrChar = System.UInt16
367#endif
368
369#endif '_INC_BASIC_STRING
Note: See TracBrowser for help on using the repository browser.