aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/strip.cpp
blob: 0765806359cce7eb24c933104949d600c6900c0f (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
#include "strip.h"
#include "ascii.h"

#include <util/string/reverse.h>

bool Collapse(const TString& from, TString& to, size_t maxLen) { 
    return CollapseImpl<TString, bool (*)(unsigned char)>(from, to, maxLen, IsAsciiSpace); 
}

void CollapseText(const TString& from, TString& to, size_t maxLen) { 
    Collapse(from, to, maxLen);
    StripInPlace(to);
    if (to.size() >= maxLen) {
        to.remove(maxLen - 5); // " ..."
        ReverseInPlace(to);
        size_t pos = to.find_first_of(" .,;");
        if (pos != TString::npos && pos < 32) {
            to.remove(0, pos + 1);
        }
        ReverseInPlace(to);
        to.append(" ...");
    }
}