# New Ticket Created by [EMAIL PROTECTED] # Please include the string: [perl #19179] # in the subject line of all future correspondence about this issue. # <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=19179 >
This patch (1) Creates a string_max_bytes() function, as described in docs/strings.pod. Code in string.c, prototype in include/parrot/string_funcs.h. It just uses s->encoding->max_bytes. (2) Modifies string_grow() in string.c to use string_max_bytes(), as described in docs/strings.pod. That is all. Apropos the TODO item String subsystem ---------------- grep docs/strings.pod for unimplemented functions and implement them and the docs/strings.pod description =head1 Non-user-visible String Manipulation Functions [...] INTVAL string_compute_strlen(STRING* s) and INTVAL string_max_bytes(STRING *s, INTVAL iv) The first updates the contents of C<< s->strlen >> by contemplating the buffer C<bufstart> and working out how many characters it contains. The second is given a number of characters which we assume are going to be added into the string at some point; it returns the maximum number of bytes that need to be allocated to admit that number of characters. For fixed-width encodings, this is trivial - the "native" encoding, for instance, encodes one byte per character, so C<string_native_max_bytes> simply returns the C<INTVAL> it is passed; C<string_utf8_max_bytes>, on the other hand, returns three times the value that it is passed because a UTF8 character may occupy up to three bytes. To grow a string to a specified size, use void string_grow(STRING *s, INTVAL newsize) The size is given in characters; C<string_max_bytes> is called to turn this into a size in bytes, and then the buffer is grown to accomodate (at least) that many bytes. Mitchell Charity -- attachment 1 ------------------------------------------------------ url: http://rt.perl.org/rt2/attach/45540/35737/fed377/create_string_max_bytes.patch
diff -ur ./include/parrot/string_funcs.h ../parrot_strings/include/parrot/string_funcs.h --- ./include/parrot/string_funcs.h Sun Dec 15 12:59:51 2002 +++ ../parrot_strings/include/parrot/string_funcs.h Mon Dec 16 06:53:11 2002 @@ -18,6 +18,7 @@ /* Declarations of accessors */ INTVAL string_compute_strlen(STRING *); +INTVAL string_max_bytes(STRING*, INTVAL); STRING *string_concat(struct Parrot_Interp *, STRING *, STRING *, UINTVAL); STRING *string_append(struct Parrot_Interp *, STRING *, STRING *, UINTVAL); STRING *string_from_c_string(struct Parrot_Interp *, const void *, UINTVAL); diff -ur ./string.c ../parrot_strings/string.c --- ./string.c Sun Dec 15 12:59:47 2002 +++ ../parrot_strings/string.c Mon Dec 16 07:16:04 2002 @@ -275,7 +275,8 @@ unmake_COW(interpreter,s); /* Don't check buflen, if we are here, we already checked. */ - Parrot_reallocate_string(interpreter, s, s->buflen + addlen); + Parrot_reallocate_string(interpreter, s, + s->buflen + string_max_bytes(s,addlen)); return s; } @@ -488,6 +489,16 @@ return s->strlen; } +/*=for api string string_max_bytes + * returns the number of bytes required to safely contain + * n characters in the string's encoding + */ +INTVAL +string_max_bytes(STRING *s, INTVAL nchars) +{ + return (nchars * s->encoding->max_bytes); +} + /*=for api string string_concat * concatenate two strings */