strip.cpp 658 B

1234567891011121314151617181920212223
  1. #include "strip.h"
  2. #include "ascii.h"
  3. #include <util/string/reverse.h>
  4. bool Collapse(const TString& from, TString& to, size_t maxLen) {
  5. return CollapseImpl<TString, bool (*)(unsigned char)>(from, to, maxLen, IsAsciiSpace);
  6. }
  7. void CollapseText(const TString& from, TString& to, size_t maxLen) {
  8. Collapse(from, to, maxLen);
  9. StripInPlace(to);
  10. if (to.size() >= maxLen) {
  11. to.remove(maxLen - 5); // " ..."
  12. ReverseInPlace(to);
  13. size_t pos = to.find_first_of(" .,;");
  14. if (pos != TString::npos && pos < 32) {
  15. to.remove(0, pos + 1);
  16. }
  17. ReverseInPlace(to);
  18. to.append(" ...");
  19. }
  20. }