Example #1
0
        /**
         * @param buffer
         * @param fromIndex index should be -1 for "at end" or the index just after the search start pos (so equivalent to buffer.lenght())
         * @return
         */
        public static int getNumberOfEmptyLinesAtEnd(StringBuilder buffer, int fromIndex)
        {
            if (fromIndex < 0)
            {
                fromIndex = buffer.Length;
            }

            //don't count the last blank line, since it will probably have data on it
            int  count        = 0;
            bool firstCRFound = false;
            int  i            = fromIndex - 1;

            while (i >= 0)
            {
                char c = buffer[i];
                if (!AntlrUtilities.isASWhitespace(c))
                {
                    return(count);
                }
                if (c == '\n')
                {
                    if (!firstCRFound)
                    {
                        firstCRFound = true;
                    }
                    else
                    {
                        count++;
                    };
                }
                i--;
            }

            return(count);
        }
Example #2
0
        public static AttrGroup load(String data)
        {
            List <String> attrs         = new List <String>();
            bool          includeStates = true;
            int           sortMode      = MXMLPrettyPrinter.MXML_Sort_AscByCase;
            int           wrapMode      = MXMLPrettyPrinter.MXML_ATTR_WRAP_DEFAULT;
            String        name          = getValue(data, Tag_name);

            if (name == null)
            {
                return(null);
            }
            String num = getValue(data, Tag_sort);

            if (num != null)
            {
                try { sortMode = Int32.Parse(num); }
                catch {}
            }
            num = getValue(data, Tag_wrap);
            if (num != null)
            {
                try { wrapMode = Int32.Parse(num); }
                catch {}
            }
            int wrapData = Wrap_Data_Use_Default;

            num = getValue(data, Tag_data);
            if (num != null)
            {
                try { wrapData = Int32.Parse(num); }
                catch {}
            }
            String attrString = getValue(data, Tag_attrs);

            if (attrString != null)
            {
                String[] atts = attrString.Split(new string[] { GroupingSplitter }, StringSplitOptions.RemoveEmptyEntries);
                foreach (String attr in atts)
                {
                    String attr2 = AntlrUtilities.asTrim(attr);
                    if (attr2.Length > 0)
                    {
                        attrs.Add(attr2);
                    }
                }
            }
            String includeStatesData = getValue(data, Tag_includeStates);

            if (includeStatesData != null)
            {
                includeStates = Boolean.Parse(includeStatesData);
            }
            AttrGroup group = new AttrGroup(name, attrs, sortMode, wrapMode, includeStates);

            group.setData(wrapData);
            return(group);
        }
Example #3
0
        public static bool isOnlyWhitespaceOnLastLine(StringBuilder buffer)
        {
            int i = buffer.Length - 1;

            while (i >= 0)
            {
                char c = buffer[i];
                if (!AntlrUtilities.isASWhitespace(c))
                {
                    return(false);
                }
                if (c == '\n')
                {
                    return(true);
                }
                i--;
            }

            return(true);
        }
Example #4
0
        public static bool validateNonWhitespaceIdentical(String s1, String s2)
        {
            String newBuffer1 = "";
            String newBuffer2 = "";

            for (int i = 0; i < s1.Length; i++)
            {
                char c = s1[i];
                if (!AntlrUtilities.isASWhitespace(c))
                {
                    newBuffer1 += c;
                }
            }
            for (int i = 0; i < s2.Length; i++)
            {
                char c = s2[i];
                if (!AntlrUtilities.isASWhitespace(c))
                {
                    newBuffer2 += c;
                }
            }
            return(newBuffer1 == newBuffer2);
        }
Example #5
0
        /**
         * This is a weaker validation that just checks to make sure that the number of occurrences of each character
         * is identical.
         * @param buffer
         * @param originalSource
         * @return
         */
        public static bool validateNonWhitespaceCharCounts(String buffer, String originalSource)
        {
            //some reasonable way of validating.  Just count non-whitespace and make sure that we have at least as many
            //chars as before.  Could improve to keep counts of each char so that ordering doesn't matter.
            Dictionary <char, Int32> originalCharMap = new Dictionary <char, Int32>();
            Dictionary <char, Int32> newCharMap      = new Dictionary <char, Int32>();

            int originalCharCount = 0;

            for (int i = 0; i < originalSource.Length; i++)
            {
                char c = originalSource[i];
                if (!AntlrUtilities.isASWhitespace(c))
                {
                    originalCharCount++;
                    try
                    {
                        int count = originalCharMap[c];
                        originalCharMap[c] = count + 1;
                    }
                    catch (KeyNotFoundException)
                    {
                        originalCharMap.Add(c, 1);
                    }
                }
            }

            int newCharCount = 0;

            for (int i = 0; i < buffer.Length; i++)
            {
                char c = buffer[i];
                if (!AntlrUtilities.isASWhitespace(buffer[i]))
                {
                    newCharCount++;
                    try
                    {
                        int count = newCharMap[c];
                        newCharMap[c] = count + 1;
                    }
                    catch (KeyNotFoundException)
                    {
                        newCharMap.Add(c, 1);
                    }
                }
            }

            if (newCharMap.Count != originalCharMap.Count)
            {
                return(false);
            }

            foreach (char charAsInt in originalCharMap.Keys)
            {
                Int32 origCount = originalCharMap[charAsInt];
                Int32 newCount  = newCharMap[charAsInt];
                if (origCount != newCount)
                {
                    return(false);
                }
            }

            if (newCharCount != originalCharCount)
            {
                return(false);
            }

            return(true);
        }