lidavidm commented on code in PR #587:
URL: https://github.com/apache/arrow-adbc/pull/587#discussion_r1177418073


##########
c/driver/common/utils.c:
##########
@@ -28,7 +28,7 @@
 static size_t kErrorBufferSize = 256;
 static char kErrorPrefix[] = "[SQLite] ";
 
-void ReleaseError(struct AdbcError* error) {
+static void ReleaseError(struct AdbcError* error) {

Review Comment:
   Hmm, were these being multiply defined somehow?



##########
c/driver/common/utils.c:
##########
@@ -124,19 +124,27 @@ void StringBuilderInit(struct StringBuilder* builder, 
size_t initial_size) {
   builder->size = 0;
   builder->capacity = initial_size;
 }
-void StringBuilderAppend(struct StringBuilder* builder, const char* value) {
-  size_t length = strlen(value);
-  size_t new_size = builder->size + length;
-  if (new_size > builder->capacity) {
-    size_t new_capacity = builder->size + length - builder->capacity;
-    if (builder->size == 0) new_capacity++;
-
-    builder->buffer = realloc(builder->buffer, new_capacity);
-    builder->capacity = new_capacity;
+void StringBuilderAppend(struct StringBuilder* builder, const char* fmt, ...) {
+  va_list argptr;
+  int bytes_available = builder->capacity - builder->size;
+
+  va_start(argptr, fmt);
+  int n = vsnprintf(builder->buffer + builder->size, bytes_available, fmt, 
argptr);
+  va_end(argptr);
+
+  if (n < 0) {                        // TODO: handle error

Review Comment:
   Should we handle the TODO here?



##########
c/driver/common/utils.c:
##########
@@ -124,19 +124,27 @@ void StringBuilderInit(struct StringBuilder* builder, 
size_t initial_size) {
   builder->size = 0;
   builder->capacity = initial_size;
 }
-void StringBuilderAppend(struct StringBuilder* builder, const char* value) {
-  size_t length = strlen(value);
-  size_t new_size = builder->size + length;
-  if (new_size > builder->capacity) {
-    size_t new_capacity = builder->size + length - builder->capacity;
-    if (builder->size == 0) new_capacity++;
-
-    builder->buffer = realloc(builder->buffer, new_capacity);
-    builder->capacity = new_capacity;
+void StringBuilderAppend(struct StringBuilder* builder, const char* fmt, ...) {
+  va_list argptr;
+  int bytes_available = builder->capacity - builder->size;
+
+  va_start(argptr, fmt);
+  int n = vsnprintf(builder->buffer + builder->size, bytes_available, fmt, 
argptr);
+  va_end(argptr);
+
+  if (n < 0) {                        // TODO: handle error
+  } else if (n >= bytes_available) {  // output was truncated
+    int bytes_needed = n - bytes_available + 1;
+    builder->buffer = (char*)realloc(builder->buffer, builder->capacity + 
bytes_needed);
+    if (builder->buffer == NULL) { /* TODO: handle error */
+    }
+    builder->capacity += bytes_needed;
+
+    va_start(argptr, fmt);
+    vsnprintf(builder->buffer + builder->size, n + 1, fmt, argptr);

Review Comment:
   possibly, assert on the return value?



##########
c/driver/common/utils.h:
##########
@@ -44,7 +48,7 @@ struct StringBuilder {
   size_t capacity;
 };
 void StringBuilderInit(struct StringBuilder* builder, size_t initial_size);
-void StringBuilderAppend(struct StringBuilder* builder, const char* value);
+void StringBuilderAppend(struct StringBuilder* builder, const char* fmt, ...);

Review Comment:
   IIRC, there's a compiler-specific attribute you can apply to make the 
compiler apply warnings like it does for printf & friends



##########
c/driver/common/utils_test.cc:
##########
@@ -0,0 +1,62 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <gtest/gtest.h>
+
+#include "utils.h"
+
+TEST(TestStringBuilder, TestBasic) {
+  struct StringBuilder str;
+  StringBuilderInit(&str, /*initial_size=*/64);
+  EXPECT_EQ(str.capacity, 64);
+  StringBuilderAppend(&str, "%s", "BASIC TEST");
+  EXPECT_EQ(str.size, 10);
+  EXPECT_STREQ(str.buffer, "BASIC TEST");
+
+  StringBuilderReset(&str);
+}
+
+TEST(TestStringBuilder, TestBoundary) {
+  struct StringBuilder str;
+  StringBuilderInit(&str, /*initial_size=*/10);
+  EXPECT_EQ(str.capacity, 10);
+  StringBuilderAppend(&str, "%s", "BASIC TEST");
+  // should resize to include \n

Review Comment:
   is that the \n or the \0?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to