>
> I would strongly recommend that perl6 mandates that buffers are not nul
> terminated. Anything that needs a nul should arrange for one to be
appended.
> [eg by ensuring that the buffer is writable, extending it by one byte if
> needs be, and writing that nul, or by copying out the contents.]
>

If this recommendation is to be implemented, it probably needs to be
done sooner rather than later, before too much code appears that needs
to be changed.

I would suggest implementing a function to supply a terminated string
if required, something like string_nt() below; this returns a char* rather
than a STRING* because terminated strings should only be used for
passing outside parrot.

s->buflen has been changed to be the true allocated length; the extra
byte could be removed along with the actual null-termination once
nobody relies on it; however, if memory allocation will be happening
on fixed boundaries, it may make sense to round the buffer length up
to match.

--
Peter Gibbs
EmKel Systems

Index: include/parrot/string.h
===================================================================
RCS file: /home/perlcvs/parrot/include/parrot/string.h,v
retrieving revision 1.20
diff -c -r1.20 string.h
*** include/parrot/string.h 7 Jan 2002 22:09:15 -0000 1.20
--- include/parrot/string.h 10 Jan 2002 18:18:23 -0000
***************
*** 67,72 ****
--- 67,74 ----
  STRING*
  string_transcode(struct Parrot_Interp *interpreter, const STRING *src,
                   const ENCODING *encoding, const CHARTYPE *type, STRING
**d);
+ char*
+ string_nt(struct Parrot_Interp *interpreter, const STRING *s);
  void
  string_init(void);
  INTVAL
Index: string.c
===================================================================
RCS file: /home/perlcvs/parrot/string.c,v
retrieving revision 1.38
diff -c -r1.38 string.c
*** string.c  9 Jan 2002 22:35:14 -0000 1.38
--- string.c  10 Jan 2002 18:18:33 -0000
***************
*** 49,55 ****
      s->encoding = encoding;
      s->flags = flags;
      s->type = type;
!     s->buflen = buflen;

      if (buffer) {
          mem_sys_memcopy(s->bufstart, buffer, buflen);
--- 49,55 ----
      s->encoding = encoding;
      s->flags = flags;
      s->type = type;
!     s->buflen = buflen+1;

      if (buffer) {
          mem_sys_memcopy(s->bufstart, buffer, buflen);
***************
*** 201,206 ****
--- 201,221 ----
      }

      return dest;
+ }
+
+ /*=for api string string_nt
+  * return pointer to null-terminated character string
+  * this may be the original string's buffer or a copy thereof
+  */
+ char*
+ string_nt(struct Parrot_Interp *interpreter, const STRING *s) {
+
+     if (s->buflen < s->bufused+1) {
+         s = string_make(interpreter, s->bufstart, s->bufused+1,
+                         s->encoding, s->flags, s->type);
+     }
+     memset((char *)s->bufstart+s->bufused,0,1);
+     return s->bufstart;
  }

  /* vtable despatch functions */



Reply via email to