source: Include/com/bstring.ab@ 272

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

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

File size: 3.0 KB
Line 
1' com/bstring.ab
2
3#require <ole2.ab>
4#require <oleauto.ab>
5
6Namespace ActiveBasic
7Namespace COM
8
9Class BString
10 'Inherits System.IDisposable, System.ICloneable
11Public
12 Sub BString()
13 bs = 0
14 End Sub
15
16 Sub BString(len As DWord)
17 bs = SysAllocStringLen(0, len)
18 End Sub
19
20 Sub BString(s As BString)
21 Init(s.bs, s.Length)
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)
33 Init(s, lstrlenA(s))
34 End Sub
35
36 Sub BString(s As PCSTR, len As DWord)
37 Dim lenBS = MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, 0, 0)
38 bs = SysAllocStringLen(0, lenBS)
39 MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, bs, lenBS)
40 End Sub
41
42 Sub BString(s As String)
43 Init(s.StrPtr, s.Length As DWord)
44 End Sub
45
46 Sub ~BString()
47 Clear()
48 End Sub
49
50 Sub Assign(bstr As BString)
51 Clear()
52 Init(bstr, bstr.Length)
53 End Sub
54
55 Sub Assign(s As LPCOLESTR)
56 Clear()
57 Init(s, lstrlenW(s))
58 End Sub
59
60 Sub AssignFromBStr(bstr As BSTR)
61 Clear()
62 String.Copy(bs, bstr)
63 End Sub
64
65 Const Function Copy() As BSTR
66 BString.Copy(Copy, bs)
67 End Function
68
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
77 Sub Clear()
78 If bs <> 0 Then
79 SysFreeString(bs)
80 bs = 0
81 End If
82 End Sub
83
84 Sub Attach(ByRef bstr As BSTR)
85 Clear()
86 BString.Move(bs, bstr)
87 End Sub
88
89 Function Detach() As BSTR
90 BString.Move(Detach, bs)
91 End Function
92
93 Function BStr() As BSTR
94 BStr = bs
95 End Function
96/*
97 Static Function Assgin(bs As BSTR) As BString
98 Assgin = New BString
99 Assgin.Assgin(bs)
100 End Function
101
102 Static Function Attach(bs As BSTR) As BString
103 Attach = New BString
104 Attach.Attach(bs)
105 End Function
106*/
107 Const Function Length() As DWord
108 Length = SysStringLen(bs)
109 End Function
110
111 Const Function Operator [](i As SIZE_T) As OLECHAR
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
130 Return New String(bs As PCWSTR, Length As Long)
131 End Function
132
133 Override Function GetHashCode() As Long
134 Return _System_GetHashFromWordArray(bs, Length)
135 End Function
136
137Private
138 bs As BSTR
139
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
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
154End Class
155
156End Namespace 'COM
157End Namespace 'ActiveBasic
Note: See TracBrowser for help on using the repository browser.