-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
C Drivers
-
None
-
None
-
None
-
None
-
None
-
None
Proposal: Add API accepting a string length for functions only supporting NULL-terminated strings.
Motivation:
This may help improve performance and/or reduce API in wrapping drivers.
Functions accepting a const char * without a length expect a NULL terminated string:
void mongoc_write_concern_set_wtag (mongoc_write_concern_t *write_concern, const char *tag); bool mongoc_read_concern_set_level (mongoc_read_concern_t *read_concern, const char *level); mongoc_uri_t * mongoc_uri_new_with_error (const char *uri_string, bson_error_t *error); void mongoc_write_concern_set_wtag (mongoc_write_concern_t *write_concern, const char *tag);
This requires wrapping drivers (in particular the C++ driver) to allocate and NULL terminate passed string views. CXX-3236 proposed adding multiple overloads to the C++ driver to handle such a case:
uri(char const* v); // v is NULL-terminated. uri(bsoncxx::v1::stdx::string_view v) : uri{std::string{v}.c_str()} {} // Copies and NULL-terminates v. uri(std::string v) : uri{v.c_str()} {} // Copies and NULL-terminates v.
If the C driver supported a length argument, the std::string and const char* overloads would no longer be motivated. The data+length from the string_view could be passed directly to the C driver.
Related:
libbson API was extended in CDRIVER-2414 to provide alternatives accepting a string length:
bool bson_iter_init_find (bson_iter_t *iter, const bson_t *bson, const char *key); // Alternative: `key` need not be NULL-terminated bool bson_iter_init_find_w_len (bson_iter_t *iter, const bson_t *bson, const char *key, int keylen);