comparison dwtx/dwtxhelper/mangoicu/UStringPrep.d @ 89:040da1cb0d76

Add a local copy of the mango ICU binding to work out the utf8 usability. Will hopefully go back into mango.
author Frank Benoit <benoit@tionex.de>
date Sun, 22 Jun 2008 22:57:31 +0200
parents
children 11e8159caf7a
comparison
equal deleted inserted replaced
88:cd18fa3b71f1 89:040da1cb0d76
1 /*******************************************************************************
2
3 @file UStringPrep.d
4
5 Copyright (c) 2004 Kris Bell
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for damages
9 of any kind arising from the use of this software.
10
11 Permission is hereby granted to anyone to use this software for any
12 purpose, including commercial applications, and to alter it and/or
13 redistribute it freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must
16 not claim that you wrote the original software. If you use this
17 software in a product, an acknowledgment within documentation of
18 said product would be appreciated but is not required.
19
20 2. Altered source versions must be plainly marked as such, and must
21 not be misrepresented as being the original software.
22
23 3. This notice may not be removed or altered from any distribution
24 of the source.
25
26 4. Derivative works are permitted, but they must carry this notice
27 in full and credit the original source.
28
29
30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
32
33 @version Initial version, November 2004
34 @author Kris
35
36 Note that this package and documentation is built around the ICU
37 project (http://oss.software.ibm.com/icu/). Below is the license
38 statement as specified by that software:
39
40
41 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42
43
44 ICU License - ICU 1.8.1 and later
45
46 COPYRIGHT AND PERMISSION NOTICE
47
48 Copyright (c) 1995-2003 International Business Machines Corporation and
49 others.
50
51 All rights reserved.
52
53 Permission is hereby granted, free of charge, to any person obtaining a
54 copy of this software and associated documentation files (the
55 "Software"), to deal in the Software without restriction, including
56 without limitation the rights to use, copy, modify, merge, publish,
57 distribute, and/or sell copies of the Software, and to permit persons
58 to whom the Software is furnished to do so, provided that the above
59 copyright notice(s) and this permission notice appear in all copies of
60 the Software and that both the above copyright notice(s) and this
61 permission notice appear in supporting documentation.
62
63 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
64 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
65 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
66 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
68 INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
69 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
70 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
71 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
72
73 Except as contained in this notice, the name of a copyright holder
74 shall not be used in advertising or otherwise to promote the sale, use
75 or other dealings in this Software without prior written authorization
76 of the copyright holder.
77
78 ----------------------------------------------------------------------
79
80 All trademarks and registered trademarks mentioned herein are the
81 property of their respective owners.
82
83 *******************************************************************************/
84
85 module dwtx.dwthelper.mangoicu.UStringPrep;
86
87 private import dwtx.dwthelper.mangoicu.ICU,
88 dwtx.dwthelper.mangoicu.UString;
89
90 /*******************************************************************************
91
92 StringPrep API implements the StingPrep framework as described
93 by RFC 3454.
94
95 StringPrep prepares Unicode strings for use in network protocols.
96 Profiles of StingPrep are set of rules and data according to with
97 the Unicode Strings are prepared. Each profiles contains tables
98 which describe how a code point should be treated. The tables are
99 broadly classied into
100
101 - Unassinged Table: Contains code points that are unassigned
102 in the Unicode Version supported by StringPrep. Currently
103 RFC 3454 supports Unicode 3.2.
104
105 - Prohibited Table: Contains code points that are prohibted
106 from the output of the StringPrep processing function.
107
108 - Mapping Table: Contains code ponts that are deleted from the
109 output or case mapped.
110
111 The procedure for preparing Unicode strings:
112
113 1. Map: For each character in the input, check if it has a mapping
114 and, if so, replace it with its mapping.
115
116 2. Normalize: Possibly normalize the result of step 1 using Unicode
117 normalization.
118
119 3. Prohibit: Check for any characters that are not allowed in the
120 output. If any are found, return an error.
121
122 4. Check bidi: Possibly check for right-to-left characters, and if
123 any are found, make sure that the whole string satisfies the
124 requirements for bidirectional strings. If the string does not
125 satisfy the requirements for bidirectional strings, return an
126 error.
127
128 See <A HREF="http://oss.software.ibm.com/icu/apiref/usprep_8h.html">
129 this page</A> for full details.
130
131 *******************************************************************************/
132
133 class UStringPrep : ICU
134 {
135 private Handle handle;
136
137 enum Options
138 {
139 Strict,
140 Lenient
141 }
142
143
144 /***********************************************************************
145
146 Creates a StringPrep profile from the data file.
147
148 path string containing the full path pointing
149 to the directory where the profile reside
150 followed by the package name e.g.
151 "/usr/resource/my_app/profiles/mydata" on
152 a Unix system. if NULL, ICU default data
153 files will be used.
154
155 fileName name of the profile file to be opened
156
157 ***********************************************************************/
158
159 this (char[] path, char[] filename)
160 {
161 Error e;
162
163 handle = usprep_open (toString(path), toString(filename), e);
164 testError (e, "failed to open string-prep");
165 }
166
167 /***********************************************************************
168
169 Close this profile
170
171 ***********************************************************************/
172
173 ~this ()
174 {
175 usprep_close (handle);
176 }
177
178 /***********************************************************************
179
180 Prepare the input buffer
181
182 This operation maps, normalizes(NFKC), checks for prohited
183 and BiDi characters in the order defined by RFC 3454 depending
184 on the options specified in the profile
185
186 ***********************************************************************/
187
188 void prepare (UText src, UString dst, Options o = Options.Strict)
189 {
190 uint fmt (wchar* p, uint len, inout Error e)
191 {
192 return usprep_prepare (handle, src.get.ptr, src.len, p, len, o, null, e);
193 }
194
195 dst.format (&fmt, "failed to prepare text");
196 }
197
198
199 /***********************************************************************
200
201 Bind the ICU functions from a shared library. This is
202 complicated by the issues regarding D and DLLs on the
203 Windows platform
204
205 ***********************************************************************/
206
207 private static void* library;
208
209 /***********************************************************************
210
211 ***********************************************************************/
212
213 private static extern (C)
214 {
215 Handle function (char*, char*, inout Error) usprep_open;
216 void function (Handle) usprep_close;
217 uint function (Handle, wchar*, uint, wchar*, uint, uint, void*, inout Error) usprep_prepare;
218 }
219
220 /***********************************************************************
221
222 ***********************************************************************/
223
224 static FunctionLoader.Bind[] targets =
225 [
226 {cast(void**) &usprep_open, "usprep_open"},
227 {cast(void**) &usprep_close, "usprep_close"},
228 {cast(void**) &usprep_prepare, "usprep_prepare"},
229 ];
230
231 /***********************************************************************
232
233 ***********************************************************************/
234
235 static this ()
236 {
237 library = FunctionLoader.bind (icuuc, targets);
238 }
239
240 /***********************************************************************
241
242 ***********************************************************************/
243
244 static ~this ()
245 {
246 FunctionLoader.unbind (library);
247 }
248 }
249