本文整理汇总了C++中StringPiece::size方法的典型用法代码示例。如果您正苦于以下问题:C++ StringPiece::size方法的具体用法?C++ StringPiece::size怎么用?C++ StringPiece::size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringPiece
的用法示例。
在下文中一共展示了StringPiece::size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: canonicalize
std::string LogName::canonicalize(StringPiece input) {
std::string cname;
cname.reserve(input.size());
// Ignore trailing category separator characters
size_t end = input.size();
while (end > 0 && isSeparator(input[end - 1])) {
--end;
}
bool ignoreSeparator = true;
for (size_t idx = 0; idx < end; ++idx) {
if (isSeparator(input[idx])) {
if (ignoreSeparator) {
continue;
}
cname.push_back('.');
ignoreSeparator = true;
} else {
cname.push_back(input[idx]);
ignoreSeparator = false;
}
}
return cname;
}
示例2: parseMessage
void parseMessage(StringPiece msg, size_t* threadID, size_t* messageIndex) {
// Validate and strip off the message prefix and suffix
constexpr StringPiece prefix{"thread "};
if (!msg.startsWith(prefix)) {
throw std::runtime_error("bad message prefix");
}
msg.advance(prefix.size());
if (!msg.endsWith(kMsgSuffix)) {
throw std::runtime_error("bad message suffix");
}
msg.subtract(kMsgSuffix.size());
// Parse then strip off the thread index
auto threadIDEnd = msg.find(' ');
if (threadIDEnd == StringPiece::npos) {
throw std::runtime_error("no middle found");
}
*threadID = folly::to<size_t>(msg.subpiece(0, threadIDEnd));
msg.advance(threadIDEnd);
// Validate that the middle of the message is what we expect,
// then strip it off
constexpr StringPiece middle{" message "};
if (!msg.startsWith(middle)) {
throw std::runtime_error("bad message middle");
}
msg.advance(middle.size());
// Parse the message index
*messageIndex = folly::to<size_t>(msg);
}
示例3: qfind_first_of
size_t qfind_first_byte_of_sse42(const StringPiece& haystack,
const StringPiece& needles) {
if (UNLIKELY(needles.empty() || haystack.empty())) {
return StringPiece::npos;
} else if (needles.size() <= 16) {
// we can save some unnecessary load instructions by optimizing for
// the common case of needles.size() <= 16
return qfind_first_byte_of_needles16(haystack, needles);
}
if (haystack.size() < 16 &&
PAGE_FOR(haystack.end() - 1) != PAGE_FOR(haystack.data() + 16)) {
// We can't safely SSE-load haystack. Use a different approach.
if (haystack.size() <= 2) {
return qfind_first_of(haystack, needles, asciiCaseSensitive);
}
return qfind_first_byte_of_byteset(haystack, needles);
}
auto ret = scanHaystackBlock<false>(haystack, needles, 0);
if (ret != StringPiece::npos) {
return ret;
}
size_t i = nextAlignedIndex(haystack.data());
for (; i < haystack.size(); i += 16) {
auto ret = scanHaystackBlock<true>(haystack, needles, i);
if (ret != StringPiece::npos) {
return ret;
}
}
return StringPiece::npos;
}
示例4: parseRes
U_CAPI int32_t U_EXPORT2
unum_parseDecimal(const UNumberFormat* fmt,
const UChar* text,
int32_t textLength,
int32_t *parsePos /* 0 = start */,
char *outBuf,
int32_t outBufLength,
UErrorCode *status)
{
if (U_FAILURE(*status)) {
return -1;
}
if ((outBuf == NULL && outBufLength != 0) || outBufLength < 0) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return -1;
}
Formattable res;
parseRes(res, fmt, text, textLength, parsePos, status);
StringPiece sp = res.getDecimalNumber(*status);
if (U_FAILURE(*status)) {
return -1;
} else if (sp.size() > outBufLength) {
*status = U_BUFFER_OVERFLOW_ERROR;
} else if (sp.size() == outBufLength) {
uprv_strncpy(outBuf, sp.data(), sp.size());
*status = U_STRING_NOT_TERMINATED_WARNING;
} else {
U_ASSERT(outBufLength > 0);
uprv_strcpy(outBuf, sp.data());
}
return sp.size();
}
示例5: WriteRecord
bool LocalSequenceFileWriter::WriteRecord(const StringPiece& key, const StringPiece& value) {
int record_size = key.size() + value.size();
int key_len = key.size();
int int_len = sizeof(int);
int size = 2 * int_len + record_size;
int used_bytes = 0;
scoped_array<char> data(new char[size]);
if (m_records_written_after_sync >= kSyncInterval) {
size += int_len + SYNC_HASH_SIZE;
data.reset(new char[size]);
used_bytes += WriteSyncToBuf(data.get() + used_bytes);
m_records_written_after_sync = 0;
}
// format:
// record_size|key_len|key|value
used_bytes += WriteInt(data.get() + used_bytes, record_size);
used_bytes += WriteInt(data.get() + used_bytes, key_len);
memcpy(data.get() + used_bytes, key.data(), key_len);
used_bytes += key_len;
memcpy(data.get() + used_bytes, value.data(), value.size());
used_bytes += value.size();
CHECK(used_bytes == size);
if (!Write(data.get(), used_bytes)) {
return false;
}
++m_records_written_after_sync;
return true;
}
示例6: resetKey
void Item::resetKey(StringPiece k)
{
assert(k.size() <= 250);
keylen_ = k.size();
receivedBytes_ = 0;
append(k.data(), k.size());
hash_ = boost::hash_range(k.begin(), k.end());
}
示例7: ReplaceAll
void ReplaceAll(std::string* s, const StringPiece& from, const StringPiece& to)
{
size_t pos = 0;
while ((pos = s->find(from.data(), pos, from.size())) != std::string::npos)
{
s->replace(pos, from.size(), to.data(), to.size());
pos += to.size();
}
}
示例8: createProtectedBuf
// Updates contents so that any read accesses past the last byte will
// cause a SIGSEGV. It accomplishes this by changing access to the page that
// begins immediately after the end of the contents (as allocators and mmap()
// all operate on page boundaries, this is a reasonable assumption).
// This function will also initialize buf, which caller must free().
void createProtectedBuf(StringPiece& contents, char** buf) {
ASSERT_LE(contents.size(), kPageSize);
const size_t kSuccess = 0;
char* tmp;
if (kSuccess != posix_memalign((void**)buf, kPageSize, 2 * kPageSize)) {
ASSERT_FALSE(true);
}
mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
size_t newBegin = kPageSize - contents.size();
memcpy(*buf + newBegin, contents.data(), contents.size());
contents.reset(*buf + newBegin, contents.size());
}
示例9:
bool IPAddressV6::validate(StringPiece ip) {
if (ip.size() > 0 && ip.front() == '[' && ip.back() == ']') {
ip = ip.subpiece(1, ip.size() - 2);
}
constexpr size_t kStrMaxLen = INET6_ADDRSTRLEN;
std::array<char, kStrMaxLen + 1> ip_cstr;
const size_t len = std::min(ip.size(), kStrMaxLen);
std::memcpy(ip_cstr.data(), ip.data(), len);
ip_cstr[len] = 0;
struct in6_addr addr;
return 1 == inet_pton(AF_INET6, ip_cstr.data(), &addr);
}
示例10: doPrint
void FDSymbolizePrinter::doPrint(StringPiece sp) {
if (buffer_) {
if (sp.size() > buffer_->tailroom()) {
flush();
writeFull(fd_, sp.data(), sp.size());
} else {
memcpy(buffer_->writableTail(), sp.data(), sp.size());
buffer_->append(sp.size());
}
} else {
writeFull(fd_, sp.data(), sp.size());
}
}
示例11: Evaluate
FFState* TargetBigramFeature::Evaluate(const Hypothesis& cur_hypo,
const FFState* prev_state,
ScoreComponentCollection* accumulator) const
{
const TargetBigramState* tbState = dynamic_cast<const TargetBigramState*>(prev_state);
assert(tbState);
// current hypothesis target phrase
const Phrase& targetPhrase = cur_hypo.GetCurrTargetPhrase();
if (targetPhrase.GetSize() == 0) {
return new TargetBigramState(*tbState);
}
// extract all bigrams w1 w2 from current hypothesis
for (size_t i = 0; i < targetPhrase.GetSize(); ++i) {
const Factor* f1 = NULL;
if (i == 0) {
f1 = tbState->GetWord().GetFactor(m_factorType);
} else {
f1 = targetPhrase.GetWord(i-1).GetFactor(m_factorType);
}
const Factor* f2 = targetPhrase.GetWord(i).GetFactor(m_factorType);
const StringPiece w1 = f1->GetString();
const StringPiece w2 = f2->GetString();
// skip bigrams if they don't belong to a given restricted vocabulary
if (m_vocab.size() &&
(FindStringPiece(m_vocab, w1) == m_vocab.end() || FindStringPiece(m_vocab, w2) == m_vocab.end())) {
continue;
}
string name(w1.data(), w1.size());
name += ":";
name.append(w2.data(), w2.size());
accumulator->PlusEquals(this,name,1);
}
if (cur_hypo.GetWordsBitmap().IsComplete()) {
const StringPiece w1 = targetPhrase.GetWord(targetPhrase.GetSize()-1).GetFactor(m_factorType)->GetString();
const string& w2 = EOS_;
if (m_vocab.empty() || (FindStringPiece(m_vocab, w1) != m_vocab.end())) {
string name(w1.data(), w1.size());
name += ":";
name += w2;
accumulator->PlusEquals(this,name,1);
}
return NULL;
}
return new TargetBigramState(targetPhrase.GetWord(targetPhrase.GetSize()-1));
}
示例12: TryMatch
int RML_RE::TryMatch(const StringPiece& text,
int startpos,
Anchor anchor,
int *vec,
int vecsize) const {
pcre* re = (anchor == ANCHOR_BOTH) ? re_full_ : re_partial_;
if (re == NULL) {
//fprintf(stderr, "Matching against invalid re: %s\n", error_->c_str());
return 0;
}
pcre_extra extra = { 0 };
if (options_.match_limit() > 0) {
extra.flags = PCRE_EXTRA_MATCH_LIMIT;
extra.match_limit = options_.match_limit();
}
int rc = pcre_exec(re, // The regular expression object
&extra,
text.data(),
text.size(),
startpos,
(anchor == UNANCHORED) ? 0 : PCRE_ANCHORED,
vec,
vecsize);
// Handle errors
if (rc == PCRE_ERROR_NOMATCH) {
return 0;
} else if (rc < 0) {
//fprintf(stderr, "Unexpected return code: %d when matching '%s'\n",
// re, pattern_.c_str());
return 0;
} else if (rc == 0) {
// pcre_exec() returns 0 as a special case when the number of
// capturing subpatterns exceeds the size of the vector.
// When this happens, there is a match and the output vector
// is filled, but we miss out on the positions of the extra subpatterns.
rc = vecsize / 2;
}
if ((anchor == ANCHOR_BOTH) && (re_full_ == re_partial_)) {
// We need an extra check to make sure that the match extended
// to the end of the input string
assert(vec[0] == 0); // PCRE_ANCHORED forces starting match
if (vec[1] != text.size()) return 0; // Did not get ending match
}
return rc;
}
示例13: qfind_first_byte_of_memchr
size_t qfind_first_byte_of_memchr(const StringPiece& haystack,
const StringPiece& needles) {
size_t best = haystack.size();
for (char needle: needles) {
const void* ptr = memchr(haystack.data(), needle, best);
if (ptr) {
auto found = static_cast<const char*>(ptr) - haystack.data();
best = std::min<size_t>(best, found);
}
}
if (best == haystack.size()) {
return StringPiece::npos;
}
return best;
}
示例14:
bool Base64::WebSafeEncode(const StringPiece& input, std::string* output)
{
std::string temp;
temp.resize(modp_b64r_encode_len(input.size())); // makes room for null byte
// null terminates result since result is base64 text!
int input_size = static_cast<int>(input.size());
int output_size = modp_b64r_encode(&(temp[0]), input.data(), input_size);
if (output_size < 0)
return false;
temp.resize(output_size); // strips off null byte
output->swap(temp);
return true;
}
示例15: createProtectedBuf
// Updates contents so that any read accesses past the last byte will
// cause a SIGSEGV. It accomplishes this by changing access to the page that
// begins immediately after the end of the contents (as allocators and mmap()
// all operate on page boundaries, this is a reasonable assumption).
// This function will also initialize buf, which caller must free().
void createProtectedBuf(StringPiece& contents, char** buf) {
ASSERT_LE(contents.size(), kPageSize);
const size_t kSuccess = 0;
char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
if (pageAlignedBuf == nullptr) {
ASSERT_FALSE(true);
}
// Protect the page after the first full page-aligned region of the
// malloc'ed buffer
mprotect(pageAlignedBuf + kPageSize, kPageSize, PROT_NONE);
size_t newBegin = kPageSize - contents.size();
memcpy(pageAlignedBuf + newBegin, contents.data(), contents.size());
contents.reset(pageAlignedBuf + newBegin, contents.size());
*buf = pageAlignedBuf;
}