# New Ticket Created by  [EMAIL PROTECTED] 
# Please include the string:  [perl #19191]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=19191 >


This patch creates a string_nprintf(), as described in docs/strings.pod.

Specifically,
  (1) string_nprintf's docs/strings.pod entry is updated to reflect
      an extra first argument, interpreter.  All the similar string_*
      functions need it, and we do too.
  (2) string_nprintf() is added to string.c, and its prototype to
      include/parrot/string_funcs.h.
  (3) Test code is added to t/src/sprintf.t.

That is all.

The Parrot_*printf functions made it nicely straightforward. :)

It looks like string_destory(), and the string_utf8_max_bytes() (et al)
functions, are all that remains before
   grep docs/strings.pod for unimplemented functions and implement them
can be removed from the TODO list.  I won't get to it today.

Mitchell



-- attachment  1 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/45565/35760/c784f8/create_string_nprintf.patch

diff -u -r ./docs/strings.pod ../parrot_nprintf/docs/strings.pod
--- ./docs/strings.pod  Tue Aug 27 02:57:55 2002
+++ ../parrot_nprintf/docs/strings.pod  Mon Dec 16 12:10:07 2002
@@ -138,10 +138,9 @@
 
 Otherwise the string will be true.
 
-B<Not implemented>: 
 To format output into a string, use
 
-    STRING* string_nprintf(STRING* dest, INTVAL len, char* format, ...) 
+    STRING* string_nprintf(struct Parrot_Interp *, STRING* dest, INTVAL len, char* 
+format, ...) 
 
 C<dest> may be a null pointer, in which case a new B<native> string will
 be created. If C<len> is zero, the behaviour becomes more C<sprintf>ish
diff -u -r ./include/parrot/string_funcs.h 
../parrot_nprintf/include/parrot/string_funcs.h
--- ./include/parrot/string_funcs.h     Sun Dec 15 12:59:51 2002
+++ ../parrot_nprintf/include/parrot/string_funcs.h     Mon Dec 16 11:31:36 2002
@@ -28,6 +28,8 @@
                       INTVAL, STRING **);
 STRING *string_replace(struct Parrot_Interp *, STRING *, INTVAL, INTVAL,
                        STRING *, STRING **);
+STRING *string_nprintf(struct Parrot_Interp *,
+                       STRING *, INTVAL, const char *, ...);
 INTVAL string_compare(struct Parrot_Interp *, STRING *, STRING *);
 INTVAL string_bool(const STRING *);
 const char *Parrot_string_cstring(const STRING *);
diff -u -r ./string.c ../parrot_nprintf/string.c
--- ./string.c  Sun Dec 15 12:59:47 2002
+++ ../parrot_nprintf/string.c  Mon Dec 16 12:07:30 2002
@@ -903,6 +903,41 @@
     return 1;                   /* it must be true */
 }
 
+/*=for api string string_nprintf
+ * like Parrot_snprintf, but writes to, and returns, a STRING.
+ *
+ * bytelen does _not_ include space for a (non-existent) trailing null.
+ * dest may be a null pointer, in which case a new native string will
+ * be created. If bytelen is zero, the behaviour becomes more
+ * sprintf-ish than snprintf-like.  bytelen is measured in dest's encoding.
+ */
+STRING*
+string_nprintf(struct Parrot_Interp *interpreter,
+               STRING *dest, INTVAL bytelen, const char *format, ...)
+{
+    STRING *output;
+    ENCODING *dest_encoding;
+    va_list args;
+
+    va_start(args, format);
+    output = Parrot_vsprintf_c(interpreter, format, args);
+    va_end(args);
+
+    dest_encoding = (dest != NULL) ? dest->encoding : NULL;
+    string_transcode(interpreter, output, dest_encoding, NULL, &output);
+
+    if(bytelen > 0 && bytelen < string_length(output)) {
+        string_substr(interpreter, output, 0, bytelen, &output);
+    }
+
+    if(dest == NULL) {
+        return output;
+    } else {
+        string_set(interpreter,dest,output);
+        return dest;
+    }
+}
+
 /* A number is such that:
   sign           =  '+' | '-'
   digit          =  "Any code point considered a digit by the chartype"
Only in ../parrot_nprintf/t/op: #string.t#
diff -u -r ./t/src/sprintf.t ../parrot_nprintf/t/src/sprintf.t
--- ./t/src/sprintf.t   Sun Dec 15 14:20:44 2002
+++ ../parrot_nprintf/t/src/sprintf.t   Mon Dec 16 11:57:28 2002
@@ -111,6 +111,24 @@
     printf("%08d %s", (int) ival,
            string_to_cstring(interpreter, S));
  
+    /* test string_nprintf() */
+    {
+       STRING *S2;
+       ival = 1000000;
+       S = string_nprintf(interpreter, NULL, 0, "== %d\n", (int) ival);
+       printf("%d %s", (int) ival, string_to_cstring(interpreter, S));
+       S2 = string_from_c_string(interpreter,"Parrot", 0);
+       S = string_nprintf(interpreter, S2, 0, "== %d\n", (int) ival);
+       printf("%d %s", (int) ival, string_to_cstring(interpreter, S));
+       printf("%s\n", (S == S2) ? "ok" : "different?!?");
+       S = string_nprintf(interpreter, NULL, 42, "== %d\n", (int) ival);
+       printf("%d %s", (int) ival, string_to_cstring(interpreter, S));
+       S = string_nprintf(interpreter, NULL, 6, "== %d\n", (int) ival);
+       printf("100 %s\n", string_to_cstring(interpreter, S));
+       S = string_nprintf(interpreter, S2, 6, "== %d\n", (int) ival);
+       printf("100 %s\n", string_to_cstring(interpreter, S));
+    }
+
     /* Test we've not left junk behind on the stack */
     S = Parrot_sprintf_c(interpreter, "That's all, %s\n", "folks!");
     fputs(string_to_cstring(interpreter, S), stdout);
@@ -150,6 +168,12 @@
 25 == 25
 0xffffffff == 0xffffffff
 -0000001 == -0000001
+1000000 == 1000000
+1000000 == 1000000
+ok
+1000000 == 1000000
+100 == 100
+100 == 100
 That's all, folks!
 OUTPUT
 

Reply via email to