• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ICING_LEGACY_CORE_ICING_STRING_UTIL_H_
16 #define ICING_LEGACY_CORE_ICING_STRING_UTIL_H_
17 
18 #include <stdarg.h>
19 #include <stdint.h>
20 
21 #include <string>
22 
23 #include "icing/legacy/core/icing-compat.h"
24 
25 namespace icing {
26 namespace lib {
27 
28 class IcingStringUtil {
29  public:
30   // Returns true if the character is not the first byte of
31   // a multi-byte UTF8 character
IsContinuationByte(char byte)32   static bool IsContinuationByte(char byte) {
33     return (static_cast<uint8_t>(byte) & 0xC0) == 0x80;
34   }
35 
36   // Update a rolling crc32. This undoes the one's complement
37   // pre-conditioning and post-conditioning of zlib's
38   // crc32. Therefore, UpdateCrc32(0, str, len) != HashCrc32(str,
39   // len).
40   static uint32_t UpdateCrc32(uint32_t crc, const char *str, int len);
41 
42   // Update a string's rolling crc for when its value at offset is
43   // xor'ed with the buffer [xored_str, xored_str + len).
44   //
45   // REQUIRES: orig_len >= offset + len.
46   static uint32_t UpdateAtPositionCrc32(uint32_t crc, int orig_len, int offset,
47                                         const char *xored_str, int len);
48 
49   // Append vsnprintf to strp. If bufsize hint is > 0 it is
50   // used. Otherwise we compute the required bufsize (which is somewhat
51   // expensive).
52   static void SStringAppendV(std::string *strp, int bufsize, const char *fmt,
53                              va_list arglist);
54   static void SStringAppendF(std::string *strp, int bufsize, const char *fmt,
55                              ...) __attribute__((format(printf, 3, 4)));
56   static std::string StringPrintf(const char *fmt, ...)
57       __attribute__((format(printf, 1, 2)));
58 };
59 
60 }  // namespace lib
61 }  // namespace icing
62 
63 #endif  // ICING_LEGACY_CORE_ICING_STRING_UTIL_H_
64