X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=database-builder.cpp;fp=database-builder.cpp;h=76479aa3dde8673e5a5f389f244d8895f3370b31;hb=a18a5157796d8db72b0ea1dfea137090bd8a52b9;hp=11a1a5a72b0cefd6cda59ecce997c4b1fb8c5b1c;hpb=9afdc095fdddb37926c075d8700cbc9e928be1e1;p=plocate diff --git a/database-builder.cpp b/database-builder.cpp index 11a1a5a..76479aa 100644 --- a/database-builder.cpp +++ b/database-builder.cpp @@ -5,6 +5,9 @@ #include #include +#ifdef HAS_ENDIAN_H +#include +#endif #include #include #include @@ -26,20 +29,14 @@ constexpr unsigned num_overflow_slots = 16; string zstd_compress(const string &src, ZSTD_CDict *cdict, string *tempbuf); -static inline uint32_t read_unigram(const string_view s, size_t idx) -{ - if (idx < s.size()) { - return (unsigned char)s[idx]; - } else { - return 0; - } -} - +// NOTE: Will read one byte past the end of the trigram, but it's OK, +// since we always call it from contexts where there's a terminating zero byte. static inline uint32_t read_trigram(const string_view s, size_t start) { - return read_unigram(s, start) | - (read_unigram(s, start + 1) << 8) | - (read_unigram(s, start + 2) << 16); + uint32_t trgm; + memcpy(&trgm, s.data() + start, sizeof(trgm)); + trgm = le32toh(trgm); + return trgm & 0xffffff; } class PostingListBuilder {