source: Include/com/bstring.ab@ 282

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

StringBuilderを追加。String不変へ。共通の文字列操作関数をActiveBasic.Strings内に配置(設計に検討の余地あり)。

File size: 3.0 KB
RevLine 
[138]1' com/bstring.ab
2
3#require <ole2.ab>
4#require <oleauto.ab>
5
[267]6Namespace ActiveBasic
7Namespace COM
8
[138]9Class BString
[272]10 'Inherits System.IDisposable, System.ICloneable
[138]11Public
12 Sub BString()
13 bs = 0
14 End Sub
15
16 Sub BString(len As DWord)
[142]17 bs = SysAllocStringLen(0, len)
[138]18 End Sub
19
[272]20 Sub BString(s As BString)
[226]21 Init(s.bs, s.Length)
[138]22 End Sub
23
24 Sub BString(s As LPCOLESTR)
25 bs = SysAllocString(s)
26 End Sub
27
28 Sub BString(s As LPCOLESTR, len As DWord)
29 bs = SysAllocStringLen(s, len)
30 End Sub
31
32 Sub BString(s As PCSTR)
[226]33 Init(s, lstrlenA(s))
[138]34 End Sub
35
36 Sub BString(s As PCSTR, len As DWord)
[208]37 Dim lenBS = MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, 0, 0)
[138]38 bs = SysAllocStringLen(0, lenBS)
[208]39 MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, bs, lenBS)
[138]40 End Sub
41
[272]42 Sub BString(s As String)
[226]43 Init(s.StrPtr, s.Length As DWord)
[138]44 End Sub
45
[142]46 Sub ~BString()
47 Clear()
48 End Sub
49
[272]50 Sub Assign(bstr As BString)
[142]51 Clear()
[226]52 Init(bstr, bstr.Length)
[142]53 End Sub
54
55 Sub Assign(s As LPCOLESTR)
56 Clear()
[226]57 Init(s, lstrlenW(s))
[142]58 End Sub
59
60 Sub AssignFromBStr(bstr As BSTR)
61 Clear()
[175]62 String.Copy(bs, bstr)
[142]63 End Sub
64
[175]65 Const Function Copy() As BSTR
66 BString.Copy(Copy, bs)
67 End Function
68
[272]69 /*Override*/ Function Clone() As BString
70 Return New BString(This)
71 End Function
72
73 /*Override*/ Sub Dispose()
74 Clear()
75 End Sub
76
[138]77 Sub Clear()
78 If bs <> 0 Then
79 SysFreeString(bs)
80 bs = 0
81 End If
82 End Sub
83
[192]84 Sub Attach(ByRef bstr As BSTR)
[138]85 Clear()
[175]86 BString.Move(bs, bstr)
[138]87 End Sub
88
89 Function Detach() As BSTR
[175]90 BString.Move(Detach, bs)
[138]91 End Function
92
93 Function BStr() As BSTR
94 BStr = bs
95 End Function
[208]96/*
97 Static Function Assgin(bs As BSTR) As BString
98 Assgin = New BString
99 Assgin.Assgin(bs)
100 End Function
[138]101
[208]102 Static Function Attach(bs As BSTR) As BString
103 Attach = New BString
104 Attach.Attach(bs)
105 End Function
106*/
[138]107 Const Function Length() As DWord
108 Length = SysStringLen(bs)
109 End Function
110
[142]111 Const Function Operator [](i As SIZE_T) As OLECHAR
[138]112#ifdef _DEBUG
113 If i > Length Then
114 'Throw OutOfRangeException
115 End If
116#endif
117 Return bs[i]
118 End Function
119
120 Sub Operator []=(i As SIZE_T, c As OLECHAR)
121#ifdef _DEBUG
122 If i > Length Then
123 'Throw OutOfRangeException
124 End If
125#endif
126 bs[i] = c
127 End Sub
128
129 Override Function ToString() As String
[208]130 Return New String(bs As PCWSTR, Length As Long)
[138]131 End Function
132
[175]133 Override Function GetHashCode() As Long
134 Return _System_GetHashFromWordArray(bs, Length)
135 End Function
136
[138]137Private
138 bs As BSTR
[175]139
[272]140 Sub Init(s As PCSTR, len As DWord)
141 Dim lenBS = MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, 0, 0)
142 bs = SysAllocStringLen(0, lenBS)
143 MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, bs, lenBS)
144 End Sub
145
[175]146 Static Sub Copy(ByRef dst As BSTR, ByVal src As BSTR)
147 dst = SysAllocStringLen(src, SysStringLen(src))
148 End Sub
149
150 Static Sub Move(ByRef dst As BSTR, ByRef src As BSTR)
151 dst = src
152 src = 0
153 End Sub
[138]154End Class
155
[267]156End Namespace 'COM
157End Namespace 'ActiveBasic
Note: See TracBrowser for help on using the repository browser.