diff options
author | Tor Brede Vekterli <vekterli@oath.com> | 2018-07-25 14:24:21 +0000 |
---|---|---|
committer | Tor Brede Vekterli <vekterli@oath.com> | 2018-07-25 14:24:21 +0000 |
commit | 12c8f3005e202b31a8e0ff3816ce9d714a269046 (patch) | |
tree | a5d9ba0eedc49df2cea2dbdfea677c4c37ed3775 /vsm | |
parent | e3af3d215feb1e416b27b92bbf421dde281f3a09 (diff) |
Remove stringref::c_str()
The expected semantics of c_str() (a null-terminated string) cannot
be satisfied with a string reference, so remove the function entirely
to prevent people from using it in buggy ways.
Replaces c_str() with data() in places where it is presumed safe,
otherwise constructs temporary string instances. Certain callsites
have been de-stringref'd in favor of regular strings, in particular
where C APIs have been transitively called. The vast majority of
these were called with string parameters anyway, so should not
cause much extra allocation.
Diffstat (limited to 'vsm')
11 files changed, 23 insertions, 23 deletions
diff --git a/vsm/src/vespa/vsm/common/document.cpp b/vsm/src/vespa/vsm/common/document.cpp index dc820cd99b3..7e4713e672c 100644 --- a/vsm/src/vespa/vsm/common/document.cpp +++ b/vsm/src/vespa/vsm/common/document.cpp @@ -12,10 +12,10 @@ namespace vsm vespalib::asciistream & operator << (vespalib::asciistream & os, const FieldRef & f) { - const char *s = f.c_str(); + const char *s = f.data(); os << f.size(); if (s) { - os << s; + os << s; // Better hope it's null terminated! } os << " : "; return os; diff --git a/vsm/src/vespa/vsm/searcher/fieldsearcher.cpp b/vsm/src/vespa/vsm/searcher/fieldsearcher.cpp index 78531f41cf8..2ba5fea3153 100644 --- a/vsm/src/vespa/vsm/searcher/fieldsearcher.cpp +++ b/vsm/src/vespa/vsm/searcher/fieldsearcher.cpp @@ -115,7 +115,7 @@ void FieldSearcher::prepare(QueryTermList & qtl, const SharedSearcherBuf & UNUSE size_t FieldSearcher::countWords(const FieldRef & f) { size_t words = 0; - const char * n = f.c_str(); + const char * n = f.data(); const char * e = n + f.size(); for( ; n < e; ++n) { for (; isspace(*n) && (n<e); ++n); diff --git a/vsm/src/vespa/vsm/searcher/futf8strchrfieldsearcher.cpp b/vsm/src/vespa/vsm/searcher/futf8strchrfieldsearcher.cpp index b388507aed5..b26b4bd5133 100644 --- a/vsm/src/vespa/vsm/searcher/futf8strchrfieldsearcher.cpp +++ b/vsm/src/vespa/vsm/searcher/futf8strchrfieldsearcher.cpp @@ -222,7 +222,7 @@ size_t FUTF8StrChrFieldSearcher::matchTerm(const FieldRef & f, QueryTerm & qt) { _folded.reserve(f.size()+16*3); //Enable fulle xmm0 store size_t unalignedStart(0); - bool ascii7Bit = lfoldua(f.c_str(), f.size(), &_folded[0], unalignedStart); + bool ascii7Bit = lfoldua(f.data(), f.size(), &_folded[0], unalignedStart); if (ascii7Bit) { char * folded = &_folded[unalignedStart]; /// Add the pattern 00 01 00 to avoid multiple eof tests of falling off the edge. @@ -240,7 +240,7 @@ size_t FUTF8StrChrFieldSearcher::matchTerms(const FieldRef & f, const size_t min { _folded.reserve(f.size()+16*3); //Enable fulle xmm0 store size_t unalignedStart(0); - bool ascii7Bit = lfoldua(f.c_str(), f.size(), &_folded[0], unalignedStart); + bool ascii7Bit = lfoldua(f.data(), f.size(), &_folded[0], unalignedStart); if (ascii7Bit) { char * folded = &_folded[unalignedStart]; /// Add the pattern 00 01 00 to avoid multiple eof tests of falling off the edge. diff --git a/vsm/src/vespa/vsm/searcher/strchrfieldsearcher.cpp b/vsm/src/vespa/vsm/searcher/strchrfieldsearcher.cpp index 93bfa76081f..1be1326807e 100644 --- a/vsm/src/vespa/vsm/searcher/strchrfieldsearcher.cpp +++ b/vsm/src/vespa/vsm/searcher/strchrfieldsearcher.cpp @@ -16,7 +16,7 @@ void StrChrFieldSearcher::onValue(const document::FieldValue & fv) { const document::LiteralFieldValueB & sfv = static_cast<const document::LiteralFieldValueB &>(fv); vespalib::stringref val = sfv.getValueRef(); - FieldRef fr(val.c_str(), std::min(maxFieldLength(), val.size())); + FieldRef fr(val.data(), std::min(maxFieldLength(), val.size())); matchDoc(fr); } diff --git a/vsm/src/vespa/vsm/searcher/utf8strchrfieldsearcher.cpp b/vsm/src/vespa/vsm/searcher/utf8strchrfieldsearcher.cpp index 82821dfeaff..b54ed2c583d 100644 --- a/vsm/src/vespa/vsm/searcher/utf8strchrfieldsearcher.cpp +++ b/vsm/src/vespa/vsm/searcher/utf8strchrfieldsearcher.cpp @@ -14,7 +14,7 @@ UTF8StrChrFieldSearcher::matchTerms(const FieldRef & f, const size_t mintsz) { (void) mintsz; termcount_t words(0); - const byte * n = reinterpret_cast<const byte *> (f.c_str()); + const byte * n = reinterpret_cast<const byte *> (f.data()); const byte * e = n + f.size(); if (f.size() >= _buf->size()) { _buf->reserve(f.size() + 1); diff --git a/vsm/src/vespa/vsm/searcher/utf8stringfieldsearcherbase.cpp b/vsm/src/vespa/vsm/searcher/utf8stringfieldsearcherbase.cpp index 0ce002e6765..872cfebfd70 100644 --- a/vsm/src/vespa/vsm/searcher/utf8stringfieldsearcherbase.cpp +++ b/vsm/src/vespa/vsm/searcher/utf8stringfieldsearcherbase.cpp @@ -104,7 +104,7 @@ size_t UTF8StringFieldSearcherBase::matchTermRegular(const FieldRef & f, QueryTerm & qt) { termcount_t words(0); - const byte * n = reinterpret_cast<const byte *> (f.c_str()); + const byte * n = reinterpret_cast<const byte *> (f.data()); // __builtin_prefetch(n, 0, 0); const cmptype_t * term; termsize_t tsz = qt.term(term); @@ -134,7 +134,7 @@ UTF8StringFieldSearcherBase::matchTermRegular(const FieldRef & f, QueryTerm & qt size_t UTF8StringFieldSearcherBase::matchTermExact(const FieldRef & f, QueryTerm & qt) { - const byte * n = reinterpret_cast<const byte *> (f.c_str()); + const byte * n = reinterpret_cast<const byte *> (f.data()); const cmptype_t * term; termsize_t tsz = qt.term(term); const cmptype_t * eterm = term+tsz; @@ -161,7 +161,7 @@ size_t UTF8StringFieldSearcherBase::matchTermSubstring(const FieldRef & f, QueryTerm & qt) { if (qt.termLen() == 0) { return 0; } - const byte * n = reinterpret_cast<const byte *> (f.c_str()); + const byte * n = reinterpret_cast<const byte *> (f.data()); const cmptype_t * term; termsize_t tsz = qt.term(term); if ( f.size() >= _buf->size()) { @@ -195,7 +195,7 @@ size_t UTF8StringFieldSearcherBase::matchTermSuffix(const FieldRef & f, QueryTerm & qt) { termcount_t words = 0; - const byte * srcbuf = reinterpret_cast<const byte *> (f.c_str()); + const byte * srcbuf = reinterpret_cast<const byte *> (f.data()); const byte * srcend = srcbuf + f.size(); const cmptype_t * term; termsize_t tsz = qt.term(term); diff --git a/vsm/src/vespa/vsm/searcher/utf8substringsearcher.cpp b/vsm/src/vespa/vsm/searcher/utf8substringsearcher.cpp index 94fb14b6217..4b8c6e31927 100644 --- a/vsm/src/vespa/vsm/searcher/utf8substringsearcher.cpp +++ b/vsm/src/vespa/vsm/searcher/utf8substringsearcher.cpp @@ -13,7 +13,7 @@ IMPLEMENT_DUPLICATE(UTF8SubStringFieldSearcher); size_t UTF8SubStringFieldSearcher::matchTerms(const FieldRef & f, const size_t mintsz) { - const byte * n = reinterpret_cast<const byte *> (f.c_str()); + const byte * n = reinterpret_cast<const byte *> (f.data()); if ( f.size() >= _buf->size()) { _buf->reserve(f.size() + 1); } diff --git a/vsm/src/vespa/vsm/searcher/utf8substringsnippetmodifier.cpp b/vsm/src/vespa/vsm/searcher/utf8substringsnippetmodifier.cpp index b229b7a5ebd..eee88b34ea6 100644 --- a/vsm/src/vespa/vsm/searcher/utf8substringsnippetmodifier.cpp +++ b/vsm/src/vespa/vsm/searcher/utf8substringsnippetmodifier.cpp @@ -14,8 +14,8 @@ size_t UTF8SubstringSnippetModifier::matchTerms(const FieldRef & f, const size_t mintsz) { _modified->reset(); - _readPtr = f.c_str(); - const byte * src = reinterpret_cast<const byte *> (f.c_str()); + _readPtr = f.data(); + const byte * src = reinterpret_cast<const byte *> (f.data()); // resize ucs4 buffer if (f.size() >= _buf->size()) { _buf->resize(f.size() + 1); @@ -46,8 +46,8 @@ UTF8SubstringSnippetModifier::matchTerms(const FieldRef & f, const size_t mintsz const cmptype_t * dtmp = ditr; for (; (titr < tend) && (*titr == *dtmp); ++titr, ++dtmp); if (titr == tend) { - const char * mbegin = f.c_str() + (*_offsets)[ditr - dbegin]; - const char * mend = f.c_str() + ((dtmp < dend) ? ((*_offsets)[dtmp - dbegin]) : f.size()); + const char * mbegin = f.data() + (*_offsets)[ditr - dbegin]; + const char * mend = f.data() + ((dtmp < dend) ? ((*_offsets)[dtmp - dbegin]) : f.size()); if (_readPtr <= mbegin) { // We will only copy from the field ref once. // If we have overlapping matches only the first one will be considered. @@ -61,9 +61,9 @@ UTF8SubstringSnippetModifier::matchTerms(const FieldRef & f, const size_t mintsz for(; (ditr < drend) && ! Fast_UnicodeUtil::IsWordChar(*ditr) ; ++ditr ); } } - assert(_readPtr <= (f.c_str() + f.size())); + assert(_readPtr <= (f.data() + f.size())); // copy remaining - size_t toCopy = f.size() - (_readPtr - f.c_str()); + size_t toCopy = f.size() - (_readPtr - f.data()); copyToModified(toCopy); return words + 1; // we must also count the last word diff --git a/vsm/src/vespa/vsm/searcher/utf8suffixstringfieldsearcher.cpp b/vsm/src/vespa/vsm/searcher/utf8suffixstringfieldsearcher.cpp index f469fa2ea73..13074937185 100644 --- a/vsm/src/vespa/vsm/searcher/utf8suffixstringfieldsearcher.cpp +++ b/vsm/src/vespa/vsm/searcher/utf8suffixstringfieldsearcher.cpp @@ -14,7 +14,7 @@ UTF8SuffixStringFieldSearcher::matchTerms(const FieldRef & f, const size_t mints { (void) mintsz; termcount_t words = 0; - const byte * srcbuf = reinterpret_cast<const byte *> (f.c_str()); + const byte * srcbuf = reinterpret_cast<const byte *> (f.data()); const byte * srcend = srcbuf + f.size(); if (f.size() >= _buf->size()) { _buf->reserve(f.size() + 1); diff --git a/vsm/src/vespa/vsm/vsm/docsumfilter.cpp b/vsm/src/vespa/vsm/vsm/docsumfilter.cpp index e6af5fb6477..034c3c57bde 100644 --- a/vsm/src/vespa/vsm/vsm/docsumfilter.cpp +++ b/vsm/src/vespa/vsm/vsm/docsumfilter.cpp @@ -74,7 +74,7 @@ public: if (fv.getClass().inherits(document::LiteralFieldValueB::classId)) { const document::LiteralFieldValueB & lfv = static_cast<const document::LiteralFieldValueB &>(fv); vespalib::stringref s = lfv.getValueRef(); - addToPacker(s.c_str(), s.size()); + addToPacker(s.data(), s.size()); } else { vespalib::string s = fv.toString(); addToPacker(s.c_str(), s.size()); diff --git a/vsm/src/vespa/vsm/vsm/flattendocsumwriter.cpp b/vsm/src/vespa/vsm/vsm/flattendocsumwriter.cpp index bf690e50719..080723e1dbd 100644 --- a/vsm/src/vespa/vsm/vsm/flattendocsumwriter.cpp +++ b/vsm/src/vespa/vsm/vsm/flattendocsumwriter.cpp @@ -21,13 +21,13 @@ FlattenDocsumWriter::onPrimitive(uint32_t, const Content & c) if (fv.getClass().inherits(document::LiteralFieldValueB::classId)) { const document::LiteralFieldValueB & lfv = static_cast<const document::LiteralFieldValueB &>(fv); vespalib::stringref value = lfv.getValueRef(); - _output.put(value.c_str(), value.size()); + _output.put(value.data(), value.size()); } else if (fv.getClass().inherits(document::NumericFieldValueBase::classId)) { vespalib::string value = fv.getAsString(); - _output.put(value.c_str(), value.size()); + _output.put(value.data(), value.size()); } else { vespalib::string value = fv.toString(); - _output.put(value.c_str(), value.size()); + _output.put(value.data(), value.size()); } _useSeparator = true; } |