Ejemplo n.º 1
0
        static long ReadString(byte[] text, uint length, long offset, Token token)
        {
            uint strLen = 0;

            for (long i = offset + 1; i < length; i++)
            {
                byte ch = text[i];

                if (ch <' ' | ch> '~')
                {
                    return(-offset - 1);
                }

                if (ch == '"')
                {
                    if (token != null)
                    {
                        char[] chars   = new char[strLen];
                        int    nextIdx = 0;
                        for (int j = 0; j < i - offset - 1; j++)
                        {
                            char currChar = (char)text[offset + j + 1];
                            if (currChar == '\\')
                            {
                                j++;
                                currChar = (char)text[offset + j + 1];
                                if (currChar == '\\' | currChar == '"')
                                {
                                    // Nothing to do here
                                }
                                else if (currChar == 'n')
                                {
                                    currChar = '\n';
                                }
                                else if (currChar == 't')
                                {
                                    currChar = '\t';
                                }
                                else
                                {
                                    currChar = (char)(
                                        4092 * Miscellanea.HexDigitValue(ch) +
                                        256 * Miscellanea.HexDigitValue(text[j + 1]) +
                                        16 * Miscellanea.HexDigitValue(text[j + 2]) +
                                        Miscellanea.HexDigitValue(text[j + 3])
                                        );
                                    j += 3;
                                }
                            }
                            chars[nextIdx++] = currChar;
                        }
                        Miscellanea.Assert(nextIdx == strLen);

                        token.offset = offset;
                        token.length = i + 1 - offset;
                        token.type   = TokenType.String;
                        token.value  = new string(chars);
                    }
                    return(i + 1);
                }

                strLen++;

                if (ch == '\\')
                {
                    if (++i == length)
                    {
                        return(-i - 1);
                    }
                    ch = text[i];
                    if (Miscellanea.IsHexDigit(ch))
                    {
                        if (i + 3 >= length || !(Miscellanea.IsHexDigit(text[i + 1]) & Miscellanea.IsHexDigit(text[i + 2]) & Miscellanea.IsHexDigit(text[i + 3])))
                        {
                            return(-i);
                        }
                        i += 3;
                    }
                    else if (ch != '\\' & ch != '"' & ch != 'n' & ch != 't')
                    {
                        return(-i);
                    }
                }
            }
            return(-(length + 1));
        }