string_util.h 2.8 KB

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