comparison dwtx/dwtxhelper/mangoicu/UCollator.d @ 91:11e8159caf7a

make the mango icu fork work.
author Frank Benoit <benoit@tionex.de>
date Mon, 07 Jul 2008 15:53:07 +0200
parents 040da1cb0d76
children f05207c07a98
comparison
equal deleted inserted replaced
90:7ffeace6c47f 91:11e8159caf7a
1 /******************************************************************************* 1 /*******************************************************************************
2 2
3 @file UCollator.d 3 @file UCollator.d
4 4
5 Copyright (c) 2004 Kris Bell 5 Copyright (c) 2004 Kris Bell
6 6
7 This software is provided 'as-is', without any express or implied 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 8 warranty. In no event will the authors be held liable for damages
9 of any kind arising from the use of this software. 9 of any kind arising from the use of this software.
10 10
11 Permission is hereby granted to anyone to use this software for any 11 Permission is hereby granted to anyone to use this software for any
12 purpose, including commercial applications, and to alter it and/or 12 purpose, including commercial applications, and to alter it and/or
13 redistribute it freely, subject to the following restrictions: 13 redistribute it freely, subject to the following restrictions:
14 14
15 1. The origin of this software must not be misrepresented; you must 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 16 not claim that you wrote the original software. If you use this
17 software in a product, an acknowledgment within documentation of 17 software in a product, an acknowledgment within documentation of
18 said product would be appreciated but is not required. 18 said product would be appreciated but is not required.
19 19
20 2. Altered source versions must be plainly marked as such, and must 20 2. Altered source versions must be plainly marked as such, and must
21 not be misrepresented as being the original software. 21 not be misrepresented as being the original software.
22 22
23 3. This notice may not be removed or altered from any distribution 23 3. This notice may not be removed or altered from any distribution
24 of the source. 24 of the source.
25 25
28 28
29 29
30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31 31
32 32
33 @version Initial version, November 2004 33 @version Initial version, November 2004
34 @author Kris 34 @author Kris
35 35
36 Note that this package and documentation is built around the ICU 36 Note that this package and documentation is built around the ICU
37 project (http://oss.software.ibm.com/icu/). Below is the license 37 project (http://oss.software.ibm.com/icu/). Below is the license
38 statement as specified by that software: 38 statement as specified by that software:
39 39
40 40
41 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 41 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42 42
43 43
44 ICU License - ICU 1.8.1 and later 44 ICU License - ICU 1.8.1 and later
45 45
46 COPYRIGHT AND PERMISSION NOTICE 46 COPYRIGHT AND PERMISSION NOTICE
47 47
48 Copyright (c) 1995-2003 International Business Machines Corporation and 48 Copyright (c) 1995-2003 International Business Machines Corporation and
49 others. 49 others.
50 50
51 All rights reserved. 51 All rights reserved.
52 52
53 Permission is hereby granted, free of charge, to any person obtaining a 53 Permission is hereby granted, free of charge, to any person obtaining a
75 or other dealings in this Software without prior written authorization 75 or other dealings in this Software without prior written authorization
76 of the copyright holder. 76 of the copyright holder.
77 77
78 ---------------------------------------------------------------------- 78 ----------------------------------------------------------------------
79 79
80 All trademarks and registered trademarks mentioned herein are the 80 All trademarks and registered trademarks mentioned herein are the
81 property of their respective owners. 81 property of their respective owners.
82 82
83 *******************************************************************************/ 83 *******************************************************************************/
84 84
85 module dwtx.dwthelper.mangoicu.UCollator; 85 module dwtx.dwtxhelper.mangoicu.UCollator;
86 86
87 private import dwtx.dwthelper.mangoicu.ICU, 87 private import dwtx.dwtxhelper.mangoicu.ICU,
88 dwtx.dwthelper.mangoicu.USet, 88 dwtx.dwtxhelper.mangoicu.USet,
89 dwtx.dwthelper.mangoicu.ULocale, 89 dwtx.dwtxhelper.mangoicu.ULocale,
90 dwtx.dwthelper.mangoicu.UString; 90 dwtx.dwtxhelper.mangoicu.UString;
91 91
92 /******************************************************************************* 92 /*******************************************************************************
93 93
94 The API for Collator performs locale-sensitive string comparison. 94 The API for Collator performs locale-sensitive string comparison.
95 You use this service to build searching and sorting routines for 95 You use this service to build searching and sorting routines for
96 natural language text. Important: The ICU collation service has been 96 natural language text. Important: The ICU collation service has been
97 reimplemented in order to achieve better performance and UCA compliance. 97 reimplemented in order to achieve better performance and UCA compliance.
98 For details, see the collation design document. 98 For details, see the collation design document.
99 99
100 For more information about the collation service see the users guide. 100 For more information about the collation service see the users guide.
101 101
102 Collation service provides correct sorting orders for most locales 102 Collation service provides correct sorting orders for most locales
103 supported in ICU. If specific data for a locale is not available, 103 supported in ICU. If specific data for a locale is not available,
104 the orders eventually falls back to the UCA sort order. 104 the orders eventually falls back to the UCA sort order.
105 105
106 Sort ordering may be customized by providing your own set of rules. 106 Sort ordering may be customized by providing your own set of rules.
107 For more on this subject see the Collation customization section of 107 For more on this subject see the Collation customization section of
108 the users guide. 108 the users guide.
109 109
110 See <A HREF="http://oss.software.ibm.com/icu/apiref/ucol_8h.html"> 110 See <A HREF="http://oss.software.ibm.com/icu/apiref/ucol_8h.html">
111 this page</A> for full details. 111 this page</A> for full details.
112 112
113 *******************************************************************************/ 113 *******************************************************************************/
114 114
115 class UCollator : ICU 115 class UCollator : ICU
116 { 116 {
117 package Handle handle; 117 package Handle handle;
118 118
119 typedef void* UParseError; 119 enum Attribute
120
121 enum Attribute
122 { 120 {
123 FrenchCollation, 121 FrenchCollation,
124 AlternateHandling, 122 AlternateHandling,
125 CaseFirst, 123 CaseFirst,
126 CaseLevel, 124 CaseLevel,
127 NormalizationMode, 125 NormalizationMode,
128 DecompositionMode = NormalizationMode, 126 DecompositionMode = NormalizationMode,
129 strength, 127 strength,
130 HiraganaQuaternaryMode, 128 HiraganaQuaternaryMode,
131 NumericCollation, 129 NumericCollation,
132 AttributeCount 130 AttributeCount
133 } 131 }
134 132
135 enum AttributeValue 133 enum AttributeValue
136 { 134 {
137 Default = -1, 135 Default = -1,
138 Primary = 0, 136 Primary = 0,
139 Secondary = 1, 137 Secondary = 1,
140 Tertiary = 2, 138 Tertiary = 2,
141 DefaultStrength = Tertiary, 139 DefaultStrength = Tertiary,
142 CeStrengthLimit, 140 CeStrengthLimit,
143 Quaternary = 3, 141 Quaternary = 3,
144 Identical = 15, 142 Identical = 15,
145 strengthLimit, 143 strengthLimit,
146 Off = 16, 144 Off = 16,
147 On = 17, 145 On = 17,
148 Shifted = 20, 146 Shifted = 20,
149 NonIgnorable = 21, 147 NonIgnorable = 21,
150 LowerFirst = 24, 148 LowerFirst = 24,
151 UpperFirst = 25, 149 UpperFirst = 25,
152 AttributeValueCount 150 AttributeValueCount
153 } 151 }
154 152
155 enum RuleOption 153 enum RuleOption
156 { 154 {
157 TailoringOnly, 155 TailoringOnly,
158 FullRules 156 FullRules
159 } 157 }
160 158
161 enum BoundMode 159 enum BoundMode
162 { 160 {
163 BoundLower = 0, 161 BoundLower = 0,
164 BoundUpper = 1, 162 BoundUpper = 1,
165 BoundUpperLong = 2, 163 BoundUpperLong = 2,
166 BoundValueCount 164 BoundValueCount
167 } 165 }
168 166
169 typedef AttributeValue Strength; 167 typedef AttributeValue Strength;
170 168
171 /*********************************************************************** 169 /***********************************************************************
172 170
173 Open a UCollator for comparing strings. The locale specified 171 Open a UCollator for comparing strings. The locale specified
174 determines the required collation rules. Special values for 172 determines the required collation rules. Special values for
175 locales can be passed in - if ULocale.Default is passed for 173 locales can be passed in - if ULocale.Default is passed for
176 the locale, the default locale collation rules will be used. 174 the locale, the default locale collation rules will be used.
177 If ULocale.Root is passed, UCA rules will be used 175 If ULocale.Root is passed, UCA rules will be used
178 176
179 ***********************************************************************/ 177 ***********************************************************************/
180 178
181 this (ULocale locale) 179 this (ULocale locale)
182 { 180 {
183 Error e; 181 UErrorCode e;
184 182
185 handle = ucol_open (toString(locale.name), e); 183 handle = ucol_open (toString(locale.name), e);
186 testError (e, "failed to open collator"); 184 testError (e, "failed to open collator");
187 } 185 }
188 186
189 /*********************************************************************** 187 /***********************************************************************
190 188
191 Produce a UCollator instance according to the rules supplied. 189 Produce a UCollator instance according to the rules supplied.
192 190
193 The rules are used to change the default ordering, defined in 191 The rules are used to change the default ordering, defined in
194 the UCA in a process called tailoring. For the syntax of the 192 the UCA in a process called tailoring. For the syntax of the
195 rules please see users guide 193 rules please see users guide
196 194
197 ***********************************************************************/ 195 ***********************************************************************/
198 196
199 this (UText rules, AttributeValue mode, Strength strength) 197 this (UStringView rules, AttributeValue mode, Strength strength)
200 { 198 {
201 Error e; 199 UErrorCode e;
202 200
203 handle = ucol_openRules (rules.get.ptr, rules.len, mode, strength, null, e); 201 handle = ucol_openRules (rules.get.ptr, rules.len, mode, strength, null, e);
204 testError (e, "failed to open rules-based collator"); 202 testError (e, "failed to open rules-based collator");
205 } 203 }
206 204
207 /*********************************************************************** 205 /***********************************************************************
208 206
209 Open a collator defined by a short form string. The 207 Open a collator defined by a short form string. The
210 structure and the syntax of the string is defined in 208 structure and the syntax of the string is defined in
211 the "Naming collators" section of the users guide: 209 the "Naming collators" section of the users guide:
212 http://oss.software.ibm.com/icu/userguide/Collate_Concepts.html#Naming_Collators 210 http://oss.software.ibm.com/icu/userguide/Collate_Concepts.html#Naming_Collators
213 Attributes are overriden by the subsequent attributes. 211 Attributes are overriden by the subsequent attributes.
214 So, for "S2_S3", final strength will be 3. 3066bis 212 So, for "S2_S3", final strength will be 3. 3066bis
215 locale overrides individual locale parts. 213 locale overrides individual locale parts.
216 214
217 The call to this constructor is equivalent to a plain 215 The call to this constructor is equivalent to a plain
218 constructor, followed by a series of calls to setAttribute 216 constructor, followed by a series of calls to setAttribute
219 and setVariableTop 217 and setVariableTop
220 218
221 ***********************************************************************/ 219 ***********************************************************************/
222 220
223 this (char[] shortName, bool forceDefaults) 221 this (char[] shortName, bool forceDefaults)
224 { 222 {
225 Error e; 223 UErrorCode e;
226 224
227 handle = ucol_openFromShortString (toString(shortName), forceDefaults, null, e); 225 handle = ucol_openFromShortString (toString(shortName), forceDefaults, null, e);
228 testError (e, "failed to open short-name collator"); 226 testError (e, "failed to open short-name collator");
229 } 227 }
230 228
238 { 236 {
239 this.handle = handle; 237 this.handle = handle;
240 } 238 }
241 239
242 /*********************************************************************** 240 /***********************************************************************
243 241
244 Close a UCollator 242 Close a UCollator
245 243
246 ***********************************************************************/ 244 ***********************************************************************/
247 245
248 ~this () 246 ~this ()
249 { 247 {
250 ucol_close (handle); 248 ucol_close (handle);
251 } 249 }
252 250
253 /*********************************************************************** 251 /***********************************************************************
254 252
255 Get a set containing the contractions defined by the 253 Get a set containing the contractions defined by the
256 collator. 254 collator.
257 255
258 The set includes both the UCA contractions and the 256 The set includes both the UCA contractions and the
259 contractions defined by the collator. This set will 257 contractions defined by the collator. This set will
260 contain only strings. If a tailoring explicitly 258 contain only strings. If a tailoring explicitly
261 suppresses contractions from the UCA (like Russian), 259 suppresses contractions from the UCA (like Russian),
262 removed contractions will not be in the resulting set. 260 removed contractions will not be in the resulting set.
263 261
264 ***********************************************************************/ 262 ***********************************************************************/
265 263
266 void getContractions (USet set) 264 void getContractions (USet set)
267 { 265 {
268 Error e; 266 UErrorCode e;
269 267
270 ucol_getContractions (handle, set.handle, e); 268 ucol_getContractions (handle, set.handle, e);
271 testError (e, "failed to get collator contractions"); 269 testError (e, "failed to get collator contractions");
272 } 270 }
273 271
274 /*********************************************************************** 272 /***********************************************************************
275 273
276 Compare two strings. Return value is -, 0, + 274 Compare two strings. Return value is -, 0, +
277 275
278 ***********************************************************************/ 276 ***********************************************************************/
279 277
280 int strcoll (UText source, UText target) 278 int strcoll (UStringView source, UStringView target)
281 { 279 {
282 return ucol_strcoll (handle, source.get.ptr, source.len, target.get.ptr, target.len); 280 return ucol_strcoll (handle, source.get.ptr, source.len, target.get.ptr, target.len);
283 } 281 }
284 282
285 /*********************************************************************** 283 /***********************************************************************
286 284
287 Determine if one string is greater than another. This 285 Determine if one string is greater than another. This
288 function is equivalent to strcoll() > 1 286 function is equivalent to strcoll() > 1
289 287
290 ***********************************************************************/ 288 ***********************************************************************/
291 289
292 bool greater (UText source, UText target) 290 bool greater (UStringView source, UStringView target)
293 { 291 {
294 return ucol_greater (handle, source.get.ptr, source.len, target.get.ptr, target.len) != 0; 292 return ucol_greater (handle, source.get.ptr, source.len, target.get.ptr, target.len) != 0;
295 } 293 }
296 294
297 /*********************************************************************** 295 /***********************************************************************
298 296
299 Determine if one string is greater than or equal to 297 Determine if one string is greater than or equal to
300 another. This function is equivalent to strcoll() >= 0 298 another. This function is equivalent to strcoll() >= 0
301 299
302 ***********************************************************************/ 300 ***********************************************************************/
303 301
304 bool greaterOrEqual (UText source, UText target) 302 bool greaterOrEqual (UStringView source, UStringView target)
305 { 303 {
306 return ucol_greaterOrEqual (handle, source.get.ptr, source.len, target.get.ptr, target.len) != 0; 304 return ucol_greaterOrEqual (handle, source.get.ptr, source.len, target.get.ptr, target.len) != 0;
307 } 305 }
308 306
309 /*********************************************************************** 307 /***********************************************************************
310 308
311 This function is equivalent to strcoll() == 0 309 This function is equivalent to strcoll() == 0
312 310
313 ***********************************************************************/ 311 ***********************************************************************/
314 312
315 bool equal (UText source, UText target) 313 bool equal (UStringView source, UStringView target)
316 { 314 {
317 return ucol_equal (handle, source.get.ptr, source.len, target.get.ptr, target.len) != 0; 315 return ucol_equal (handle, source.get.ptr, source.len, target.get.ptr, target.len) != 0;
318 } 316 }
319 317
320 /*********************************************************************** 318 /***********************************************************************
321 319
322 Get the collation strength used in a UCollator. The 320 Get the collation strength used in a UCollator. The
323 strength influences how strings are compared. 321 strength influences how strings are compared.
324 322
325 ***********************************************************************/ 323 ***********************************************************************/
326 324
327 Strength getStrength () 325 Strength getStrength ()
328 { 326 {
329 return ucol_getStrength (handle); 327 return ucol_getStrength (handle);
330 } 328 }
331 329
332 /*********************************************************************** 330 /***********************************************************************
333 331
334 Set the collation strength used in this UCollator. The 332 Set the collation strength used in this UCollator. The
335 strength influences how strings are compared. one of 333 strength influences how strings are compared. one of
336 Primary, Secondary, Tertiary, Quaternary, Dentical, or 334 Primary, Secondary, Tertiary, Quaternary, Dentical, or
337 Default 335 Default
338 336
339 ***********************************************************************/ 337 ***********************************************************************/
340 338
341 void setStrength (Strength s) 339 void setStrength (Strength s)
342 { 340 {
343 ucol_setStrength (handle, s); 341 ucol_setStrength (handle, s);
344 } 342 }
345 343
346 /*********************************************************************** 344 /***********************************************************************
347 345
348 Get the display name for a UCollator. The display name is 346 Get the display name for a UCollator. The display name is
349 suitable for presentation to a user 347 suitable for presentation to a user
350 348
351 ***********************************************************************/ 349 ***********************************************************************/
352 350
353 void getDisplayName (ULocale obj, ULocale display, UString dst) 351 void getDisplayName (ULocale obj, ULocale display, UString dst)
354 { 352 {
355 uint fmt (wchar* p, uint len, inout Error e) 353 uint fmt (wchar* p, uint len, inout UErrorCode e)
356 { 354 {
357 return ucol_getDisplayName (toString(obj.name), toString(display.name), dst.get.ptr, dst.len, e); 355 return ucol_getDisplayName (toString(obj.name), toString(display.name), dst.get.ptr, dst.len, e);
358 } 356 }
359 357
360 dst.format (&fmt, "failed to get collator display name"); 358 dst.format (&fmt, "failed to get collator display name");
361 } 359 }
362 360
363 /*********************************************************************** 361 /***********************************************************************
364 362
365 Returns current rules. Options define whether full rules 363 Returns current rules. Options define whether full rules
366 are returned or just the tailoring. 364 are returned or just the tailoring.
367 365
368 ***********************************************************************/ 366 ***********************************************************************/
369 367
370 void getRules (UString dst, RuleOption o = RuleOption.FullRules) 368 void getRules (UString dst, RuleOption o = RuleOption.FullRules)
371 { 369 {
372 uint fmt (wchar* p, uint len, inout Error e) 370 uint fmt (wchar* p, uint len, inout UErrorCode e)
373 { 371 {
374 uint needed = ucol_getRulesEx (handle, o, dst.get.ptr, dst.len); 372 uint needed = ucol_getRulesEx (handle, o, dst.get.ptr, dst.len);
375 if (needed > len) 373 if (needed > len)
376 e = e.BufferOverflow; 374 e = e.BufferOverflow;
377 return needed; 375 return needed;
378 } 376 }
379 377
380 dst.format (&fmt, "failed to get collator rules"); 378 dst.format (&fmt, "failed to get collator rules");
381 } 379 }
382 380
383 /*********************************************************************** 381 /***********************************************************************
384 382
385 Get the short definition string for a collator. 383 Get the short definition string for a collator.
386 384
387 This API harvests the collator's locale and the attribute 385 This API harvests the collator's locale and the attribute
388 set and produces a string that can be used for opening a 386 set and produces a string that can be used for opening a
389 collator with the same properties using the char[] style 387 collator with the same properties using the char[] style
390 constructor. This string will be normalized. 388 constructor. This string will be normalized.
391 389
392 The structure and the syntax of the string is defined in the 390 The structure and the syntax of the string is defined in the
393 "Naming collators" section of the users guide: 391 "Naming collators" section of the users guide:
394 http://oss.software.ibm.com/icu/userguide/Collate_Concepts.html#Naming_Collators 392 http://oss.software.ibm.com/icu/userguide/Collate_Concepts.html#Naming_Collators
395 393
396 ***********************************************************************/ 394 ***********************************************************************/
397 395
398 char[] getShortDefinitionString (ULocale locale = ULocale.Default) 396 char[] getShortDefinitionString (ULocale locale = ULocale.Default)
399 { 397 {
400 Error e; 398 UErrorCode e;
401 char[64] dst; 399 char[64] dst;
402 400
403 uint len = ucol_getShortDefinitionString (handle, toString(locale.name), dst.ptr, dst.length, e); 401 uint len = ucol_getShortDefinitionString (handle, toString(locale.name), dst.ptr, dst.length, e);
404 testError (e, "failed to get collator short name"); 402 testError (e, "failed to get collator short name");
405 return dst[0..len].dup; 403 return dst[0..len].dup;
406 } 404 }
407 405
408 /*********************************************************************** 406 /***********************************************************************
409 407
410 Verifies and normalizes short definition string. Normalized 408 Verifies and normalizes short definition string. Normalized
411 short definition string has all the option sorted by the 409 short definition string has all the option sorted by the
412 argument name, so that equivalent definition strings are the 410 argument name, so that equivalent definition strings are the
413 same 411 same
414 412
415 ***********************************************************************/ 413 ***********************************************************************/
416 414
417 char[] normalizeShortDefinitionString (char[] source) 415 char[] normalizeShortDefinitionString (char[] source)
418 { 416 {
419 Error e; 417 UErrorCode e;
420 char[64] dst; 418 char[64] dst;
421 419
422 uint len = ucol_normalizeShortDefinitionString (toString(source), dst.ptr, dst.length, null, e); 420 uint len = ucol_normalizeShortDefinitionString (toString(source), dst.ptr, dst.length, null, e);
423 testError (e, "failed to normalize collator short name"); 421 testError (e, "failed to normalize collator short name");
424 return dst[0..len].dup; 422 return dst[0..len].dup;
425 } 423 }
426 424
427 /*********************************************************************** 425 /***********************************************************************
428 426
429 Get a sort key for a string from a UCollator. Sort keys 427 Get a sort key for a string from a UCollator. Sort keys
430 may be compared using strcmp. 428 may be compared using strcmp.
431 429
432 ***********************************************************************/ 430 ***********************************************************************/
433 431
434 ubyte[] getSortKey (UText t, ubyte[] result) 432 ubyte[] getSortKey (UStringView t, ubyte[] result)
435 { 433 {
436 uint len = ucol_getSortKey (handle, t.get.ptr, t.len, result.ptr, result.length); 434 uint len = ucol_getSortKey (handle, t.get.ptr, t.len, result.ptr, result.length);
437 if (len < result.length) 435 if (len < result.length)
438 return result [0..len]; 436 return result [0..len];
439 return null; 437 return null;
440 } 438 }
441 439
442 /*********************************************************************** 440 /***********************************************************************
443 441
444 Merge two sort keys. The levels are merged with their 442 Merge two sort keys. The levels are merged with their
445 corresponding counterparts (primaries with primaries, 443 corresponding counterparts (primaries with primaries,
446 secondaries with secondaries etc.). Between the values 444 secondaries with secondaries etc.). Between the values
447 from the same level a separator is inserted. example 445 from the same level a separator is inserted. example
448 (uncompressed): 191B1D 01 050505 01 910505 00 and 446 (uncompressed): 191B1D 01 050505 01 910505 00 and
449 1F2123 01 050505 01 910505 00 will be merged as 447 1F2123 01 050505 01 910505 00 will be merged as
450 191B1D 02 1F212301 050505 02 050505 01 910505 02 910505 00 448 191B1D 02 1F212301 050505 02 050505 01 910505 02 910505 00
451 This allows for concatenating of first and last names for 449 This allows for concatenating of first and last names for
452 sorting, among other things. If the destination buffer is 450 sorting, among other things. If the destination buffer is
453 not big enough, the results are undefined. If any of source 451 not big enough, the results are undefined. If any of source
454 lengths are zero or any of source pointers are null/undefined, 452 lengths are zero or any of source pointers are null/undefined,
455 result is of size zero. 453 result is of size zero.
456 454
457 ***********************************************************************/ 455 ***********************************************************************/
458 456
459 ubyte[] mergeSortkeys (ubyte[] left, ubyte[] right, ubyte[] result) 457 ubyte[] mergeSortkeys (ubyte[] left, ubyte[] right, ubyte[] result)
460 { 458 {
461 uint len = ucol_mergeSortkeys (left.ptr, left.length, right.ptr, right.length, result.ptr, result.length); 459 uint len = ucol_mergeSortkeys (left.ptr, left.length, right.ptr, right.length, result.ptr, result.length);
462 if (len < result.length) 460 if (len < result.length)
463 return result [0..len]; 461 return result [0..len];
464 return null; 462 return null;
465 } 463 }
466 464
467 /*********************************************************************** 465 /***********************************************************************
468 466
469 Produce a bound for a given sortkey and a number of levels. 467 Produce a bound for a given sortkey and a number of levels.
470 468
471 Return value is always the number of bytes needed, regardless 469 Return value is always the number of bytes needed, regardless
472 of whether the result buffer was big enough or even valid. 470 of whether the result buffer was big enough or even valid.
473 471
474 Resulting bounds can be used to produce a range of strings 472 Resulting bounds can be used to produce a range of strings
475 that are between upper and lower bounds. For example, if 473 that are between upper and lower bounds. For example, if
476 bounds are produced for a sortkey of string "smith", strings 474 bounds are produced for a sortkey of string "smith", strings
477 between upper and lower bounds with one level would include 475 between upper and lower bounds with one level would include
478 "Smith", "SMITH", "sMiTh". 476 "Smith", "SMITH", "sMiTh".
479 477
480 There are two upper bounds that can be produced. If BoundUpper 478 There are two upper bounds that can be produced. If BoundUpper
481 is produced, strings matched would be as above. However, if 479 is produced, strings matched would be as above. However, if
482 bound produced using BoundUpperLong is used, the above example 480 bound produced using BoundUpperLong is used, the above example
483 will also match "Smithsonian" and similar. 481 will also match "Smithsonian" and similar.
484 482
485 ***********************************************************************/ 483 ***********************************************************************/
486 484
487 ubyte[] getBound (BoundMode mode, ubyte[] source, ubyte[] result, uint levels = 1) 485 ubyte[] getBound (BoundMode mode, ubyte[] source, ubyte[] result, uint levels = 1)
488 { 486 {
489 Error e; 487 UErrorCode e;
490 488
491 uint len = ucol_getBound (source.ptr, source.length, mode, levels, result.ptr, result.length, e); 489 uint len = ucol_getBound (source.ptr, source.length, mode, levels, result.ptr, result.length, e);
492 testError (e, "failed to get sortkey bound"); 490 testError (e, "failed to get sortkey bound");
493 if (len < result.length) 491 if (len < result.length)
494 return result [0..len]; 492 return result [0..len];
495 return null; 493 return null;
496 } 494 }
497 495
498 /*********************************************************************** 496 /***********************************************************************
499 497
500 Gets the version information for a Collator. 498 Gets the version information for a Collator.
501 499
502 Version is currently an opaque 32-bit number which depends, 500 Version is currently an opaque 32-bit number which depends,
503 among other things, on major versions of the collator 501 among other things, on major versions of the collator
504 tailoring and UCA 502 tailoring and UCA
505 503
506 ***********************************************************************/ 504 ***********************************************************************/
507 505
508 void getVersion (inout Version v) 506 void getVersion (inout Version v)
509 { 507 {
510 ucol_getVersion (handle, v); 508 ucol_getVersion (handle, v);
511 } 509 }
512 510
513 /*********************************************************************** 511 /***********************************************************************
514 512
515 Gets the UCA version information for this Collator 513 Gets the UCA version information for this Collator
516 514
517 ***********************************************************************/ 515 ***********************************************************************/
518 516
519 void getUCAVersion (inout Version v) 517 void getUCAVersion (inout Version v)
520 { 518 {
521 ucol_getUCAVersion (handle, v); 519 ucol_getUCAVersion (handle, v);
522 } 520 }
523 521
524 /*********************************************************************** 522 /***********************************************************************
525 523
526 Universal attribute setter 524 Universal attribute setter
527 525
528 ***********************************************************************/ 526 ***********************************************************************/
529 527
530 void setAttribute (Attribute attr, AttributeValue value) 528 void setAttribute (Attribute attr, AttributeValue value)
531 { 529 {
532 Error e; 530 UErrorCode e;
533 531
534 ucol_setAttribute (handle, attr, value, e); 532 ucol_setAttribute (handle, attr, value, e);
535 testError (e, "failed to set collator attribute"); 533 testError (e, "failed to set collator attribute");
536 } 534 }
537 535
538 /*********************************************************************** 536 /***********************************************************************
539 537
540 Universal attribute getter 538 Universal attribute getter
541 539
542 ***********************************************************************/ 540 ***********************************************************************/
543 541
544 AttributeValue getAttribute (Attribute attr) 542 AttributeValue getAttribute (Attribute attr)
545 { 543 {
546 Error e; 544 UErrorCode e;
547 545
548 AttributeValue v = ucol_getAttribute (handle, attr, e); 546 AttributeValue v = ucol_getAttribute (handle, attr, e);
549 testError (e, "failed to get collator attribute"); 547 testError (e, "failed to get collator attribute");
550 return v; 548 return v;
551 } 549 }
552 550
553 /*********************************************************************** 551 /***********************************************************************
554 552
555 Variable top is a two byte primary value which causes all 553 Variable top is a two byte primary value which causes all
556 the codepoints with primary values that are less or equal 554 the codepoints with primary values that are less or equal
557 than the variable top to be shifted when alternate handling 555 than the variable top to be shifted when alternate handling
558 is set to Shifted. 556 is set to Shifted.
559 557
560 ***********************************************************************/ 558 ***********************************************************************/
561 559
562 void setVariableTop (UText t) 560 void setVariableTop (UStringView t)
563 { 561 {
564 Error e; 562 UErrorCode e;
565 563
566 ucol_setVariableTop (handle, t.get.ptr, t.len, e); 564 ucol_setVariableTop (handle, t.get.ptr, t.len, e);
567 testError (e, "failed to set variable-top"); 565 testError (e, "failed to set variable-top");
568 } 566 }
569 567
570 /*********************************************************************** 568 /***********************************************************************
571 569
572 Sets the variable top to a collation element value 570 Sets the variable top to a collation element value
573 supplied.Variable top is set to the upper 16 bits. 571 supplied.Variable top is set to the upper 16 bits.
574 Lower 16 bits are ignored. 572 Lower 16 bits are ignored.
575 573
576 ***********************************************************************/ 574 ***********************************************************************/
577 575
578 void setVariableTop (uint x) 576 void setVariableTop (uint x)
579 { 577 {
580 Error e; 578 UErrorCode e;
581 579
582 ucol_restoreVariableTop (handle, x, e); 580 ucol_restoreVariableTop (handle, x, e);
583 testError (e, "failed to restore variable-top"); 581 testError (e, "failed to restore variable-top");
584 } 582 }
585 583
586 /*********************************************************************** 584 /***********************************************************************
587 585
588 Gets the variable top value of this Collator. Lower 16 bits 586 Gets the variable top value of this Collator. Lower 16 bits
589 are undefined and should be ignored. 587 are undefined and should be ignored.
590 588
591 ***********************************************************************/ 589 ***********************************************************************/
592 590
593 uint getVariableTop () 591 uint getVariableTop ()
594 { 592 {
595 Error e; 593 UErrorCode e;
596 594
597 uint x = ucol_getVariableTop (handle, e); 595 uint x = ucol_getVariableTop (handle, e);
598 testError (e, "failed to get variable-top"); 596 testError (e, "failed to get variable-top");
599 return x; 597 return x;
600 } 598 }
601 599
602 /*********************************************************************** 600 /***********************************************************************
603 601
604 Gets the locale name of the collator. If the collator is 602 Gets the locale name of the collator. If the collator is
605 instantiated from the rules, then this function will throw 603 instantiated from the rules, then this function will throw
606 an exception 604 an exception
607 605
608 ***********************************************************************/ 606 ***********************************************************************/
609 607
610 void getLocale (ULocale locale, ULocale.Type type) 608 void getLocale (ULocale locale, ULocale.Type type)
611 { 609 {
612 Error e; 610 UErrorCode e;
613 611
614 locale.name = toArray (ucol_getLocaleByType (handle, type, e)); 612 locale.name = toArray (ucol_getLocaleByType (handle, type, e));
615 if (isError(e) || locale.name is null) 613 if (isError(e) || locale.name is null)
616 exception ("failed to get collator locale"); 614 exception ("failed to get collator locale");
617 } 615 }
618 616
619 /*********************************************************************** 617 /***********************************************************************
620 618
621 Get the Unicode set that contains all the characters and 619 Get the Unicode set that contains all the characters and
622 sequences tailored in this collator. 620 sequences tailored in this collator.
623 621
624 ***********************************************************************/ 622 ***********************************************************************/
625 623
626 USet getTailoredSet () 624 USet getTailoredSet ()
627 { 625 {
628 Error e; 626 UErrorCode e;
629 627
630 Handle h = ucol_getTailoredSet (handle, e); 628 Handle h = ucol_getTailoredSet (handle, e);
631 testError (e, "failed to get tailored set"); 629 testError (e, "failed to get tailored set");
632 return new USet (h); 630 return new USet (h);
633 } 631 }
634 632
635 633
636 /*********************************************************************** 634 /***********************************************************************
637 635
638 Bind the ICU functions from a shared library. This is 636 Bind the ICU functions from a shared library. This is
639 complicated by the issues regarding D and DLLs on the 637 complicated by the issues regarding D and DLLs on the
640 Windows platform 638 Windows platform
641 639
642 ***********************************************************************/ 640 ***********************************************************************/
645 643
646 /*********************************************************************** 644 /***********************************************************************
647 645
648 ***********************************************************************/ 646 ***********************************************************************/
649 647
650 private static extern (C) 648 private static extern (C)
651 { 649 {
652 void function (Handle) ucol_close; 650 void function (Handle) ucol_close;
653 Handle function (char *loc, inout Error e) ucol_open; 651 Handle function (char *loc, inout UErrorCode e) ucol_open;
654 Handle function (wchar* rules, uint rulesLength, AttributeValue normalizationMode, Strength strength, UParseError *parseError, inout Error e) ucol_openRules; 652 Handle function (wchar* rules, uint rulesLength, AttributeValue normalizationMode, Strength strength, UParseError *parseError, inout UErrorCode e) ucol_openRules;
655 Handle function (char *definition, byte forceDefaults, UParseError *parseError, inout Error e) ucol_openFromShortString; 653 Handle function (char *definition, byte forceDefaults, UParseError *parseError, inout UErrorCode e) ucol_openFromShortString;
656 uint function (Handle, Handle conts, inout Error e) ucol_getContractions; 654 uint function (Handle, Handle conts, inout UErrorCode e) ucol_getContractions;
657 int function (Handle, wchar* source, uint sourceLength, wchar* target, uint targetLength) ucol_strcoll; 655 int function (Handle, wchar* source, uint sourceLength, wchar* target, uint targetLength) ucol_strcoll;
658 byte function (Handle, wchar* source, uint sourceLength, wchar* target, uint targetLength) ucol_greater; 656 byte function (Handle, wchar* source, uint sourceLength, wchar* target, uint targetLength) ucol_greater;
659 byte function (Handle, wchar* source, uint sourceLength, wchar* target, uint targetLength) ucol_greaterOrEqual; 657 byte function (Handle, wchar* source, uint sourceLength, wchar* target, uint targetLength) ucol_greaterOrEqual;
660 byte function (Handle, wchar* source, uint sourceLength, wchar* target, uint targetLength) ucol_equal; 658 byte function (Handle, wchar* source, uint sourceLength, wchar* target, uint targetLength) ucol_equal;
661 Strength function (Handle) ucol_getStrength; 659 Strength function (Handle) ucol_getStrength;
662 void function (Handle, Strength strength) ucol_setStrength; 660 void function (Handle, Strength strength) ucol_setStrength;
663 uint function (char *objLoc, char *dispLoc, wchar* result, uint resultLength, inout Error e) ucol_getDisplayName; 661 uint function (char *objLoc, char *dispLoc, wchar* result, uint resultLength, inout UErrorCode e) ucol_getDisplayName;
664 uint function (Handle, char *locale, char *buffer, uint capacity, inout Error e) ucol_getShortDefinitionString; 662 uint function (Handle, char *locale, char *buffer, uint capacity, inout UErrorCode e) ucol_getShortDefinitionString;
665 uint function (char *source, char *destination, uint capacity, UParseError *parseError, inout Error e) ucol_normalizeShortDefinitionString; 663 uint function (char *source, char *destination, uint capacity, UParseError *parseError, inout UErrorCode e) ucol_normalizeShortDefinitionString;
666 uint function (Handle, wchar* source, uint sourceLength, ubyte *result, uint resultLength) ucol_getSortKey; 664 uint function (Handle, wchar* source, uint sourceLength, ubyte *result, uint resultLength) ucol_getSortKey;
667 uint function (ubyte *source, uint sourceLength, BoundMode boundType, uint noOfLevels, ubyte *result, uint resultLength, inout Error e) ucol_getBound; 665 uint function (ubyte *source, uint sourceLength, BoundMode boundType, uint noOfLevels, ubyte *result, uint resultLength, inout UErrorCode e) ucol_getBound;
668 void function (Handle, Version info) ucol_getVersion; 666 void function (Handle, Version info) ucol_getVersion;
669 void function (Handle, Version info) ucol_getUCAVersion; 667 void function (Handle, Version info) ucol_getUCAVersion;
670 uint function (ubyte *src1, uint src1Length, ubyte *src2, uint src2Length, ubyte *dest, uint destCapacity) ucol_mergeSortkeys; 668 uint function (ubyte *src1, uint src1Length, ubyte *src2, uint src2Length, ubyte *dest, uint destCapacity) ucol_mergeSortkeys;
671 void function (Handle, Attribute attr, AttributeValue value, inout Error e) ucol_setAttribute; 669 void function (Handle, Attribute attr, AttributeValue value, inout UErrorCode e) ucol_setAttribute;
672 AttributeValue function (Handle, Attribute attr, inout Error e) ucol_getAttribute; 670 AttributeValue function (Handle, Attribute attr, inout UErrorCode e) ucol_getAttribute;
673 uint function (Handle, wchar* varTop, uint len, inout Error e) ucol_setVariableTop; 671 uint function (Handle, wchar* varTop, uint len, inout UErrorCode e) ucol_setVariableTop;
674 uint function (Handle, inout Error e) ucol_getVariableTop; 672 uint function (Handle, inout UErrorCode e) ucol_getVariableTop;
675 void function (Handle, uint varTop, inout Error e) ucol_restoreVariableTop; 673 void function (Handle, uint varTop, inout UErrorCode e) ucol_restoreVariableTop;
676 uint function (Handle, RuleOption delta, wchar* buffer, uint bufferLen) ucol_getRulesEx; 674 uint function (Handle, RuleOption delta, wchar* buffer, uint bufferLen) ucol_getRulesEx;
677 char* function (Handle, ULocale.Type type, inout Error e) ucol_getLocaleByType; 675 char* function (Handle, ULocale.Type type, inout UErrorCode e) ucol_getLocaleByType;
678 Handle function (Handle, inout Error e) ucol_getTailoredSet; 676 Handle function (Handle, inout UErrorCode e) ucol_getTailoredSet;
679 } 677 }
680 678
681 /*********************************************************************** 679 /***********************************************************************
682 680
683 ***********************************************************************/ 681 ***********************************************************************/
684 682
685 static FunctionLoader.Bind[] targets = 683 static FunctionLoader.Bind[] targets =
686 [ 684 [
687 {cast(void**) &ucol_open, "ucol_open"}, 685 {cast(void**) &ucol_open, "ucol_open"},
688 {cast(void**) &ucol_close, "ucol_close"}, 686 {cast(void**) &ucol_close, "ucol_close"},
689 {cast(void**) &ucol_openRules, "ucol_openRules"}, 687 {cast(void**) &ucol_openRules, "ucol_openRules"},
690 {cast(void**) &ucol_openFromShortString, "ucol_openFromShortString"}, 688 {cast(void**) &ucol_openFromShortString, "ucol_openFromShortString"},
691 {cast(void**) &ucol_getContractions, "ucol_getContractions"}, 689 {cast(void**) &ucol_getContractions, "ucol_getContractions"},
692 {cast(void**) &ucol_strcoll, "ucol_strcoll"}, 690 {cast(void**) &ucol_strcoll, "ucol_strcoll"},