string_util.h 3.0 KB

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