Skip to content

Commit bec8c04

Browse files
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 <[email protected]> Co-authored-by: Elliot Jackson <[email protected]>
1 parent d153612 commit bec8c04

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

validate/validate.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#endif
2929

3030
#include "google/protobuf/message.h"
31-
#include "google/protobuf/stubs/strutil.h" // for UTF8Len
3231

3332
namespace pgv {
3433
using std::string;
@@ -151,20 +150,34 @@ static inline bool IsHostname(const string& to_validate) {
151150
return true;
152151
}
153152

154-
static inline size_t Utf8Len(const string& narrow_string) {
153+
namespace {
154+
155+
inline int OneCharLen(const char* src) {
156+
return "\1\1\1\1\1\1\1\1\1\1\1\1\2\2\3\4"[(*src & 0xFF) >> 4];
157+
}
158+
159+
inline int UTF8FirstLetterNumBytes(const char *utf8_str, int str_len) {
160+
if (str_len == 0)
161+
return 0;
162+
return OneCharLen(utf8_str);
163+
}
164+
165+
inline size_t Utf8Len(const string& narrow_string) {
155166
const char* str_char = narrow_string.c_str();
156167
ptrdiff_t byte_len = narrow_string.length();
157168
size_t unicode_len = 0;
158169
int char_len = 1;
159170
while (byte_len > 0 && char_len > 0) {
160-
char_len = google::protobuf::UTF8FirstLetterNumBytes(str_char, byte_len);
171+
char_len = UTF8FirstLetterNumBytes(str_char, byte_len);
161172
str_char += char_len;
162173
byte_len -= char_len;
163174
++unicode_len;
164175
}
165176
return unicode_len;
166177
}
167178

179+
} // namespace
180+
168181
} // namespace pgv
169182

170183
#endif // _VALIDATE_H

0 commit comments

Comments
 (0)