诸暨麻将添加redis
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

199 行
6.0 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include <google/protobuf/stubs/bytestream.h>
  31. #include <string.h>
  32. #include <algorithm>
  33. #include <google/protobuf/stubs/logging.h>
  34. namespace google {
  35. namespace protobuf {
  36. namespace strings {
  37. void ByteSource::CopyTo(ByteSink* sink, size_t n) {
  38. while (n > 0) {
  39. StringPiece fragment = Peek();
  40. if (fragment.empty()) {
  41. GOOGLE_LOG(DFATAL) << "ByteSource::CopyTo() overran input.";
  42. break;
  43. }
  44. std::size_t fragment_size = std::min<std::size_t>(n, fragment.size());
  45. sink->Append(fragment.data(), fragment_size);
  46. Skip(fragment_size);
  47. n -= fragment_size;
  48. }
  49. }
  50. void ByteSink::Flush() {}
  51. void UncheckedArrayByteSink::Append(const char* data, size_t n) {
  52. if (data != dest_) {
  53. // Catch cases where the pointer returned by GetAppendBuffer() was modified.
  54. GOOGLE_DCHECK(!(dest_ <= data && data < (dest_ + n)))
  55. << "Append() data[] overlaps with dest_[]";
  56. memcpy(dest_, data, n);
  57. }
  58. dest_ += n;
  59. }
  60. CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, size_t capacity)
  61. : outbuf_(outbuf), capacity_(capacity), size_(0), overflowed_(false) {
  62. }
  63. void CheckedArrayByteSink::Append(const char* bytes, size_t n) {
  64. size_t available = capacity_ - size_;
  65. if (n > available) {
  66. n = available;
  67. overflowed_ = true;
  68. }
  69. if (n > 0 && bytes != (outbuf_ + size_)) {
  70. // Catch cases where the pointer returned by GetAppendBuffer() was modified.
  71. GOOGLE_DCHECK(!(outbuf_ <= bytes && bytes < (outbuf_ + capacity_)))
  72. << "Append() bytes[] overlaps with outbuf_[]";
  73. memcpy(outbuf_ + size_, bytes, n);
  74. }
  75. size_ += n;
  76. }
  77. GrowingArrayByteSink::GrowingArrayByteSink(size_t estimated_size)
  78. : capacity_(estimated_size),
  79. buf_(new char[estimated_size]),
  80. size_(0) {
  81. }
  82. GrowingArrayByteSink::~GrowingArrayByteSink() {
  83. delete[] buf_; // Just in case the user didn't call GetBuffer.
  84. }
  85. void GrowingArrayByteSink::Append(const char* bytes, size_t n) {
  86. size_t available = capacity_ - size_;
  87. if (bytes != (buf_ + size_)) {
  88. // Catch cases where the pointer returned by GetAppendBuffer() was modified.
  89. // We need to test for this before calling Expand() which may reallocate.
  90. GOOGLE_DCHECK(!(buf_ <= bytes && bytes < (buf_ + capacity_)))
  91. << "Append() bytes[] overlaps with buf_[]";
  92. }
  93. if (n > available) {
  94. Expand(n - available);
  95. }
  96. if (n > 0 && bytes != (buf_ + size_)) {
  97. memcpy(buf_ + size_, bytes, n);
  98. }
  99. size_ += n;
  100. }
  101. char* GrowingArrayByteSink::GetBuffer(size_t* nbytes) {
  102. ShrinkToFit();
  103. char* b = buf_;
  104. *nbytes = size_;
  105. buf_ = nullptr;
  106. size_ = capacity_ = 0;
  107. return b;
  108. }
  109. void GrowingArrayByteSink::Expand(size_t amount) { // Expand by at least 50%.
  110. size_t new_capacity = std::max(capacity_ + amount, (3 * capacity_) / 2);
  111. char* bigger = new char[new_capacity];
  112. memcpy(bigger, buf_, size_);
  113. delete[] buf_;
  114. buf_ = bigger;
  115. capacity_ = new_capacity;
  116. }
  117. void GrowingArrayByteSink::ShrinkToFit() {
  118. // Shrink only if the buffer is large and size_ is less than 3/4
  119. // of capacity_.
  120. if (capacity_ > 256 && size_ < (3 * capacity_) / 4) {
  121. char* just_enough = new char[size_];
  122. memcpy(just_enough, buf_, size_);
  123. delete[] buf_;
  124. buf_ = just_enough;
  125. capacity_ = size_;
  126. }
  127. }
  128. void StringByteSink::Append(const char* data, size_t n) {
  129. dest_->append(data, n);
  130. }
  131. size_t ArrayByteSource::Available() const {
  132. return input_.size();
  133. }
  134. StringPiece ArrayByteSource::Peek() {
  135. return input_;
  136. }
  137. void ArrayByteSource::Skip(size_t n) {
  138. GOOGLE_DCHECK_LE(n, input_.size());
  139. input_.remove_prefix(n);
  140. }
  141. LimitByteSource::LimitByteSource(ByteSource *source, size_t limit)
  142. : source_(source),
  143. limit_(limit) {
  144. }
  145. size_t LimitByteSource::Available() const {
  146. size_t available = source_->Available();
  147. if (available > limit_) {
  148. available = limit_;
  149. }
  150. return available;
  151. }
  152. StringPiece LimitByteSource::Peek() {
  153. StringPiece piece(source_->Peek());
  154. if (piece.size() > limit_) {
  155. piece.set(piece.data(), limit_);
  156. }
  157. return piece;
  158. }
  159. void LimitByteSource::Skip(size_t n) {
  160. GOOGLE_DCHECK_LE(n, limit_);
  161. source_->Skip(n);
  162. limit_ -= n;
  163. }
  164. void LimitByteSource::CopyTo(ByteSink *sink, size_t n) {
  165. GOOGLE_DCHECK_LE(n, limit_);
  166. source_->CopyTo(sink, n);
  167. limit_ -= n;
  168. }
  169. } // namespace strings
  170. } // namespace protobuf
  171. } // namespace google