诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

89 line
2.0 KiB

  1. #include "stdafx.h"
  2. #include "StringUtil.h"
  3. #include <stdarg.h>
  4. namespace zl
  5. {
  6. namespace base
  7. {
  8. #ifdef OS_WINDOWS
  9. #define VADUP(aq, ap) (backup_ap = ap)
  10. #else
  11. #define VADUP(aq, ap) va_copy(backup_ap, ap)
  12. #endif
  13. size_t stringFormatAppendImpl(std::string *dst, const char *format, va_list ap)
  14. {
  15. char space[1024]; // just try with a small fixed size buffer
  16. va_list backup_ap; // see "man va_start"
  17. VADUP(backup_ap, ap);
  18. int result = vsnprintf(space, sizeof(space), format, backup_ap);
  19. va_end(backup_ap);
  20. if ((result >= 0) && (result < static_cast<int>(sizeof(space))))
  21. {
  22. dst->append(space, result);
  23. return result;
  24. }
  25. int length = sizeof(space);
  26. while (true) // Repeatedly increase buffer size until it fits
  27. {
  28. if (result < 0)
  29. {
  30. length *= 2;
  31. }
  32. else
  33. {
  34. length = result + 1;
  35. }
  36. char* buf = new char[length];
  37. VADUP(backup_ap, ap); // Restore the va_list before we use it again
  38. result = vsnprintf(buf, length, format, backup_ap);
  39. va_end(backup_ap);
  40. if ((result >= 0) && (result < length))
  41. {
  42. dst->append(buf, result);
  43. delete[] buf;
  44. break;
  45. }
  46. delete[] buf;
  47. }
  48. return result;
  49. }
  50. size_t stringFormatAppend(std::string *dst, const char *format, ...)
  51. {
  52. va_list ap;
  53. va_start(ap, format);
  54. size_t result = stringFormatAppendImpl(dst, format, ap);
  55. va_end(ap);
  56. return result;
  57. }
  58. size_t stringFormat(std::string *dst, const char *format, ...)
  59. {
  60. va_list ap;
  61. va_start(ap, format);
  62. dst->clear();
  63. size_t result = stringFormatAppendImpl(dst, format, ap);
  64. va_end(ap);
  65. return result;
  66. }
  67. std::string stringFormat(const char *format, ...)
  68. {
  69. va_list ap;
  70. va_start(ap, format);
  71. std::string result;
  72. stringFormatAppendImpl(&result, format, ap);
  73. va_end(ap);
  74. return result;
  75. }
  76. } // namespace base
  77. } // namespace zl