Beispiel #1
0
	// returns the unicode codepoint and the number of bytes of the utf8 sequence
	// that was parsed. The codepoint is -1 if it's invalid
	std::pair<std::int32_t, int> parse_utf8_codepoint(string_view str)
	{
		int const sequence_len = trailingBytesForUTF8[static_cast<std::uint8_t>(str[0])] + 1;
		if (sequence_len > int(str.size())) return std::make_pair(-1, static_cast<int>(str.size()));

		if (sequence_len > 4)
		{
			return std::make_pair(-1, sequence_len);
		}

		if (!isLegalUTF8(reinterpret_cast<UTF8 const*>(str.data()), sequence_len))
		{
			return std::make_pair(-1, sequence_len);
		}

		std::uint32_t ch = 0;
		for (int i = 0; i < sequence_len; ++i)
		{
			ch <<= 6;
			ch += static_cast<std::uint8_t>(str[static_cast<std::size_t>(i)]);
		}
		ch -= offsetsFromUTF8[sequence_len-1];

		if (ch > 0x7fffffff)
		{
			return std::make_pair(-1, sequence_len);
		}

		return std::make_pair(static_cast<std::int32_t>(ch), sequence_len);
	}
Beispiel #2
0
void
write_buffer(DynamicBuffer& b, string_view s)
{
    b.commit(boost::asio::buffer_copy(
        b.prepare(s.size()), boost::asio::buffer(
            s.data(), s.size())));
}
Beispiel #3
0
//===========================================================================
bool ConsoleReader::onFileRead(
    size_t * bytesRead,
    string_view data,
    bool more,
    int64_t offset,
    FileHandle f
) {
    *bytesRead = data.size();
    auto bytes = (int) data.size();
    socketWrite(&s_socket, move(m_buffer), bytes);

    // stop reading (return false) so we can get a new buffer
    if (m_input) {
        if (m_isFile) {
            if (!bytes || (size_t) offset == fileSize(f)) {
                fileClose(m_input);
                consoleResetStdin();
                init();
            }
        } else {
            if (!bytes) {
                m_buffer.reset();
                return false;
            }
        }
        if (m_suspended) {
            m_offset = offset;
        } else {
            read(offset);
        }
    } else {
        m_buffer.reset();
    }
    return false;
}
Beispiel #4
0
// Is name the same as suffix, or does it end in ".suffix"?
inline bool
is_or_endswithdot (string_view name, string_view suffix)
{
    return (Strutil::iequals (name, suffix) ||
            (name.size() > suffix.size() &&
             Strutil::iends_with (name, suffix) &&
             name[name.size()-suffix.size()-1] == '.'));
}
Beispiel #5
0
	std::wstring utf8_wchar(string_view utf8, error_code& ec)
	{
		// allocate space for worst-case
		std::wstring wide;
		wide.resize(utf8.size());
		char const* src_start = utf8.data();
		utf8_errors::error_code_enum const ret = convert_to_wide<sizeof(wchar_t)>::convert(
			&src_start, src_start + utf8.size(), wide);
		if (ret != utf8_errors::error_code_enum::conversion_ok)
			ec = make_error_code(ret);
		return wide;
	}
bool
ShadingSystemImpl::LoadMemoryCompiledShader (string_view shadername,
                                             string_view buffer)
{
    if (! shadername.size()) {
        error ("Attempt to load shader with empty name \"\".");
        return false;
    }
    if (! buffer.size()) {
        error ("Attempt to load shader \"%s\" with empty OSO data.", shadername);
        return false;
    }

    ustring name (shadername);
    lock_guard guard (m_mutex);  // Thread safety
    ShaderNameMap::const_iterator found = m_shader_masters.find (name);
    if (found != m_shader_masters.end()) {
        if (debug())
            info ("Preload shader %s already exists in shader_masters", name.c_str());
        return false;
    }

    // Not found in the map
    OSOReaderToMaster reader (*this);
    OIIO::Timer timer;
    bool ok = reader.parse_memory (buffer);
    ShaderMaster::ref r = ok ? reader.master() : nullptr;
    m_shader_masters[name] = r;
    double loadtime = timer();
    {
        spin_lock lock (m_stat_mutex);
        m_stat_master_load_time += loadtime;
    }
    if (ok) {
        ++m_stat_shaders_loaded;
        info ("Loaded \"%s\" (took %s)", shadername,
              Strutil::timeintervalformat(loadtime, 2).c_str());
        ASSERT (r);
        r->resolve_syms ();
        // if (debug()) {
        //     std::string s = r->print ();
        //     if (s.length())
        //         info ("%s", s.c_str());
        // }
    } else {
        error ("Unable to parse preloaded shader \"%s\"", shadername);
    }

    return true;
}
Beispiel #7
0
size_t GetMountPointLen(string_view const abs_path, string_view const drive_root)
{
	if (starts_with_icase(abs_path, drive_root))
		return drive_root.size();

	size_t dir_offset = 0;
	if (ParsePath(abs_path, &dir_offset) == root_type::volume)
		return dir_offset;

	string vol_guid(drive_root);
	switch (ParsePath(drive_root))
	{
	case root_type::volume:
		break;
	case root_type::drive_letter:
		if (os::fs::GetVolumeNameForVolumeMountPoint(null_terminated(drive_root).c_str(), vol_guid))
			break;
		[[fallthrough]];
	default:
		return 0;
	}

	string mount_point = TryConvertVolumeGuidToDrivePath(vol_guid, abs_path);
	return mount_point.size();
}
Beispiel #8
0
void test_strview_basics(const string_view& sv, const char *p, size_t n) {
    ASSERT_EQ(p, sv.data());
    ASSERT_EQ(n, sv.size());
    ASSERT_EQ(n, sv.length());
    ASSERT_EQ((n == 0), sv.empty());

    ASSERT_EQ(p, sv.cbegin());
    ASSERT_EQ(p, sv.begin());
    ASSERT_EQ(p + n, sv.cend());
    ASSERT_EQ(p + n, sv.end());

    using reviter_t = std::reverse_iterator<string_view::const_iterator>;

    ASSERT_EQ(reviter_t(sv.end()),    sv.rbegin());
    ASSERT_EQ(reviter_t(sv.begin()),  sv.rend());
    ASSERT_EQ(reviter_t(sv.cend()),   sv.crbegin());
    ASSERT_EQ(reviter_t(sv.cbegin()), sv.crend());

    for (size_t i = 0; i < n; ++i) {
        ASSERT_EQ(p[i], sv[i]);
        ASSERT_EQ(p[i], sv.at(i));
    }
    ASSERT_THROW(sv.at(n), std::out_of_range);
    ASSERT_THROW(sv.at(string_view::npos), std::out_of_range);

    if (n > 0) {
        ASSERT_EQ(p,         &(sv.front()));
        ASSERT_EQ(p + (n-1), &(sv.back()));
        ASSERT_EQ(p[0],        sv.front());
        ASSERT_EQ(p[n-1],      sv.back());
    }
}
Beispiel #9
0
std::size_t custom_parser<isRequest>::
on_body_impl(string_view body, error_code& ec)
{
    boost::ignore_unused(body);
    ec = {};
    return body.size();
}
Beispiel #10
0
void stdLogger(int level, string_view msg) noexcept
{
  const auto now = UnixClock::now();
  const auto t = UnixClock::to_time_t(now);
  const auto ms = timeToMs(now);

  struct tm tm;
  localtime_r(&t, &tm);

  // The following format has an upper-bound of 42 characters:
  // "%b %d %H:%M:%S.%03d %-7s [%d]: "
  //
  // Example:
  // Mar 14 00:00:00.000 WARNING [0123456789]: msg...
  // <---------------------------------------->
  char head[42 + 1];
  size_t hlen = strftime(head, sizeof(head), "%b %d %H:%M:%S", &tm);
  hlen += sprintf(head + hlen, ".%03d %-7s [%d]: ", static_cast<int>(ms % 1000), logLabel(level),
                  static_cast<int>(getpid()));
  char tail = '\n';
  iovec iov[] = {
    {head, hlen}, //
    {const_cast<char*>(msg.data()), msg.size()}, //
    {&tail, 1} //
  };

  int fd{level > LogWarning ? STDOUT_FILENO : STDERR_FILENO};
// Best effort given that this is the logger.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
  writev(fd, iov, sizeof(iov) / sizeof(iov[0]));
#pragma GCC diagnostic pop
}
Beispiel #11
0
	bdecode_node bdecode_node::dict_find(string_view key) const
	{
		TORRENT_ASSERT(type() == dict_t);

		bdecode_token const* tokens = m_root_tokens;

		// this is the first item
		int token = m_token_idx + 1;

		while (tokens[token].type != bdecode_token::end)
		{
			bdecode_token const& t = tokens[token];
			TORRENT_ASSERT(t.type == bdecode_token::string);
			int const size = m_root_tokens[token + 1].offset - t.offset - t.start_offset();
			if (int(key.size()) == size
				&& std::equal(key.data(), key.data() + size, m_buffer
					+ t.offset + t.start_offset()))
			{
				// skip key
				token += t.next_item;
				TORRENT_ASSERT(tokens[token].type != bdecode_token::end);

				return bdecode_node(tokens, m_buffer, m_buffer_size, token);
			}

			// skip key
			token += t.next_item;
			TORRENT_ASSERT(tokens[token].type != bdecode_token::end);

			// skip value
			token += tokens[token].next_item;
		}

		return bdecode_node();
	}
Beispiel #12
0
bool is_brace_expansion(string_view s)
{
  int brace_count = 0;
  int arr_count = 0;
  bool b = false;

  for (std::size_t i = 0; i < s.size(); i++)
  {
    if (s[i] == '{')
    {
      b = true;
      brace_count++;
    }
    else if (s[i] == '[')
    {
      b = true;
      arr_count++;
    }
    else if (s[i] == '}')
    {
      brace_count--;
      if (brace_count < 0)
        return false;
    }
    else if (s[i] == ']')
    {
      arr_count--;
      if (arr_count < 0)
        return false;
    }
  }

  return b && brace_count == 0 && arr_count == 0;
}
Beispiel #13
0
size_t find_icase(string_view const Str, wchar_t const What, size_t Pos)
{
	if (Pos >= Str.size())
		return Str.npos;

	const auto It = std::find_if(Str.cbegin() + Pos, Str.cend(), [&](wchar_t const Char) { return equal_icase_t{}(What, Char); });
	return It == Str.cend() ? Str.npos : It - Str.cbegin();
}
Beispiel #14
0
//===========================================================================
Count::Count(string_view path) {
    auto * base = path.data();
    auto * ptr = base;
    auto * eptr = ptr + path.size();
    char const * colon = nullptr;    // first ':'
    char const * slash = nullptr;    // last '/' or '\'
    char const * dot = nullptr;      // last '.'
    for (; ptr != eptr; ++ptr) {
        switch(*ptr) {
        case '.':
            dot = ptr;
            break;
        case '/':
        case '\\':
            slash = ptr;
            break;
        case ':':
            if (!colon)
                colon = ptr;
            break;
        }
    }

    if (!slash) {
        m_rootLen = colon ? unsigned(colon - base + 1) : 0;
        m_dirLen = 0;
    } else {
        m_rootLen = (colon && colon < slash) ? unsigned(colon - base + 1) : 0;
        m_dirLen = unsigned(slash - base + 1 - m_rootLen);
    }
    if (!dot || unsigned(dot - base) <= m_rootLen + m_dirLen) {
        m_stemLen = unsigned(path.size() - m_rootLen - m_dirLen);
        m_extLen = 0;
    } else {
        m_stemLen = unsigned(dot - base - m_rootLen - m_dirLen);
        m_extLen = unsigned(eptr - dot);
        if (m_extLen == 1 && m_stemLen == 1 && dot[-1] == '.') {
            m_stemLen = 2;
            m_extLen = 0;
        } else if (!m_stemLen) {
            m_stemLen = m_extLen;
            m_extLen = 0;
        }
    }
    assert(m_extLen == path.size() - m_rootLen - m_dirLen - m_stemLen);
}
	allocation_slot stack_allocator::copy_string(string_view str)
	{
		int const ret = int(m_storage.size());
		m_storage.resize(ret + numeric_cast<int>(str.size()) + 1);
		std::memcpy(&m_storage[ret], str.data(), str.size());
		m_storage[ret + int(str.length())] = '\0';
		return allocation_slot(ret);
	}
Beispiel #16
0
size_t find_icase(string_view const Str, string_view const What, size_t Pos)
{
	if (Pos >= Str.size())
		return Str.npos;

	const auto It = std::search(Str.cbegin() + Pos, Str.cend(), ALL_CONST_RANGE(What), equal_icase_t{});
	return It == Str.cend()? Str.npos : It - Str.cbegin();
}
Beispiel #17
0
Exception::Exception(string_view what) noexcept
{
    const auto len = min(what.size(), MaxErrMsg);
    if (len > 0) {
        memcpy(what_, what.data(), len);
    }
    what_[len] = '\0';
}
Beispiel #18
0
std::size_t custom_parser<isRequest>::
on_chunk_body_impl(
    std::uint64_t remain,
    string_view body,
    error_code& ec)
{
    boost::ignore_unused(remain);
    ec = {};
    return body.size();
}
Beispiel #19
0
//===========================================================================
static bool matchHost(string_view authority, string_view host) {
    if (!host.size())
        return true;
    if (DnsNameCompare_W(toWstring(authority).data(), toWstring(host).data()))
        return true;

    // TODO: match against wildcard authority

    return false;
}
ShaderGroup::ShaderGroup (string_view name)
{
    m_id = ++(*(atomic_int *)&next_id);
    if (name.size()) {
        m_name = name;
    } else {
        // No name -- make one up using the unique
        m_name = ustring::format ("unnamed_group_%d", m_id);
    }
}
Beispiel #21
0
static lng_line_type parse_lng_line(const string_view str, bool ParseLabels, string_view& Label, string_view& Data)
{
	Label = {};
	Data = {};

	//-- "Text"
	if (starts_with(str, L'"'))
	{
		Data = str.substr(1, str.size() - (ends_with(str, L'"')? 2 : 1));
		return lng_line_type::text;
	}

	//-- //[Label]
	if (ParseLabels)
	{
		const auto Prefix = L"//["sv, Suffix = L"]"sv;
		if (starts_with(str, Prefix) && ends_with(str, Suffix))
		{
			Label = str.substr(Prefix.size(), str.size() - Prefix.size() - Suffix.size());
			return lng_line_type::label;
		}
	}

	//-- MLabel="Text"
	if (ParseLabels && !str.empty() && str.back() == L'"')
	{
		const auto eq_pos = str.find(L'=');
		if (eq_pos != str.npos && std::iswalpha(str[0]))
		{
			const auto Value = trim(str.substr(eq_pos + 1));

			if (starts_with(Value, L'"'))
			{
				Label = trim(str.substr(0, eq_pos));
				Data = Value.substr(1, Value.size() - 2);
				return lng_line_type::both;
			}
		}
	}

	return lng_line_type::none;
}
ShaderMaster::ref
ShadingSystemImpl::loadshader (string_view cname)
{
    if (Strutil::ends_with (cname, ".oso"))
        cname.remove_suffix (4);   // strip superfluous .oso
    if (! cname.size()) {
        error ("Attempt to load shader with empty name \"\".");
        return NULL;
    }
    ++m_stat_shaders_requested;
    ustring name (cname);
    lock_guard guard (m_mutex);  // Thread safety
    ShaderNameMap::const_iterator found = m_shader_masters.find (name);
    if (found != m_shader_masters.end()) {
        // if (debug())
        //     info ("Found %s in shader_masters", name.c_str());
        // Already loaded this shader, return its reference
        return (*found).second;
    }

    // Not found in the map
    OSOReaderToMaster oso (*this);
    std::string filename = OIIO::Filesystem::searchpath_find (name.string() + ".oso",
                                                        m_searchpath_dirs);
    if (filename.empty ()) {
        error ("No .oso file could be found for shader \"%s\"", name.c_str());
        return NULL;
    }
    OIIO::Timer timer;
    bool ok = oso.parse_file (filename);
    ShaderMaster::ref r = ok ? oso.master() : nullptr;
    m_shader_masters[name] = r;
    double loadtime = timer();
    {
        spin_lock lock (m_stat_mutex);
        m_stat_master_load_time += loadtime;
    }
    if (ok) {
        ++m_stat_shaders_loaded;
        info ("Loaded \"%s\" (took %s)", filename.c_str(),
              Strutil::timeintervalformat(loadtime, 2).c_str());
        ASSERT (r);
        r->resolve_syms ();
        // if (debug()) {
        //     std::string s = r->print ();
        //     if (s.length())
        //         info ("%s", s.c_str());
        // }
    } else {
        error ("Unable to read \"%s\"", filename.c_str());
    }

    return r;
}
Beispiel #23
0
int
parse_bits(string_view s)
{
    if(s.size() == 0)
        return -1;
    if(s.size() > 2)
        return -1;
    if(s[0] < '1' || s[0] > '9')
        return -1;
    unsigned i = 0;
    for(auto c : s)
    {
        if(c < '0' || c > '9')
            return -1;
        auto const i0 = i;
        i = 10 * i + (c - '0');
        if(i < i0)
            return -1;
    }
    return static_cast<int>(i);
}
Beispiel #24
0
// -----------------------------------------------------------------------------
// Shows the splash window with [message].
// If [progress] is true, the progress bar is displayed
// -----------------------------------------------------------------------------
void UI::showSplash(string_view message, bool progress, wxWindow* parent)
{
	if (!splash_enabled || !isMainThread())
		return;

	if (!splash_window)
	{
		SplashWindow::init();
		splash_window = std::make_unique<SplashWindow>();
	}

	splash_window->show(wxString{ message.data(), message.size() }, progress, parent);
}
Beispiel #25
0
 void
 doMatrix(string_view s0, F const& f)
 {
     using boost::asio::buffer;
     // parse a single buffer
     {
         auto s = s0;
         error_code ec;
         parser_type<isRequest> p;
         put(buffer(s.data(), s.size()), p, ec);
         if(! BEAST_EXPECTS(! ec, ec.message()))
             return;
         f(p);
     }
     // parse two buffers
     for(auto n = s0.size() - 1; n >= 1; --n)
     {
         auto s = s0;
         error_code ec;
         parser_type<isRequest> p;
         p.eager(true);
         auto used =
             p.put(buffer(s.data(), n), ec);
         s.remove_prefix(used);
         if(ec == error::need_more)
             ec.assign(0, ec.category());
         if(! BEAST_EXPECTS(! ec, ec.message()))
             continue;
         BEAST_EXPECT(! p.is_done());
         used = p.put(
             buffer(s.data(), s.size()), ec);
         s.remove_prefix(used);
         if(! BEAST_EXPECTS(! ec, ec.message()))
             continue;
         BEAST_EXPECT(s.empty());
         if(p.need_eof())
         {
             p.put_eof(ec);
             if(! BEAST_EXPECTS(! ec, ec.message()))
                 continue;
         }
         if(BEAST_EXPECT(p.is_done()))
             f(p);
     }
 }
Beispiel #26
0
 void
 readgrind(string_view s, Pred&& pred)
 {
     using boost::asio::buffer;
     for(std::size_t n = 1; n < s.size() - 1; ++n)
     {
         Parser p;
         error_code ec = test::error::fail_error;
         flat_buffer b;
         test::pipe c{ios_};
         ostream(c.server.buffer) << s;
         c.server.read_size(n);
         read(c.server, b, p, ec);
         if(! BEAST_EXPECTS(! ec, ec.message()))
             continue;
         pred(p);
     }
 }
path_match_result path_match(string_view input, string_view& match_contents)
{
    if (input.length() < 2)
        return path_match_result::invalid;
    
    match_result result;
    token_kind kind;
    std::size_t length;
    
    switch (input.at(0))
    {
    case '.':
        result = match_simple_string(input.data() + 1, input.data() + input.size(), kind, length);
        if (result == match_result::complete)
        {
            match_contents = input.substr(0, length + 1);
            return path_match_result::simple_object;
        }
        else
        {
            return path_match_result::invalid;
        }
    case '[':
        result = attempt_match(input.data() + 1, input.data() + input.length(), kind, length);
        if (result == match_result::complete)
        {
            if (input.length() == length + 1 || input.at(1 + length) != ']')
                return path_match_result::invalid;
            if (kind != token_kind::string && kind != token_kind::number)
                return path_match_result::invalid;
            
            match_contents = input.substr(0, length + 2);
            return path_match_result::brace;
        }
        else
        {
            return path_match_result::invalid;
        }
    default:
        return path_match_result::invalid;
    }
}
Beispiel #28
0
static string ProcessMetasymbols(string_view Str, subst_data& Data)
{
	string Result;
	Result.reserve(Str.size());

	while (!Str.empty())
	{
		if (Str.front() == L'!')
		{
			Str = ProcessMetasymbol(Str, Data, Result);
		}
		else
		{
			Result.push_back(Str.front());
			Str.remove_prefix(1);
		}
	}

	return Result;
}
Beispiel #29
0
static void parse_lng_line(const string_view str, string& label, string& data, bool& have_data)
{
	have_data = false;

	//-- //[Label]
	if (starts_with(str, L"//["sv) && ends_with(str, L"]"sv))
	{
		const auto LabelView = str.substr(3, str.size() - 3 - 1);
		//-- //[Label=0]
		assign(label, LabelView.substr(0, LabelView.find(L'=')));
		return;
	}

	//-- "Text"
	if (starts_with(str, L'"'))
	{
		have_data = true;
		assign(data, str.substr(1));
		if (!data.empty() && data.back() == L'"')
			data.pop_back();
		return;
	}

	//-- MLabel="Text"
	if (!str.empty() && str.back() == L'"')
	{
		const auto eq_pos = str.find(L'=');
		if (eq_pos != string::npos && InRange(L'A', upper(str[0]), L'Z'))
		{
			assign(data, trim(str.substr(eq_pos + 1)));
			if (data.size() > 1 && data[0] == L'"')
			{
				assign(label, trim(str.substr(0, eq_pos)));
				have_data = true;
				data.pop_back();
				data.erase(0, 1);
			}
		}
	}
}
Beispiel #30
0
void sysLogger(int level, string_view msg) noexcept
{
  int prio;
  switch (level) {
  case LogCrit:
    prio = LOG_CRIT;
    break;
  case LogError:
    prio = LOG_ERR;
    break;
  case LogWarning:
    prio = LOG_WARNING;
    break;
  case LogNotice:
    prio = LOG_NOTICE;
    break;
  case LogInfo:
    prio = LOG_INFO;
    break;
  default:
    prio = LOG_DEBUG;
  }
  syslog(prio, "%.*s", static_cast<int>(msg.size()), msg.data());
}