Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break dependency on google::protobuf::UTF8FirstLetterNumBytes() #814

Merged
merged 2 commits into from Mar 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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