예제 #1
0
bool Uri::Private::parseIPvFuture()
{
    if (!expectChar('v') && !expectChar('V')) {
        return false;
    }
    m_parserAux.clear();
    Char curr = m_uri[m_parserPos];
    size_t currValue = curr.value();
    if (currValue > 127 || !isHexdig[currValue]) {
        return false;
    }
    ++m_parserPos;
    if (!expectChar('.')) {
        return false;
    }
    curr = m_uri[m_parserPos];
    currValue = curr.value();
    if (currValue < 128 && isUnreserved[currValue]) {
        ++m_parserPos;
        return true;
    }
    if (currValue < 128 && isSubdelim[currValue]) {
        ++m_parserPos;
        return true;
    }
    if (curr == ':') {
        ++m_parserPos;
        return true;
    }
    return false;
}
예제 #2
0
bool Uri::Private::parsePctEncoded()
{
    if (expectChar('%')) {
        Char curr = m_uri[m_parserPos];
        size_t currValue = curr.value();
        if (!currValue || currValue > 127 || !isHexdig[currValue]) {
            return false;
        }
        m_parserAux += curr;
        ++m_parserPos;
        curr = m_uri[m_parserPos];
        currValue = curr.value();
        if (!currValue || currValue > 127 || !isHexdig[currValue]) {
            return false;
        }
        m_parserAux += curr;
        ++m_parserPos;
    } else {
        const Char curr = m_uri[m_parserPos];
        const iuint32 currValue = curr.value();
        if (curr.octetsRequired() > 1) {
            m_parserAux += curr;
            ++m_parserPos;
        } else if (currValue && currValue < 128 && (!isUnreserved[currValue] && !isGendelim[currValue] &&
                                                    !isSubdelim[currValue] && curr != ':' && curr != '@')) {
            m_parserTrick = true;
            m_parserAux += getHex(curr);
            ++m_parserPos;
        } else {
            return false;
        }
    }
    return true;
}
예제 #3
0
bool JsonParser::JsonStringParser::advance(Char ch)
{
    switch (_state)
    {
        case state_0:
            if (ch == '\\')
                _state = state_esc;
            else if (ch == '"')
                return true;
            else
                _str += ch;
            break;

        case state_esc:
            _state = state_0;
            if (ch == '"' || ch == '\\' || ch == '/')
                _str += ch;
            else if (ch == 'b')
                _str += '\b';
            else if (ch == 'f')
                _str += '\f';
            else if (ch == 'n')
                _str += '\n';
            else if (ch == 'r')
                _str += '\r';
            else if (ch == 't')
                _str += '\t';
            else if (ch == 'u')
            {
                _value = 0;
                _count = 4;
                _state = state_hex;
            }
            else
                _jsonParser->doThrow(std::string("invalid character '") + ch.narrow() + "' in string");
            break;

        case state_hex:
            if (ch >= '0' && ch <= '9')
                _value = (_value << 4) | (ch.value() - '0');
            else if (ch >= 'a' && ch <= 'f')
                _value = (_value << 4) | (ch.value() - 'a' + 10);
            else if (ch >= 'A' && ch <= 'F')
                _value = (_value << 4) | (ch.value() - 'A' + 10);
            else
                _jsonParser->doThrow(std::string("invalid character '") + ch.narrow() + "' in hex sequence");

            if (--_count == 0)
            {
                _str += Char(static_cast<wchar_t>(_value));
                _state = state_0;
            }

            break;

    }

    return false;
}
예제 #4
0
void Uri::Private::parsePort()
{
    m_parserAux.clear();
    Char curr = m_uri[m_parserPos];
    iuint32 currValue = curr.value();
    while (currValue && currValue < 128 && isDigit[currValue]) {
        m_parserAux += curr;
        m_parserPos++;
        curr = m_uri[m_parserPos];
        currValue = curr.value();
    }
    m_port = m_parserAux.empty() ? -1 : m_parserAux.toInt();
}
예제 #5
0
bool Uri::Private::parsePchar()
{
    const Char curr = m_uri[m_parserPos];
    const iuint32 currValue = curr.value();
    if (!currValue) {
        return false;
    }
    if (currValue < 128 && isUnreserved[currValue]) {
        m_parserAux += curr;
        ++m_parserPos;
        return true;
    }
    const size_t parserOldPos = m_parserPos;
    if (parsePctEncoded()) {
        return true;
    }
    m_parserPos = parserOldPos;
    if (currValue < 128 && isSubdelim[currValue]) {
        m_parserAux += curr;
        ++m_parserPos;
        return true;
    }
    if (curr == ':' || curr == '@') {
        m_parserAux += curr;
        ++m_parserPos;
        return true;
    }
    return false;
}
예제 #6
0
bool Uri::Private::parseSegmentNzNc()
{
    bool res = false;
    while (true) {
        const Char curr = m_uri[m_parserPos];
        const iuint32 currValue = curr.value();
        if (currValue < 128 && isUnreserved[currValue]) {
            m_parserAux += curr;
            ++m_parserPos;
            res = true;
            continue;
        }
        const size_t parserOldPos = m_parserPos;
        if (parsePctEncoded()) {
            res = true;
            continue;
        }
        m_parserPos = parserOldPos;
        if (currValue < 128 && isSubdelim[currValue]) {
            m_parserAux += curr;
            ++m_parserPos;
            res = true;
            continue;
        }
        if (curr == '@') {
            m_parserAux += '@';
            ++m_parserPos;
            res = true;
            continue;
        }
        return res;
    }
}
예제 #7
0
bool Uri::Private::parseDecOctet()
{
    String decOctet;
    for (size_t i = 0; i < 3; ++i) {
        const Char currChar = m_uri[m_parserPos];
        const iuint32 currValue = currChar.value();
        if (currValue && currValue < 128 && isDigit[currValue]) {
            decOctet += currChar;
            ++m_parserPos;
        } else {
            break;
        }
    }
    if (decOctet.empty()) {
        return false;
    } else {
        const size_t retValue = decOctet.toInt();
        if (retValue < 256) {
            m_parserAux += decOctet;
            return true;
        } else {
            return false;
        }
    }
}
예제 #8
0
void EntityResolver::getEntity(std::basic_ostream<Char>& os, Char ch) const
{
    unsigned u = 0;
    unsigned o = sizeof(rent)/sizeof(Ent) - 1;
    while (o - u > 1)
    {
        unsigned m = (o + u) / 2;
        if (rent[m].charValue == ch.value())
        {
            printEntity(os, rent[m].entity);
            return;
        }

        if (ch.value() < rent[m].charValue)
            o = m;
        else
            u = m;
    }

    if (rent[u].charValue == ch.value())
        printEntity(os, rent[u].entity);
    else if (rent[o].charValue == ch.value())
        printEntity(os, rent[o].entity);
    else if (ch.value() >= ' ' && ch.value() <= 0x7F)
        os << ch;
    else
        os << Char('&') << Char('#') << static_cast<uint32_t>(ch.value()) << Char(';');
}
예제 #9
0
bool Uri::Private::parseScheme()
{
    m_parserAux.clear();
    Char curr = m_uri[m_parserPos];
    iuint32 currValue = curr.value();
    if (!currValue || currValue > 127 || !isAlpha[currValue]) {
        return false;
    }
    while (currValue && currValue < 128 && (isAlpha[currValue] || isDigit[currValue] ||
                                            curr == '+' || curr == '-' || curr == '.')) {
        m_parserAux += curr;
        ++m_parserPos;
        curr = m_uri[m_parserPos];
        currValue = curr.value();
    }
    m_scheme = m_parserAux;
    return true;
}
예제 #10
0
bool Uri::Private::expectChar(Char c)
{
    const Char curr = m_uri[m_parserPos];
    const iuint32 currValue = curr.value();
    if (!currValue || currValue > 127 || curr != c) {
        return false;
    }
    ++m_parserPos;
    return true;
}
예제 #11
0
bool Uri::Private::parseReserved()
{
    const Char curr = m_uri[m_parserPos];
    const iuint32 currValue = curr.value();
    if (currValue < 128 && (isGendelim[currValue] || isSubdelim[currValue])) {
        m_parserAux += curr;
        ++m_parserPos;
        return true;
    }
    
    return false;
}
예제 #12
0
bool Uri::Private::parseH16()
{
    for (size_t i = 0; i < 4; ++i) {
        const Char curr = m_uri[m_parserPos];
        const size_t currValue = curr.value();
        if (!currValue || currValue > 127 || !isHexdig[currValue]) {
            return i;
        }
        m_parserAux += curr;
        ++m_parserPos;
    }
    return true;
}
예제 #13
0
String Uri::Private::getHex(Char ch) const
{
    String res;
    union {
        iuint32 value;
        ichar v[4];
    } fragmentedValue;
    fragmentedValue.value = ch.value();
    const iint32 octetsRequired = ch.octetsRequired();
    for (iint32 i = 0; i < octetsRequired; ++i) {
        res += '%';
        res += uri_hex[(fragmentedValue.v[octetsRequired - i - 1] >> 4) & 0xf];
        res += uri_hex[fragmentedValue.v[octetsRequired - i - 1] & 0xf];
    }
    return res;
}
예제 #14
0
void Uri::Private::parseUserinfo()
{
    m_parserAux.clear();
    bool usernameFound = false;
    while (true) {
        const Char curr = m_uri[m_parserPos];
        const iuint32 currValue = curr.value();
        if (!currValue) {
            return;
        }
        if (currValue < 128 && isUnreserved[currValue]) {
            if (!usernameFound && curr == ':') {
                usernameFound = true;
            } else if (!usernameFound) {
                m_username += curr;
            } else {
                m_password += curr;
            }
            m_parserAux += curr;
            ++m_parserPos;
            continue;
        }
        const size_t parserOldPos = m_parserPos;
        if (parsePctEncoded()) {
            continue;
        }
        m_parserPos = parserOldPos;
        if (currValue < 128 && (isSubdelim[currValue] ||
                                curr == ':')) {
            if (!usernameFound && curr == ':') {
                usernameFound = true;
            } else if (!usernameFound) {
                m_username += curr;
            } else {
                m_password += curr;
            }
            m_parserAux += curr;
            ++m_parserPos;
            continue;
        }
        m_userInfo = m_parserAux;
        return;
    }
}
예제 #15
0
void Uri::Private::parseRegName()
{
    m_parserAux.clear();
    while (true) {
        const Char curr = m_uri[m_parserPos];
        const size_t currValue = curr.value();
        if (currValue < 128 && isUnreserved[currValue]) {
            m_parserAux += curr;
            ++m_parserPos;
            continue;
        }
        const size_t parserOldPos = m_parserPos;
        if (parsePctEncoded()) {
            continue;
        }
        m_parserPos = parserOldPos;
        if (currValue < 128 && isSubdelim[currValue]) {
            m_parserAux += curr;
            ++m_parserPos;
            continue;
        }
        return;
    }
}
예제 #16
0
int JsonParser::advance(Char ch)
{
    int ret;

    if (ch == '\n')
      ++_lineNo;

    try
    {
        switch (_state)
        {
            case state_0:
                if (ch == '{')
                {
                    _state = state_object;
                    _deserializer->setCategory(SerializationInfo::Object);
                }
                else if (ch == '[')
                {
                    _state = state_array;
                    _deserializer->setCategory(SerializationInfo::Array);
                }
                else if (ch == '"')
                {
                    _state = state_string;
                    _deserializer->setCategory(SerializationInfo::Value);
                }
                else if ((ch >= '0' && ch <= '9') || ch == '+' || ch == '-')
                {
                    _token = ch;
                    _state = state_number;
                    _deserializer->setCategory(SerializationInfo::Value);
                }
                else if (ch == '/')
                {
                    _nextState = _state;
                    _state = state_comment0;
                }
                else if (!std::isspace(ch.value()))
                {
                    _token = ch;
                    _state = state_token;
                }
                break;

            case state_object:
                if (ch == '"')
                {
                    _state = state_object_name;
                    _stringParser.clear();
                }
                else if (ch == '}')
                    return 1;
                else if (ch == '/')
                {
                    _nextState = _state;
                    _state = state_comment0;
                }
                else if (std::isalpha(ch.value()))
                {
                    _token = ch;
                    _state = state_object_plainname;
                }
                else if (!std::isspace(ch.value()))
                    throwInvalidCharacter(ch);
                break;

            case state_object_plainname:
                if (std::isalnum(ch.value()))
                    _token += ch;
                else if (std::isspace(ch.value()))
                {
                    _stringParser.str(_token);
                    _state = state_object_after_name;
                }
                else if (ch == ':')
                {
                    _stringParser.str(_token);
                    if (_next == 0)
                        _next = new JsonParser();
                    log_debug("begin object member " << _stringParser.str());
                    _deserializer->beginMember(Utf8Codec::encode(_stringParser.str()),
                            std::string(), SerializationInfo::Void);
                    _next->begin(*_deserializer);
                    _stringParser.clear();
                    _state = state_object_value;
                }
                else
                    throwInvalidCharacter(ch);

                break;

            case state_object_name:
                if (_stringParser.advance(ch))
                    _state = state_object_after_name;
                break;

            case state_object_after_name:
                if (ch == ':')
                {
                    if (_next == 0)
                        _next = new JsonParser();
                    log_debug("begin object member " << _stringParser.str());
                    _deserializer->beginMember(Utf8Codec::encode(_stringParser.str()),
                            std::string(), SerializationInfo::Void);
                    _next->begin(*_deserializer);
                    _stringParser.clear();
                    _state = state_object_value;
                }
                else if (ch == '/')
                {
                    _nextState = _state;
                    _state = state_comment0;
                }
                else if (!std::isspace(ch.value()))
                    throwInvalidCharacter(ch);
                break;

            case state_object_value:
                ret = _next->advance(ch);

                if (ret != 0)
                {
                    log_debug("leave member");
                    _deserializer->leaveMember();
                    _state = state_object_e;
                }

                if (ret != -1)
                    break;

            case state_object_e:
                if (ch == ',')
                    _state = state_object_next_member;
                else if (ch == '}')
                    return 1;
                else if (ch == '/')
                {
                    _nextState = _state;
                    _state = state_comment0;
                }
                else if (!std::isspace(ch.value()))
                    throwInvalidCharacter(ch);
                break;

            case state_object_next_member:
                if (ch == '"')
                {
                    _state = state_object_name;
                    _stringParser.clear();
                }
                else if (ch == '/')
                {
                    _nextState = _state;
                    _state = state_comment0;
                }
                else if (std::isalpha(ch.value()))
                {
                    _token = ch;
                    _state = state_object_plainname;
                }
                else if (!std::isspace(ch.value()))
                    throwInvalidCharacter(ch);
                break;

            case state_array:
                if (ch == ']')
                {
                    return 1;
                }
                else if (ch == '/')
                {
                    _nextState = _state;
                    _state = state_comment0;
                }
                else if (!std::isspace(ch.value()))
                {
                    if (_next == 0)
                        _next = new JsonParser();

                    log_debug("begin array member");
                    _deserializer->beginMember(std::string(),
                            std::string(), SerializationInfo::Void);
                    _next->begin(*_deserializer);
                    _next->advance(ch);
                    _state = state_array_value;
                }
                break;

            case state_array_value:
                ret = _next->advance(ch);
                if (ret != 0)
                    _state = state_array_e;
                if (ret != -1)
                    break;

            case state_array_e:
                if (ch == ']')
                {
                    log_debug("leave member");
                    _deserializer->leaveMember();
                    return 1;
                }
                else if (ch == ',')
                {
                    log_debug("leave member");
                    _deserializer->leaveMember();

                    log_debug("begin array member");
                    _deserializer->beginMember(std::string(),
                            std::string(), SerializationInfo::Void);
                    _next->begin(*_deserializer);
                    _state = state_array_value;
                }
                else if (ch == '/')
                {
                    _nextState = _state;
                    _state = state_comment0;
                }
                else if (!std::isspace(ch.value()))
                    throwInvalidCharacter(ch);
                break;

            case state_string:
                if (_stringParser.advance(ch))
                {
                    log_debug("set string value \"" << _stringParser.str() << '"');
                    _deserializer->setValue(_stringParser.str());
                    _deserializer->setTypeName("string");
                    _stringParser.clear();
                    _state = state_end;
                    return 1;
                }
                break;

            case state_number:
                if (std::isspace(ch.value()))
                {
                    log_debug("set int value \"" << _token << '"');
                    _deserializer->setValue(_token);
                    _deserializer->setTypeName("int");
                    _token.clear();
                    return 1;
                }
                else if (ch == '.' || ch == 'e' || ch == 'E')
                {
                    _token += ch;
                    _state = state_float;
                }
                else if (ch >= '0' && ch <= '9')
                {
                    _token += ch;
                }
                else
                {
                    log_debug("set int value \"" << _token << '"');
                    _deserializer->setValue(_token);
                    _deserializer->setTypeName("int");
                    _token.clear();
                    return -1;
                }
                break;

            case state_float:
                if (std::isspace(ch.value()))
                {
                    log_debug("set double value \"" << _token << '"');
                    _deserializer->setValue(_token);
                    _deserializer->setTypeName("double");
                    _token.clear();
                    return 1;
                }
                else if ((ch >= '0' && ch <= '9') || ch == '+' || ch == '-'
                        || ch == '.' || ch == 'e' || ch == 'E')
                    _token += ch;
                else
                {
                    log_debug("set double value \"" << _token << '"');
                    _deserializer->setValue(_token);
                    _deserializer->setTypeName("double");
                    _token.clear();
                    return -1;
                }
                break;

            case state_token:
                if (std::isalpha(ch.value()))
                    _token += Char(std::tolower(ch));
                else
                {
                    if (_token == "true" || _token == "false")
                    {
                        log_debug("set bool value \"" << _token << '"');
                        _deserializer->setValue(_token);
                        _deserializer->setTypeName("bool");
                        _token.clear();
                    }
                    else if (_token == "null")
                    {
                        log_debug("set null value \"" << _token << '"');
                        _deserializer->setTypeName("null");
                        _deserializer->setNull();
                        _token.clear();
                    }

                    return -1;
                }

                break;

            case state_comment0:
                if (ch == '/')
                    _state = state_commentline;
                else if (ch == '*')
                    _state = state_comment;
                else
                    throwInvalidCharacter(ch);
                break;

            case state_commentline:
                if (ch == '\n')
                    _state = _nextState;
                break;

            case state_comment:
                if (ch == '*')
                    _state = state_comment_e;
                break;

            case state_comment_e:
                if (ch == '/')
                    _state = _nextState;
                else if (ch != '*')
                    _state = state_comment;
                break;

            case state_end:
                if (ch == '/')
                {
                    _nextState = _state;
                    _state = state_comment0;
                }
                else if (!std::isspace(ch.value()))
                    doThrow(std::string("unexpected character '") + ch.narrow() + "\' after end");
                break;
        }
    }
    catch (JsonParserError& e)
    {
        e._lineNo = _lineNo;
        throw;
    }

    return 0;
}
예제 #17
0
파일: Regex.cpp 프로젝트: 3Nigma/frayon
Pt::String RegexSMatch::format(const Pt::String& str) const
{
  enum state_type
  {
    state_0,
    state_esc,
    state_var0,
    state_var1,
    state_1
  } state;

  state = state_0;
  Pt::String ret;

  for (Pt::String::const_iterator it = str.begin(); it != str.end(); ++it)
  {
    Char ch = *it;

    switch (state)
    {
      case state_0:
        if (ch == '$')
          state = state_var0;
        else if (ch == '\\')
          state = state_esc;
        break;

      case state_esc:
        ret += ch;
        state = state_1;
        break;

      case state_var0:
        if( isdigit(ch) )
        {
          ret = Pt::String(str.begin(), it - 1);
            
          unsigned n = ch.value() - '0';
          if(n < _size)
          {
            const Pt::Char* s = _match->startp[n];
            const Pt::Char* e = _match->endp[n];
            assert(s && e);

            ret.append(s, e-s);
          }

            state = state_1;
        }
        else
          state = state_0;
        break;

      case state_1:
        if (ch == '$')
          state = state_var1;
        else if (ch == '\\')
          state = state_esc;
        else
          ret += ch;
        break;

      case state_var1:
        if( isdigit(ch) )
        {
          unsigned n = ch.value() - '0';

          if(n < _size)
          {
            const Pt::Char* s = _match->startp[n];
            const Pt::Char* e = _match->endp[n];
            assert(s && e);

            ret.append(s, e-s);
          }

          state = state_1;
        }
        else if (ch == '$')
          ret += '$';
        else
        {
          ret += '$';
          ret += ch;
        }
        break;
    }
  }

  switch (state)
  {
    case state_0:
    case state_var0:
      return str;

    case state_esc:
      return ret + '\\';

    case state_var1:
      return ret + '$';

    case state_1:
      return ret;
  }

  return ret;
}
예제 #18
0
Utf8Codec::result Utf8Codec::do_out(MBState& /*s*/, const Char* fromBegin, const Char* fromEnd, const Char*& fromNext,
                                                  char* toBegin, char* toEnd, char*& toNext) const
{
    result retstat = ok;
    fromNext  = fromBegin;
    toNext = toBegin;
    Char ch;

    size_t bytesToWrite;

    while(fromNext < fromEnd)
    {
        ch = *fromNext;
        if (ch >= SurHighStart && ch <= SurLowEnd)
        {
            retstat = error;
            break;
        }

        // Figure out how many bytes the result will require. Turn any
        // illegally large UTF32 things (> Plane 17) into replacement chars.
        if (ch < Char(0x80))
        {
            bytesToWrite = 1;
        }
        else if (ch < Char(0x800))
        {
            bytesToWrite = 2;
        }
        else if (ch < Char(0x10000))
        {
            bytesToWrite = 3;
        }
        else if (ch <= MaxLegalUtf32)
        {
            bytesToWrite = 4;
        }
        else
        {
            bytesToWrite = 3;
            ch = ReplacementChar;
        }

        uint8_t* current = (uint8_t*)(toNext + bytesToWrite);
        if( current >= (uint8_t*)(toEnd) )
        {
            retstat = partial;
            break;
        }

        Char::value_type chValue = ch.value();
        switch(bytesToWrite)
        { // note: everything falls through...
            case 4: *--current = static_cast<uint8_t>((chValue | byteMark) & byteMask); chValue >>= 6;
            case 3: *--current = static_cast<uint8_t>((chValue | byteMark) & byteMask); chValue >>= 6;
            case 2: *--current = static_cast<uint8_t>((chValue | byteMark) & byteMask); chValue >>= 6;
            case 1: *--current = static_cast<uint8_t> (chValue | firstByteMark[bytesToWrite]);
        }

        toNext += bytesToWrite;
        ++fromNext;
    }

    return retstat;
}