[Groonga-commit] groonga/groonga at ee8bfcf [master] Add grn_text_printf() and grn_text_vprintf()

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Jun 16 16:54:22 JST 2014


Kouhei Sutou	2014-06-16 16:54:22 +0900 (Mon, 16 Jun 2014)

  New Revision: ee8bfcff1a148331f6f6ea0924a687b2623cadaf
  https://github.com/groonga/groonga/commit/ee8bfcff1a148331f6f6ea0924a687b2623cadaf

  Message:
    Add grn_text_printf() and grn_text_vprintf()
    
    TODO: Document it.

  Modified files:
    include/groonga.h
    lib/str.c
    test/unit/core/test-text.c

  Modified: include/groonga.h (+6 -0)
===================================================================
--- include/groonga.h    2014-06-16 16:36:07 +0900 (ac0f2eb)
+++ include/groonga.h    2014-06-16 16:54:22 +0900 (0a1e264)
@@ -22,6 +22,8 @@
 extern "C" {
 #endif
 
+#include <stdarg.h>
+
 #ifndef GRN_API
 #if defined(_WIN32) || defined(_WIN64)
 #define GRN_API __declspec(dllimport)
@@ -1204,6 +1206,10 @@ GRN_API const char *grn_text_urldec(grn_ctx *ctx, grn_obj *buf,
 GRN_API grn_rc grn_text_escape_xml(grn_ctx *ctx, grn_obj *buf,
                                    const char *s, unsigned int len);
 GRN_API grn_rc grn_text_time2rfc1123(grn_ctx *ctx, grn_obj *bulk, int sec);
+GRN_API grn_rc grn_text_printf(grn_ctx *ctx, grn_obj *bulk,
+                               const char *format, ...) GRN_ATTRIBUTE_PRINTF(3);
+GRN_API grn_rc grn_text_vprintf(grn_ctx *ctx, grn_obj *bulk,
+                                const char *format, va_list args);
 
 typedef struct _grn_obj_format grn_obj_format;
 

  Modified: lib/str.c (+47 -0)
===================================================================
--- lib/str.c    2014-06-16 16:36:07 +0900 (7bc0f24)
+++ lib/str.c    2014-06-16 16:54:22 +0900 (0883757)
@@ -17,6 +17,7 @@
 #include "groonga_in.h"
 #include <limits.h>
 #include <stdio.h>
+#include <stdarg.h>
 #include <string.h>
 #include "db.h"
 #include "str.h"
@@ -2463,6 +2464,52 @@ grn_text_time2rfc1123(grn_ctx *ctx, grn_obj *bulk, int sec)
   return GRN_SUCCESS;
 }
 
+grn_rc
+grn_text_printf(grn_ctx *ctx, grn_obj *bulk, const char *format, ...)
+{
+  va_list args;
+
+  va_start(args, format);
+  grn_text_vprintf(ctx, bulk, format, args);
+  va_end(args);
+
+  return GRN_SUCCESS;
+}
+
+grn_rc
+grn_text_vprintf(grn_ctx *ctx, grn_obj *bulk, const char *format, va_list args)
+{
+  int rest_size, written_size;
+
+  {
+    va_list copied_args;
+
+    rest_size = GRN_BULK_REST(bulk);
+    va_copy(copied_args, args);
+    written_size = vsnprintf(GRN_BULK_CURR(bulk), rest_size,
+                             format, copied_args);
+    va_end(copied_args);
+  }
+
+  if (written_size >= rest_size) {
+    grn_rc rc;
+    int required_size = written_size + 1; /* "+ 1" for terminate '\0'. */
+
+    rc = grn_bulk_reserve(ctx, bulk, GRN_BULK_VSIZE(bulk) + required_size);
+    if (rc) {
+      return rc;
+    }
+    written_size = vsnprintf(GRN_BULK_CURR(bulk), required_size,
+                             format, args);
+  }
+
+  if (written_size < 0) {
+    return GRN_INVALID_ARGUMENT;
+  }
+
+  GRN_BULK_INCR_LEN(bulk, written_size);
+  return GRN_SUCCESS;
+}
 
 grn_rc
 grn_bulk_fin(grn_ctx *ctx, grn_obj *buf)

  Modified: test/unit/core/test-text.c (+23 -0)
===================================================================
--- test/unit/core/test-text.c    2014-06-16 16:36:07 +0900 (6654bc0)
+++ test/unit/core/test-text.c    2014-06-16 16:54:22 +0900 (f2b8335)
@@ -103,3 +103,26 @@ test_urldec(void)
                           GRN_TEXT_VALUE(&buffer),
                           GRN_TEXT_LEN(&buffer));
 }
+
+void
+test_printf_inplace_size(void)
+{
+  const char *inplace_size_string = "inplace size is <= 24  ";
+  grn_text_printf(&context, &buffer, "%s", inplace_size_string);
+  cut_assert_equal_memory(inplace_size_string,
+                          strlen(inplace_size_string),
+                          GRN_TEXT_VALUE(&buffer),
+                          GRN_BULK_VSIZE(&buffer));
+}
+
+void
+test_printf_outplace_size(void)
+{
+  const char *outplace_size_string = "outplace size is > 24   ";
+  grn_text_printf(&context, &buffer, "%s", outplace_size_string);
+  cut_assert_equal_memory(outplace_size_string,
+                          strlen(outplace_size_string),
+                          GRN_TEXT_VALUE(&buffer),
+                          GRN_BULK_VSIZE(&buffer));
+}
+
-------------- next part --------------
HTML����������������������������...
Télécharger 



More information about the Groonga-commit mailing list
Back to archive index