From bec8c046525386413dbcfd225b6480ca2e2c31b7 Mon Sep 17 00:00:00 2001 From: yanavlasov Date: Mon, 20 Mar 2023 14:45:26 -0400 Subject: [PATCH] Break dependency on google::protobuf::UTF8FirstLetterNumBytes() (#814) Remove dependency on the `google::protobuf::UTF8FirstLetterNumBytes()` to prep for the 22.x upgrade. The UTF8 utilities were removed from protobuf in 22.x. Jump to 22.x protobuf is needed for the C++20 transition. Since the needed code was very simple I have copied the functions from the protobuf to the validate.h file. Signed-off-by: Yan Avlasov Co-authored-by: Elliot Jackson <13633636+elliotmjackson@users.noreply.github.com> --- validate/validate.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/validate/validate.h b/validate/validate.h index 190272e37..d6cf6c9d9 100644 --- a/validate/validate.h +++ b/validate/validate.h @@ -28,7 +28,6 @@ #endif #include "google/protobuf/message.h" -#include "google/protobuf/stubs/strutil.h" // for UTF8Len namespace pgv { using std::string; @@ -151,13 +150,25 @@ static inline bool IsHostname(const string& to_validate) { return true; } -static inline size_t Utf8Len(const string& narrow_string) { +namespace { + +inline int OneCharLen(const char* src) { + return "\1\1\1\1\1\1\1\1\1\1\1\1\2\2\3\4"[(*src & 0xFF) >> 4]; +} + +inline int UTF8FirstLetterNumBytes(const char *utf8_str, int str_len) { + if (str_len == 0) + return 0; + return OneCharLen(utf8_str); +} + +inline size_t Utf8Len(const string& narrow_string) { const char* str_char = narrow_string.c_str(); ptrdiff_t byte_len = narrow_string.length(); size_t unicode_len = 0; int char_len = 1; while (byte_len > 0 && char_len > 0) { - char_len = google::protobuf::UTF8FirstLetterNumBytes(str_char, byte_len); + char_len = UTF8FirstLetterNumBytes(str_char, byte_len); str_char += char_len; byte_len -= char_len; ++unicode_len; @@ -165,6 +176,8 @@ static inline size_t Utf8Len(const string& narrow_string) { return unicode_len; } +} // namespace + } // namespace pgv #endif // _VALIDATE_H