Esempio n. 1
0
        public static string decodeURI(sbyte[] bytes)
        {
            StringBuilder result = new StringBuilder(bytes.Length);

            for (int i = 0; i < bytes.Length; i++)
            {
                switch ((char)bytes[i])
                {
                case '+':
                    result.Append(' ');
                    break;

                case '%':
                    if (i + 2 < bytes.Length)
                    {
                        int next1 = MyChar.digit((char)bytes[i + 1], 16);
                        int next2 = MyChar.digit((char)bytes[i + 2], 16);
                        if (next1 > -1 && next2 > -1)
                        {
                            int b = (next1 << 4) + next2;
                            result.Append((char)b);
                            i += 2;
                            break;
                        }
                    }
                    goto default;

                default:
                    result.Append((char)bytes[i]);
                    break;
                }
            }
            return(result.ToString());
        }
Esempio n. 2
0
        /**
         * Reads an octal escape sequence of up to 3 octal digits.
         */
        private void readOctalEscapeSequence()
        {
            int value = MyChar.digit((char)c, BASE_OCTAL);

            readChar();
            if (isOctalDigit())
            {
                value = (value << 3) + MyChar.digit((char)c, BASE_OCTAL);
                readChar();
                if (isOctalDigit())
                {
                    value = (value << 3) + MyChar.digit((char)c, BASE_OCTAL);
                }
                else
                {
                    unreadChar();
                }
            }
            else
            {
                unreadChar();
            }

            c = value;
        }
Esempio n. 3
0
        /**
         * Reads a hexadecimal escape sequence.
         *
         * @param count number of characters to read
         * @throws CompilerException if the escape sequence was malformed
         */
        private void readHexEscapeSequence(int count)
        {
            int value = MyChar.digit((char)c, BASE_HEXADECIMAL);

            while (--count > 0)
            {
                readChar();

                if (!isHexadecimalDigit())
                {
                    throwCompilerException("Bad escape sequence");
                }
                else
                {
                    value = (value << 4) + MyChar.digit((char)c, BASE_HEXADECIMAL);
                }
            }

            c = value;
        }
Esempio n. 4
0
        /**
         * Parses a time string in the format [Tt]HH:MM:SS(.[0-9]+)?
         * followed by an optional timezone which is [Zz] or [-+]HH:MM
         *
         * @param time the time string that will be parsed
         * @param calendar the calendar object that will be updated with the parsed time
         * @return
         */
        private static long parseTime(String time, Calendar calendar)
        {
            if (time.Length < MIN_TIME_STRING_LENGTH)
            {
                throw new FormatException();
            }

            long timeZoneOffset = 0;

            char first = time[0];

            // There should be a t separating the date and time and
            // it should be long enought to fit "THHMMSS".
            if (time.Length < MIN_TIME_STRING_LENGTH ||
                (first != 'T' && first != 't'))
            {
                throw new FormatException();
            }
            // everything after the 'T' or 't'
            time = time.Substring(1);

            int value = Convert.ToInt32(time.Substring(0, 2));

            if (value > 23)
            {
                throw new FormatException();
            }
            calendar.set(Calendar.HOUR_OF_DAY, value);
            if (time[2] == ':')
            {
                time = time.Substring(3);
            }
            else
            {
                time = time.Substring(2);
            }

            value = Convert.ToInt32(time.Substring(0, 2));
            if (value > 59)
            {
                throw new FormatException();
            }
            calendar.set(Calendar.MINUTE, value);
            if (time[2] == ':')
            {
                time = time.Substring(3);
            }
            else
            {
                time = time.Substring(2);
            }

            value = Convert.ToInt32(time.Substring(0, 2));
            if (value > 59)
            {
                throw new FormatException();
            }
            calendar.set(Calendar.SECOND, value);
            time = time.Substring(2);

            calendar.set(Calendar.MILLISECOND, 0);

            if (time.Length > 0)
            {
                try {
                    // If the next character is a '.' we have a miliseconds specified in
                    // the string.
                    int currentStringPos = 0;
                    if (time[currentStringPos] == '.')
                    {
                        ++currentStringPos;
                        // Next character must be a digit
                        if (!Char.IsDigit(time[currentStringPos]))
                        {
                            throw new FormatException();
                        }
                        int multiplier   = 100;
                        int milliseconds =
                            MyChar.digit(time[currentStringPos], 10) * multiplier;

                        // There may be 0 or more more digits to process
                        // Note if there are more than 3 digits in total we will be ignoring
                        // the fractions of a millisecond sepecified.
                        ++currentStringPos;
                        while (currentStringPos < time.Length &&
                               Char.IsDigit(time[currentStringPos]))
                        {
                            multiplier   /= 10;
                            milliseconds += MyChar.digit(time[currentStringPos], 10) * multiplier;
                            currentStringPos++;
                        }
                        calendar.set(Calendar.MILLISECOND, milliseconds);
                    }
                    // If we still have characters to process this should
                    // be timezone information. Which is either a 'Z' or
                    // +HH:MM or -HH:MM
                    if (currentStringPos < time.Length)
                    {
                        String timezone = time.Substring(currentStringPos);

                        // If its a string of 1 character which is a Z we are
                        // using Universal time, so no change nessesary.
                        if ((timezone[0] == 'Z' || timezone[0] == 'z') &&
                            timezone.Length == 1)
                        {
                            // Nothing to do here.

                            // Check to see if it might be of the form [+-]HH:MM
                        }
                        else if (timezone.Length == NUMERIC_TIME_ZONE_LENGTH && (timezone[0] == '+' ||
                                                                                 timezone[0] == '-') && timezone[3] == ':')
                        {
                            int hours   = Convert.ToInt32(timezone.Substring(1, 2));
                            int minutes = Convert.ToInt32(timezone.Substring(4, 2));
                            timeZoneOffset = ((hours * 60) + minutes) * 60000;
                            if (timezone[0] == '+')
                            {
                                timeZoneOffset = -timeZoneOffset;
                            }
                        }
                        else
                        {
                            // We have spare characters that aren't a valid timezone,
                            // so its an invalid date.
                            throw new FormatException();
                        }
                    }
                } catch (IndexOutOfRangeException) {
                    throw new FormatException();
                }
            }
            return(timeZoneOffset);
        }