reverse.cpp 894 B

123456789101112131415161718192021222324252627282930313233
  1. #include "reverse.h"
  2. #include <util/generic/string.h>
  3. #include <util/generic/vector.h>
  4. #include <util/charset/wide_specific.h>
  5. #include <algorithm>
  6. void ReverseInPlace(TString& string) {
  7. auto* begin = string.begin();
  8. std::reverse(begin, begin + string.size());
  9. }
  10. void ReverseInPlace(TUtf16String& string) {
  11. auto* begin = string.begin();
  12. const auto len = string.size();
  13. auto* end = begin + string.size();
  14. TVector<wchar16> buffer(len);
  15. wchar16* rbegin = buffer.data() + len;
  16. for (wchar16* p = begin; p < end;) {
  17. const size_t symbolSize = W16SymbolSize(p, end);
  18. rbegin -= symbolSize;
  19. std::copy(p, p + symbolSize, rbegin);
  20. p += symbolSize;
  21. }
  22. std::copy(buffer.begin(), buffer.end(), begin);
  23. }
  24. void ReverseInPlace(TUtf32String& string) {
  25. auto* begin = string.begin();
  26. std::reverse(begin, begin + string.size());
  27. }