string_util.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-FileCopyrightText: 2013 Dolphin Emulator Project
  2. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #pragma once
  5. #include <cstddef>
  6. #include <string>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. namespace Common {
  10. /// Make a string lowercase
  11. [[nodiscard]] std::string ToLower(std::string str);
  12. /// Make a string uppercase
  13. [[nodiscard]] std::string ToUpper(std::string str);
  14. [[nodiscard]] std::string StringFromBuffer(const std::vector<u8>& data);
  15. [[nodiscard]] std::string StripSpaces(const std::string& s);
  16. [[nodiscard]] std::string StripQuotes(const std::string& s);
  17. [[nodiscard]] std::string StringFromBool(bool value);
  18. [[nodiscard]] std::string TabsToSpaces(int tab_size, std::string in);
  19. void SplitString(const std::string& str, char delim, std::vector<std::string>& output);
  20. // "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
  21. bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
  22. std::string* _pExtension);
  23. [[nodiscard]] std::string ReplaceAll(std::string result, const std::string& src,
  24. const std::string& dest);
  25. [[nodiscard]] std::string UTF16ToUTF8(const std::u16string& input);
  26. [[nodiscard]] std::u16string UTF8ToUTF16(const std::string& input);
  27. #ifdef _WIN32
  28. [[nodiscard]] std::string UTF16ToUTF8(const std::wstring& input);
  29. [[nodiscard]] std::wstring UTF8ToUTF16W(const std::string& str);
  30. #endif
  31. /**
  32. * Compares the string defined by the range [`begin`, `end`) to the null-terminated C-string
  33. * `other` for equality.
  34. */
  35. template <typename InIt>
  36. [[nodiscard]] bool ComparePartialString(InIt begin, InIt end, const char* other) {
  37. for (; begin != end && *other != '\0'; ++begin, ++other) {
  38. if (*begin != *other) {
  39. return false;
  40. }
  41. }
  42. // Only return true if both strings finished at the same point
  43. return (begin == end) == (*other == '\0');
  44. }
  45. /**
  46. * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't
  47. * NUL-terminated then the string ends at max_len characters.
  48. */
  49. [[nodiscard]] std::string StringFromFixedZeroTerminatedBuffer(std::string_view buffer,
  50. std::size_t max_len);
  51. /**
  52. * Creates a UTF-16 std::u16string from a fixed-size NUL-terminated char buffer. If the buffer isn't
  53. * null-terminated, then the string ends at the greatest multiple of two less then or equal to
  54. * max_len_bytes.
  55. */
  56. [[nodiscard]] std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer,
  57. std::size_t max_len);
  58. } // namespace Common