示例#1
0
        public static new Collator Create(string localeId, Fallback fallback)
        {
            RuleBasedCollator instance = new RuleBasedCollator();
            ErrorCode         status;

            instance.collatorHandle = NativeMethods.ucol_open(localeId, out status);
            if (status == ErrorCode.USING_FALLBACK_WARNING && fallback == Fallback.NoFallback)
            {
                throw new ArgumentException("Could only create Collator by falling back to '" +
                                            instance.Id +
                                            "'. You can use the fallback option to create this.");
            }
            if (status == ErrorCode.INTERNAL_PROGRAM_ERROR && fallback == Fallback.FallbackAllowed)
            {
                instance = new RuleBasedCollator(string.Empty);                 // fallback to UCA
            }
            else
            {
                try
                {
                    ExceptionFromErrorCode.ThrowIfError(status);
                }
                catch (Exception e)
                {
                    throw new ArgumentException(
                              "Unable to create a collator using the given localeId.\nThis is likely because the ICU data file was created without collation rules for this locale. You can provide the rules yourself or replace the data dll.",
                              e);
                }
            }
            return(instance);
        }
示例#2
0
        ///<summary>
        ///Creates a new object that is a copy of the current instance.
        ///</summary>
        ///
        ///<returns>
        ///A new object that is a copy of this instance.
        ///</returns>
        public override object Clone()
        {
            RuleBasedCollator copy = new RuleBasedCollator();
            ErrorCode         status;
            int buffersize = 512;

            copy._collatorHandle = NativeMethods.ucol_safeClone(
                _collatorHandle,
                IntPtr.Zero,
                ref buffersize,
                out status);
            try
            {
                ExceptionFromErrorCode.ThrowIfError(status);
                return(copy);
            }
            catch
            {
                if (copy._collatorHandle != default(SafeRuleBasedCollatorHandle))
                {
                    copy._collatorHandle.Dispose();
                }
                copy._collatorHandle = default(SafeRuleBasedCollatorHandle);
                throw;
            }
        }
示例#3
0
 /// <summary>
 /// Creates a collator with the given locale and whether to use locale
 /// fallback when creating collator.
 /// </summary>
 /// <param name="localeId">The locale</param>
 /// <param name="fallback">Whether to use locale fallback or not.</param>
 public static Collator Create(string localeId, Fallback fallback)
 {
     if (localeId == null)
     {
         throw new ArgumentNullException();
     }
     return(RuleBasedCollator.Create(localeId, fallback));
 }
示例#4
0
        /// <summary>
        /// Opens a Collator for comparing strings using the given locale id.
        /// </summary>
        /// <param name="localeId">Locale to use</param>
        /// <param name="fallback">Whether to allow locale fallback or not.</param>
        public new static Collator Create(string localeId, Fallback fallback)
        {
            // Culture identifiers in .NET are created using '-', while ICU
            // expects '_'.  We want to make sure that the localeId is the
            // format that ICU expects.
            var locale = new Locale(localeId);

            var       instance = new RuleBasedCollator();
            ErrorCode status;

            instance._collatorHandle = NativeMethods.ucol_open(locale.Id, out status);
            try
            {
                if (status == ErrorCode.USING_FALLBACK_WARNING && fallback == Fallback.NoFallback)
                {
                    throw new ArgumentException(
                              $"Could only create Collator '{localeId}' by falling back to " +
                              $"'{instance.ActualId}'. You can use the fallback option to create this.");
                }
                if (status == ErrorCode.USING_DEFAULT_WARNING && fallback == Fallback.NoFallback &&
                    !instance.ValidId.Equals(locale.Id) &&
                    locale.Id.Length > 0 && !locale.Id.Equals("root"))
                {
                    throw new ArgumentException(
                              $"Could only create Collator '{localeId}' by falling back to the default " +
                              $"'{instance.ActualId}'. You can use the fallback option to create this.");
                }
                if (status == ErrorCode.INTERNAL_PROGRAM_ERROR && fallback == Fallback.FallbackAllowed)
                {
                    instance = new RuleBasedCollator(string.Empty);                     // fallback to UCA
                }
                else
                {
                    try
                    {
                        ExceptionFromErrorCode.ThrowIfError(status);
                    }
                    catch (Exception e)
                    {
                        throw new ArgumentException(
                                  $"Unable to create a collator using the given localeId '{localeId}'.\n" +
                                  "This is likely because the ICU data file was created without collation " +
                                  "rules for this locale. You can provide the rules yourself or replace " +
                                  "the data dll.", e);
                    }
                }
                return(instance);
            }
            catch
            {
                if (instance._collatorHandle != default(SafeRuleBasedCollatorHandle))
                {
                    instance._collatorHandle.Dispose();
                }
                instance._collatorHandle = default(SafeRuleBasedCollatorHandle);
                throw;
            }
        }
示例#5
0
        ///<summary>
        ///Creates a new object that is a copy of the current instance.
        ///</summary>
        ///
        ///<returns>
        ///A new object that is a copy of this instance.
        ///</returns>
        public override object Clone()
        {
            RuleBasedCollator copy = new RuleBasedCollator();
            ErrorCode         status;
            int buffersize = 512;

            copy._collatorHandle = NativeMethods.ucol_safeClone(_collatorHandle,
                                                                IntPtr.Zero,
                                                                ref buffersize,
                                                                out status);
            ExceptionFromErrorCode.ThrowIfError(status);
            return(copy);
        }