本文整理汇总了C++中string_view类的典型用法代码示例。如果您正苦于以下问题:C++ string_view类的具体用法?C++ string_view怎么用?C++ string_view使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了string_view类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: write_buffer
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())));
}
示例2: main
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(), "" );
}
}
示例3: dict_find
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();
}
示例4: deconstruct_uri
/// 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;
}
示例5: onFileRead
//===========================================================================
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;
}
示例6: parse_utf8_codepoint
// 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);
}
示例7: shouldConsiderUnaryLiteral
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;
}
示例8: test_strview_compare
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);
}
示例9: getImpl
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;
}
示例10: readIntegerFile
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;
}
示例11: stdLogger
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
}
示例12: to_rna
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;
}
示例13: add_line_to_multiline_header
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();
}
示例14: min
Exception::Exception(string_view what) noexcept
{
const auto len = min(what.size(), MaxErrMsg);
if (len > 0) {
memcpy(what_, what.data(), len);
}
what_[len] = '\0';
}
示例15: is_or_endswithdot
// 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] == '.'));
}