aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/util/foldedstringcompare.h
blob: cd7cd325667bbe9b074a6a4b906a109102364bb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <cstddef>
#include <cstdint>
#include <functional>
#include <vector>

namespace search {

namespace detail {

template <typename T>
concept FoldableString = std::same_as<const char*,T> || std::same_as<std::reference_wrapper<const std::vector<uint32_t>>, T>;

}

class FoldedStringCompare
{
public:
    /**
     * count number of UCS-4 characters in UTF-8 string
     *
     * @param key       NUL terminated UTF-8 string
     * @return integer  number of symbols in UTF-8 string before NUL
     */
    static size_t size(const char *key);

    /**
     * Compare UTF-8 key with UTF-8 other key after folding both
     *
     * @param key       NUL terminated UTF-8 string or vector<uint32_t>
     * @param okey      NUL terminated UTF-8 string or vector<uint32_t>
     * @return integer   -1 if key < okey, 0 if key == okey, 1 if key > okey
     **/
    template <bool fold_lhs, bool fold_rhs, detail::FoldableString KeyType, detail::FoldableString OKeyType>
    static int compareFolded(KeyType key, OKeyType okey);

    /**
     * Compare UTF-8 key with UTF-8 other key after folding both.
     *
     * @param key         NUL terminated UTF-8 string
     * @param okey        NUL terminated UTF-8 string
     * @param prefixLen   max number of symbols to compare before
     *                    considering keys identical.
     *
     * @return integer   -1 if key < okey, 0 if key == okey, 1 if key > okey
     */
    template <bool fold_lhs, bool fold_rhs>
    static int compareFoldedPrefix(const char *key, const char *okey, size_t prefixLen);

    /*
     * Compare UTF-8 key with UTF-8 other key after folding both, if
     * they seem equal then fall back to comparing without folding.
     *
     * @param key       NUL terminated UTF-8 string
     * @param okey      NUL terminated UTF-8 string
     * @return integer   -1 if key < okey, 0 if key == okey, 1 if key > okey
     */
    static int compare(const char *key, const char *okey);

    /*
     * Compare UTF-8 key with UTF-8 other key after folding both for prefix, if
     * they seem equal then fall back to comparing without folding.
     *
     * @param key         NUL terminated UTF-8 string
     * @param okey        NUL terminated UTF-8 string
     * @param prefixLen   max number of symbols to compare before
     *                    considering keys identical.
     * @return integer   -1 if key < okey, 0 if key == okey, 1 if key > okey
     */
    static int comparePrefix(const char *key, const char *okey, size_t prefixLen);
};

} // namespace search