Skip to content

Commit

Permalink
Break dependency on google::protobuf::UTF8FirstLetterNumBytes() (#814)
Browse files Browse the repository at this point in the history
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 <yavlasov@google.com>
Co-authored-by: Elliot Jackson <13633636+elliotmjackson@users.noreply.github.com>
  • Loading branch information
yanavlasov and elliotmjackson committed Mar 20, 2023
1 parent d153612 commit bec8c04
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions validate/validate.h
Expand Up @@ -28,7 +28,6 @@
#endif

#include "google/protobuf/message.h"
#include "google/protobuf/stubs/strutil.h" // for UTF8Len

namespace pgv {
using std::string;
Expand Down Expand Up @@ -151,20 +150,34 @@ 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;
}
return unicode_len;
}

} // namespace

} // namespace pgv

#endif // _VALIDATE_H

0 comments on commit bec8c04

Please sign in to comment.