Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BauzoidNET.parser
{
    public class Preprocessor
    {
        public static string stripComments(string str)
        {
            return stripComments(str, "//", "/*", "*/");
        }


        public static string stripComments(string str, string lineCommentBegin, string commentBegin, string commentEnd)
        {
            String result = "";
            int pos = 0;

            while (pos < str.Length)
            {
                if ((lineCommentBegin.Length != 0) && isNextString(str, pos, lineCommentBegin))
                {
                    // skip until end of line
                    while ((pos < str.Length) && (!isNextString(str, pos, "\n")))
                    {
                        pos++;
                    }
                }
                else if ((commentBegin.Length != 0) && (commentEnd.Length != 0) && isNextString(str, pos, commentBegin))
                {
                    // skip until commentEnd encountered
                    while (pos < str.Length)
                    {
                        if (isNextString(str, pos, commentEnd))
                        {
                            // skip over commentEnd and then exit loop
                            pos += commentEnd.Length;
                            break;
                        }

                        pos++;
                    }
                }

                result = result + str[pos];
                pos++;
            }

            return result;
        }

        public static bool isNextString(string str, int pos, string substr)
        {
            for (int i = 0; i < substr.Length; i++)
            {
                if ((pos + i) >= str.Length)
                    return false;

                if (substr[i] != str[pos + i])
                    return false;
            }

            return true;
        }

        /** Trim whitespaces from the begin and end of the string. */
        public static string trim(string str, char[] whitespaces)
            {
                    int begin = 0;
                    int end = str.Length-1;
               
                    while (begin < str.Length)
                    {
                            for (int i = 0; i < whitespaces.Length; i++)
                            {
                    char c = whitespaces[i];
                                    if (str[begin] == c)
                                    {
                                            begin++;
                                            continue;
                                    }
                            }
                       
                            // no whitespace -> break;
                            break;
                    }
               
                    while (end >= 0)
                    {
                for (int i = 0; i < whitespaces.Length; i++)
                            {
                    char c = whitespaces[i];
                                    if (str[end] == c)
                                    {
                                            end--;
                                            continue;
                                    }
                            }
                       
                            // no whitespace -> break;
                            end++;
                            break;
                    }
               
                    if (begin >= end)
                            return "";
               
                    return str.Substring(begin, end);
            }

        public static string escapeString(string src)
        {
            StringBuilder result = new StringBuilder();

            int i = 0;
            while (i < src.Length)
            {
                char c = src[i];
                i++;

                if (c == '\\')
                {
                    // ecape character
                    char next = src[i];
                    i++;
                    switch (next)
                    {
                        case '\\':
                            // escaped backslash
                            result.Append('\\');
                            break;
                        case 'u':
                            // escape Unicode character
                            String codeStr = src.Substring(i, i + 4);
                            i += 4;
                            //Integer code = Integer.decode("0x" + codeStr);
                            int code = Convert.ToInt32("0x" + codeStr, 16);
                            //result.Append(Character.toChars(code));
                            result.Append(Convert.ToChar(code));
                            break;
                        case 'n':
                            result.Append('\n');
                            break;
                    }
                }
                else
                {
                    result.Append(c);
                }
            }

            return result.ToString();
        }
    }
}