xref: /aosp_15_r20/external/icing/icing/legacy/core/icing-string-util.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
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 <cstdarg>
19 #include <cstdint>
20 #include <string>
21 
22 #include "icing/legacy/core/icing-compat.h"
23 
24 namespace icing {
25 namespace lib {
26 
27 class IcingStringUtil {
28  public:
29   // Returns true if the character is not the first byte of
30   // a multi-byte UTF8 character
IsContinuationByte(char byte)31   static bool IsContinuationByte(char byte) {
32     return (static_cast<uint8_t>(byte) & 0xC0) == 0x80;
33   }
34 
35   // Update a rolling crc32. This undoes the one's complement
36   // pre-conditioning and post-conditioning of zlib's
37   // crc32. Therefore, UpdateCrc32(0, str, len) != HashCrc32(str,
38   // len).
39   static uint32_t UpdateCrc32(uint32_t crc, const char *str, int len);
40 
41   // Update a string's rolling crc for when its value at offset is
42   // xor'ed with the buffer [xored_str, xored_str + len).
43   //
44   // REQUIRES: orig_len >= offset + len.
45   static uint32_t UpdateAtPositionCrc32(uint32_t crc, int orig_len, int offset,
46                                         const char *xored_str, int len);
47 
48   // Append vsnprintf to strp. If bufsize hint is > 0 it is
49   // used. Otherwise we compute the required bufsize (which is somewhat
50   // expensive).
51   static void SStringAppendV(std::string *strp, int bufsize, const char *fmt,
52                              va_list arglist);
53   static void SStringAppendF(std::string *strp, int bufsize, const char *fmt,
54                              ...) __attribute__((format(printf, 3, 4)));
55   static std::string StringPrintf(const char *fmt, ...)
56       __attribute__((format(printf, 1, 2)));
57 };
58 
59 }  // namespace lib
60 }  // namespace icing
61 
62 #endif  // ICING_LEGACY_CORE_ICING_STRING_UTIL_H_
63