summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Dmitri Gribenko <dmitrig@google.com>2024-01-25 09:49:41 -0800
committerGravatar Copybara-Service <copybara-worker@google.com>2024-01-25 09:50:27 -0800
commit42624b3d9d56ae6fd5c48b28715044667942b9d8 (patch)
tree819eb7cfcf3b76a3dc47b4c0f67e21b250d09e73
parent27f15a052ba54c984faa1eb6044de59b237e6f37 (diff)
Add nullability annotations
PiperOrigin-RevId: 601477629 Change-Id: I3b683d94192b04f233ce5ec636586ccb90d24602
-rw-r--r--absl/strings/escaping.cc46
-rw-r--r--absl/strings/escaping.h17
2 files changed, 40 insertions, 23 deletions
diff --git a/absl/strings/escaping.cc b/absl/strings/escaping.cc
index 1c0eac42..5de12afa 100644
--- a/absl/strings/escaping.cc
+++ b/absl/strings/escaping.cc
@@ -25,6 +25,7 @@
#include "absl/base/config.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/base/internal/unaligned_access.h"
+#include "absl/base/nullability.h"
#include "absl/strings/ascii.h"
#include "absl/strings/charset.h"
#include "absl/strings/internal/escaping.h"
@@ -54,7 +55,8 @@ inline unsigned int hex_digit_to_int(char c) {
return x & 0xf;
}
-inline bool IsSurrogate(char32_t c, absl::string_view src, std::string* error) {
+inline bool IsSurrogate(char32_t c, absl::string_view src,
+ absl::Nullable<std::string*> error) {
if (c >= 0xD800 && c <= 0xDFFF) {
if (error) {
*error = absl::StrCat("invalid surrogate character (0xD800-DFFF): \\",
@@ -83,7 +85,9 @@ inline bool IsSurrogate(char32_t c, absl::string_view src, std::string* error) {
// UnescapeCEscapeSequences().
// ----------------------------------------------------------------------
bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
- char* dest, ptrdiff_t* dest_len, std::string* error) {
+ absl::Nonnull<char*> dest,
+ absl::Nonnull<ptrdiff_t*> dest_len,
+ absl::Nullable<std::string*> error) {
char* d = dest;
const char* p = source.data();
const char* end = p + source.size();
@@ -290,7 +294,8 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
// may be the same.
// ----------------------------------------------------------------------
bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
- std::string* dest, std::string* error) {
+ absl::Nonnull<std::string*> dest,
+ absl::Nullable<std::string*> error) {
strings_internal::STLStringResizeUninitialized(dest, source.size());
ptrdiff_t dest_size;
@@ -392,7 +397,8 @@ inline size_t CEscapedLength(absl::string_view src) {
return escaped_len;
}
-void CEscapeAndAppendInternal(absl::string_view src, std::string* dest) {
+void CEscapeAndAppendInternal(absl::string_view src,
+ absl::Nonnull<std::string*> dest) {
size_t escaped_len = CEscapedLength(src);
if (escaped_len == src.size()) {
dest->append(src.data(), src.size());
@@ -446,9 +452,10 @@ void CEscapeAndAppendInternal(absl::string_view src, std::string* dest) {
// Reverses the mapping in Base64EscapeInternal; see that method's
// documentation for details of the mapping.
-bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest,
- size_t szdest, const signed char* unbase64,
- size_t* len) {
+bool Base64UnescapeInternal(absl::Nullable<const char*> src_param, size_t szsrc,
+ absl::Nullable<char*> dest, size_t szdest,
+ absl::Nonnull<const signed char*> unbase64,
+ absl::Nonnull<size_t*> len) {
static const char kPad64Equals = '=';
static const char kPad64Dot = '.';
@@ -784,8 +791,9 @@ constexpr signed char kUnWebSafeBase64[] = {
/* clang-format on */
template <typename String>
-bool Base64UnescapeInternal(const char* src, size_t slen, String* dest,
- const signed char* unbase64) {
+bool Base64UnescapeInternal(absl::Nullable<const char*> src, size_t slen,
+ absl::Nonnull<String*> dest,
+ absl::Nonnull<const signed char*> unbase64) {
// Determine the size of the output string. Base64 encodes every 3 bytes into
// 4 characters. Any leftover chars are added directly for good measure.
const size_t dest_len = 3 * (slen / 4) + (slen % 4);
@@ -835,7 +843,8 @@ constexpr char kHexValueLenient[256] = {
// or a string. This works because we use the [] operator to access
// individual characters at a time.
template <typename T>
-void HexStringToBytesInternal(const char* from, T to, size_t num) {
+void HexStringToBytesInternal(absl::Nullable<const char*> from, T to,
+ size_t num) {
for (size_t i = 0; i < num; i++) {
to[i] = static_cast<char>(kHexValueLenient[from[i * 2] & 0xFF] << 4) +
(kHexValueLenient[from[i * 2 + 1] & 0xFF]);
@@ -845,7 +854,8 @@ void HexStringToBytesInternal(const char* from, T to, size_t num) {
// This is a templated function so that T can be either a char* or a
// std::string.
template <typename T>
-void BytesToHexStringInternal(const unsigned char* src, T dest, size_t num) {
+void BytesToHexStringInternal(absl::Nullable<const unsigned char*> src, T dest,
+ size_t num) {
auto dest_ptr = &dest[0];
for (auto src_ptr = src; src_ptr != (src + num); ++src_ptr, dest_ptr += 2) {
const char* hex_p = &numbers_internal::kHexTable[*src_ptr * 2];
@@ -860,8 +870,8 @@ void BytesToHexStringInternal(const unsigned char* src, T dest, size_t num) {
//
// See CUnescapeInternal() for implementation details.
// ----------------------------------------------------------------------
-bool CUnescape(absl::string_view source, std::string* dest,
- std::string* error) {
+bool CUnescape(absl::string_view source, absl::Nonnull<std::string*> dest,
+ absl::Nullable<std::string*> error) {
return CUnescapeInternal(source, kUnescapeNulls, dest, error);
}
@@ -883,21 +893,23 @@ std::string Utf8SafeCHexEscape(absl::string_view src) {
return CEscapeInternal(src, true, true);
}
-bool Base64Unescape(absl::string_view src, std::string* dest) {
+bool Base64Unescape(absl::string_view src, absl::Nonnull<std::string*> dest) {
return Base64UnescapeInternal(src.data(), src.size(), dest, kUnBase64);
}
-bool WebSafeBase64Unescape(absl::string_view src, std::string* dest) {
+bool WebSafeBase64Unescape(absl::string_view src,
+ absl::Nonnull<std::string*> dest) {
return Base64UnescapeInternal(src.data(), src.size(), dest, kUnWebSafeBase64);
}
-void Base64Escape(absl::string_view src, std::string* dest) {
+void Base64Escape(absl::string_view src, absl::Nonnull<std::string*> dest) {
strings_internal::Base64EscapeInternal(
reinterpret_cast<const unsigned char*>(src.data()), src.size(), dest,
true, strings_internal::kBase64Chars);
}
-void WebSafeBase64Escape(absl::string_view src, std::string* dest) {
+void WebSafeBase64Escape(absl::string_view src,
+ absl::Nonnull<std::string*> dest) {
strings_internal::Base64EscapeInternal(
reinterpret_cast<const unsigned char*>(src.data()), src.size(), dest,
false, strings_internal::kWebSafeBase64Chars);
diff --git a/absl/strings/escaping.h b/absl/strings/escaping.h
index bf2a5898..f6f7a29c 100644
--- a/absl/strings/escaping.h
+++ b/absl/strings/escaping.h
@@ -28,6 +28,7 @@
#include <vector>
#include "absl/base/macros.h"
+#include "absl/base/nullability.h"
#include "absl/strings/ascii.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
@@ -69,10 +70,12 @@ ABSL_NAMESPACE_BEGIN
// ...
// }
// EXPECT_EQ(unescaped_s, "foo\rbar\nbaz\t");
-bool CUnescape(absl::string_view source, std::string* dest, std::string* error);
+bool CUnescape(absl::string_view source, absl::Nonnull<std::string*> dest,
+ absl::Nullable<std::string*> error);
// Overload of `CUnescape()` with no error reporting.
-inline bool CUnescape(absl::string_view source, std::string* dest) {
+inline bool CUnescape(absl::string_view source,
+ absl::Nonnull<std::string*> dest) {
return CUnescape(source, dest, nullptr);
}
@@ -122,7 +125,7 @@ std::string Utf8SafeCHexEscape(absl::string_view src);
// Encodes a `src` string into a base64-encoded 'dest' string with padding
// characters. This function conforms with RFC 4648 section 4 (base64) and RFC
// 2045.
-void Base64Escape(absl::string_view src, std::string* dest);
+void Base64Escape(absl::string_view src, absl::Nonnull<std::string*> dest);
std::string Base64Escape(absl::string_view src);
// WebSafeBase64Escape()
@@ -130,7 +133,8 @@ std::string Base64Escape(absl::string_view src);
// Encodes a `src` string into a base64 string, like Base64Escape() does, but
// outputs '-' instead of '+' and '_' instead of '/', and does not pad 'dest'.
// This function conforms with RFC 4648 section 5 (base64url).
-void WebSafeBase64Escape(absl::string_view src, std::string* dest);
+void WebSafeBase64Escape(absl::string_view src,
+ absl::Nonnull<std::string*> dest);
std::string WebSafeBase64Escape(absl::string_view src);
// Base64Unescape()
@@ -140,7 +144,7 @@ std::string WebSafeBase64Escape(absl::string_view src);
// `src` contains invalid characters, `dest` is cleared and returns `false`.
// If padding is included (note that `Base64Escape()` does produce it), it must
// be correct. In the padding, '=' and '.' are treated identically.
-bool Base64Unescape(absl::string_view src, std::string* dest);
+bool Base64Unescape(absl::string_view src, absl::Nonnull<std::string*> dest);
// WebSafeBase64Unescape()
//
@@ -149,7 +153,8 @@ bool Base64Unescape(absl::string_view src, std::string* dest);
// invalid characters, `dest` is cleared and returns `false`. If padding is
// included (note that `WebSafeBase64Escape()` does not produce it), it must be
// correct. In the padding, '=' and '.' are treated identically.
-bool WebSafeBase64Unescape(absl::string_view src, std::string* dest);
+bool WebSafeBase64Unescape(absl::string_view src,
+ absl::Nonnull<std::string*> dest);
// HexStringToBytes()
//