Exemple #1
0
        /// <summary>
        /// Reverse a string.
        /// </summary>
        //shemran/Note: this is used for secondary order value reverse, no
        //              need to consider supplementary pair.
        internal static void Reverse(StringBuffer result, int from, int to)
        {
            int  i = from;
            char swap;

            int j = to - 1;

            while (i < j)
            {
                swap = result.CharAt(i);
                result.SetCharAt(i, result.CharAt(j));
                result.SetCharAt(j, swap);
                i++;
                j--;
            }
        }
Exemple #2
0
            private StringBuffer CheckForPositiveExponent(StringBuffer number)
            {
                int len = number.Length();

                for (int i = len - 2; i >= 0; --i)
                {
                    var c = number.CharAt(i);
                    if (c == 'e' || c == 'E')
                    {
                        if (number.CharAt(i + 1) == '-')
                        {
                            return(number);
                        }
                        return(number.Insert(i + 1, "+"));
                    }
                }
                return(number);
            }
Exemple #3
0
        private static StringBuffer ToASCIILower(StringBuffer input)
        {
            StringBuffer dest = new StringBuffer();

            for (int i = 0; i < input.Length(); i++)
            {
                dest.Append(ToASCIILower(input.CharAt(i)));
            }
            return(dest);
        }
Exemple #4
0
        //
        // to check if a string starts with ACE-prefix
        //
        private static bool StartsWithACEPrefix(StringBuffer input)
        {
            bool startsWithPrefix = true;

            if (input.Length() < ACE_PREFIX_LENGTH)
            {
                return(false);
            }
            for (int i = 0; i < ACE_PREFIX_LENGTH; i++)
            {
                if (ToASCIILower(input.CharAt(i)) != ACE_PREFIX.CharAt(i))
                {
                    startsWithPrefix = false;
                }
            }
            return(startsWithPrefix);
        }
        /* Normalize the given pathname, whose length is len, starting at the given
         * offset; everything before this offset is already normal. */
        private String Normalize(String path, int len, int off)
        {
            if (len == 0)
            {
                return(path);
            }
            if (off < 3)             // Avoid fencepost cases with UNC pathnames
            {
                off = 0;
            }
            int          src;
            char         slash = this.Slash;
            StringBuffer sb    = new StringBuffer(len);

            if (off == 0)
            {
                /* Complete normalization, including prefix */
                src = NormalizePrefix(path, len, sb);
            }
            else
            {
                /* Partial normalization */
                src = off;
                sb.Append(path.Substring(0, off));
            }

            /* Remove redundant slashes from the remainder of the path, forcing all
             * slashes into the preferred slash */
            while (src < len)
            {
                char c = path.CharAt(src++);
                if (IsSlash(c))
                {
                    while ((src < len) && IsSlash(path.CharAt(src)))
                    {
                        src++;
                    }
                    if (src == len)
                    {
                        /* Check for trailing separator */
                        int sn = sb.Length();
                        if ((sn == 2) && (sb.CharAt(1) == ':'))
                        {
                            /* "z:\\" */
                            sb.Append(slash);
                            break;
                        }
                        if (sn == 0)
                        {
                            /* "\\" */
                            sb.Append(slash);
                            break;
                        }
                        if ((sn == 1) && (IsSlash(sb.CharAt(0))))
                        {
                            /* "\\\\" is not collapsed to "\\" because "\\\\" marks
                             * the beginning of a UNC pathname.  Even though it is
                             * not, by itself, a valid UNC pathname, we leave it as
                             * is in order to be consistent with the win32 APIs,
                             * which treat this case as an invalid UNC pathname
                             * rather than as an alias for the root directory of
                             * the current drive. */
                            sb.Append(slash);
                            break;
                        }

                        /* Path does not denote a root directory, so do not append
                         * trailing slash */
                        break;
                    }
                    else
                    {
                        sb.Append(slash);
                    }
                }
                else
                {
                    sb.Append(c);
                }
            }

            String rv = sb.ToString();

            return(rv);
        }
        /*
           * Converts unicodes to encoded &#92;uxxxx and writes out any of the
           * characters in specialSaveChars with a preceding slash
           *
           * @param theString
           *            the string needing convert.
           * @param dst
           *            Save of the result
           * @param offset
           *            the offset of result
           * @param escapeSpace
           *            if <code>true</code>, escape Space
           * @param lengthFlag
           *            Whether add one byte of length in the result.
           *            <code>true</code> add one byte of length in the result
           * @param getLengthFlag
           *            Calculate the length of result, if <code>true</code>, thestring length that return.
           * @return  if getLengthFlag = false, return offset of the result.
           *          if getLengthFlag = true, the length of the sequence of characters represented by this
           *          object.
           */
        public static int saveConvert(string theString, byte[] dst, int offset, bool escapeSpace, bool lengthFlag, bool getLengthFlag)
        {
            if (false == getLengthFlag
                && (null == dst || dst.Length < (offset + (lengthFlag ? 1 : 0))
                || dst.Length < 1 || offset < 0))
                return -1;
            if (null == theString)
                theString = "";
            int length = theString.Length;

            StringBuffer outBuffer = new StringBuffer (length * 2);

            for (int x = 0; x < length; x++) {
                char aChar = theString [x];
                switch (aChar) {
                case ' ':
                    if (x == 0 || escapeSpace)
                        outBuffer.Append ('\\');

                    outBuffer.Append (' ');
                    break;
                case '\\':
                    outBuffer.Append ('\\');
                    break;
                case '\t':
                    outBuffer.Append ('\\');
                    outBuffer.Append ('t');
                    break;
                case '\n':
                    outBuffer.Append ('\\');
                    outBuffer.Append ('n');
                    break;
                case '\r':
                    outBuffer.Append ('\\');
                    outBuffer.Append ('r');
                    break;
                case '\f':
                    outBuffer.Append ('\\');
                    outBuffer.Append ('f');
                    break;
                default:
                    if ((aChar < 0x0020) || (aChar > 0x007e)) {
                        outBuffer.Append ('\\');
                        outBuffer.Append ('u');
                        outBuffer.Append (toHexChar ((aChar >> 12) & 0xF));
                        outBuffer.Append (toHexChar ((aChar >> 8) & 0xF));
                        outBuffer.Append (toHexChar ((aChar >> 4) & 0xF));
                        outBuffer.Append (toHexChar (aChar & 0xF));
                    } else {
                        if (specialSaveChars.IndexOf (aChar) != -1)
                            outBuffer.Append ('\\');
                        outBuffer.Append (aChar);
                    }
                    break;
                }
            }
            length = outBuffer.Length ();
            if (length > 255)
                length = 255;
            if (!getLengthFlag) {
                if (dst.Length >= offset + length + (lengthFlag ? 1 : 0)) {
                    if (lengthFlag) {
                        dst [offset] = (byte)(length & 0xFF);
                        offset++;
                    }
                    for (int i = 0; i < length; i++) {
                        dst [offset] = (byte)outBuffer.CharAt (i);
                        offset++;
                    }
                    length = offset;
                } else {
                    length = -1;
                }
            } else {
                if (lengthFlag)
                    length = length + 1;
            }

            outBuffer = null;

            return length;
        }
Exemple #7
0
        /*
         * Converts unicodes to encoded &#92;uxxxx and writes out any of the
         * characters in specialSaveChars with a preceding slash
         *
         * @param theString
         *            the string needing convert.
         * @param dst
         *            Save of the result
         * @param offset
         *            the offset of result
         * @param escapeSpace
         *            if <code>true</code>, escape Space
         * @param lengthFlag
         *            Whether add one byte of length in the result.
         *            <code>true</code> add one byte of length in the result
         * @param getLengthFlag
         *            Calculate the length of result, if <code>true</code>, thestring length that return.
         * @return  if getLengthFlag = false, return offset of the result.
         *          if getLengthFlag = true, the length of the sequence of characters represented by this
         *          object.
         */
        public static int saveConvert(string theString, byte[] dst, int offset, bool escapeSpace, bool lengthFlag, bool getLengthFlag)
        {
            if (false == getLengthFlag &&
                (null == dst || dst.Length < (offset + (lengthFlag ? 1 : 0)) ||
                 dst.Length < 1 || offset < 0))
            {
                return(-1);
            }
            if (null == theString)
            {
                theString = "";
            }
            int length = theString.Length;

            StringBuffer outBuffer = new StringBuffer(length * 2);

            for (int x = 0; x < length; x++)
            {
                char aChar = theString [x];
                switch (aChar)
                {
                case ' ':
                    if (x == 0 || escapeSpace)
                    {
                        outBuffer.Append('\\');
                    }

                    outBuffer.Append(' ');
                    break;

                case '\\':
                    outBuffer.Append('\\');
                    break;

                case '\t':
                    outBuffer.Append('\\');
                    outBuffer.Append('t');
                    break;

                case '\n':
                    outBuffer.Append('\\');
                    outBuffer.Append('n');
                    break;

                case '\r':
                    outBuffer.Append('\\');
                    outBuffer.Append('r');
                    break;

                case '\f':
                    outBuffer.Append('\\');
                    outBuffer.Append('f');
                    break;

                default:
                    if ((aChar < 0x0020) || (aChar > 0x007e))
                    {
                        outBuffer.Append('\\');
                        outBuffer.Append('u');
                        outBuffer.Append(toHexChar((aChar >> 12) & 0xF));
                        outBuffer.Append(toHexChar((aChar >> 8) & 0xF));
                        outBuffer.Append(toHexChar((aChar >> 4) & 0xF));
                        outBuffer.Append(toHexChar(aChar & 0xF));
                    }
                    else
                    {
                        if (specialSaveChars.IndexOf(aChar) != -1)
                        {
                            outBuffer.Append('\\');
                        }
                        outBuffer.Append(aChar);
                    }
                    break;
                }
            }
            length = outBuffer.Length();
            if (length > 255)
            {
                length = 255;
            }
            if (!getLengthFlag)
            {
                if (dst.Length >= offset + length + (lengthFlag ? 1 : 0))
                {
                    if (lengthFlag)
                    {
                        dst [offset] = (byte)(length & 0xFF);
                        offset++;
                    }
                    for (int i = 0; i < length; i++)
                    {
                        dst [offset] = (byte)outBuffer.CharAt(i);
                        offset++;
                    }
                    length = offset;
                }
                else
                {
                    length = -1;
                }
            }
            else
            {
                if (lengthFlag)
                {
                    length = length + 1;
                }
            }

            outBuffer = null;

            return(length);
        }