1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "lang_id/common/utf8.h"
18 
19 namespace libtextclassifier3 {
20 namespace mobile {
21 namespace utils {
22 
GetSafeEndOfUtf8String(const char * data,size_t size)23 const char *GetSafeEndOfUtf8String(const char *data, size_t size) {
24   const char *const hard_end = data + size;
25   const char *curr = data;
26   while (curr < hard_end && *curr) {
27     int num_bytes = utils::OneCharLen(curr);
28     const char *new_curr = curr + num_bytes;
29     if (new_curr > hard_end) {
30       return curr;
31     }
32     curr = new_curr;
33   }
34   return curr;
35 }
36 
37 }  // namespace utils
38 }  // namespace mobile
39 }  // namespace nlp_saft
40