本文整理汇总了C++中string_view::data方法的典型用法代码示例。如果您正苦于以下问题:C++ string_view::data方法的具体用法?C++ string_view::data怎么用?C++ string_view::data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类string_view
的用法示例。
在下文中一共展示了string_view::data方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: inputStream
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;
}
示例2: 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();
}
示例3: reload
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);
}
示例4: make_pair
// 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);
}
示例5: 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
}
示例6: test_strview_basics
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());
}
}
示例7:
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())));
}
示例8: 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';
}
示例9: copy_string
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);
}
示例10: path_match
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;
}
}
示例11:
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;
}
示例12: readFile
void ArticleCitations::readFile(string_view fileName)
{
// Open the file and check for failure.
ifstream inputFile(fileName.data());
if (inputFile.fail()) {
throw invalid_argument("Unable to open file");
}
// Read the article author, title, etc. line.
getline(inputFile, mArticle);
// Skip the white space before the citations start.
inputFile >> ws;
int count = 0;
// Save the current position so we can return to it.
streampos citationsStart = inputFile.tellg();
// First count the number of citations.
cout << "readFile(): counting number of citations" << endl;
while (!inputFile.eof()) {
// Skip white space before the next entry.
inputFile >> ws;
string temp;
getline(inputFile, temp);
if (!temp.empty()) {
cout << "Citation " << count << ": " << temp << endl;
count++;
}
}
cout << "Found " << count << " citations" << endl;
cout << "readFile(): reading citations" << endl;
if (count != 0) {
// Allocate an array of strings to store the citations.
mCitations = new string[count];
mNumCitations = count;
// Seek back to the start of the citations.
inputFile.seekg(citationsStart);
// Read each citation and store it in the new array.
for (count = 0; count < mNumCitations; count++) {
string temp;
getline(inputFile, temp);
if (!temp.empty()) {
cout << temp << endl;
mCitations[count] = temp;
}
}
} else {
mNumCitations = -1;
}
cout << "readFile(): finished" << endl;
}
示例13: showSplash
// -----------------------------------------------------------------------------
// 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);
}
示例14: inputStream
vector<int> readIntegerFile(string_view fileName)
{
ifstream inputStream(fileName.data());
if (inputStream.fail()) {
// We failed to open the file: throw an exception
throw exception();
}
// Read the integers one-by-one and add them to a vector
vector<int> integers;
int temp;
while (inputStream >> temp) {
integers.push_back(temp);
}
return integers;
}
示例15: switch
//===========================================================================
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);
}