aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/complete/text/word.cpp
blob: 0468f62b03125297908e2b0ae42f89c99beaffb8 (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
#include "word.h"

#include <util/generic/strbuf.h>

namespace NSQLComplete {

    bool IsWordBoundary(char ch) { // Is optimized into table lookup by clang
        for (size_t i = 0; i < sizeof(WordBreakCharacters) - 1; ++i) {
            if (WordBreakCharacters[i] == ch) {
                return true;
            }
        }
        return false;
    }

    size_t LastWordIndex(TStringBuf text) {
        for (auto it = std::rbegin(text); it != std::rend(text); std::advance(it, 1)) {
            if (IsWordBoundary(*it)) {
                return std::distance(it, std::rend(text));
            }
        }
        return 0;
    }

    TStringBuf LastWord(TStringBuf text) {
        return text.SubStr(LastWordIndex(text));
    }

} // namespace NSQLComplete