示例#1
0
        // ICU4N: Avoid static constructor by initializing inline
        private static CollationTailoring LoadCollationTailoring(out Exception exception)
        {
            // Corresponds to C++ load() function.
            CollationTailoring t  = null;
            Exception          e2 = null;

            try
            {
                ByteBuffer         bytes = ICUBinary.GetRequiredData("coll/ucadata.icu");
                CollationTailoring t2    = new CollationTailoring(null);
                CollationDataReader.Read(null, bytes, t2);
                // Keep t=null until after the root data has been read completely.
                // Otherwise we would set a non-null root object if the data reader throws an exception.
                t = t2;
            }
            catch (IOException e)
            {
                e2 = new MissingManifestResourceException(
                    "IOException while reading CLDR root data, " +
                    "type: CollationRoot, bundle: " + ICUData.IcuBundle + "/coll/ucadata.icu", e);
            }
            catch (Exception e)
            {
                e2 = e;
            }
            exception = e2;
            return(t);
        }
示例#2
0
        static CollationRoot()
        {  // Corresponds to C++ load() function.
            CollationTailoring t  = null;
            Exception          e2 = null;

            try
            {
                // ICU4N specific - passing in assembly name so we resolve to this assembly rather than ICU4N.dll
                ByteBuffer         bytes = ICUBinary.GetRequiredData(typeof(CollationRoot).GetTypeInfo().Assembly, null, "coll/ucadata.icu");
                CollationTailoring t2    = new CollationTailoring(null);
                CollationDataReader.Read(null, bytes, t2);
                // Keep t=null until after the root data has been read completely.
                // Otherwise we would set a non-null root object if the data reader throws an exception.
                t = t2;
            }
            catch (IOException e)
            {
                e2 = new MissingManifestResourceException(
                    "IOException while reading CLDR root data, " +
                    "type: CollationRoot, bundle: " + ICUData.ICU_BUNDLE + "/coll/ucadata.icu", e);
            }
            catch (Exception e)
            {
                e2 = e;
            }
            rootSingleton = t;
            exception     = e2;
        }
示例#3
0
        /// <summary>
        /// Gets a <see cref="StringPrep"/> instance for the specified profile.
        /// </summary>
        /// <param name="profile">The profile passed to find the <see cref="StringPrep"/> instance.</param>
        /// <stable>ICU 4.2</stable>
        public static StringPrep GetInstance(StringPrepProfile profile)
        {
            if (profile < 0 || profile > MAX_PROFILE)
            {
                throw new ArgumentException("Bad profile type");
            }

            StringPrep instance = null;

            // A StringPrep instance is immutable.  We use a single instance
            // per type and store it in the internal cache.
            lock (CACHE)
            {
#if FEATURE_TYPEDWEAKREFERENCE
                WeakReference <StringPrep> @ref = CACHE[(int)profile];
#else
                WeakReference @ref = CACHE[(int)profile];
#endif
                if (@ref != null)
                {
#if FEATURE_TYPEDWEAKREFERENCE
                    //instance = @ref.Get();
                    @ref.TryGetTarget(out instance);
#else
                    instance = (StringPrep)@ref.Target;
#endif
                }

                if (instance == null)
                {
                    ByteBuffer bytes = ICUBinary.GetRequiredData(PROFILE_NAMES[(int)profile] + ".spp");
                    if (bytes != null)
                    {
                        try
                        {
                            instance = new StringPrep(bytes);
                        }
                        catch (IOException e)
                        {
                            throw new ICUUncheckedIOException(e);
                        }
                    }
                    if (instance != null)
                    {
#if FEATURE_TYPEDWEAKREFERENCE
                        CACHE[(int)profile] = new WeakReference <StringPrep>(instance);
#else
                        CACHE[(int)profile] = new WeakReference(instance);
#endif
                    }
                }
            }
            return(instance);
        }
示例#4
0
        public static DictionaryMatcher LoadDictionaryFor(string dictType)
        {
            ICUResourceBundle rb           = (ICUResourceBundle)UResourceBundle.GetBundleInstance(ICUData.IcuBreakIteratorBaseName); // com/ibm/icu/impl/data/icudt60b/brkitr
            string            dictFileName = rb.GetStringWithFallback("dictionaries/" + dictType);

            // ICU4N TODO: Possibly rename the above and use this syntax instead...?
            //var rm = new ResourceManager(ICUData.ICU_BRKITR_BASE_NAME, typeof(DictionaryData).GetTypeInfo().Assembly);
            //string dictFileName = rm.GetString("dictionaries_" + dictType);

            dictFileName = ICUData.IcuBreakIteratorName + '/' + dictFileName;
            ByteBuffer bytes = ICUBinary.GetRequiredData(dictFileName);

            ICUBinary.ReadHeader(bytes, DATA_FORMAT_ID, null);
            int[] indexes = new int[IX_COUNT];
            // TODO: read indexes[IX_STRING_TRIE_OFFSET] first, then read a variable-length indexes[]
            for (int i = 0; i < IX_COUNT; i++)
            {
                indexes[i] = bytes.GetInt32();
            }
            int offset = indexes[IX_STRING_TRIE_OFFSET];

            Assert.Assrt(offset >= (4 * IX_COUNT));
            if (offset > (4 * IX_COUNT))
            {
                int diff = offset - (4 * IX_COUNT);
                ICUBinary.SkipBytes(bytes, diff);
            }
            int trieType        = indexes[IX_TRIE_TYPE] & TRIE_TYPE_MASK;
            int totalSize       = indexes[IX_TOTAL_SIZE] - offset;
            DictionaryMatcher m = null;

            if (trieType == TRIE_TYPE_BYTES)
            {
                int    transform = indexes[IX_TRANSFORM];
                byte[] data      = new byte[totalSize];
                bytes.Get(data);
                m = new BytesDictionaryMatcher(data, transform);
            }
            else if (trieType == TRIE_TYPE_UCHARS)
            {
                Assert.Assrt(totalSize % 2 == 0);
                string data = ICUBinary.GetString(bytes, totalSize / 2, totalSize & 1);
                m = new CharsDictionaryMatcher(data);
            }
            else
            {
                m = null;
            }
            return(m);
        }