aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/case_insensitive_string/case_insensitive_char_traits.cpp
blob: 8bec888b55fad94875f28a2f4316b1e2beaf062d (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
#include "case_insensitive_char_traits.h"

#include <util/string/escape.h>

template <typename TImpl>
const char* ::NPrivate::TCommonCaseInsensitiveCharTraits<TImpl>::find(const char* s, std::size_t n, char a) {
    const auto ca(TImpl::ToCommonCase(a));
    while (n-- != 0) {
        if (TImpl::ToCommonCase(*s) == ca)
            return s;
        s++;
    }
    return nullptr;
}

int TCaseInsensitiveCharTraits::compare(const char* s1, const char* s2, std::size_t n) {
    while (n-- != 0) {
        auto c1 = ToCommonCase(*s1), c2 = ToCommonCase(*s2);
        if (c1 < c2) {
            return -1;
        }
        if (c1 > c2) {
            return 1;
        }
        ++s1;
        ++s2;
    }
    return 0;
}

template struct ::NPrivate::TCommonCaseInsensitiveCharTraits<TCaseInsensitiveCharTraits>;
template struct ::NPrivate::TCommonCaseInsensitiveCharTraits<TCaseInsensitiveAsciiCharTraits>;