Index: build_tools/debug_server/debug_server/common/debug_blob.cpp |
=================================================================== |
--- build_tools/debug_server/debug_server/common/debug_blob.cpp (revision 0) |
+++ build_tools/debug_server/debug_server/common/debug_blob.cpp (revision 0) |
@@ -0,0 +1,126 @@ |
+#include "debug_blob.h" |
+#include <algorithm> |
+ |
+namespace { |
+char GetHexDigit(unsigned int value, int digit_position) { |
+ const char* kDigitStrings = "0123456789ABCDEF"; |
+ unsigned int digit = (value >> (4 * digit_position)) & 0xF; |
+ return kDigitStrings[digit]; |
+} |
+} // namespace |
+ |
+namespace debug { |
+Blob::Blob() { |
+} |
+ |
+Blob::Blob(const Blob& other) { |
+ value_ = other.value_; |
+} |
+ |
+Blob::Blob(const void* buff, size_t buff_sz) { |
+ const char* char_buff = static_cast<const char*>(buff); |
+ for (size_t i = 0; i < buff_sz; i++ ) |
+ PushBack(*char_buff++); |
+} |
+ |
+Blob::~Blob() { |
+} |
+ |
+Blob& Blob::operator = (const Blob& other) { |
+ value_ = other.value_; |
+ return *this; |
+} |
+ |
+bool Blob::operator == (const Blob& other) const { |
+ return value_ == other.value_; |
+} |
+ |
+size_t Blob::Size() const { |
+ return value_.size(); |
+} |
+ |
+char Blob::operator[] (size_t position) const { |
+ return value_[position]; |
+} |
+ |
+char Blob::Front() const { |
+ return value_.front(); |
+} |
+ |
+char Blob::Back() const { |
+ return value_.back(); |
+} |
+ |
+char Blob::PopFront() { |
+ char c = value_.front(); |
+ value_.pop_front(); |
+ return c; |
+} |
+ |
+char Blob::PopBack() { |
+ char c = value_.back(); |
+ value_.pop_back(); |
+ return c; |
+} |
+ |
+void Blob::PushFront(char c) { |
+ value_.push_front(c); |
+} |
+ |
+void Blob::PushBack(char c) { |
+ value_.push_back(c); |
+} |
+ |
+void Blob::Clear() { |
+ value_.clear(); |
+} |
+ |
+std::string Blob::ToString() const { |
+ std::string result; |
+ for (size_t i = 0; i < Size(); i++) |
+ result.append(1, value_[i]); |
+ return result; |
+} |
+ |
+std::string Blob::ToHexString() const { |
+ std::string result; |
+ size_t num = Size(); |
+ for (size_t i = 0; i < num; i++) { |
+ result.append(1, GetHexDigit(value_[i], 1)); |
+ result.append(1, GetHexDigit(value_[i], 0)); |
+ } |
+ return result; |
+} |
+ |
+void Blob::Append(const Blob& blob) { |
+ for (size_t i = 0; i < blob.Size(); i++) |
+ PushBack(blob[i]); |
+} |
+ |
+bool Blob::Compare(const Blob& blob, size_t to_length) const { |
+ if (-1 == to_length) { |
+ if (Size() != blob.Size()) |
+ return false; |
+ to_length = Size(); |
+ } |
+ for (size_t i = 0; i < to_length; i++) |
+ if (value_[i] != blob[i]) |
+ return false; |
+ return true; |
+} |
+ |
+bool Blob::HasPrefix(const std::string& prefix) const { |
+ size_t num = prefix.size(); |
+ if (Size() < num) |
+ return false; |
+ for (size_t i = 0; i < num; i++) |
+ if (value_[i] != prefix[i]) |
+ return false; |
+ return true; |
+} |
+ |
+void Blob::Reverse() { |
+ std::reverse(value_.begin(), value_.end()); |
+} |
+ |
+} // namespace debug |