aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/reverse.cpp
blob: 167cd11f49b4b66ef12d932eef978e989f7ad358 (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
#include "reverse.h"

#include <util/generic/string.h>
#include <util/generic/vector.h>
#include <util/charset/wide_specific.h>

#include <algorithm>

void ReverseInPlace(TString& string) {
    auto* begin = string.begin();
    std::reverse(begin, begin + string.size());
}

void ReverseInPlace(TUtf16String& string) {
    auto* begin = string.begin();
    const auto len = string.size();
    auto* end = begin + string.size();

    TVector<wchar16> buffer(len);
    wchar16* rbegin = buffer.data() + len;
    for (wchar16* p = begin; p < end;) {
        const size_t symbolSize = W16SymbolSize(p, end);
        rbegin -= symbolSize;
        std::copy(p, p + symbolSize, rbegin);
        p += symbolSize;
    }
    std::copy(buffer.begin(), buffer.end(), begin);
}

void ReverseInPlace(TUtf32String& string) {
    auto* begin = string.begin();
    std::reverse(begin, begin + string.size());
}