-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
NativeMethods.cs
1024 lines (889 loc) · 37.4 KB
/
NativeMethods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2013-2022 SIL International
// This software is licensed under the MIT license (http://opensource.org/licenses/MIT)
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
// ReSharper disable once CheckNamespace
namespace Icu
{
[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "IdentifierTypo")]
internal static partial class NativeMethods
{
private static readonly object _lock = new object();
#if NET
private static readonly DllResolver _DllResolver =
new DllResolver(Assembly.GetExecutingAssembly());
#endif
internal static int MinIcuVersion { get; private set; } = Wrapper.MinSupportedIcuVersion;
internal static int MaxIcuVersion { get; private set; } = Wrapper.MaxSupportedIcuVersion;
internal static string PreferredDirectory { get; set; }
internal static bool Verbose { get; set; }
internal static void SetMinMaxIcuVersions(int minVersion = Wrapper.MinSupportedIcuVersion,
int maxVersion = Wrapper.MaxSupportedIcuVersion)
{
Trace.WriteLineIf(Verbose, $"Setting min/max ICU versions to {minVersion} and {maxVersion}");
if (minVersion < Wrapper.MinSupportedIcuVersion || minVersion > Wrapper.MaxSupportedIcuVersion)
{
throw new ArgumentOutOfRangeException(nameof(minVersion),
$"supported ICU versions are between {Wrapper.MinSupportedIcuVersion} and {Wrapper.MaxSupportedIcuVersion}");
}
if (maxVersion < Wrapper.MinSupportedIcuVersion || maxVersion > Wrapper.MaxSupportedIcuVersion)
{
throw new ArgumentOutOfRangeException(nameof(maxVersion),
$"supported ICU versions are between {Wrapper.MinSupportedIcuVersion} and {Wrapper.MaxSupportedIcuVersion}");
}
lock (_lock)
{
MinIcuVersion = Math.Min(minVersion, maxVersion);
MaxIcuVersion = Math.Max(minVersion, maxVersion);
}
if (!IsInitialized)
{
// Now that we set min/max versions, reset the existing info again
ResetIcuVersionInfo();
return;
}
var rescueDataDir = Wrapper.DataDirectory;
Cleanup();
Wrapper.DataDirectory = rescueDataDir;
Wrapper.Init();
}
private static MethodsContainer Methods;
static NativeMethods()
{
Methods = new MethodsContainer();
ResetIcuVersionInfo();
}
#region Dynamic method loading
#region Native methods for Unix
private const int RTLD_NOW = 2;
private const string LIBDL_NAME = "libdl.so";
[DllImport(LIBDL_NAME, SetLastError = true)]
private static extern IntPtr dlopen(string file, int mode);
[DllImport(LIBDL_NAME, SetLastError = true)]
private static extern int dlclose(IntPtr handle);
[DllImport(LIBDL_NAME, SetLastError = true)]
private static extern IntPtr dlsym(IntPtr handle, string name);
[DllImport(LIBDL_NAME, EntryPoint = "dlerror")]
private static extern IntPtr _dlerror();
private static string dlerror()
{
// Don't free the string returned from _dlerror()!
var ptr = _dlerror();
return Marshal.PtrToStringAnsi(ptr);
}
#endregion
#region Native methods for Windows
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr LoadLibraryEx(string dllToLoad, IntPtr hReservedNull, LoadLibraryFlags dwFlags);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[Flags]
[SuppressMessage("ReSharper", "UnusedMember.Local")]
private enum LoadLibraryFlags : uint
{
NONE = 0x00000000,
DONT_RESOLVE_DLL_REFERENCES = 0x00000001,
LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010,
LOAD_LIBRARY_AS_DATAFILE = 0x00000002,
LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040,
LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020,
LOAD_LIBRARY_SEARCH_APPLICATION_DIR = 0x00000200,
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x00001000,
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR = 0x00000100,
LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800,
LOAD_LIBRARY_SEARCH_USER_DIRS = 0x00000400,
LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
}
#endregion
private static int IcuVersion;
private static string _IcuPath;
private static IntPtr _IcuCommonLibHandle;
private static IntPtr _IcuI18NLibHandle;
private static bool IsWindows => Platform.OperatingSystem == OperatingSystemType.Windows;
private static IntPtr IcuCommonLibHandle
{
get
{
if (_IcuCommonLibHandle == IntPtr.Zero)
// ReSharper disable once StringLiteralTypo
_IcuCommonLibHandle = LoadIcuLibrary("icuuc");
return _IcuCommonLibHandle;
}
}
private static IntPtr IcuI18NLibHandle
{
get
{
if (_IcuI18NLibHandle == IntPtr.Zero)
_IcuI18NLibHandle = LoadIcuLibrary(IsWindows ? "icuin" : "icui18n");
return _IcuI18NLibHandle;
}
}
internal static string DirectoryOfThisAssembly
{
get
{
//NOTE: .GetTypeInfo() is not supported until .NET 4.5 onwards.
#if NET40
var currentAssembly = typeof(NativeMethods).Assembly;
#else
var currentAssembly = typeof(NativeMethods).GetTypeInfo().Assembly;
#endif
#if NET
var managedPath = currentAssembly.Location;
#else
var managedPath = currentAssembly.CodeBase ?? currentAssembly.Location;
#endif
var uri = new Uri(managedPath);
var directoryName = Path.GetDirectoryName(uri.LocalPath);
Trace.WriteLineIf(Verbose, $"icu.net: Directory of this assembly is {directoryName}");
return directoryName;
}
}
private static bool IsRunning64Bit => Platform.ProcessArchitecture == Platform.x64;
private static bool IsInitialized { get; set; }
private static void AddDirectoryToSearchPath(string directory)
{
// Only perform this for Linux because we are using LoadLibraryEx
// to ensure that a library's dependencies is loaded starting from
// where that library is located.
if (IsWindows)
return;
var ldLibPath = Environment.GetEnvironmentVariable("LD_LIBRARY_PATH");
Environment.SetEnvironmentVariable("LD_LIBRARY_PATH", $"{directory}:{ldLibPath}");
Trace.WriteLineIf(Verbose, $"icu.net: adding directory '{directory}' to LD_LIBRARY_PATH '{ldLibPath}'");
}
private static bool CheckDirectoryForIcuBinaries(string directory, string libraryName)
{
Trace.WriteLineIf(Verbose, $"icu.net: checking '{directory}' for ICU binaries");
if (!Directory.Exists(directory))
{
Trace.WriteLineIf(Verbose, $"icu.net: directory '{directory}' doesn't exist");
return false;
}
var filePattern = IsWindows ? libraryName + "*.dll" : "lib" + libraryName + ".so.*";
var files = Directory.EnumerateFiles(directory, filePattern).ToList();
Trace.WriteLineIf(Verbose, $"icu.net: {files.Count} files in '{directory}' match the pattern '{filePattern}'");
if (files.Count > 0)
{
// Do a reverse sort so that we use the highest version
files.Sort((x, y) => string.CompareOrdinal(y, x));
var filePath = files[0];
var version = IsWindows
? Path.GetFileNameWithoutExtension(filePath).Substring(5) // strip icuuc
: Path.GetFileName(filePath).Substring(12); // strip libicuuc.so.
Trace.WriteLineIf(Verbose, $"icu.net: Extracted version '{version}' from '{filePath}'");
if (int.TryParse(version, out var icuVersion))
{
Trace.TraceInformation("Setting IcuVersion to {0} (found in {1})",
icuVersion, directory);
IcuVersion = icuVersion;
_IcuPath = directory;
AddDirectoryToSearchPath(directory);
return true;
}
Trace.WriteLineIf(Verbose, $"icu.net: couldn't parse '{version}' as an int. Returning false.");
}
Trace.WriteLineIf(Verbose && files.Count <= 0, "icu.net: No files matching pattern. Returning false.");
return false;
}
private static bool LocateIcuLibrary(string libraryName)
{
Trace.WriteLineIf(Verbose, $"icu.net: Locating ICU library '{libraryName}'");
if (!string.IsNullOrEmpty(PreferredDirectory) &&
CheckDirectoryForIcuBinaries(PreferredDirectory, libraryName))
{
return true;
}
var arch = IsRunning64Bit ? "x64" : "x86";
// Look for ICU binaries in lib/{win,linux}-{x86,x64} subdirectory first
var platform = IsWindows ? "win" : "linux";
if (CheckDirectoryForIcuBinaries(
Path.Combine(DirectoryOfThisAssembly, "lib", $"{platform}-{arch}"),
libraryName))
return true;
// Next look in lib/x86 or lib/x64 subdirectory
if (CheckDirectoryForIcuBinaries(
Path.Combine(DirectoryOfThisAssembly, "lib", arch),
libraryName))
return true;
// next try just {win,linux}-x86/x64 subdirectory
if (CheckDirectoryForIcuBinaries(
Path.Combine(DirectoryOfThisAssembly, $"{platform}-{arch}"),
libraryName))
return true;
// next try just x86/x64 subdirectory
if (CheckDirectoryForIcuBinaries(
Path.Combine(DirectoryOfThisAssembly, arch),
libraryName))
return true;
// Might also be in runtimes/win7-x64/native
if (CheckDirectoryForIcuBinaries(
Path.Combine(DirectoryOfThisAssembly, "runtimes", $"win7-{arch}", "native"),
libraryName))
return true;
// otherwise check the current directory
// If we don't find it here we rely on it being in the PATH somewhere...
return CheckDirectoryForIcuBinaries(DirectoryOfThisAssembly, libraryName);
}
private static IntPtr LoadIcuLibrary(string libraryName)
{
Trace.WriteLineIf(!IsInitialized,
"WARNING: ICU is not initialized. Please call Icu.Wrapper.Init() at the start of your application.");
lock (_lock)
{
if (IcuVersion <= 0)
LocateIcuLibrary(libraryName);
var handle = GetIcuLibHandle(libraryName, IcuVersion > 0 ? IcuVersion : MaxIcuVersion);
if (handle == IntPtr.Zero)
{
throw new FileLoadException($"Can't load ICU library (version {IcuVersion})",
libraryName);
}
return handle;
}
}
private static IntPtr GetIcuLibHandle(string basename, int icuVersion)
{
while (true)
{
Trace.WriteLineIf(Verbose, $"icu.net: Get ICU Lib handle for {basename}, version {icuVersion}");
if (icuVersion < MinIcuVersion)
return IntPtr.Zero;
IntPtr handle;
string libPath;
int lastError;
if (IsWindows)
{
var libName = $"{basename}{icuVersion}.dll";
var isIcuPathSpecified = !string.IsNullOrEmpty(_IcuPath);
libPath = isIcuPathSpecified ? Path.Combine(_IcuPath, libName) : libName;
var loadLibraryFlags = LoadLibraryFlags.NONE;
if (isIcuPathSpecified) loadLibraryFlags |= LoadLibraryFlags.LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LoadLibraryFlags.LOAD_LIBRARY_SEARCH_DEFAULT_DIRS;
handle = LoadLibraryEx(libPath, IntPtr.Zero, loadLibraryFlags);
lastError = Marshal.GetLastWin32Error();
Trace.WriteLineIf(handle == IntPtr.Zero && lastError != 0, $"Unable to load [{libPath}]. Error: {new Win32Exception(lastError).Message}");
}
else
{
var libName = $"lib{basename}.so.{icuVersion}";
libPath = string.IsNullOrEmpty(_IcuPath) ? libName : Path.Combine(_IcuPath, libName);
handle = dlopen(libPath, RTLD_NOW);
lastError = Marshal.GetLastWin32Error();
Trace.WriteLineIf(handle == IntPtr.Zero && lastError != 0, $"Unable to load [{libPath}]. Error: {lastError} ({dlerror()})");
}
if (handle != IntPtr.Zero)
{
IcuVersion = icuVersion;
return handle;
}
Trace.TraceWarning("{0} of {1} failed with error {2}", IsWindows ? "LoadLibraryEx" : "dlopen", libPath, lastError);
icuVersion -= 1;
}
}
internal static void Cleanup()
{
Trace.WriteLineIf(Verbose, "icu.net: Cleanup");
lock (_lock)
{
try
{
u_cleanup();
}
catch
{
// ignore failures - can happen when running unit tests
}
if (IsWindows)
{
if (_IcuCommonLibHandle != IntPtr.Zero)
FreeLibrary(_IcuCommonLibHandle);
if (_IcuI18NLibHandle != IntPtr.Zero)
FreeLibrary(_IcuI18NLibHandle);
}
else
{
if (_IcuCommonLibHandle != IntPtr.Zero)
dlclose(_IcuCommonLibHandle);
if (_IcuI18NLibHandle != IntPtr.Zero)
dlclose(_IcuI18NLibHandle);
}
_IcuCommonLibHandle = IntPtr.Zero;
_IcuI18NLibHandle = IntPtr.Zero;
Methods = new MethodsContainer();
BiDiMethods = new BiDiMethodsContainer();
BreakIteratorMethods = new BreakIteratorMethodsContainer();
CodepageConversionMethods = new CodepageConversionMethodsContainer();
CollatorMethods = new CollatorMethodsContainer();
LocalesMethods = new LocalesMethodsContainer();
MessageFormatMethods = new MessageFormatMethodsContainer();
NormalizeMethods = new NormalizeMethodsContainer();
RegexMethods = new RegexMethodsContainer();
ResourceBundleMethods = new ResourceBundleMethodsContainer();
TransliteratorMethods = new TransliteratorMethodsContainer();
UnicodeSetMethods = new UnicodeSetMethodsContainer();
ResetIcuVersionInfo();
}
}
private static void ResetIcuVersionInfo()
{
Trace.WriteLineIf(Verbose, "icu.net: Resetting ICU version info");
IcuVersion = 0;
_IcuPath = null;
#if !NET40
NativeMethodsHelper.Reset();
var icuInfo = NativeMethodsHelper.GetIcuVersionInfoForNetCoreOrWindows();
if (icuInfo.Success)
{
_IcuPath = icuInfo.IcuPath.FullName;
IcuVersion = icuInfo.IcuVersion;
}
#endif
}
// This method is thread-safe and idempotent
private static T GetMethod<T>(IntPtr handle, string methodName, bool missingInMinimal = false) where T : class
{
var versionedMethodName = $"{methodName}_{IcuVersion}";
var methodPointer = IsWindows ?
GetProcAddress(handle, versionedMethodName) :
dlsym(handle, versionedMethodName);
// Some systems (eg. Tizen) don't use methods with IcuVersion suffix
if (methodPointer == IntPtr.Zero)
{
methodPointer = IsWindows ?
GetProcAddress(handle, methodName) :
dlsym(handle, methodName);
}
if (methodPointer != IntPtr.Zero)
{
// NOTE: Starting in .NET 4.5.1, Marshal.GetDelegateForFunctionPointer(IntPtr, Type) is obsolete.
#if NET40
return Marshal.GetDelegateForFunctionPointer(
methodPointer, typeof(T)) as T;
#else
return Marshal.GetDelegateForFunctionPointer<T>(methodPointer);
#endif
}
if (missingInMinimal)
{
throw new MissingMemberException(
"Do you have the full version of ICU installed? " +
$"The method '{methodName}' is not included in the minimal version of ICU.");
}
return default(T);
}
#endregion
internal static string GetAnsiString(Func<IntPtr, int, Tuple<ErrorCode, int>> lambda,
int initialLength = 255)
{
return GetString(lambda, false, initialLength);
}
internal static string GetUnicodeString(Func<IntPtr, int, Tuple<ErrorCode, int>> lambda,
int initialLength = 255)
{
return GetString(lambda, true, initialLength);
}
private static string GetString(Func<IntPtr, int, Tuple<ErrorCode, int>> lambda,
bool isUnicodeString = false, int initialLength = 255)
{
var length = initialLength;
var resPtr = Marshal.AllocCoTaskMem(length * 2);
try
{
var (err, outLength) = lambda(resPtr, length);
if (err != ErrorCode.BUFFER_OVERFLOW_ERROR && err != ErrorCode.STRING_NOT_TERMINATED_WARNING)
ExceptionFromErrorCode.ThrowIfError(err);
if (outLength >= length)
{
err = ErrorCode.NoErrors; // ignore possible U_BUFFER_OVERFLOW_ERROR or STRING_NOT_TERMINATED_WARNING
Marshal.FreeCoTaskMem(resPtr);
length = outLength + 1; // allow room for the terminating NUL (FWR-505)
resPtr = Marshal.AllocCoTaskMem(length * 2);
(err, outLength) = lambda(resPtr, length);
}
ExceptionFromErrorCode.ThrowIfError(err);
if (outLength < 0)
return null;
var result = isUnicodeString
? Marshal.PtrToStringUni(resPtr)
: Marshal.PtrToStringAnsi(resPtr);
// Strip any garbage left over at the end of the string.
if (err == ErrorCode.STRING_NOT_TERMINATED_WARNING && result != null)
return result.Substring(0, outLength);
return result;
}
finally
{
Marshal.FreeCoTaskMem(resPtr);
}
}
/// <summary>
/// This function does cleanup of the enumerator object
/// </summary>
/// <param name="en">Enumeration to be closed</param>
internal static void uenum_close(IntPtr en)
{
if (Methods.uenum_close == null)
Methods.uenum_close = GetMethod<MethodsContainer.uenum_closeDelegate>(IcuCommonLibHandle, "uenum_close");
Methods.uenum_close(en);
}
/// <summary>
/// This function returns the next element as a string, or <c>null</c> after all
/// elements haven been enumerated.
/// </summary>
/// <returns>next element as string, or <c>null</c> after all elements haven been
/// enumerated</returns>
internal static IntPtr uenum_unext(
SafeEnumeratorHandle en,
out int resultLength,
out ErrorCode status)
{
if (Methods.uenum_unext == null)
Methods.uenum_unext = GetMethod<MethodsContainer.uenum_unextDelegate>(IcuCommonLibHandle, "uenum_unext");
return Methods.uenum_unext(en, out resultLength, out status);
}
internal enum LocaleType
{
/// <summary>
/// This is locale the data actually comes from
/// </summary>
ActualLocale = 0,
/// <summary>
/// This is the most specific locale supported by ICU
/// </summary>
ValidLocale = 1,
}
internal enum CollationAttributeValue
{
Default = -1, //accepted by most attributes
Primary = 0, // primary collation strength
Secondary = 1, // secondary collation strength
Tertiary = 2, // tertiary collation strength
Default_Strength = Tertiary,
Quaternary = 3, //Quaternary collation strength
Identical = 15, //Identical collation strength
Off = 16, //Turn the feature off - works for FrenchCollation, CaseLevel, HiraganaQuaternaryMode, DecompositionMode
On = 17, //Turn the feature on - works for FrenchCollation, CaseLevel, HiraganaQuaternaryMode, DecompositionMode
Shifted = 20, // Valid for AlternateHandling. Alternate handling will be shifted
NonIgnorable = 21, // Valid for AlternateHandling. Alternate handling will be non-ignorable
LowerFirst = 24, // Valid for CaseFirst - lower case sorts before upper case
UpperFirst = 25 // Valid for CaseFirst - upper case sorts before lower case
}
internal enum CollationAttribute
{
FrenchCollation,
AlternateHandling,
CaseFirst,
CaseLevel,
NormalizationMode,
DecompositionMode = NormalizationMode,
Strength,
HiraganaQuaternaryMode,
NumericCollation,
AttributeCount
}
internal enum CollationResult
{
Equal = 0,
Greater = 1,
Less = -1
}
[SuppressMessage("ReSharper", "MemberHidesStaticFromOuterClass")]
private class MethodsContainer
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate void u_initDelegate(out ErrorCode errorCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate void u_cleanupDelegate();
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate IntPtr u_getDataDirectoryDelegate();
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate void u_setDataDirectoryDelegate(
[MarshalAs(UnmanagedType.LPStr)]string directory);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate int u_charNameDelegate(
int code,
Character.UCharNameChoice nameChoice,
IntPtr buffer,
int bufferLength,
out ErrorCode errorCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate int u_charDirectionDelegate(int characterCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate int u_digitDelegate(
int characterCode,
byte radix);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate int u_getIntPropertyValueDelegate(
int characterCode,
Character.UProperty choice);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate void u_getUnicodeVersionDelegate(out VersionInfo versionArray);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate void u_getVersionDelegate(out VersionInfo versionArray);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate sbyte u_charTypeDelegate(int characterCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate double u_getNumericValueDelegate(
int characterCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
// Required because ICU returns a one-byte boolean. Without this C# assumes 4, and picks up 3 more random bytes,
// which are usually zero, especially in debug builds...but one day we will be sorry.
[return: MarshalAs(UnmanagedType.I1)]
internal delegate bool u_ispunctDelegate(int characterCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
// Required because ICU returns a one-byte boolean. Without this C# assumes 4, and picks up 3 more random bytes,
// which are usually zero, especially in debug builds...but one day we will be sorry.
[return: MarshalAs(UnmanagedType.I1)]
internal delegate bool u_isMirroredDelegate(int characterCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
// Required because ICU returns a one-byte boolean. Without this C# assumes 4, and picks up 3 more random bytes,
// which are usually zero, especially in debug builds...but one day we will be sorry.
[return: MarshalAs(UnmanagedType.I1)]
internal delegate bool u_iscntrlDelegate(int characterCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
// Required because ICU returns a one-byte boolean. Without this C# assumes 4, and picks up 3 more random bytes,
// which are usually zero, especially in debug builds...but one day we will be sorry.
[return: MarshalAs(UnmanagedType.I1)]
internal delegate bool u_isspaceDelegate(int characterCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate int u_tolowerDelegate(int characterCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate int u_totitleDelegate(int characterCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate int u_toupperDelegate(int characterCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate void uenum_closeDelegate(IntPtr en);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate IntPtr uenum_unextDelegate(
SafeEnumeratorHandle en,
out int resultLength,
out ErrorCode status);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate int u_strToLowerDelegate(IntPtr dest, int destCapacity, string src,
int srcLength, [MarshalAs(UnmanagedType.LPStr)] string locale, out ErrorCode errorCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate int u_strToTitleDelegate(IntPtr dest, int destCapacity, string src,
int srcLength, IntPtr titleIter, [MarshalAs(UnmanagedType.LPStr)] string locale,
out ErrorCode errorCode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
internal delegate int u_strToUpperDelegate(IntPtr dest, int destCapacity, string src,
int srcLength, [MarshalAs(UnmanagedType.LPStr)] string locale, out ErrorCode errorCode);
internal u_initDelegate u_init;
internal u_cleanupDelegate u_cleanup;
internal u_getDataDirectoryDelegate u_getDataDirectory;
internal u_setDataDirectoryDelegate u_setDataDirectory;
internal u_charNameDelegate u_charName;
internal u_charDirectionDelegate u_charDirection;
internal u_digitDelegate u_digit;
internal u_getIntPropertyValueDelegate u_getIntPropertyValue;
internal u_getUnicodeVersionDelegate u_getUnicodeVersion;
internal u_getVersionDelegate u_getVersion;
internal u_charTypeDelegate u_charType;
internal u_getNumericValueDelegate u_getNumericValue;
internal u_ispunctDelegate u_ispunct;
internal u_isMirroredDelegate u_isMirrored;
internal u_iscntrlDelegate u_iscntrl;
internal u_isspaceDelegate u_isspace;
internal u_tolowerDelegate u_tolower;
internal u_totitleDelegate u_totitle;
internal u_toupperDelegate u_toupper;
internal uenum_closeDelegate uenum_close;
internal uenum_unextDelegate uenum_unext;
internal u_strToLowerDelegate u_strToLower;
internal u_strToTitleDelegate u_strToTitle;
internal u_strToUpperDelegate u_strToUpper;
}
/// <summary>get the name of an ICU code point</summary>
internal static void u_init(out ErrorCode errorCode)
{
IsInitialized = true;
if (Methods.u_init == null)
Methods.u_init = GetMethod<MethodsContainer.u_initDelegate>(IcuCommonLibHandle, "u_init");
Methods.u_init(out errorCode);
}
/// <summary>Clean up the ICU files that could be locked</summary>
// ReSharper disable once MemberCanBePrivate.Global
internal static void u_cleanup()
{
if (Methods.u_cleanup == null)
Methods.u_cleanup = GetMethod<MethodsContainer.u_cleanupDelegate>(IcuCommonLibHandle, "u_cleanup");
Methods.u_cleanup();
IsInitialized = false;
}
/// <summary>Return the ICU data directory</summary>
internal static IntPtr u_getDataDirectory()
{
if (Methods.u_getDataDirectory == null)
Methods.u_getDataDirectory = GetMethod<MethodsContainer.u_getDataDirectoryDelegate>(IcuCommonLibHandle, "u_getDataDirectory");
return Methods.u_getDataDirectory();
}
/// <summary>Set the ICU data directory</summary>
internal static void u_setDataDirectory(
[MarshalAs(UnmanagedType.LPStr)]string directory)
{
if (Methods.u_setDataDirectory == null)
Methods.u_setDataDirectory = GetMethod<MethodsContainer.u_setDataDirectoryDelegate>(IcuCommonLibHandle, "u_setDataDirectory");
Methods.u_setDataDirectory(directory);
}
/// <summary>get the name of an ICU code point</summary>
internal static int u_charName(
int code,
Character.UCharNameChoice nameChoice,
IntPtr buffer,
int bufferLength,
out ErrorCode errorCode)
{
if (Methods.u_charName == null)
Methods.u_charName = GetMethod<MethodsContainer.u_charNameDelegate>(IcuCommonLibHandle, "u_charName");
return Methods.u_charName(code, nameChoice, buffer, bufferLength, out errorCode);
}
/// <summary>Returns the bidirectional category value for the code point, which is used in the Unicode bidirectional algorithm</summary>
internal static int u_charDirection(int characterCode)
{
if (Methods.u_charDirection == null)
Methods.u_charDirection = GetMethod<MethodsContainer.u_charDirectionDelegate>(IcuCommonLibHandle, "u_charDirection");
return Methods.u_charDirection(characterCode);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// get the numeric value for the Unicode digit
/// </summary>
/// ------------------------------------------------------------------------------------
internal static int u_digit(
int characterCode,
byte radix)
{
if (Methods.u_digit == null)
Methods.u_digit = GetMethod<MethodsContainer.u_digitDelegate>(IcuCommonLibHandle, "u_digit");
return Methods.u_digit(characterCode, radix);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Gets the property value for an enumerated or integer Unicode property for a code point.
/// </summary>
/// <param name="codePoint">The codepoint to look up</param>
/// <param name="which">The property value to look up</param>
/// <returns>Numeric value that is directly the property value or, for enumerated
/// properties, corresponds to the numeric value of the enumerated constant of the
/// respective property value enumeration type (cast to enum type if necessary). Returns
/// 0 or 1 (for <c>false/true</c>) for binary Unicode properties. Returns a bit-mask for
/// mask properties. Returns 0 if <paramref name="which"/> is out of bounds or if the
/// Unicode version does not have data for the property at all, or not for this code point.
/// </returns>
/// <remarks>Consider adding a specific implementation for each property!</remarks>
/// ------------------------------------------------------------------------------------
internal static int u_getIntPropertyValue(
int codePoint,
Character.UProperty which)
{
if (Methods.u_getIntPropertyValue == null)
Methods.u_getIntPropertyValue = GetMethod<MethodsContainer.u_getIntPropertyValueDelegate>(IcuCommonLibHandle, "u_getIntPropertyValue");
return Methods.u_getIntPropertyValue(codePoint, which);
}
internal static void u_getUnicodeVersion(out VersionInfo versionArray)
{
if (Methods.u_getUnicodeVersion == null)
Methods.u_getUnicodeVersion = GetMethod<MethodsContainer.u_getUnicodeVersionDelegate>(IcuCommonLibHandle, "u_getUnicodeVersion");
Methods.u_getUnicodeVersion(out versionArray);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Gets the ICU release version.
/// </summary>
/// <param name="versionArray">Stores the version information for ICU.</param>
/// ------------------------------------------------------------------------------------
internal static void u_getVersion(out VersionInfo versionArray)
{
if (Methods.u_getVersion == null)
Methods.u_getVersion = GetMethod<MethodsContainer.u_getVersionDelegate>(IcuCommonLibHandle, "u_getVersion");
Methods.u_getVersion(out versionArray);
}
/// <summary>
/// Get the general character type.
/// </summary>
/// <param name="characterCode"></param>
/// <returns></returns>
internal static sbyte u_charType(int characterCode)
{
if (Methods.u_charType == null)
Methods.u_charType = GetMethod<MethodsContainer.u_charTypeDelegate>(IcuCommonLibHandle, "u_charType");
return Methods.u_charType(characterCode);
}
/// ------------------------------------------------------------------------------------
/// <summary>
///Get the numeric value for a Unicode code point as defined in the Unicode Character Database.
///A "double" return type is necessary because some numeric values are fractions, negative, or too large for int32_t.
///For characters without any numeric values in the Unicode Character Database,
///this function will return U_NO_NUMERIC_VALUE.
///
///Similar to java.lang.Character.getNumericValue(), but u_getNumericValue() also supports negative values,
///large values, and fractions, while Java's getNumericValue() returns values 10..35 for ASCII letters.
///</summary>
///<remarks>
/// See also:
/// U_NO_NUMERIC_VALUE
/// Stable:
/// ICU 2.2
/// http://oss.software.ibm.com/icu/apiref/uchar_8h.html#a477
/// </remarks>
///<param name="characterCode">Code point to get the numeric value for</param>
///<returns>Numeric value of c, or U_NO_NUMERIC_VALUE if none is defined.</returns>
/// ------------------------------------------------------------------------------------
internal static double u_getNumericValue(
int characterCode)
{
if (Methods.u_getNumericValue == null)
Methods.u_getNumericValue = GetMethod<MethodsContainer.u_getNumericValueDelegate>(IcuCommonLibHandle, "u_getNumericValue");
return Methods.u_getNumericValue(characterCode);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Determines whether the specified code point is a punctuation character.
/// </summary>
/// <param name="characterCode">the code point to be tested</param>
/// ------------------------------------------------------------------------------------
internal static bool u_ispunct(
int characterCode)
{
if (Methods.u_ispunct == null)
Methods.u_ispunct = GetMethod<MethodsContainer.u_ispunctDelegate>(IcuCommonLibHandle, "u_ispunct");
return Methods.u_ispunct(characterCode);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Determines whether the code point has the Bidi_Mirrored property.
///
/// This property is set for characters that are commonly used in Right-To-Left contexts
/// and need to be displayed with a "mirrored" glyph.
///
/// Same as java.lang.Character.isMirrored(). Same as UCHAR_BIDI_MIRRORED
/// </summary>
/// <remarks>
/// See also:
/// UCHAR_BIDI_MIRRORED
///
/// Stable:
/// ICU 2.0
/// </remarks>
/// <param name="characterCode">the code point to be tested</param>
/// <returns><c>true</c> if the character has the Bidi_Mirrored property</returns>
/// ------------------------------------------------------------------------------------
internal static bool u_isMirrored(
int characterCode)
{
if (Methods.u_isMirrored == null)
Methods.u_isMirrored = GetMethod<MethodsContainer.u_isMirroredDelegate>(IcuCommonLibHandle, "u_isMirrored");
return Methods.u_isMirrored(characterCode);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Determines whether the specified code point is a control character. A control
/// character is one of the following:
/// <list>
/// <item>ISO 8-bit control character (U+0000..U+001f and U+007f..U+009f)</item>
/// <item>U_CONTROL_CHAR (Cc)</item>
/// <item>U_FORMAT_CHAR (Cf)</item>
/// <item>U_LINE_SEPARATOR (Zl)</item>
/// <item>U_PARAGRAPH_SEPARATOR (Zp)</item>
/// </list>
/// </summary>
/// <param name="characterCode">the code point to be tested</param>
/// ------------------------------------------------------------------------------------
internal static bool u_iscntrl(
int characterCode)
{
if (Methods.u_iscntrl == null)
Methods.u_iscntrl = GetMethod<MethodsContainer.u_iscntrlDelegate>(IcuCommonLibHandle, "u_iscntrl");
return Methods.u_iscntrl(characterCode);
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Determines whether the specified character is a space character.
/// </summary>
/// <remarks>
/// See also:
/// <list>
/// <item>u_isJavaSpaceChar</item>
/// <item>u_isWhitespace</item>
/// <item>u_isUWhiteSpace</item>
/// </list>
///
/// Stable:
/// ICU 2.0
/// </remarks>
/// <param name="characterCode">the code point to be tested</param>
/// ------------------------------------------------------------------------------------
internal static bool u_isspace(
int characterCode)
{
if (Methods.u_isspace == null)
Methods.u_isspace = GetMethod<MethodsContainer.u_isspaceDelegate>(IcuCommonLibHandle, "u_isspace");
return Methods.u_isspace(characterCode);
}
/// <summary>Map character to its lowercase equivalent according to UnicodeData.txt</summary>
internal static int u_tolower(int characterCode)
{
if (Methods.u_tolower == null)
Methods.u_tolower = GetMethod<MethodsContainer.u_tolowerDelegate>(IcuCommonLibHandle, "u_tolower");
return Methods.u_tolower(characterCode);
}
/// <summary>Map character to its title-case equivalent according to UnicodeData.txt</summary>
internal static int u_totitle(int characterCode)
{
if (Methods.u_totitle == null)
Methods.u_totitle = GetMethod<MethodsContainer.u_totitleDelegate>(IcuCommonLibHandle, "u_totitle");
return Methods.u_totitle(characterCode);
}
/// <summary>Map character to its uppercase equivalent according to UnicodeData.txt</summary>
internal static int u_toupper(int characterCode)
{
if (Methods.u_toupper == null)
Methods.u_toupper = GetMethod<MethodsContainer.u_toupperDelegate>(IcuCommonLibHandle, "u_toupper");
return Methods.u_toupper(characterCode);
}
/// <summary>Return the lower case equivalent of the string.</summary>
internal static int u_strToLower(IntPtr dest, int destCapacity, string src,
int srcLength, [MarshalAs(UnmanagedType.LPStr)] string locale, out ErrorCode errorCode)
{
if (Methods.u_strToLower == null)
Methods.u_strToLower = GetMethod<MethodsContainer.u_strToLowerDelegate>(IcuCommonLibHandle, "u_strToLower");
return Methods.u_strToLower(dest, destCapacity, src, srcLength, locale, out errorCode);
}
internal static int u_strToTitle(IntPtr dest, int destCapacity, string src,
int srcLength, [MarshalAs(UnmanagedType.LPStr)] string locale,
out ErrorCode errorCode)
{
return u_strToTitle(dest, destCapacity, src, srcLength, IntPtr.Zero, locale,
out errorCode);
}