Example #1
0
void conn_info::append_escaped(const string_ref& rng, std::string& dst)
{
    for(string_ref::difference_type i=0; i < rng.size(); ++i) {
        if(rng[i]=='\\')
            dst+="\\\\";
        else if(rng[i]=='\'')
            dst+="\\\'";
        else
            dst+=rng[i];
    }
}
//  Should be equal
void interop ( const std::string &str, string_ref ref ) {
//  BOOST_CHECK ( str == ref );
    BOOST_CHECK ( str.size () == ref.size ());
    BOOST_CHECK ( std::equal ( str.begin (),  str.end (),  ref.begin ()));
    BOOST_CHECK ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ()));
    }
Example #3
0
//------------------------------------------------------------------------------
void transitory_text_layout::reset(
    font_face&       face
  , string_ref const string
  , int16_t    const max_w
  , int16_t    const max_h
) {
    constexpr auto tab_size = 20;
    
    clear();

    //assume 2 bytes on average per codepoint.
    //overly pessimistic for mostly latin text
    data_.reserve(string.size() / 2);

    auto       it   = utf8::iterator<char const*> {cbegin(string), cbegin(string), cend(string)};
    auto const end  = utf8::iterator<char const*> {cend(string),   cbegin(string), cend(string)};

    record_t   cur_record {};
    format_tag cur_format {};

    format_t const default_format {
        make_color(255, 255, 255, 255)
      , 0
      , 0
    };

    int pos = 0;

    while (it != end) {
        auto cp = *it;

        switch (cp) {
        case token::escape :
            if (auto const result = parse_escape(it, end)) {
                cp = *result;
            } else {
                BK_TODO_FAIL();
                continue;
            }

            break;
        case token::tag_open :
            cur_format = parse_tag(it, end);
            if (cur_format.type == format_tag::tag_type::tag_color) {
                if (cur_format.state == format_tag::tag_state::type_begin) {
                    format_.emplace_back(format_t {make_color(255, 0, 0, 255), pos, pos});
                } else if (cur_format.state == format_tag::tag_state::type_end) {
                    format_.back().end = pos;
                }
            } else {
                BK_TODO_FAIL();
            }

            continue;
        default :
            ++it;
            ++pos;

            break;
        }

        cur_record.codepoint = cp;
        data_.push_back(cur_record);
    }

    auto const line_gap = face.line_gap();
    auto       x        = 0;
    auto       y        = face.ascender();

    //--------------------------------------------------------------------------
    auto const next_line = [&] {
        x  = 0;
        y += line_gap;
    };

    //--------------------------------------------------------------------------
    auto const next_tab = [&] {
        auto const rem = x % tab_size;
        auto const tab = tab_size - rem;
        x += tab;
    };
    //--------------------------------------------------------------------------    
    auto const do_escape = [&](codepoint_t const cp) {
        switch (cp) {
        case '\t' : next_tab();  return true;
        case '\n' : next_line(); return true;
        }

        return false;
    };

    //--------------------------------------------------------------------------    
    auto left = unicode::codepoint {};
    for (auto& rec : data_) {
        if (do_escape(rec.codepoint)) {
            continue;
        }

        auto const cp = unicode::codepoint {rec.codepoint};

        auto const metrics = face.metrics(left, cp);
        left = cp;

        if ((max_w != unlimited) && x > max_w) {
            next_line();
        }

        if ((max_h != unlimited) && y > max_h) {
            break;
        }

        auto& p = rec.position;

        p.x = static_cast<int16_t>(x + metrics.left);
        p.y = static_cast<int16_t>(y - metrics.top);

        actual_w_ = std::max(actual_w_, static_cast<int16_t>(p.x + metrics.width));
        actual_h_ = std::max(actual_h_, static_cast<int16_t>(p.y + metrics.height));

        x += metrics.advance_x;
        y -= metrics.advance_y;
    }
}
Example #4
0
inline bool
operator !=(const string_ref& x, const string_ref& y) {
    return (x.size() != y.size()) ||
           (::memcmp(x.begin(), y.begin(), x.size()) != 0);
}
Example #5
0
inline bool
operator ==(const string_ref& x, const string_ref& y) {
    return (x.size() == y.size()) &&
           (::memcmp(x.begin(), y.begin(), x.size()) == 0);
}
Example #6
0
template <size_t N> inline bool
operator ==(const string_ref& x, const char (& y)[N]) {
    return (x.size() == N - 1) && (::memcmp(x.begin(), y, N - 1) == 0);
}
Example #7
0
void
basic_url_base::parse_impl (string_ref s, boost::system::error_code& ec)
{
    joyent::http_parser_url p;

    value_type const* const data (s.data());
    
    int const error (joyent::http_parser_parse_url (
        data, s.size(), false, &p));

    if (error)
    {
        ec = boost::system::error_code (
            boost::system::errc::invalid_argument,
            boost::system::generic_category());
        return;
    }

    if ((p.field_set & (1<<joyent::UF_SCHEMA)) != 0)
    {
        m_scheme = string_ref (
            data + p.field_data [joyent::UF_SCHEMA].off,
                p.field_data [joyent::UF_SCHEMA].len);
    }
    else
    {
        m_scheme = string_ref {};
    }

    if ((p.field_set & (1<<joyent::UF_HOST)) != 0)
    {
        m_host = string_ref (
            data + p.field_data [joyent::UF_HOST].off,
                p.field_data [joyent::UF_HOST].len);
    }
    else
    {
        m_host = string_ref {};
    }

    if ((p.field_set & (1<<joyent::UF_PORT)) != 0)
    {
        m_port = p.port;
        m_port_string = string_ref (
            data + p.field_data [joyent::UF_PORT].off,
                p.field_data [joyent::UF_PORT].len);
    }
    else
    {
        m_port = 0;
        m_port_string = string_ref {};
    }

    if ((p.field_set & (1<<joyent::UF_PATH)) != 0)
    {
        m_path = string_ref (
            data + p.field_data [joyent::UF_PATH].off,
                p.field_data [joyent::UF_PATH].len);
    }
    else
    {
        m_path = string_ref {};
    }

    if ((p.field_set & (1<<joyent::UF_QUERY)) != 0)
    {
        m_query = string_ref (
            data + p.field_data [joyent::UF_QUERY].off,
                p.field_data [joyent::UF_QUERY].len);
    }
    else
    {
        m_query = string_ref {};
    }

    if ((p.field_set & (1<<joyent::UF_FRAGMENT)) != 0)
    {
        m_fragment = string_ref (
            data + p.field_data [joyent::UF_FRAGMENT].off,
                p.field_data [joyent::UF_FRAGMENT].len);
    }
    else
    {
        m_fragment = string_ref {};
    }

    if ((p.field_set & (1<<joyent::UF_USERINFO)) != 0)
    {
        m_userinfo = string_ref (
            data + p.field_data [joyent::UF_USERINFO].off,
                p.field_data [joyent::UF_USERINFO].len);
    }
    else
    {
        m_userinfo = string_ref {};
    }
}
Example #8
0
void
hstcpcli::request_buf_exec_generic(size_t pst_id, const string_ref& op,
  const string_ref *kvs, size_t kvslen, uint32_t limit, uint32_t skip,
  const string_ref& mod_op, const string_ref *mvs, size_t mvslen,
  const hstcpcli_filter *fils, size_t filslen, int invalues_keypart,
  const string_ref *invalues, size_t invalueslen)
{
  if (num_req_sent > 0 || num_req_rcvd > 0) {
    close();
    set_error(-1, "request_buf_exec_generic: protocol out of sync");
    return;
  }
  append_uint32(writebuf, pst_id); // FIXME size_t ?
  writebuf.append_literal("\t");
  writebuf.append(op.begin(), op.end());
  writebuf.append_literal("\t");
  append_uint32(writebuf, kvslen); // FIXME size_t ?
  for (size_t i = 0; i < kvslen; ++i) {
    const string_ref& kv = kvs[i];
    append_delim_value(writebuf, kv.begin(), kv.end());
  }
  if (limit != 0 || skip != 0 || invalues_keypart >= 0 ||
    mod_op.size() != 0 || filslen != 0) {
    /* has more option */
    writebuf.append_literal("\t");
    append_uint32(writebuf, limit); // FIXME size_t ?
    if (skip != 0 || invalues_keypart >= 0 ||
      mod_op.size() != 0 || filslen != 0) {
      writebuf.append_literal("\t");
      append_uint32(writebuf, skip); // FIXME size_t ?
    }
    if (invalues_keypart >= 0) {
      writebuf.append_literal("\t@\t");
      append_uint32(writebuf, invalues_keypart);
      writebuf.append_literal("\t");
      append_uint32(writebuf, invalueslen);
      for (size_t i = 0; i < invalueslen; ++i) {
	const string_ref& s = invalues[i];
	append_delim_value(writebuf, s.begin(), s.end());
      }
    }
    for (size_t i = 0; i < filslen; ++i) {
      const hstcpcli_filter& f = fils[i];
      writebuf.append_literal("\t");
      writebuf.append(f.filter_type.begin(), f.filter_type.end());
      writebuf.append_literal("\t");
      writebuf.append(f.op.begin(), f.op.end());
      writebuf.append_literal("\t");
      append_uint32(writebuf, f.ff_offset);
      append_delim_value(writebuf, f.val.begin(), f.val.end());
    }
    if (mod_op.size() != 0) {
      writebuf.append_literal("\t");
      writebuf.append(mod_op.begin(), mod_op.end());
      for (size_t i = 0; i < mvslen; ++i) {
	const string_ref& mv = mvs[i];
	append_delim_value(writebuf, mv.begin(), mv.end());
      }
    }
  }
  writebuf.append_literal("\n");
  ++num_req_bufd;
}
Example #9
0
void
basic_url_base::parse_impl (string_ref s, boost::system::error_code& ec)
{
    joyent::http_parser_url p;

    value_type const* const data (s.data());
    
    int const error (joyent::http_parser_parse_url (
        data, s.size(), false, &p));

    if (error)
    {
        ec = boost::system::error_code (
            boost::system::errc::invalid_argument,
            boost::system::generic_category());
        return;
    }

    if ((p.field_set & (1<<joyent::uf_schema)) != 0)
    {
        m_scheme = string_ref (
            data + p.field_data [joyent::uf_schema].off,
                p.field_data [joyent::uf_schema].len);
    }
    else
    {
        m_scheme = string_ref {};
    }

    if ((p.field_set & (1<<joyent::uf_host)) != 0)
    {
        m_host = string_ref (
            data + p.field_data [joyent::uf_host].off,
                p.field_data [joyent::uf_host].len);
    }
    else
    {
        m_host = string_ref {};
    }

    if ((p.field_set & (1<<joyent::uf_port)) != 0)
    {
        m_port = p.port;
        m_port_string = string_ref (
            data + p.field_data [joyent::uf_port].off,
                p.field_data [joyent::uf_port].len);
    }
    else
    {
        m_port = 0;
        m_port_string = string_ref {};
    }

    if ((p.field_set & (1<<joyent::uf_path)) != 0)
    {
        m_path = string_ref (
            data + p.field_data [joyent::uf_path].off,
                p.field_data [joyent::uf_path].len);
    }
    else
    {
        m_path = string_ref {};
    }

    if ((p.field_set & (1<<joyent::uf_query)) != 0)
    {
        m_query = string_ref (
            data + p.field_data [joyent::uf_query].off,
                p.field_data [joyent::uf_query].len);
    }
    else
    {
        m_query = string_ref {};
    }

    if ((p.field_set & (1<<joyent::uf_fragment)) != 0)
    {
        m_fragment = string_ref (
            data + p.field_data [joyent::uf_fragment].off,
                p.field_data [joyent::uf_fragment].len);
    }
    else
    {
        m_fragment = string_ref {};
    }

    if ((p.field_set & (1<<joyent::uf_userinfo)) != 0)
    {
        m_userinfo = string_ref (
            data + p.field_data [joyent::uf_userinfo].off,
                p.field_data [joyent::uf_userinfo].len);
    }
    else
    {
        m_userinfo = string_ref {};
    }
}