string_stream.inl - エンジンの C API リファレンス

string_stream.inl
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "encoding.h"
  5. namespace stingray_plugin_foundation {
  6. inline StringStream::StringStream(Allocator &a) : _buffer(a) {}
  7. inline StringStream & StringStream::operator<<(char c)
  8. {
  9. _buffer.push_back(c);
  10. return *this;
  11. }
  12. inline StringStream &StringStream::operator<<(const char *s)
  13. {
  14. int end = _buffer.size();
  15. int n = (int)strlen(s);
  16. _buffer.resize(end + n);
  17. if (n > 0)
  18. memmove(&_buffer[end], s, n);
  19. return *this;
  20. }
  21. inline StringStream &StringStream::operator<<(char *s)
  22. {
  23. int end = _buffer.size();
  24. int n = (int)strlen(s);
  25. _buffer.resize(end + n);
  26. if (n > 0)
  27. memmove(&_buffer[end], s, n);
  28. return *this;
  29. }
  30. inline StringStream &StringStream::operator<<(const wchar_t *s)
  31. {
  32. while (*s) {
  33. char utf8[6];
  34. *encoding::utf8_encode(*s, utf8) = 0;
  35. (*this) << utf8;
  36. ++s;
  37. }
  38. return *this;
  39. }
  40. inline StringStream &StringStream::operator<<(wchar_t *s)
  41. {
  42. while (*s) {
  43. char utf8[6];
  44. *encoding::utf8_encode(*s, utf8) = 0;
  45. (*this) << utf8;
  46. ++s;
  47. }
  48. return *this;
  49. }
  50. inline StringStream &StringStream::operator<<(double d)
  51. {
  52. char buffer[64];
  53. sprintf(buffer, "%.17g", d);
  54. (*this) << buffer;
  55. return *this;
  56. }
  57. inline StringStream &StringStream::operator<<(float f)
  58. {
  59. char buffer[20];
  60. sprintf(buffer, "%.9g", f);
  61. (*this) << buffer;
  62. return *this;
  63. }
  64. inline StringStream &StringStream::operator<<(int i)
  65. {
  66. char buffer[20];
  67. sprintf(buffer, "%i", i);
  68. (*this) << buffer;
  69. return *this;
  70. }
  71. inline StringStream &StringStream::operator<<(unsigned u)
  72. {
  73. char buffer[20];
  74. sprintf(buffer, "%u", u);
  75. (*this) << buffer;
  76. return *this;
  77. }
  78. inline StringStream &StringStream::operator<<(uint64_t u)
  79. {
  80. char buffer[20];
  81. sprintf(buffer, "%016" "llx", u);
  82. (*this) << buffer;
  83. return *this;
  84. }
  85. inline void StringStream::set_capacity(unsigned s)
  86. {
  87. _buffer.set_capacity(s);
  88. }
  89. inline const char *StringStream::c_str() const
  90. {
  91. _buffer.push_back(0);
  92. _buffer.pop_back();
  93. return _buffer.begin();
  94. }
  95. inline unsigned StringStream::size() const
  96. {
  97. return _buffer.size();
  98. }
  99. } // namespace stingray_plugin_foundation