You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ostringstream.h 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_STRINGS_INTERNAL_OSTRINGSTREAM_H_
  15. #define ABSL_STRINGS_INTERNAL_OSTRINGSTREAM_H_
  16. #include <cassert>
  17. #include <ostream>
  18. #include <streambuf>
  19. #include <string>
  20. #include "absl/base/port.h"
  21. namespace absl
  22. {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace strings_internal
  25. {
  26. // The same as std::ostringstream but appends to a user-specified std::string,
  27. // and is faster. It is ~70% faster to create, ~50% faster to write to, and
  28. // completely free to extract the result std::string.
  29. //
  30. // std::string s;
  31. // OStringStream strm(&s);
  32. // strm << 42 << ' ' << 3.14; // appends to `s`
  33. //
  34. // The stream object doesn't have to be named. Starting from C++11 operator<<
  35. // works with rvalues of std::ostream.
  36. //
  37. // std::string s;
  38. // OStringStream(&s) << 42 << ' ' << 3.14; // appends to `s`
  39. //
  40. // OStringStream is faster to create than std::ostringstream but it's still
  41. // relatively slow. Avoid creating multiple streams where a single stream will
  42. // do.
  43. //
  44. // Creates unnecessary instances of OStringStream: slow.
  45. //
  46. // std::string s;
  47. // OStringStream(&s) << 42;
  48. // OStringStream(&s) << ' ';
  49. // OStringStream(&s) << 3.14;
  50. //
  51. // Creates a single instance of OStringStream and reuses it: fast.
  52. //
  53. // std::string s;
  54. // OStringStream strm(&s);
  55. // strm << 42;
  56. // strm << ' ';
  57. // strm << 3.14;
  58. //
  59. // Note: flush() has no effect. No reason to call it.
  60. class OStringStream : private std::basic_streambuf<char>, public std::ostream
  61. {
  62. public:
  63. // The argument can be null, in which case you'll need to call str(p) with a
  64. // non-null argument before you can write to the stream.
  65. //
  66. // The destructor of OStringStream doesn't use the std::string. It's OK to
  67. // destroy the std::string before the stream.
  68. explicit OStringStream(std::string* s) :
  69. std::ostream(this),
  70. s_(s)
  71. {
  72. }
  73. std::string* str()
  74. {
  75. return s_;
  76. }
  77. const std::string* str() const
  78. {
  79. return s_;
  80. }
  81. void str(std::string* s)
  82. {
  83. s_ = s;
  84. }
  85. private:
  86. using Buf = std::basic_streambuf<char>;
  87. Buf::int_type overflow(int c) override;
  88. std::streamsize xsputn(const char* s, std::streamsize n) override;
  89. std::string* s_;
  90. };
  91. } // namespace strings_internal
  92. ABSL_NAMESPACE_END
  93. } // namespace absl
  94. #endif // ABSL_STRINGS_INTERNAL_OSTRINGSTREAM_H_