This is the mail archive of the
kawa@sources.redhat.com
mailing list for the Kawa project.
Re: string-append/shared
- From: Per Bothner <per at bothner dot com>
- To: Chris Dean <Chris dot Dean at sokitomi dot com>
- Cc: Kawa List <kawa at sources dot redhat dot com>
- Date: Fri, 12 Sep 2003 10:50:11 -0700
- Subject: Re: string-append/shared
- References: <22874.1063352038@mercedsystems.com>
Chris Dean wrote:
What's the correct way to write string-append/shared in Kawa?
(The string-append/shared procedure is documented in SRFI-13:
http://srfi.schemers.org/srfi-13/srfi-13.html#string-append/shared
)
Here's one way, but is it acceptable to call the addAll method? or is
that looking inside Kawa internals too much?
I think it acceptable, given that addAll is part of the public
collections-based API of FString. Furthermore, we should probably
make SRFI-13 a standard part of Kawa, like other SRFIs, in which case
there is no problem.
(define (string-append/shared str #!rest rest)
(let loop ((acc :: <string> str)
(rest rest))
(if (null? rest)
acc
(let ((s (car rest)))
(invoke acc 'add-all s)
(loop acc (cdr rest))))))
(I would write that as (invoke acc 'addAll s). After all,
we are invoking a specific Java method, defined in Java,
and it possible the Kawa mangling might be changed.)
However, this isn't terribly efficient, because addAll
uses iterators to loop throgh the argument. I think we
should improve the FString implementation of addAll:
/* BEGIN JAVA2 */
public boolean addAll(int index, Collection c)
{
if (c instanceof FString)
return optimized_implementation;
else
return super.addAll(index, c);
}
/* END JAVA2 */
(We don't need a JAVA1 implementaton as this is just
an optimization.)
However, this isn't really that good if there are more
than two arguments, since we have have to re-allocate and
increase the buffer for each argument. (If you double
the buffer each time, the wasted effort is at least linear.)
A more optimal implementation would be to do an initial
pass over the arguments to calculate the needed length.
We should probably also re-consider the implementation
of string-append in kawa/standard/string_append.java.
It also does more object allocation and copying than it
should. There is no need to use a StringBuffer.
--
--Per Bothner
per@bothner.com http://per.bothner.com/