source: trunk/ab5.0/ablib/src/system/string.sbp@ 697

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

string.sbpの変換ルーチンで、ヌル終端無し文字列から有り文字列への変換でヌルを付け忘れるバグのため、常にヌル終端を付加する前の方式に戻す。GetWCStr, GetMBStr, GetTCStrを削除。

File size: 6.0 KB
RevLine 
[1]1'string.sbp
2'文字列変数の操作用
3
[671]4/*!
5@brief Stringが内部で保持しているポインタを返す。
6@param[in] s 文字列。
7@return sの内部バッファへのポインタ、ただしsがNothingならNULL。
8*/
[478]9Function StrPtr(s As String) As *Char
[386]10 If Not ActiveBasic.IsNothing(s) Then
[385]11 StrPtr = s.StrPtr
12 End If
[1]13End Function
[671]14'StringBuilder版はClasses/System/Text/StringBuilder.abに定義されている。
[1]15
[671]16/*!
17@brief 指定した長さの空文字(\0)を持つ文字列を作成する。
18@param[in] length 長さ
19@return Legnth = lengthとなっている文字列。
20*/
21Function ZeroString(length As Long) As String
22 ZeroString = New String(0 As Char, length)
23End Function
[1]24
[167]25Function MakeStr(psz As PSTR) As String
26 Return New String(psz)
[1]27End Function
28
[167]29Function MakeStr(psz As PWSTR) As String
30 Return New String(psz)
[142]31End Function
32
33Dim _System_AllocForConvertedString As *Function(size As SIZE_T) As VoidPtr
34_System_AllocForConvertedString = AddressOf (GC_malloc_atomic)
35
[395]36Namespace Detail
37 Function GetWCStr(mbsSrc As PSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
[654]38 Dim lenWCS = MultiByteToWideChar(CP_ACP, 0, mbsSrc, (len As DWord) As Long, 0, 0)
[697]39 wcsDst = _System_AllocForConvertedString(SizeOf (WCHAR) * (lenWCS + 1)) As PWSTR
[654]40 GetWCStr = MultiByteToWideChar(CP_ACP, 0, mbsSrc, (len As DWord) As Long, wcsDst, lenWCS)
[697]41 wcsDst[lenWCS] = 0
[395]42 End Function
43
44 Function GetWCStr(wcsSrc As PWSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
45 wcsDst = wcsSrc
46 GetWCStr = len
47 End Function
48
49 Function GetMBStr(wcsSrc As PWSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
[654]50 Dim lenMBS = WideCharToMultiByte(CP_ACP, 0, wcsSrc, (len As DWord) As Long, 0, 0, 0, 0)
[697]51 mbsDst = _System_AllocForConvertedString(SizeOf (CHAR) * (lenMBS + 1)) As PSTR
[671]52 GetMBStr = WideCharToMultiByte(CP_ACP, 0, wcsSrc, (len As DWord) As Long, mbsDst, lenMBS, 0, 0) As SIZE_T
[697]53 mbsDst[lenMBS] = 0
[395]54 End Function
55
56 Function GetMBStr(mbsSrc As PSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
57 mbsDst = mbsSrc
58 GetMBStr = len
59 End Function
[652]60
61 ' ToTCStrの補助
62 Sub GetStrNT(mbszSrc As PSTR, ByRef mbszDst As PSTR)
63 mbszDst = mbszSrc
64 End Sub
65
66 Sub GetStrNT(mbszSrc As PSTR, ByRef wcszDst As PWSTR)
67 GetStr(mbszSrc, wcszDst)
68 End Sub
69
70 Sub GetStrNT(wcszSrc As PWSTR, ByRef mbszDst As PSTR)
71 GetStr(wcszSrc, mbszDst)
72 End Sub
73
74 Sub GetStrNT(wcszSrc As PWSTR, ByRef wcszDst As PWSTR)
75 wcszDst = wcszSrc
76 End Sub
77
[395]78End Namespace
79
80/*
81変換の組み合わせは、
82入力引数: wcsz, wcs + len, mbsz, mbs + len, str
[697]83出力関数: wcs(z)出力GetStr, mbs(z)出力GetStr, ToWCStr, ToMBStr, ToTCStr,
84で、5 * 5 = 25通り。
[395]85*/
86
87Function GetStr(mbszSrc As PSTR, ByRef wcsDst As PWSTR) As SIZE_T
88 If mbszSrc = 0 Then
89 wcsDst = 0
90 Return 0
[142]91 Else
[697]92 Return Detail.GetWCStr(mbszSrc, lstrlenA(mbszSrc) As SIZE_T, wcsDst)
[142]93 End If
94End Function
95
[395]96Function GetStr(mbsSrc As PSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
97 If mbsSrc = 0 Then
98 wcsDst = 0
99 Return 0
100 Else
101 Return Detail.GetWCStr(mbsSrc, len, wcsDst)
102 End If
[142]103End Function
104
[395]105Function GetStr(wcszSrc As PWSTR, ByRef wcsDst As PWSTR) As SIZE_T
106 If wcszSrc = 0 Then
107 wcsDst = 0
108 Return 0
[142]109 Else
[395]110 wcsDst = wcszSrc
[652]111 Return lstrlenW(wcszSrc) As SIZE_T + 1
[142]112 End If
113End Function
114
[395]115Function GetStr(wcsSrc As PWSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
116 If wcsSrc = 0 Then
117 wcsDst = 0
118 Return 0
119 Else
120 wcsDst = wcsSrc
[149]121 Return len
[142]122 End If
123End Function
124
[395]125Function GetStr(wcszSrc As PWSTR, ByRef mbsDst As PSTR) As SIZE_T
126 If wcszSrc = 0 Then
127 mbsDst = 0
[142]128 Return 0
129 Else
[697]130 Return Detail.GetMBStr(wcszSrc, lstrlenW(wcszSrc) As SIZE_T, mbsDst)
[142]131 End If
132End Function
133
[395]134Function GetStr(wcsSrc As PWSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
135 If wcsSrc = 0 Then
136 mbsDst = 0
137 Return 0
138 Else
139 Return Detail.GetMBStr(wcsSrc, len As SIZE_T, mbsDst)
140 End If
[142]141End Function
142
[395]143Function GetStr(mbszSrc As PSTR, ByRef mbsDst As PSTR) As SIZE_T
144 If mbszSrc = 0 Then
145 mbsDst = 0
146 Return 0
[142]147 Else
[395]148 mbsDst = mbszSrc
[652]149 Return lstrlenA(mbszSrc) As SIZE_T + 1
[142]150 End If
151End Function
152
[395]153Function GetStr(mbsSrc As PSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
154 If mbsSrc = 0 Then
155 mbsDst = 0
[149]156 Return len
157 Else
[395]158 mbsDst = mbsSrc
[149]159 Return 0
160 End If
[142]161End Function
162
[395]163Function GetStr(strSrc As String, ByRef wcsDst As PWSTR) As SIZE_T
164 If ActiveBasic.IsNothing(strSrc) Then
165 wcsDst = 0
166 Return 0
167 Else
168 Return Detail.GetWCStr(strSrc.StrPtr, strSrc.Length As SIZE_T, wcsDst)
169 End If
[142]170End Function
171
[395]172Function GetStr(strSrc As String, ByRef mbsDst As PSTR) As SIZE_T
173 If ActiveBasic.IsNothing(strSrc) Then
174 mbsDst = 0
175 Return 0
176 Else
177 Return Detail.GetMBStr(strSrc.StrPtr, strSrc.Length As SIZE_T, mbsDst)
178 End If
[142]179End Function
180
[395]181Function ToWCStr(mbsz As PSTR) As PWSTR
[652]182 Detail.GetStrNT(mbsz, ToWCStr)
[142]183End Function
184
[395]185Function ToWCStr(mbs As PSTR, len As SIZE_T) As PWSTR
[652]186 GetStr(mbs, len, ToWCStr)
[142]187End Function
188
[395]189Function ToWCStr(wcsz As PWSTR) As PWSTR
190 ToWCStr = wcsz
[142]191End Function
192
[395]193Function ToWCStr(wcs As PWSTR, len As SIZE_T) As PWSTR
194 ToWCStr = wcs
[142]195End Function
196
[237]197Function ToWCStr(s As String) As PWSTR
[652]198 GetStr(s, ToWCStr)
[142]199End Function
200
[395]201Function ToMBStr(mbsz As PSTR) As PSTR
202 ToMBStr = mbsz
[142]203End Function
204
[395]205Function ToMBStr(mbs As PSTR, len As SIZE_T) As PSTR
206 ToMBStr = mbs
[142]207End Function
208
[395]209Function ToMBStr(wcsz As PWSTR) As PSTR
[652]210 Detail.GetStrNT(wcsz, ToMBStr)
[142]211End Function
212
[395]213Function ToMBStr(wcs As PWSTR, len As SIZE_T) As PSTR
[652]214 GetStr(wcs, len, ToMBStr)
[142]215End Function
216
[237]217Function ToMBStr(s As String) As PSTR
[652]218 GetStr(s, ToMBStr)
[142]219End Function
220
[395]221Function ToTCStr(mbsz As PSTR) As PCTSTR
[652]222 Detail.GetStrNT(mbsz, ToTCStr)
[142]223End Function
224
[395]225Function ToTCStr(mbs As PSTR, len As SIZE_T) As PCTSTR
[652]226 GetStr(mbs, len, ToTCStr)
[142]227End Function
228
[395]229Function ToTCStr(wcsz As PWSTR) As PCTSTR
[652]230 Detail.GetStrNT(wcsz, ToTCStr)
[142]231End Function
232
[395]233Function ToTCStr(wcs As PWSTR, len As SIZE_T) As PCTSTR
[652]234 GetStr(wcs, len, ToTCStr)
[142]235End Function
236
[237]237Function ToTCStr(s As String) As PCTSTR
[652]238 Return StrPtr(s)
[142]239End Function
240
[398]241#ifndef UNICODE
[497]242TypeDef BoxedStrChar = System.SByte
[383]243#else
244TypeDef BoxedStrChar = System.UInt16
245#endif
Note: See TracBrowser for help on using the repository browser.