Open
Description
The constructors on Uri
that take a string have the following usage requirements:
/// If the `uri` input
/// string doesn't not represent a valid URI, this object is left in an
/// invalid state (isValid() will return false).
This is an easy footgun, as a URI parse failure is silent and requires the user to verify the validity of the Uri
after construction. A better interface would make the fallibility of parsing a URI more directly addressable. Therefore, I propose a new interface to Uri
:
class Uri {
public:
/// Parse the specified `uriString` into the specified `result` object
/// if `uriString` is a valid URI, otherwise load the specified
/// `errorDescription` with a description of the syntax error present in
/// `uriString`. Return 0 on success and non-zero if `uriString` does
/// not have a valid syntax. Note that `errorDescription` may be null
/// if the caller does not care about getting error messages. The
/// behavior is undefined unless `initialize` has been called
/// previously.
BSLA_NODISCARD
static int parse(Uri* result,
const bslstl::StringRef& uriString,
bsl::string* errorDescription);
};
This is basically the function UriParser::parse
, except that interface requires the user to call UriParser::init(allocator)
prior to use. The Uri::parse
function will effectively do this:
int Uri::parse(Uri* result,
const bslstl::StringRef& uriString,
bsl::string* errorDescription)
{
UriParser::initialize();
result->d_wasParserInitialized = true;
return UriParser::parse(result, uriString, errorDescription);
}