subst.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #pragma once
  2. #include <util/generic/fwd.h>
  3. #include <stlfwd>
  4. /* Replace all occurences of substring `what` with string `with` starting from position `from`.
  5. *
  6. * @param text String to modify.
  7. * @param what Substring to replace.
  8. * @param with Substring to use as replacement.
  9. * @param from Position at with to start replacement.
  10. *
  11. * @return Number of replacements occured.
  12. */
  13. size_t SubstGlobal(TString& text, TStringBuf what, TStringBuf with, size_t from = 0);
  14. size_t SubstGlobal(std::string& text, TStringBuf what, TStringBuf with, size_t from = 0);
  15. size_t SubstGlobal(TUtf16String& text, TWtringBuf what, TWtringBuf with, size_t from = 0);
  16. size_t SubstGlobal(std::u16string& text, TWtringBuf what, TWtringBuf with, size_t from = 0);
  17. size_t SubstGlobal(TUtf32String& text, TUtf32StringBuf what, TUtf32StringBuf with, size_t from = 0);
  18. /* Replace all occurences of character `what` with character `with` starting from position `from`.
  19. *
  20. * @param text String to modify.
  21. * @param what Character to replace.
  22. * @param with Character to use as replacement.
  23. * @param from Position at with to start replacement.
  24. *
  25. * @return Number of replacements occured.
  26. */
  27. size_t SubstGlobal(TString& text, char what, char with, size_t from = 0);
  28. size_t SubstGlobal(std::string& text, char what, char with, size_t from = 0);
  29. size_t SubstGlobal(TUtf16String& text, wchar16 what, wchar16 with, size_t from = 0);
  30. size_t SubstGlobal(std::u16string& text, wchar16 what, wchar16 with, size_t from = 0);
  31. size_t SubstGlobal(TUtf32String& text, wchar32 what, wchar32 with, size_t from = 0);
  32. // TODO(yazevnul):
  33. // - rename `SubstGlobal` to `ReplaceAll` for convenience
  34. // - add `SubstGlobalCopy(TStringBuf)` for convenience
  35. // - add `RemoveAll(text, what, from)` as a shortcut for `SubstGlobal(text, what, "", from)`
  36. // - rename file to `replace.h`
  37. /* Replace all occurences of substring or character `what` with string or character `with` starting from position `from`, and return result string.
  38. *
  39. * @param text String to modify.
  40. * @param what Substring/character to replace.
  41. * @param with Substring/character to use as replacement.
  42. * @param from Position at with to start replacement.
  43. *
  44. * @return Result string
  45. */
  46. template <class TStringType, class TPatternType>
  47. Y_WARN_UNUSED_RESULT TStringType SubstGlobalCopy(TStringType result, TPatternType what, TPatternType with, size_t from = 0) {
  48. SubstGlobal(result, what, with, from);
  49. return result;
  50. }