Esempio n. 1
0
        public void PoolsReferences()
        {
            var pool = new StringPool();

            var s1 = "s";
            var s2 = "S".ToLower();

            Assert.NotSame(s1, s2);
            Assert.Equal(s1, s2);

            Assert.Same(s1, pool.Pool(s1));
            Assert.Same(s1, pool.Pool(s1));
            Assert.Same(s1, pool.Pool(s2));
            Assert.Same(s1, pool.Pool(s2));
        }
Esempio n. 2
0
        internal static XDocument PoolStrings(this XDocument doc, StringPool pool)
        {
            Stack <XNode>     stack          = new Stack <XNode>();
            List <XAttribute> attributesList = new List <XAttribute>();

            stack.Push(doc.Root);
            while (stack.Count != 0)
            {
                switch (stack.Pop())
                {
                case XElement nxt:
                    nxt.Name = pool.Pool(nxt.Name);
                    attributesList.AddRange(nxt.Attributes());
                    foreach (XAttribute attribute in attributesList)
                    {
                        nxt.SetAttributeValue(pool.Pool(attribute.Name), pool.Pool(attribute.Value));
                    }

                    attributesList.Clear();
                    break;

                case XText txt:
                    txt.Value = pool.Pool(txt.Value);
                    break;

                case XContainer cont:
                    foreach (XNode child in cont.Nodes())
                    {
                        stack.Push(child);
                    }

                    break;
                }
            }

            return(doc);
        }
Esempio n. 3
0
        protected SimpleDictionary.Section GenerateSecondHalf(IDictionarySection firstSection)
        {
            Dictionary <string, List <string> > entries = new Dictionary <string, List <string> >();

            StringPool pool = new StringPool();

            // Normalize all translations (and entries, for good measure).
            // Also, pool them so that the "Contains" method of the List<string> works.
            foreach (Entry entry in firstSection)
            {
                entry.Phrase = pool.Pool(entry.Phrase.Normalize());

                foreach (Translation t in entry.Translations)
                {
                    t.Value = pool.Pool(t.Value.Normalize());
                }
            }

            // Perform the actual reversion.
            foreach (Entry entry in firstSection)
            {
                foreach (Translation t in entry.Translations)
                {
                    List <string> reverse;
                    if (entries.TryGetValue(t.Value, out reverse))
                    {
                        if (!reverse.Contains(entry.Phrase))
                        {
                            reverse.Add(entry.Phrase);
                        }
                    }
                    else
                    {
                        reverse = new List <string>();
                        entries.Add(t.Value, reverse);
                        if (!reverse.Contains(entry.Phrase))
                        {
                            reverse.Add(entry.Phrase);
                        }
                    }
                }
            }

            // Convert it into a list.
            List <Entry> list = new List <Entry>();

            foreach (KeyValuePair <string, List <string> > kv in entries)
            {
                var   translations = new List <Translation>();
                Entry e            = new Entry(kv.Key, translations);

                foreach (string t in kv.Value)
                {
                    translations.Add(new Translation(t));
                }

                // Sort the list of translations in each entry.
                translations.Sort((a, b) => a.Value.CompareTo(b.Value));

                list.Add(e);
            }

            // Sort the list of entries.
            list.Sort((a, b) => (a.Phrase.CompareTo(b.Phrase)));

            SimpleDictionary.Section section = new SimpleDictionary.Section(list, true, null);
            foreach (var e in list)
            {
                e.Tag = new EntryTag(section, null);
            }
            return(section);
        }
Esempio n. 4
0
 private static XName Pool(this StringPool pool, XName name) => XName.Get(pool.Pool(name.LocalName), pool.Pool(name.NamespaceName));