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 /vespalib | |
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 'vespalib')
-rw-r--r-- | vespalib/src/tests/stllike/string_test.cpp | 4 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/component/version.cpp | 3 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/io/fileutil.cpp | 57 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/io/fileutil.h | 52 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/objects/nbostream.h | 2 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/asciistream.cpp | 6 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/asciistream.h | 8 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/hash_fun.h | 2 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/string.cpp | 2 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/string.h | 95 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/exceptions.cpp | 3 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/regexp.cpp | 14 |
12 files changed, 123 insertions, 125 deletions
diff --git a/vespalib/src/tests/stllike/string_test.cpp b/vespalib/src/tests/stllike/string_test.cpp index 2973ffd1ef1..96c095667ee 100644 --- a/vespalib/src/tests/stllike/string_test.cpp +++ b/vespalib/src/tests/stllike/string_test.cpp @@ -265,10 +265,10 @@ TEST("testString") { // Test std::string conversion of empty string stringref sref; std::string stdString(sref); - EXPECT_TRUE(strcmp("", sref.c_str()) == 0); + EXPECT_TRUE(strcmp("", sref.data()) == 0); stdString = "abc"; stringref sref2(stdString); - EXPECT_TRUE(stdString.c_str() == sref2.c_str()); + EXPECT_TRUE(stdString.c_str() == sref2.data()); EXPECT_TRUE(stdString == sref2); EXPECT_TRUE(sref2 == stdString); { diff --git a/vespalib/src/vespa/vespalib/component/version.cpp b/vespalib/src/vespa/vespalib/component/version.cpp index af38a675de8..3aa8e134e36 100644 --- a/vespalib/src/vespa/vespalib/component/version.cpp +++ b/vespalib/src/vespa/vespalib/component/version.cpp @@ -62,10 +62,11 @@ Version::verifySanity() } } +// Precondition: input.empty() == false static int parseInteger(const stringref & input) __attribute__((noinline)); static int parseInteger(const stringref & input) { - const char *s = input.c_str(); + const char *s = input.data(); unsigned char firstDigit = s[0]; if (!isdigit(firstDigit)) throw IllegalArgumentException("integer must start with a digit"); diff --git a/vespalib/src/vespa/vespalib/io/fileutil.cpp b/vespalib/src/vespa/vespalib/io/fileutil.cpp index 389218cea35..e360c84f569 100644 --- a/vespalib/src/vespa/vespalib/io/fileutil.cpp +++ b/vespalib/src/vespa/vespalib/io/fileutil.cpp @@ -19,7 +19,7 @@ namespace vespalib { namespace { FileInfo::UP - processStat(struct stat& filestats, bool result, const stringref & path) { + processStat(struct stat& filestats, bool result, stringref path) { FileInfo::UP resval; if (result) { resval.reset(new FileInfo); @@ -36,7 +36,7 @@ namespace { } LOG(debug, "stat(%s): Existed? %s, Plain file? %s, Directory? %s, " "Size: %" PRIu64, - path.c_str(), + string(path).c_str(), resval.get() ? "true" : "false", resval.get() && resval->_plainfile ? "true" : "false", resval.get() && resval->_directory ? "true" : "false", @@ -69,7 +69,7 @@ operator<<(std::ostream& out, const FileInfo& info) return out; } -File::File(const stringref & filename) +File::File(stringref filename) : _fd(-1), _flags(0), _filename(filename), @@ -79,7 +79,7 @@ File::File(const stringref & filename) { } -File::File(int fileDescriptor, const stringref & filename) +File::File(int fileDescriptor, stringref filename) : _fd(fileDescriptor), _flags(0), _filename(filename), @@ -128,7 +128,7 @@ File::operator=(File& f) } void -File::setFilename(const stringref & filename) +File::setFilename(stringref filename) { if (_filename == filename) return; if (_close && _fd != -1) close(); @@ -139,14 +139,14 @@ File::setFilename(const stringref & filename) } namespace { - int openAndCreateDirsIfMissing(const stringref & filename, int flags, + int openAndCreateDirsIfMissing(const string & filename, int flags, bool createDirsIfMissing) { int fd = ::open(filename.c_str(), flags, 0644); if (fd < 0 && errno == ENOENT && ((flags & O_CREAT) != 0) && createDirsIfMissing) { - string::size_type pos = filename.rfind('/'); + auto pos = filename.rfind('/'); if (pos != string::npos) { string path(filename.substr(0, pos)); mkdir(path); @@ -381,7 +381,7 @@ File::readAll() const } vespalib::string -File::readAll(const vespalib::stringref & path) +File::readAll(vespalib::stringref path) { File file(path); file.open(File::READONLY); @@ -452,14 +452,14 @@ getCurrentDirectory() } bool -mkdir(const stringref & directory, bool recursive) +mkdir(const string & directory, bool recursive) { if (::mkdir(directory.c_str(), 0777) == 0) { LOG(debug, "mkdir(%s): Created directory", directory.c_str()); return true; } if (recursive && errno == ENOENT) { - string::size_type slashpos = directory.rfind('/'); + auto slashpos = directory.rfind('/'); if (slashpos != string::npos) { /* Recursively make superdirs.*/ string superdir = directory.substr(0, slashpos); @@ -499,8 +499,8 @@ mkdir(const stringref & directory, bool recursive) } void -symlink(const stringref & oldPath, - const stringref & newPath) +symlink(const string & oldPath, + const string & newPath) { if (::symlink(oldPath.c_str(), newPath.c_str())) { asciistream ss; @@ -513,7 +513,7 @@ symlink(const stringref & oldPath, } string -readLink(const stringref & path) +readLink(const string & path) { char buf[256]; ssize_t bytes(::readlink(path.c_str(), buf, sizeof(buf))); @@ -528,7 +528,7 @@ readLink(const stringref & path) } void -chdir(const stringref & directory) +chdir(const string & directory) { if (::chdir(directory.c_str()) != 0) { asciistream ost; @@ -537,12 +537,11 @@ chdir(const stringref & directory) throw IoException(ost.str(), IoException::getErrorType(errno), VESPA_STRLOC); } - LOG(debug, "chdir(%s): Working directory changed.", - directory.c_str()); + LOG(debug, "chdir(%s): Working directory changed.", directory.c_str()); } bool -rmdir(const stringref & directory, bool recursive) +rmdir(const string & directory, bool recursive) { string dirname(directory); if (!dirname.empty() && *dirname.rbegin() == '/') { @@ -595,26 +594,26 @@ rmdir(const stringref & directory, bool recursive) } FileInfo::UP -stat(const stringref & path) +stat(const string & path) { struct ::stat filestats; return processStat(filestats, ::stat(path.c_str(), &filestats) == 0, path); } FileInfo::UP -lstat(const stringref & path) +lstat(const string & path) { struct ::stat filestats; return processStat(filestats, ::lstat(path.c_str(), &filestats) == 0, path); } bool -fileExists(const vespalib::stringref & path) { +fileExists(const string & path) { return (stat(path).get() != 0); } bool -unlink(const stringref & filename) +unlink(const string & filename) { if (::unlink(filename.c_str()) != 0) { if (errno == ENOENT) { @@ -631,7 +630,7 @@ unlink(const stringref & filename) } bool -rename(const stringref & frompath, const stringref & topath, +rename(const string & frompath, const string & topath, bool copyDeleteBetweenFilesystems, bool createTargetDirectoryIfMissing) { LOG(spam, "rename(%s, %s): Renaming file%s.", @@ -696,7 +695,7 @@ namespace { } void -copy(const stringref & frompath, const stringref & topath, +copy(const string & frompath, const string & topath, bool createTargetDirectoryIfMissing, bool useDirectIO) { // Get aligned buffer, so it works with direct IO @@ -730,7 +729,7 @@ copy(const stringref & frompath, const stringref & topath, } DirectoryList -listDirectory(const stringref & path) +listDirectory(const string & path) { DIR* dir = ::opendir(path.c_str()); struct dirent* entry; @@ -762,7 +761,7 @@ getAlignedBuffer(size_t size) return MallocAutoPtr(ptr); } -string dirname(const stringref name) +string dirname(stringref name) { size_t found = name.rfind('/'); if (found == string::npos) { @@ -776,7 +775,7 @@ string dirname(const stringref name) namespace { -void addStat(asciistream &os, const stringref name) +void addStat(asciistream &os, const string & name) { struct ::stat filestat; memset(&filestat, '\0', sizeof(filestat)); @@ -801,7 +800,7 @@ void addStat(asciistream &os, const stringref name) } string -getOpenErrorString(const int osError, const stringref filename) +getOpenErrorString(const int osError, stringref filename) { asciistream os; string dirName(dirname(filename)); @@ -809,12 +808,12 @@ getOpenErrorString(const int osError, const stringref filename) getErrorString(osError) << "\") fileStat"; addStat(os, filename); os << " dirStat"; - addStat(os, dirName.c_str()); + addStat(os, dirName); return os.str(); } bool -isDirectory(const vespalib::stringref & path) { +isDirectory(const string & path) { FileInfo::UP info(stat(path)); return (info.get() && info->_directory); } diff --git a/vespalib/src/vespa/vespalib/io/fileutil.h b/vespalib/src/vespa/vespalib/io/fileutil.h index ece37ab0108..187939ed412 100644 --- a/vespalib/src/vespa/vespalib/io/fileutil.h +++ b/vespalib/src/vespa/vespalib/io/fileutil.h @@ -83,10 +83,10 @@ public: enum Flag { READONLY = 1, CREATE = 2, DIRECTIO = 4, TRUNC = 8 }; /** Create a file instance, without opening the file. */ - File(const vespalib::stringref & filename); + File(vespalib::stringref filename); /** Create a file instance of an already open file. */ - File(int fileDescriptor, const vespalib::stringref & filename); + File(int fileDescriptor, vespalib::stringref filename); /** Copying a file instance, moves any open file descriptor. */ File(File& f); @@ -99,7 +99,7 @@ public: * Make this instance point at another file. * Closes the old file it it was open. */ - void setFilename(const vespalib::stringref & filename); + void setFilename(vespalib::stringref filename); const vespalib::string& getFilename() const { return _filename; } @@ -188,7 +188,7 @@ public: * @throw IoException If we failed to read from file. * @return The content of the file. */ - static vespalib::string readAll(const vespalib::stringref & path); + static vespalib::string readAll(vespalib::stringref path); virtual void sync(); virtual bool close(); @@ -273,7 +273,7 @@ extern vespalib::string getCurrentDirectory(); * * @return True if it did not exist, false if it did. */ -extern bool mkdir(const vespalib::stringref & directory, bool recursive = true); +extern bool mkdir(const vespalib::string & directory, bool recursive = true); /** * Change working directory. @@ -281,7 +281,7 @@ extern bool mkdir(const vespalib::stringref & directory, bool recursive = true); * @param directory The directory to change to. * @throw IoException If we failed to change to the new working directory. */ -extern void chdir(const vespalib::stringref & directory); +extern void chdir(const vespalib::string & directory); /** * Remove a directory. @@ -293,7 +293,7 @@ extern void chdir(const vespalib::stringref & directory); * * @return True if directory existed, false if not. */ -extern bool rmdir(const vespalib::stringref & directory, bool recursive = false); +extern bool rmdir(const vespalib::string & directory, bool recursive = false); /** * Stat a file. @@ -302,7 +302,7 @@ extern bool rmdir(const vespalib::stringref & directory, bool recursive = false) * @return A file info object if everything went well, a null pointer if the * file was not found. */ -extern FileInfo::UP stat(const vespalib::stringref & path); +extern FileInfo::UP stat(const vespalib::string & path); /** * Stat a file. Give info on symlink rather than on file pointed to. @@ -311,14 +311,14 @@ extern FileInfo::UP stat(const vespalib::stringref & path); * @return A file info object if everything went well, a null pointer if the * file was not found. */ -extern FileInfo::UP lstat(const vespalib::stringref & path); +extern FileInfo::UP lstat(const vespalib::string & path); /** * Check if a file exists or not. See also pathExists. * * @throw IoException If we failed to stat the file. */ -extern bool fileExists(const vespalib::stringref & path); +extern bool fileExists(const vespalib::string & path); /** * Check if a path exists, i.e. whether it's a symbolic link, regular file, @@ -328,7 +328,7 @@ extern bool fileExists(const vespalib::stringref & path); * This function returns true, while fileExists returns true only if the path * the symbolic link points to exists. */ -extern inline bool pathExists(const vespalib::stringref & path) { +extern inline bool pathExists(const vespalib::string & path) { return (lstat(path).get() != 0); } @@ -336,7 +336,7 @@ extern inline bool pathExists(const vespalib::stringref & path) { * Get the filesize of the given file. Ignoring if it exists or not. * (None-existing files will be reported to have size zero) */ -extern inline off_t getFileSize(const vespalib::stringref & path) { +extern inline off_t getFileSize(const vespalib::string & path) { FileInfo::UP info(stat(path)); return (info.get() == 0 ? 0 : info->_size); } @@ -347,7 +347,7 @@ extern inline off_t getFileSize(const vespalib::stringref & path) { * @return True if it is a plain file, false if it don't exist or isn't. * @throw IoException If we failed to stat the file. */ -extern inline bool isPlainFile(const vespalib::stringref & path) { +extern inline bool isPlainFile(const vespalib::string & path) { FileInfo::UP info(stat(path)); return (info.get() && info->_plainfile); } @@ -358,7 +358,7 @@ extern inline bool isPlainFile(const vespalib::stringref & path) { * @return True if it is a directory, false if it don't exist or isn't. * @throw IoException If we failed to stat the file. */ -extern bool isDirectory(const vespalib::stringref & path); +extern bool isDirectory(const vespalib::string & path); /** * Check whether a path is a symlink. @@ -366,7 +366,7 @@ extern bool isDirectory(const vespalib::stringref & path); * @return True if path exists and is a symbolic link. * @throw IoException If there's an unexpected stat failure. */ -extern inline bool isSymLink(const vespalib::stringref & path) { +extern inline bool isSymLink(const vespalib::string & path) { FileInfo::UP info(lstat(path)); return (info.get() && info->_symlink); } @@ -384,8 +384,8 @@ extern inline bool isSymLink(const vespalib::stringref & path) { * @param newPath Relative link to be created. See above note for semantics. * @throw IoException if we fail to create the symlink. */ -extern void symlink(const vespalib::stringref & oldPath, - const vespalib::stringref & newPath); +extern void symlink(const vespalib::string & oldPath, + const vespalib::string & newPath); /** * Read and return the contents of symbolic link at the given path. @@ -394,7 +394,7 @@ extern void symlink(const vespalib::stringref & oldPath, * @return Contents of symbolic link. * @throw IoException if we cannot read the link. */ -extern vespalib::string readLink(const vespalib::stringref & path); +extern vespalib::string readLink(const vespalib::string & path); /** * Remove the given file. @@ -403,7 +403,7 @@ extern vespalib::string readLink(const vespalib::stringref & path); * @return True if file was removed, false if it did not exist. * @throw IoException If we failed to unlink the file. */ -extern bool unlink(const vespalib::stringref & filename); +extern bool unlink(const vespalib::string & filename); /** * Rename the file at frompath to topath. @@ -421,16 +421,16 @@ extern bool unlink(const vespalib::stringref & filename); * @throw IoException If we failed to rename the file. * @return True if file was renamed, false if frompath did not exist. */ -extern bool rename(const vespalib::stringref & frompath, - const vespalib::stringref & topath, +extern bool rename(const vespalib::string & frompath, + const vespalib::string & topath, bool copyDeleteBetweenFilesystems = true, bool createTargetDirectoryIfMissing = false); /** * Copies a file to a destination using Direct IO. */ -extern void copy(const vespalib::stringref & frompath, - const vespalib::stringref & topath, +extern void copy(const vespalib::string & frompath, + const vespalib::string & topath, bool createTargetDirectoryIfMissing = false, bool useDirectIO = true); @@ -438,11 +438,11 @@ extern void copy(const vespalib::stringref & frompath, * List the contents of the given directory. */ typedef std::vector<vespalib::string> DirectoryList; -extern DirectoryList listDirectory(const vespalib::stringref & path); +extern DirectoryList listDirectory(const vespalib::string & path); extern MallocAutoPtr getAlignedBuffer(size_t size); -string dirname(const stringref name); -string getOpenErrorString(const int osError, const stringref name); +string dirname(stringref name); +string getOpenErrorString(const int osError, stringref name); } // vespalib diff --git a/vespalib/src/vespa/vespalib/objects/nbostream.h b/vespalib/src/vespa/vespalib/objects/nbostream.h index b51fff1b7cc..c3127e06133 100644 --- a/vespalib/src/vespa/vespalib/objects/nbostream.h +++ b/vespalib/src/vespa/vespalib/objects/nbostream.h @@ -71,7 +71,7 @@ public: return *this; } nbostream & operator << (const char * v) { uint32_t sz(strlen(v)); (*this) << sz; write(v, sz); return *this; } - nbostream & operator << (const vespalib::stringref & v) { uint32_t sz(v.size()); (*this) << sz; write(v.c_str(), sz); return *this; } + nbostream & operator << (const vespalib::stringref & v) { uint32_t sz(v.size()); (*this) << sz; write(v.data(), sz); return *this; } nbostream & operator << (const vespalib::string & v) { uint32_t sz(v.size()); (*this) << sz; write(v.c_str(), sz); return *this; } nbostream & operator >> (vespalib::string & v) { uint32_t sz; (*this) >> sz; diff --git a/vespalib/src/vespa/vespalib/stllike/asciistream.cpp b/vespalib/src/vespa/vespalib/stllike/asciistream.cpp index 1be24175ede..7e8570b3d61 100644 --- a/vespalib/src/vespa/vespalib/stllike/asciistream.cpp +++ b/vespalib/src/vespa/vespalib/stllike/asciistream.cpp @@ -520,7 +520,7 @@ void asciistream::write(const void * buf, size_t len) if (_rPos > 0 && _rPos == length()) { clear(); } - if (_rbuf.c_str() != _wbuf.c_str()) { + if (_rbuf.data() != _wbuf.data()) { if (_wbuf.empty()) { _wbuf = _rbuf; // Read only to RW } else { @@ -557,7 +557,7 @@ string asciistream::getline(char delim) asciistream asciistream::createFromFile(const stringref & fileName) { - FastOS_File file(fileName.c_str()); + FastOS_File file(vespalib::string(fileName).c_str()); asciistream is; if (file.OpenReadOnly()) { ssize_t sz = file.getSize(); @@ -578,7 +578,7 @@ asciistream asciistream::createFromFile(const stringref & fileName) asciistream asciistream::createFromDevice(const stringref & fileName) { - FastOS_File file(fileName.c_str()); + FastOS_File file(vespalib::string(fileName).c_str()); asciistream is; if (file.OpenReadOnly()) { char buf[8192]; diff --git a/vespalib/src/vespa/vespalib/stllike/asciistream.h b/vespalib/src/vespa/vespalib/stllike/asciistream.h index 1e2b4bc1823..9dd73706d0a 100644 --- a/vespalib/src/vespa/vespalib/stllike/asciistream.h +++ b/vespalib/src/vespa/vespalib/stllike/asciistream.h @@ -42,9 +42,9 @@ public: asciistream & operator << (char v) { doFill(1); write(&v, 1); return *this; } asciistream & operator << (unsigned char v) { doFill(1); write(&v, 1); return *this; } asciistream & operator << (const char * v) { if (v != nullptr) { size_t n(strlen(v)); doFill(n); write(v, n); } return *this; } - asciistream & operator << (const string & v) { doFill(v.size()); write(v.c_str(), v.size()); return *this; } - asciistream & operator << (const stringref & v) { doFill(v.size()); write(v.c_str(), v.size()); return *this; } - asciistream & operator << (const std::string & v) { doFill(v.size()); write(v.c_str(), v.size()); return *this; } + asciistream & operator << (const string & v) { doFill(v.size()); write(v.data(), v.size()); return *this; } + asciistream & operator << (const stringref & v) { doFill(v.size()); write(v.data(), v.size()); return *this; } + asciistream & operator << (const std::string & v) { doFill(v.size()); write(v.data(), v.size()); return *this; } asciistream & operator << (int16_t v) { return *this << static_cast<int64_t>(v); } asciistream & operator << (uint16_t v) { return *this << static_cast<uint64_t>(v); } asciistream & operator << (int32_t v) { return *this << static_cast<int64_t>(v); } @@ -74,7 +74,7 @@ public: asciistream & operator >> (float & v); asciistream & operator >> (double & v); stringref str() const { return stringref(c_str(), size()); } - const char * c_str() const { return _rbuf.c_str() + _rPos; } + const char * c_str() const { return _rbuf.data() + _rPos; } size_t size() const { return length() - _rPos; } bool empty() const { return size() == 0; } bool eof() const { return empty(); } diff --git a/vespalib/src/vespa/vespalib/stllike/hash_fun.h b/vespalib/src/vespa/vespalib/stllike/hash_fun.h index 8b14d5ce67c..7d7be666136 100644 --- a/vespalib/src/vespa/vespalib/stllike/hash_fun.h +++ b/vespalib/src/vespa/vespalib/stllike/hash_fun.h @@ -67,7 +67,7 @@ template<> struct hash<const char *> { }; template<> struct hash<vespalib::stringref> { - size_t operator() (const vespalib::stringref & arg) const { return hashValue(arg.c_str(), arg.size()); } + size_t operator() (const vespalib::stringref & arg) const { return hashValue(arg.data(), arg.size()); } }; template<> struct hash<vespalib::string> { diff --git a/vespalib/src/vespa/vespalib/stllike/string.cpp b/vespalib/src/vespa/vespalib/stllike/string.cpp index 081d45936c7..47d424e93aa 100644 --- a/vespalib/src/vespa/vespalib/stllike/string.cpp +++ b/vespalib/src/vespa/vespalib/stllike/string.cpp @@ -47,7 +47,7 @@ stringref::find(const stringref & s, size_type start) const { std::ostream & operator << (std::ostream & os, const stringref & v) { - return os.write(v.c_str(), v.size()); + return os.write(v.data(), v.size()); } template<uint32_t SS> diff --git a/vespalib/src/vespa/vespalib/stllike/string.h b/vespalib/src/vespa/vespalib/stllike/string.h index 3db36f5fd41..98ed0929a9b 100644 --- a/vespalib/src/vespa/vespalib/stllike/string.h +++ b/vespalib/src/vespa/vespalib/stllike/string.h @@ -31,24 +31,21 @@ public: * return a pointer to the data held, or NULL. * Note that the data may not be zero terminated, and a default * constructed stringref will give a NULL pointer back. If you - * need to make sure c_str() gives a valid zero-terminated string + * need to make sure data() gives a valid zero-terminated string * you should make a vespalib::string from the stringref. **/ - const char * c_str() const { return _s; } - - /** return a pointer to the data held, or NULL. See c_str(). */ const char * data() const { return _s; } size_type size() const { return _sz; } size_type length() const { return size(); } bool empty() const { return _sz == 0; } - const char * begin() const { return c_str(); } + const char * begin() const { return data(); } const char * end() const { return begin() + size(); } const char * rbegin() const { return end() - 1; } const char * rend() const { return begin() - 1; } stringref substr(size_type start, size_type sz=npos) const { if (start < size()) { - return stringref(c_str() + start, std::min(sz, size()-start)); + return stringref(data() + start, std::min(sz, size()-start)); } return stringref(); } @@ -119,7 +116,7 @@ public: * was found, or npos if the substring could not be located */ size_type rfind(const char * s, size_type e=npos) const; - int compare(const stringref & s) const { return compare(s.c_str(), s.size()); } + int compare(const stringref & s) const { return compare(s.data(), s.size()); } int compare(const char *s, size_type sz) const { int diff(memcmp(_s, s, std::min(sz, size()))); return (diff != 0) ? diff : (size() - sz); @@ -127,23 +124,23 @@ public: const char & operator [] (size_t i) const { return _s[i]; } operator std::string () const { return std::string(_s, _sz); } bool operator < (const char * s) const { return compare(s, strlen(s)) < 0; } - bool operator < (const std::string & s) const { return compare(s.c_str(), s.size()) < 0; } - bool operator < (const stringref & s) const { return compare(s.c_str(), s.size()) < 0; } + bool operator < (const std::string & s) const { return compare(s.data(), s.size()) < 0; } + bool operator < (const stringref & s) const { return compare(s.data(), s.size()) < 0; } bool operator <= (const char * s) const { return compare(s, strlen(s)) <= 0; } - bool operator <= (const std::string & s) const { return compare(s.c_str(), s.size()) <= 0; } - bool operator <= (const stringref & s) const { return compare(s.c_str(), s.size()) <= 0; } + bool operator <= (const std::string & s) const { return compare(s.data(), s.size()) <= 0; } + bool operator <= (const stringref & s) const { return compare(s.data(), s.size()) <= 0; } bool operator != (const char * s) const { return compare(s, strlen(s)) != 0; } - bool operator != (const std::string & s) const { return compare(s.c_str(), s.size()) != 0; } - bool operator != (const stringref & s) const { return compare(s.c_str(), s.size()) != 0; } + bool operator != (const std::string & s) const { return compare(s.data(), s.size()) != 0; } + bool operator != (const stringref & s) const { return compare(s.data(), s.size()) != 0; } bool operator == (const char * s) const { return compare(s, strlen(s)) == 0; } - bool operator == (const std::string & s) const { return compare(s.c_str(), s.size()) == 0; } - bool operator == (const stringref & s) const { return compare(s.c_str(), s.size()) == 0; } + bool operator == (const std::string & s) const { return compare(s.data(), s.size()) == 0; } + bool operator == (const stringref & s) const { return compare(s.data(), s.size()) == 0; } bool operator >= (const char * s) const { return compare(s, strlen(s)) >= 0; } - bool operator >= (const std::string & s) const { return compare(s.c_str(), s.size()) >= 0; } - bool operator >= (const stringref & s) const { return compare(s.c_str(), s.size()) >= 0; } + bool operator >= (const std::string & s) const { return compare(s.data(), s.size()) >= 0; } + bool operator >= (const stringref & s) const { return compare(s.data(), s.size()) >= 0; } bool operator > (const char * s) const { return compare(s, strlen(s)) > 0; } - bool operator > (const std::string & s) const { return compare(s.c_str(), s.size()) > 0; } - bool operator > (const stringref & s) const { return compare(s.c_str(), s.size()) > 0; } + bool operator > (const std::string & s) const { return compare(s.data(), s.size()) > 0; } + bool operator > (const stringref & s) const { return compare(s.data(), s.size()) > 0; } private: const char *_s; size_type _sz; @@ -178,13 +175,13 @@ public: small_string() : _buf(_stack), _sz(0), _bufferSize(StackSize) { _stack[0] = '\0'; } small_string(const char * s) : _buf(_stack), _sz(s ? strlen(s) : 0) { init(s); } small_string(const void * s, size_type sz) : _buf(_stack), _sz(sz) { init(s); } - small_string(const stringref & s) : _buf(_stack), _sz(s.size()) { init(s.c_str()); } - small_string(const std::string & s) : _buf(_stack), _sz(s.size()) { init(s.c_str()); } - small_string(const small_string & rhs) noexcept : _buf(_stack), _sz(rhs.size()) { init(rhs.c_str()); } + small_string(const stringref & s) : _buf(_stack), _sz(s.size()) { init(s.data()); } + small_string(const std::string & s) : _buf(_stack), _sz(s.size()) { init(s.data()); } + small_string(const small_string & rhs) noexcept : _buf(_stack), _sz(rhs.size()) { init(rhs.data()); } small_string(const small_string & rhs, size_type pos, size_type sz=npos) noexcept : _buf(_stack), _sz(std::min(sz, rhs.size()-pos)) { - init(rhs.c_str()+pos); + init(rhs.data()+pos); } small_string(size_type sz, char c) : _buf(_stack), _sz(0), _bufferSize(StackSize) @@ -204,10 +201,10 @@ public: } } small_string& operator= (const small_string &rhs) { - return assign(rhs.c_str(), rhs.size()); + return assign(rhs.data(), rhs.size()); } small_string & operator= (const stringref &rhs) { - return assign(rhs.c_str(), rhs.size()); + return assign(rhs.data(), rhs.size()); } small_string& operator= (const char *s) { return assign(s); @@ -321,18 +318,18 @@ public: small_string & assign(const char * s) { return assign(s, strlen(s)); } small_string & assign(const void * s, size_type sz); small_string & assign(const stringref &s, size_type pos, size_type sz) { - return assign(s.c_str() + pos, sz); + return assign(s.data() + pos, sz); } small_string & assign(const stringref &rhs) { - if (c_str() != rhs.c_str()) assign(rhs.c_str(), rhs.size()); + if (data() != rhs.data()) assign(rhs.data(), rhs.size()); return *this; } small_string & push_back(char c) { return append(&c, 1); } small_string & append(char c) { return append(&c, 1); } small_string & append(const char * s) { return append(s, strlen(s)); } - small_string & append(const stringref & s) { return append(s.c_str(), s.size()); } - small_string & append(const std::string & s) { return append(s.c_str(), s.size()); } - small_string & append(const small_string & s) { return append(s.c_str(), s.size()); } + small_string & append(const stringref & s) { return append(s.data(), s.size()); } + small_string & append(const std::string & s) { return append(s.data(), s.size()); } + small_string & append(const small_string & s) { return append(s.data(), s.size()); } small_string & append(const void * s, size_type sz); small_string & operator += (char c) { return append(c); } small_string & operator += (const char * s) { return append(s); } @@ -358,7 +355,7 @@ public: } small_string & insert(iterator p, const_iterator f, const_iterator l) { return insert(p-c_str(), f, l-f); } - small_string & insert(size_type start, const stringref & v) { return insert(start, v.c_str(), v.size()); } + small_string & insert(size_type start, const stringref & v) { return insert(start, v.data(), v.size()); } small_string & insert(size_type start, const void * v, size_type sz); /** @@ -447,29 +444,29 @@ public: */ bool operator < (const char * s) const { return compare(s, strlen(s)) < 0; } - bool operator < (const std::string & s) const { return compare(s.c_str(), s.size()) < 0; } - bool operator < (const small_string & s) const { return compare(s.c_str(), s.size()) < 0; } - bool operator < (const stringref & s) const { return compare(s.c_str(), s.size()) < 0; } + bool operator < (const std::string & s) const { return compare(s.data(), s.size()) < 0; } + bool operator < (const small_string & s) const { return compare(s.data(), s.size()) < 0; } + bool operator < (const stringref & s) const { return compare(s.data(), s.size()) < 0; } bool operator <= (const char * s) const { return compare(s, strlen(s)) <= 0; } - bool operator <= (const std::string & s) const { return compare(s.c_str(), s.size()) <= 0; } - bool operator <= (const small_string & s) const { return compare(s.c_str(), s.size()) <= 0; } - bool operator <= (const stringref & s) const { return compare(s.c_str(), s.size()) <= 0; } + bool operator <= (const std::string & s) const { return compare(s.data(), s.size()) <= 0; } + bool operator <= (const small_string & s) const { return compare(s.data(), s.size()) <= 0; } + bool operator <= (const stringref & s) const { return compare(s.data(), s.size()) <= 0; } bool operator == (const char * s) const { return compare(s, strlen(s)) == 0; } - bool operator == (const std::string & s) const { return compare(s.c_str(), s.size()) == 0; } - bool operator == (const small_string & s) const { return compare(s.c_str(), s.size()) == 0; } - bool operator == (const stringref & s) const { return compare(s.c_str(), s.size()) == 0; } + bool operator == (const std::string & s) const { return compare(s.data(), s.size()) == 0; } + bool operator == (const small_string & s) const { return compare(s.data(), s.size()) == 0; } + bool operator == (const stringref & s) const { return compare(s.data(), s.size()) == 0; } bool operator != (const char * s) const { return compare(s, strlen(s)) != 0; } - bool operator != (const std::string & s) const { return compare(s.c_str(), s.size()) != 0; } - bool operator != (const small_string & s) const { return compare(s.c_str(), s.size()) != 0; } - bool operator != (const stringref & s) const { return compare(s.c_str(), s.size()) != 0; } + bool operator != (const std::string & s) const { return compare(s.data(), s.size()) != 0; } + bool operator != (const small_string & s) const { return compare(s.data(), s.size()) != 0; } + bool operator != (const stringref & s) const { return compare(s.data(), s.size()) != 0; } bool operator >= (const char * s) const { return compare(s, strlen(s)) >= 0; } - bool operator >= (const std::string & s) const { return compare(s.c_str(), s.size()) >= 0; } - bool operator >= (const small_string & s) const { return compare(s.c_str(), s.size()) >= 0; } - bool operator >= (const stringref & s) const { return compare(s.c_str(), s.size()) >= 0; } + bool operator >= (const std::string & s) const { return compare(s.data(), s.size()) >= 0; } + bool operator >= (const small_string & s) const { return compare(s.data(), s.size()) >= 0; } + bool operator >= (const stringref & s) const { return compare(s.data(), s.size()) >= 0; } bool operator > (const char * s) const { return compare(s, strlen(s)) > 0; } - bool operator > (const std::string & s) const { return compare(s.c_str(), s.size()) > 0; } - bool operator > (const small_string & s) const { return compare(s.c_str(), s.size()) > 0; } - bool operator > (const stringref & s) const { return compare(s.c_str(), s.size()) > 0; } + bool operator > (const std::string & s) const { return compare(s.data(), s.size()) > 0; } + bool operator > (const small_string & s) const { return compare(s.data(), s.size()) > 0; } + bool operator > (const stringref & s) const { return compare(s.data(), s.size()) > 0; } template<typename T> bool operator != (const T& s) const { return ! operator == (s); } diff --git a/vespalib/src/vespa/vespalib/util/exceptions.cpp b/vespalib/src/vespa/vespalib/util/exceptions.cpp index 3db697e3b68..fc64aa79ca4 100644 --- a/vespalib/src/vespa/vespalib/util/exceptions.cpp +++ b/vespalib/src/vespa/vespalib/util/exceptions.cpp @@ -74,7 +74,8 @@ PortListenException::make_message(int port, const vespalib::stringref &protocol, const vespalib::stringref &msg) { return make_string("failed to listen on port %d with protocol %s%s%s", - port, protocol.c_str(), msg.empty() ? "" : ": ", msg.c_str()); + port, vespalib::string(protocol).c_str(), msg.empty() ? "" : ": ", + vespalib::string(msg).c_str()); } PortListenException::PortListenException(int port, const vespalib::stringref &protocol, diff --git a/vespalib/src/vespa/vespalib/util/regexp.cpp b/vespalib/src/vespa/vespalib/util/regexp.cpp index b376b451b6d..e1aa4e9b189 100644 --- a/vespalib/src/vespa/vespalib/util/regexp.cpp +++ b/vespalib/src/vespa/vespalib/util/regexp.cpp @@ -32,13 +32,13 @@ Regexp::compile(const vespalib::stringref & re, Flags flags) preg->fastmap = static_cast<char *>(malloc(256)); preg->buffer = NULL; preg->allocated = 0; - const char * error = re_compile_pattern(re.c_str(), re.size(), preg); + const char * error = re_compile_pattern(re.data(), re.size(), preg); if (error != 0) { - LOG(warning, "invalid regexp '%s': %s", re.c_str(), error); + LOG(warning, "invalid regexp '%s': %s", vespalib::string(re).c_str(), error); return false; } if (re_compile_fastmap(preg) != 0) { - LOG(warning, "re_compile_fastmap failed for regexp '%s'", re.c_str()); + LOG(warning, "re_compile_fastmap failed for regexp '%s'", vespalib::string(re).c_str()); return false; } return true; @@ -57,7 +57,7 @@ Regexp::match(const vespalib::stringref & s) const { if ( ! valid() ) { return false; } regex_t *preg = const_cast<regex_t *>(static_cast<const regex_t *>(_data)); - int pos(re_search(preg, s.c_str(), s.size(), 0, s.size(), NULL)); + int pos(re_search(preg, s.data(), s.size(), 0, s.size(), NULL)); if (pos < -1) { throw IllegalArgumentException(make_string("re_search failed with code(%d)", pos)); } @@ -70,13 +70,13 @@ vespalib::string Regexp::replace(const vespalib::stringref & s, const vespalib:: regex_t *preg = const_cast<regex_t *>(static_cast<const regex_t *>(_data)); vespalib::string modified; int prev(0); - for(int pos(re_search(preg, s.c_str(), s.size(), 0, s.size(), NULL)); + for(int pos(re_search(preg, s.data(), s.size(), 0, s.size(), NULL)); pos >=0; - pos = re_search(preg, s.c_str()+prev, s.size()-prev, 0, s.size()-prev, NULL)) + pos = re_search(preg, s.data()+prev, s.size()-prev, 0, s.size()-prev, NULL)) { modified += s.substr(prev, pos); modified += replacement; - int count = re_match(preg, s.c_str()+prev, s.size()-prev, pos, NULL); + int count = re_match(preg, s.data()+prev, s.size()-prev, pos, NULL); prev += pos + count; } modified += s.substr(prev); |