string_util.h 2.9 KB

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