Exemplo n.º 1
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())));
}
Exemplo n.º 2
0
int main()
{
    using string_view    = std::experimental::string_view;
    using u16string_view = std::experimental::u16string_view;
    using u32string_view = std::experimental::u32string_view;
    using wstring_view   = std::experimental::wstring_view;

    test(string_view   {});
    test(string_view   { "123"});
    test(u16string_view{});
    test(u16string_view{u"123"});
    test(u32string_view{});
    test(u32string_view{U"123"});
    test(wstring_view  {});
    test(wstring_view  {L"123"});

    {
    constexpr string_view       sv { "123", 3 };
    constexpr u16string_view u16sv {u"123", 3 };
    constexpr u32string_view u32sv {U"123", 3 };
    constexpr wstring_view     wsv {L"123", 3 };
    
    static_assert (    sv.begin() !=    sv.end(), "" );
    static_assert ( u16sv.begin() != u16sv.end(), "" );
    static_assert ( u32sv.begin() != u32sv.end(), "" );
    static_assert (   wsv.begin() !=   wsv.end(), "" );

    static_assert (    sv.begin() !=    sv.cend(), "" );
    static_assert ( u16sv.begin() != u16sv.cend(), "" );
    static_assert ( u32sv.begin() != u32sv.cend(), "" );
    static_assert (   wsv.begin() !=   wsv.cend(), "" );
    }
}
Exemplo n.º 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;
}
string transcription::to_rna(string_view strand)
{
    string result;
    transform(strand.begin(), strand.end(), back_inserter(result),
        static_cast<char (*)(char)>(to_rna));
    return result;
}
Exemplo n.º 5
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);
	}
/// Deconstruct a "URI" string into the "fllename" part (returned) and turn
/// the "query" part into a series of pairs of id and value. For example,
///     deconstruct_uri("foo.tif?bar=1&blah=\"hello world\"", args) 
/// would be expected to return "foo.tif" and *args would contain two
/// pairs: ("foo","1") and ("bar","\"hello world\"").
static string_view
deconstruct_uri (string_view uri,
                 std::vector<std::pair<string_view,string_view> > *args=NULL)
{
    if (args)
        args->clear ();
    size_t arg_start = uri.find ('?');
    if (arg_start == string_view::npos)
        return uri;
    string_view argstring = uri.substr (arg_start+1);
    string_view filename = uri.substr (0, arg_start);
    if (! args)
        return filename;
    while (! argstring.empty()) {
        string_view id = Strutil::parse_until (argstring, "=&");
        string_view value;
        if (! id.size())
            break;
        if (! Strutil::parse_char (argstring, '=') || argstring.empty())
            break;
        if (argstring[0] == '\"')
            Strutil::parse_string (argstring, value, true, Strutil::KeepQuotes);
        else
            value = Strutil::parse_until (argstring, "&\t\r\n");
        args->push_back (std::make_pair(id, value));
        Strutil::parse_char (argstring, '&');
    }
    return filename;
}
Exemplo n.º 7
0
bool
ColorConfig::reset (string_view filename)
{
    bool ok = true;
    delete m_impl;

    m_impl = new ColorConfig::Impl;
#ifdef USE_OCIO
    OCIO::SetLoggingLevel (OCIO::LOGGING_LEVEL_NONE);
    try {
        if (filename.empty()) {
            getImpl()->config_ = OCIO::GetCurrentConfig();
        } else {
            getImpl()->config_ = OCIO::Config::CreateFromFile (filename.c_str());
        }
    }
    catch(OCIO::Exception &e) {
        getImpl()->error_ = e.what();
        ok = false;
    }
    catch(...) {
        getImpl()->error_ = "An unknown error occurred in OpenColorIO creating the config";
        ok = false;
    }
#endif

    getImpl()->inventory ();

    // If we populated our own, remove any errors.
    if (getNumColorSpaces() && !getImpl()->error_.empty())
        getImpl()->error_.clear();

    return ok;
}
Exemplo n.º 8
0
	static bool shouldConsiderUnaryLiteral(string_view& stream, Location& pos)
	{
		// check the previous token
		bool should = (prevType != TokenType::Invalid && prevID == pos.fileID && (
			prevType != TokenType::RParen &&
			prevType != TokenType::RSquare &&
			prevType != TokenType::Identifier &&
			prevType != TokenType::Number &&
			prevType != TokenType::Dollar &&
			prevType != TokenType::StringLiteral
		));

		if(!should) return false;

		// check if the current char is a + or -
		if(stream.length() == 0) return false;
		if(stream[0] != '+' && stream[0] != '-') return false;

		// check if there's only spaces between this and the number itself
		for(size_t i = 1; i < stream.length(); i++)
		{
			if(isdigit(stream[i])) return true;
			else if(stream[i] != ' ') return false;
		}

		return false;
	}
vector<int> readIntegerFile(string_view fileName)
{
	ifstream inputStream(fileName.data());
	if (inputStream.fail()) {
		// We failed to open the file: throw an exception
		const string error = "Unable to open file "s + fileName.data();
		throw invalid_argument(error);
	}

	// Read the integers one-by-one and add them to a vector
	vector<int> integers;
	int temp;
	while (inputStream >> temp) {
		integers.push_back(temp);
	}

	if (!inputStream.eof()) {
		// We did not reach the end-of-file.
		// This means that some error occurred while reading the file.
		// Throw an exception.
		const string error = "Unable to read file "s + fileName.data();
		throw runtime_error(error);
	}

	return integers;
}
Exemplo n.º 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
}
Exemplo n.º 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();
	}
Exemplo n.º 12
0
void Request::add_line_to_multiline_header(const string_view& line,
                                           const string_view& header)
{
    auto non_whitespace_pos = line.find_first_not_of("\t ");
    if (non_whitespace_pos != line.end())
        if (!header.empty())
            headers_[header.str()] += line.substr(non_whitespace_pos).str();
}
Exemplo n.º 13
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';
}
Exemplo n.º 14
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] == '.'));
}
Exemplo n.º 15
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();
}
Exemplo n.º 16
0
	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);
	}
Exemplo n.º 17
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();
}
Exemplo n.º 18
0
size_t
TypeDesc::fromstring(string_view typestring)
{
    *this            = TypeDesc::UNKNOWN;
    string_view orig = typestring;
    if (typestring.empty()) {
        return 0;
    }

    // The first "word" should be a type name.
    string_view type = Strutil::parse_identifier(typestring);

    // Check the scalar types in our table above
    TypeDesc t;
    for (int i = 0; i < LASTBASE; ++i) {
        if (type == basetype_name[i]) {
            t.basetype = i;
            break;
        }
    }

    // Some special case names for aggregates
    if (t.basetype != UNKNOWN) {
        // already solved
    } else if (type == "color")
        t = TypeColor;
    else if (type == "point")
        t = TypePoint;
    else if (type == "vector")
        t = TypeVector;
    else if (type == "normal")
        t = TypeNormal;
    else if (type == "matrix33")
        t = TypeMatrix33;
    else if (type == "matrix" || type == "matrix44")
        t = TypeMatrix44;
    else if (type == "timecode")
        t = TypeTimeCode;
    else if (type == "rational")
        t = TypeRational;
    else {
        return 0;  // unknown
    }

    // Is there an array length following the type name?
    if (Strutil::parse_char(typestring, '[')) {
        int arraylen = -1;
        Strutil::parse_int(typestring, arraylen);
        if (!Strutil::parse_char(typestring, ']'))
            return 0;  // malformed
        t.arraylen = arraylen;
    }

    *this = t;
    return orig.length() - typestring.length();
}
Exemplo n.º 19
0
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;
}
Exemplo n.º 20
0
    bool ParserKeyword::validDeckName( const string_view& name) {

        if( !validNameStart( name ) )
            return false;

        const auto valid = []( char c ) {
            return std::isalnum( c ) || c == '-' || c == '_' || c == '+';
        };

        return std::all_of( name.begin() + 1, name.end(), valid );
    }
Exemplo n.º 21
0
pair<string_view, string_view> splitPair(string_view s, char delim) noexcept
{
  const auto pos = s.find_first_of(delim);
  string_view key, val;
  if (pos == string_view::npos) {
    key = s;
  } else {
    key = s.substr(0, pos);
    val = s.substr(pos + 1);
  }
  return {key, val};
}
Exemplo n.º 22
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;
	}
Exemplo n.º 23
0
    bool ParserKeyword::matches(const string_view& name ) const {
        if (!validDeckName(name ))
            return false;

        else if( m_deckNames.count( name.string() ) )
            return true;

        else if (hasMatchRegex()) {
            return boost::regex_match( name.begin(), name.end(), m_matchRegex);
        }

        return false;
    }
Exemplo n.º 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);
}
Exemplo n.º 25
0
// try to replace volume GUID (if present) with drive letter
// used by ConvertNameToReal() only
static string TryConvertVolumeGuidToDrivePath(string_view const Path, const string_view AbsPath = {})
{
	string Result(Path);
	size_t DirectoryOffset;
	if (ParsePath(Path, &DirectoryOffset) == root_type::volume)
	{
		if (imports.GetVolumePathNamesForVolumeNameW)
		{
			string VolumePathNames;
			if (os::fs::GetVolumePathNamesForVolumeName(ExtractPathRoot(Path), VolumePathNames))
			{
				for(const auto& i: enum_substrings(VolumePathNames.c_str()))
				{
					if (!AbsPath.empty() && starts_with_icase(AbsPath, i))
						return string(i);

					if (IsRootPath(i))
					{
						Result.replace(0, DirectoryOffset, i.data(), i.size());
						break;
					}
				}
			}

			if (!AbsPath.empty())
				Result.clear();
		}

		else if (!AbsPath.empty())
			Result.clear();

		else
		{
			string strVolumeGuid;
			const os::fs::enum_drives Enumerator(os::fs::get_logical_drives());
			const auto ItemIterator = std::find_if(ALL_CONST_RANGE(Enumerator), [&](const auto& i)
			{
				return os::fs::GetVolumeNameForVolumeMountPoint(os::fs::get_root_directory(i), strVolumeGuid) && starts_with(Path, string_view(strVolumeGuid).substr(0, DirectoryOffset));
			});
			if (ItemIterator != Enumerator.cend())
			{
				Result.replace(0, DirectoryOffset, os::fs::get_drive(*ItemIterator));
			}
		}
	}

	else if (!AbsPath.empty())
		Result.clear();

	return Result;
}
Exemplo n.º 26
0
    int reload(lua_State* L, const Data& chunk, const string_view& name)
    {
        lua_getglobal(L, "package");
        lua_pushliteral(L, "loaded");
        lua_rawget(L, -2);

        R_ASSERT(lua_istable(L, -1), "Missing control table 'package.loaded'");

        lua_pushlstring(L, name.data(), name.length());
        lua_pushnil(L);
        lua_rawset(L, -3);
        lua_pop(L, 2);
        return load(L, chunk, name.data(), true);
    }
Exemplo n.º 27
0
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;
}
Exemplo n.º 28
0
bool
ImageBufAlgo::ociofiletransform (ImageBuf &dst, const ImageBuf &src,
                        string_view name, bool inverse, bool unpremult,
                        ColorConfig *colorconfig, ROI roi, int nthreads)
{
    if (name.empty()) {
        dst.error ("Unknown filetransform name");
        return false;
    }
    ColorProcessor *processor = NULL;
    {
        spin_lock lock (colorconfig_mutex);
        if (! colorconfig)
            colorconfig = default_colorconfig.get();
        if (! colorconfig)
            default_colorconfig.reset (colorconfig = new ColorConfig);
        processor = colorconfig->createFileTransform (name, inverse);
        if (! processor) {
            if (colorconfig->error())
                dst.error ("%s", colorconfig->geterror());
            else
                dst.error ("Could not construct the color transform");
            return false;
        }
    }
    bool ok = colorconvert (dst, src, processor, unpremult, roi, nthreads);
    if (ok)
        dst.specmod().attribute ("oiio:ColorSpace", name);
    {
        spin_lock lock (colorconfig_mutex);
        colorconfig->deleteColorProcessor (processor);
    }
    return ok;
}
Exemplo n.º 29
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();
}
Exemplo n.º 30
-1
void test_strview_compare(const string_view& a, const string_view& b) {

    std::string as = a.to_string();
    std::string bs = b.to_string();

    auto sg = [](int x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); };
    int c = as.compare(bs);

    ASSERT_EQ(sg(c), sg(a.compare(b)));
    ASSERT_EQ(sg(c), sg(a.compare(bs.c_str())));

    ASSERT_EQ(c == 0, a == b);
    ASSERT_EQ(c != 0, a != b);
    ASSERT_EQ(c <  0, a <  b);
    ASSERT_EQ(c <= 0, a <= b);
    ASSERT_EQ(c >  0, a >  b);
    ASSERT_EQ(c >= 0, a >= b);

    ASSERT_EQ((-c) == 0, b == a);
    ASSERT_EQ((-c) != 0, b != a);
    ASSERT_EQ((-c) <  0, b <  a);
    ASSERT_EQ((-c) <= 0, b <= a);
    ASSERT_EQ((-c) >  0, b >  a);
    ASSERT_EQ((-c) >= 0, b >= a);
}