[ 
https://issues.apache.org/jira/browse/ARROW-1623?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16348934#comment-16348934
 ] 

ASF GitHub Bot commented on ARROW-1623:
---------------------------------------

wesm closed pull request #1518: ARROW-1623: [C++] Add convenience method to 
construct Buffer from a string that owns its memory
URL: https://github.com/apache/arrow/pull/1518
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/cpp/src/arrow/buffer-test.cc b/cpp/src/arrow/buffer-test.cc
index 398cc0636..a24384a38 100644
--- a/cpp/src/arrow/buffer-test.cc
+++ b/cpp/src/arrow/buffer-test.cc
@@ -52,6 +52,23 @@ TEST(TestBuffer, FromStdString) {
   ASSERT_EQ(static_cast<int64_t>(val.size()), buf.size());
 }
 
+TEST(TestBuffer, FromStdStringWithMemory) {
+  std::string expected = "hello, world";
+  std::shared_ptr<Buffer> buf;
+
+  {
+    std::string temp = "hello, world";
+    ASSERT_OK(Buffer::FromString(temp, &buf));
+    ASSERT_EQ(0, memcmp(buf->data(), temp.c_str(), temp.size()));
+    ASSERT_EQ(static_cast<int64_t>(temp.size()), buf->size());
+  }
+
+  // Now temp goes out of scope and we check if created buffer
+  // is still valid to make sure it actually owns its space
+  ASSERT_EQ(0, memcmp(buf->data(), expected.c_str(), expected.size()));
+  ASSERT_EQ(static_cast<int64_t>(expected.size()), buf->size());
+}
+
 TEST(TestBuffer, Resize) {
   PoolBuffer buf;
 
diff --git a/cpp/src/arrow/buffer.cc b/cpp/src/arrow/buffer.cc
index 1b8e43754..29e2c242a 100644
--- a/cpp/src/arrow/buffer.cc
+++ b/cpp/src/arrow/buffer.cc
@@ -58,6 +58,18 @@ bool Buffer::Equals(const Buffer& other) const {
                              !memcmp(data_, other.data_, 
static_cast<size_t>(size_))));
 }
 
+Status Buffer::FromString(const std::string& data, MemoryPool* pool,
+                          std::shared_ptr<Buffer>* out) {
+  auto size = static_cast<int64_t>(data.size());
+  RETURN_NOT_OK(AllocateBuffer(pool, size, out));
+  std::copy(data.c_str(), data.c_str() + size, (*out)->mutable_data());
+  return Status::OK();
+}
+
+Status Buffer::FromString(const std::string& data, std::shared_ptr<Buffer>* 
out) {
+  return FromString(data, default_memory_pool(), out);
+}
+
 PoolBuffer::PoolBuffer(MemoryPool* pool) : ResizableBuffer(nullptr, 0) {
   if (pool == nullptr) {
     pool = default_memory_pool();
diff --git a/cpp/src/arrow/buffer.h b/cpp/src/arrow/buffer.h
index 44c352a93..d12eeb4df 100644
--- a/cpp/src/arrow/buffer.h
+++ b/cpp/src/arrow/buffer.h
@@ -97,6 +97,20 @@ class ARROW_EXPORT Buffer {
   Status Copy(const int64_t start, const int64_t nbytes,
               std::shared_ptr<Buffer>* out) const;
 
+  /// \brief Construct a new buffer that owns its memory from a std::string
+  ///
+  /// \param[in] data a std::string object
+  /// \param[in] pool a memory pool
+  /// \param[out] out the created buffer
+  ///
+  /// \return Status message
+  static Status FromString(const std::string& data, MemoryPool* pool,
+                           std::shared_ptr<Buffer>* out);
+
+  /// \brief Construct a new buffer that owns its memory from a std::string
+  /// using the default memory pool
+  static Status FromString(const std::string& data, std::shared_ptr<Buffer>* 
out);
+
   int64_t capacity() const { return capacity_; }
   const uint8_t* data() const { return data_; }
   uint8_t* mutable_data() { return mutable_data_; }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> [C++] Add convenience method to construct Buffer from a string that owns its 
> memory
> -----------------------------------------------------------------------------------
>
>                 Key: ARROW-1623
>                 URL: https://issues.apache.org/jira/browse/ARROW-1623
>             Project: Apache Arrow
>          Issue Type: Improvement
>          Components: C++
>            Reporter: Wes McKinney
>            Assignee: Panchen Xue
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 0.9.0
>
>
> The memory would need to be allocated from a memory pool / buffer allocator



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to