string_util.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2013 Dolphin Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstdarg>
  6. #include <iomanip>
  7. #include <string>
  8. #include <sstream>
  9. #include <vector>
  10. #include "common/common.h"
  11. /// Make a string lowercase
  12. void LowerStr(char* str);
  13. /// Make a string uppercase
  14. void UpperStr(char* str);
  15. std::string StringFromFormat(const char* format, ...);
  16. // Cheap!
  17. bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args);
  18. template<size_t Count>
  19. inline void CharArrayFromFormat(char (& out)[Count], const char* format, ...)
  20. {
  21. va_list args;
  22. va_start(args, format);
  23. CharArrayFromFormatV(out, Count, format, args);
  24. va_end(args);
  25. }
  26. // Good
  27. std::string ArrayToString(const u8 *data, u32 size, int line_len = 20, bool spaces = true);
  28. std::string StripSpaces(const std::string &s);
  29. std::string StripQuotes(const std::string &s);
  30. // Thousand separator. Turns 12345678 into 12,345,678
  31. template <typename I>
  32. std::string ThousandSeparate(I value, int spaces = 0)
  33. {
  34. std::ostringstream oss;
  35. // std::locale("") seems to be broken on many platforms
  36. #if defined _WIN32 || (defined __linux__ && !defined __clang__)
  37. oss.imbue(std::locale(""));
  38. #endif
  39. oss << std::setw(spaces) << value;
  40. return oss.str();
  41. }
  42. std::string StringFromInt(int value);
  43. std::string StringFromBool(bool value);
  44. bool TryParse(const std::string &str, bool *output);
  45. bool TryParse(const std::string &str, u32 *output);
  46. template <typename N>
  47. static bool TryParse(const std::string &str, N *const output)
  48. {
  49. std::istringstream iss(str);
  50. N tmp = 0;
  51. if (iss >> tmp)
  52. {
  53. *output = tmp;
  54. return true;
  55. }
  56. else
  57. return false;
  58. }
  59. // TODO: kill this
  60. bool AsciiToHex(const char* _szValue, u32& result);
  61. std::string TabsToSpaces(int tab_size, const std::string &in);
  62. void SplitString(const std::string& str, char delim, std::vector<std::string>& output);
  63. // "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
  64. bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension);
  65. void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, const std::string& _Filename);
  66. std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest);
  67. std::string UriDecode(const std::string & sSrc);
  68. std::string UriEncode(const std::string & sSrc);
  69. std::string CP1252ToUTF8(const std::string& str);
  70. std::string SHIFTJISToUTF8(const std::string& str);
  71. std::string UTF16ToUTF8(const std::wstring& str);
  72. #ifdef _WIN32
  73. std::wstring UTF8ToUTF16(const std::string& str);
  74. #ifdef _UNICODE
  75. inline std::string TStrToUTF8(const std::wstring& str)
  76. { return UTF16ToUTF8(str); }
  77. inline std::wstring UTF8ToTStr(const std::string& str)
  78. { return UTF8ToUTF16(str); }
  79. #else
  80. inline std::string TStrToUTF8(const std::string& str)
  81. { return str; }
  82. inline std::string UTF8ToTStr(const std::string& str)
  83. { return str; }
  84. #endif
  85. #endif