MCPcopy Create free account
hub / github.com/Icinga/icinga2 / TruncateUsingHash

Method TruncateUsingHash

lib/base/utility.hpp:169–187  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

167 */
168 template<size_t maxLength>
169 static String TruncateUsingHash(const String &in) {
170 /*
171 * Note: be careful when changing this function as it is used to derive file names that should not change
172 * between versions or would need special handling if they do (/var/lib/icinga2/api/packages/_api).
173 */
174
175 const size_t sha1HexLength = SHA_DIGEST_LENGTH*2;
176 static_assert(maxLength >= 1 + 3 + sha1HexLength,
177 "maxLength must be at least 44 to hold one character, '...', and a hex-encoded SHA1 hash");
178
179 /* If the input is shorter than the limit, no truncation is needed */
180 if (in.GetLength() < maxLength) {
181 return in;
182 }
183
184 const char *trunc = "...";
185
186 return in.SubStr(0, maxLength - sha1HexLength - strlen(trunc)) + trunc + SHA1(in);
187 }
188
189 static time_t NormalizeTm(tm *t);
190 static time_t TmToTimestamp(const tm *t);

Callers

nothing calls this directly

Calls 3

SHA1Function · 0.85
SubStrMethod · 0.80
GetLengthMethod · 0.45

Tested by

no test coverage detected