mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
Use std::string_view and fmtlib across all libraries (#3402)
- Twine, StringRef, Format, and NativeFormatting have been removed - Logging now uses fmtlib style formatting - Nearly all uses of wpi::outs/errs have been replaced with fmt::print() or std::puts()/std::fputs() (for unformatted strings). - A wpi/fmt/raw_ostream.h header has been added to enable fmt::print() with wpi::raw_ostream
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -17,8 +18,6 @@
|
||||
#include "wpi/SmallString.h"
|
||||
#include "wpi/SmallVector.h"
|
||||
#include "wpi/StringMap.h"
|
||||
#include "wpi/StringRef.h"
|
||||
#include "wpi/Twine.h"
|
||||
#include "wpi/raw_istream.h"
|
||||
#include "wpi/raw_socket_istream.h"
|
||||
#include "wpi/raw_socket_ostream.h"
|
||||
@@ -29,15 +28,15 @@ namespace wpi {
|
||||
// @param buf Buffer for output
|
||||
// @param error Set to true if an error occurred
|
||||
// @return Escaped string
|
||||
StringRef UnescapeURI(const Twine& str, SmallVectorImpl<char>& buf,
|
||||
bool* error);
|
||||
std::string_view UnescapeURI(std::string_view str, SmallVectorImpl<char>& buf,
|
||||
bool* error);
|
||||
|
||||
// Escape a string with %xx-encoding.
|
||||
// @param buf Buffer for output
|
||||
// @param spacePlus If true, encodes spaces to '+' rather than "%20"
|
||||
// @return Escaped string
|
||||
StringRef EscapeURI(const Twine& str, SmallVectorImpl<char>& buf,
|
||||
bool spacePlus = true);
|
||||
std::string_view EscapeURI(std::string_view str, SmallVectorImpl<char>& buf,
|
||||
bool spacePlus = true);
|
||||
|
||||
// Parse a set of HTTP headers. Saves just the Content-Type and Content-Length
|
||||
// fields.
|
||||
@@ -55,7 +54,7 @@ bool ParseHttpHeaders(raw_istream& is, SmallVectorImpl<char>* contentType,
|
||||
// @param saveBuf If not null, all scanned characters up to but not including
|
||||
// the boundary are saved to this string
|
||||
// @return False if error occurred on input stream, true if boundary found.
|
||||
bool FindMultipartBoundary(wpi::raw_istream& is, StringRef boundary,
|
||||
bool FindMultipartBoundary(wpi::raw_istream& is, std::string_view boundary,
|
||||
std::string* saveBuf);
|
||||
|
||||
/**
|
||||
@@ -77,7 +76,7 @@ class HttpQueryMap {
|
||||
*
|
||||
* @param query query string
|
||||
*/
|
||||
explicit HttpQueryMap(StringRef query);
|
||||
explicit HttpQueryMap(std::string_view query);
|
||||
|
||||
/**
|
||||
* Gets an element of the query string. Both the name and the returned
|
||||
@@ -88,11 +87,11 @@ class HttpQueryMap {
|
||||
* @return Optional unescaped value. Returns an empty optional if the
|
||||
* name is not present in the query map.
|
||||
*/
|
||||
std::optional<StringRef> Get(StringRef name,
|
||||
SmallVectorImpl<char>& buf) const;
|
||||
std::optional<std::string_view> Get(std::string_view name,
|
||||
SmallVectorImpl<char>& buf) const;
|
||||
|
||||
private:
|
||||
StringMap<StringRef> m_elems;
|
||||
StringMap<std::string_view> m_elems;
|
||||
};
|
||||
|
||||
class HttpPathRef;
|
||||
@@ -120,7 +119,7 @@ class HttpPath {
|
||||
* Constructs a HTTP path from an escaped path string. Makes a copy of the
|
||||
* path, so it's safe to be a temporary.
|
||||
*/
|
||||
explicit HttpPath(StringRef path);
|
||||
explicit HttpPath(std::string_view path);
|
||||
|
||||
/**
|
||||
* Evaluates to true if the path is not empty.
|
||||
@@ -143,11 +142,15 @@ class HttpPath {
|
||||
* @param match match list
|
||||
* @return True if path equals match list
|
||||
*/
|
||||
bool equals(std::initializer_list<StringRef> match) const {
|
||||
bool equals(std::initializer_list<std::string_view> match) const {
|
||||
return equals(0, makeArrayRef(match.begin(), match.end()));
|
||||
}
|
||||
bool equals(ArrayRef<StringRef> match) const { return equals(0, match); }
|
||||
bool equals(StringRef match) const { return equals(0, makeArrayRef(match)); }
|
||||
bool equals(ArrayRef<std::string_view> match) const {
|
||||
return equals(0, match);
|
||||
}
|
||||
bool equals(std::string_view match) const {
|
||||
return equals(0, makeArrayRef(match));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the elements of the path starting at the "start" element
|
||||
@@ -157,16 +160,17 @@ class HttpPath {
|
||||
* @param match match list
|
||||
* @return True if match
|
||||
*/
|
||||
bool equals(size_t start, std::initializer_list<StringRef> match) const {
|
||||
bool equals(size_t start,
|
||||
std::initializer_list<std::string_view> match) const {
|
||||
return equals(start, makeArrayRef(match.begin(), match.end()));
|
||||
}
|
||||
bool equals(size_t start, ArrayRef<StringRef> match) const {
|
||||
bool equals(size_t start, ArrayRef<std::string_view> match) const {
|
||||
if (m_pathEnds.size() != (start + match.size())) {
|
||||
return false;
|
||||
}
|
||||
return startswith(start, match);
|
||||
}
|
||||
bool equals(size_t start, StringRef match) const {
|
||||
bool equals(size_t start, std::string_view match) const {
|
||||
return equals(start, makeArrayRef(match));
|
||||
}
|
||||
|
||||
@@ -177,13 +181,13 @@ class HttpPath {
|
||||
* @param match match list
|
||||
* @return True if path starts with match list
|
||||
*/
|
||||
bool startswith(std::initializer_list<StringRef> match) const {
|
||||
bool startswith(std::initializer_list<std::string_view> match) const {
|
||||
return startswith(0, makeArrayRef(match.begin(), match.end()));
|
||||
}
|
||||
bool startswith(ArrayRef<StringRef> match) const {
|
||||
bool startswith(ArrayRef<std::string_view> match) const {
|
||||
return startswith(0, match);
|
||||
}
|
||||
bool startswith(StringRef match) const {
|
||||
bool startswith(std::string_view match) const {
|
||||
return startswith(0, makeArrayRef(match));
|
||||
}
|
||||
|
||||
@@ -195,22 +199,21 @@ class HttpPath {
|
||||
* @param match match list
|
||||
* @return True if path starting at the start element matches the match list
|
||||
*/
|
||||
bool startswith(size_t start, std::initializer_list<StringRef> match) const {
|
||||
bool startswith(size_t start,
|
||||
std::initializer_list<std::string_view> match) const {
|
||||
return startswith(start, makeArrayRef(match.begin(), match.end()));
|
||||
}
|
||||
|
||||
bool startswith(size_t start, ArrayRef<StringRef> match) const;
|
||||
bool startswith(size_t start, ArrayRef<std::string_view> match) const;
|
||||
|
||||
bool startswith(size_t start, StringRef match) const {
|
||||
bool startswith(size_t start, std::string_view match) const {
|
||||
return startswith(start, makeArrayRef(match));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a single element of the path.
|
||||
*/
|
||||
StringRef operator[](size_t n) const {
|
||||
return m_pathBuf.slice(n == 0 ? 0 : m_pathEnds[n - 1], m_pathEnds[n]);
|
||||
}
|
||||
std::string_view operator[](size_t n) const;
|
||||
|
||||
/**
|
||||
* Returns a path reference with the first N elements of the path removed.
|
||||
@@ -236,44 +239,50 @@ class HttpPathRef {
|
||||
bool empty() const { return m_path && m_path->size() == m_start; }
|
||||
size_t size() const { return m_path ? m_path->size() - m_start : 0; }
|
||||
|
||||
bool equals(std::initializer_list<StringRef> match) const {
|
||||
bool equals(std::initializer_list<std::string_view> match) const {
|
||||
return equals(0, makeArrayRef(match.begin(), match.end()));
|
||||
}
|
||||
bool equals(ArrayRef<StringRef> match) const { return equals(0, match); }
|
||||
bool equals(StringRef match) const { return equals(0, makeArrayRef(match)); }
|
||||
bool equals(ArrayRef<std::string_view> match) const {
|
||||
return equals(0, match);
|
||||
}
|
||||
bool equals(std::string_view match) const {
|
||||
return equals(0, makeArrayRef(match));
|
||||
}
|
||||
|
||||
bool equals(size_t start, std::initializer_list<StringRef> match) const {
|
||||
bool equals(size_t start,
|
||||
std::initializer_list<std::string_view> match) const {
|
||||
return equals(start, makeArrayRef(match.begin(), match.end()));
|
||||
}
|
||||
bool equals(size_t start, ArrayRef<StringRef> match) const {
|
||||
bool equals(size_t start, ArrayRef<std::string_view> match) const {
|
||||
return m_path ? m_path->equals(m_start + start, match) : false;
|
||||
}
|
||||
bool equals(size_t start, StringRef match) const {
|
||||
bool equals(size_t start, std::string_view match) const {
|
||||
return equals(start, makeArrayRef(match));
|
||||
}
|
||||
|
||||
bool startswith(std::initializer_list<StringRef> match) const {
|
||||
bool startswith(std::initializer_list<std::string_view> match) const {
|
||||
return startswith(0, makeArrayRef(match.begin(), match.end()));
|
||||
}
|
||||
bool startswith(ArrayRef<StringRef> match) const {
|
||||
bool startswith(ArrayRef<std::string_view> match) const {
|
||||
return startswith(0, match);
|
||||
}
|
||||
bool startswith(StringRef match) const {
|
||||
bool startswith(std::string_view match) const {
|
||||
return startswith(0, makeArrayRef(match));
|
||||
}
|
||||
|
||||
bool startswith(size_t start, std::initializer_list<StringRef> match) const {
|
||||
bool startswith(size_t start,
|
||||
std::initializer_list<std::string_view> match) const {
|
||||
return startswith(start, makeArrayRef(match.begin(), match.end()));
|
||||
}
|
||||
bool startswith(size_t start, ArrayRef<StringRef> match) const {
|
||||
bool startswith(size_t start, ArrayRef<std::string_view> match) const {
|
||||
return m_path ? m_path->startswith(m_start + start, match) : false;
|
||||
}
|
||||
bool startswith(size_t start, StringRef match) const {
|
||||
bool startswith(size_t start, std::string_view match) const {
|
||||
return startswith(start, makeArrayRef(match));
|
||||
}
|
||||
|
||||
StringRef operator[](size_t n) const {
|
||||
return m_path ? m_path->operator[](m_start + n) : StringRef{};
|
||||
std::string_view operator[](size_t n) const {
|
||||
return m_path ? m_path->operator[](m_start + n) : std::string_view{};
|
||||
}
|
||||
HttpPathRef drop_front(size_t n) const {
|
||||
return m_path ? m_path->drop_front(m_start + n) : HttpPathRef{};
|
||||
@@ -287,7 +296,7 @@ class HttpPathRef {
|
||||
class HttpLocation {
|
||||
public:
|
||||
HttpLocation() = default;
|
||||
HttpLocation(const Twine& url_, bool* error, std::string* errorMsg);
|
||||
HttpLocation(std::string_view url_, bool* error, std::string* errorMsg);
|
||||
|
||||
std::string url; // retain copy
|
||||
std::string user; // unescaped
|
||||
@@ -312,13 +321,13 @@ class HttpRequest {
|
||||
template <typename T>
|
||||
HttpRequest(const HttpLocation& loc, const T& extraParams);
|
||||
|
||||
HttpRequest(const HttpLocation& loc, StringRef path_)
|
||||
HttpRequest(const HttpLocation& loc, std::string_view path_)
|
||||
: host{loc.host}, port{loc.port}, path{path_} {
|
||||
SetAuth(loc);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
HttpRequest(const HttpLocation& loc, StringRef path_, const T& params)
|
||||
HttpRequest(const HttpLocation& loc, std::string_view path_, const T& params)
|
||||
: host{loc.host}, port{loc.port} {
|
||||
SetPath(path_, params);
|
||||
SetAuth(loc);
|
||||
@@ -332,18 +341,18 @@ class HttpRequest {
|
||||
private:
|
||||
void SetAuth(const HttpLocation& loc);
|
||||
template <typename T>
|
||||
void SetPath(StringRef path_, const T& params);
|
||||
void SetPath(std::string_view path_, const T& params);
|
||||
|
||||
template <typename T>
|
||||
static StringRef GetFirst(const T& elem) {
|
||||
static std::string_view GetFirst(const T& elem) {
|
||||
return elem.first;
|
||||
}
|
||||
template <typename T>
|
||||
static StringRef GetFirst(const StringMapEntry<T>& elem) {
|
||||
static std::string_view GetFirst(const StringMapEntry<T>& elem) {
|
||||
return elem.getKey();
|
||||
}
|
||||
template <typename T>
|
||||
static StringRef GetSecond(const T& elem) {
|
||||
static std::string_view GetSecond(const T& elem) {
|
||||
return elem.second;
|
||||
}
|
||||
};
|
||||
@@ -368,14 +377,15 @@ class HttpConnection {
|
||||
|
||||
class HttpMultipartScanner {
|
||||
public:
|
||||
explicit HttpMultipartScanner(StringRef boundary, bool saveSkipped = false) {
|
||||
explicit HttpMultipartScanner(std::string_view boundary,
|
||||
bool saveSkipped = false) {
|
||||
Reset(saveSkipped);
|
||||
SetBoundary(boundary);
|
||||
}
|
||||
|
||||
// Change the boundary. This is only safe to do when IsDone() is true (or
|
||||
// immediately after construction).
|
||||
void SetBoundary(StringRef boundary);
|
||||
void SetBoundary(std::string_view boundary);
|
||||
|
||||
// Reset the scanner. This allows reuse of internal buffers.
|
||||
void Reset(bool saveSkipped = false);
|
||||
@@ -384,14 +394,14 @@ class HttpMultipartScanner {
|
||||
// is true.
|
||||
// @param in input data
|
||||
// @return the input not consumed; empty if all input consumed
|
||||
StringRef Execute(StringRef in);
|
||||
std::string_view Execute(std::string_view in);
|
||||
|
||||
// Returns true when the boundary has been found.
|
||||
bool IsDone() const { return m_state == kDone; }
|
||||
|
||||
// Get the skipped data. Will be empty if saveSkipped was false.
|
||||
StringRef GetSkipped() const {
|
||||
return m_saveSkipped ? StringRef{m_buf} : StringRef{};
|
||||
std::string_view GetSkipped() const {
|
||||
return m_saveSkipped ? std::string_view{m_buf} : std::string_view{};
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user