OR-Tools  8.1
encodingutils.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_BASE_ENCODINGUTILS_H_
15 #define OR_TOOLS_BASE_ENCODINGUTILS_H_
16 
17 #include <string>
18 
19 namespace EncodingUtils {
20 
21 // Returns the number of characters of a UTF8-encoded string.
22 inline int UTF8StrLen(const std::string& utf8_str) {
23  if (utf8_str.empty()) return 0;
24  const char* c = utf8_str.c_str();
25  int count = 0;
26  while (*c != '\0') {
27  ++count;
28  // See http://en.wikipedia.org/wiki/UTF-8#Description .
29  const unsigned char x = *c;
30  c += x < 0xC0 ? 1 : x < 0xE0 ? 2 : x < 0xF0 ? 3 : 4;
31  }
32  return count;
33 }
34 
35 } // namespace EncodingUtils
36 
37 #endif // OR_TOOLS_BASE_ENCODINGUTILS_H_
EncodingUtils::UTF8StrLen
int UTF8StrLen(const std::string &utf8_str)
Definition: encodingutils.h:22
EncodingUtils
Definition: encodingutils.h:19