[haiku] Re: Proposals for the LocaleKit

  • From: Oliver Tappe <zooey@xxxxxxxxxxxxxxx>
  • To: haiku@xxxxxxxxxxxxx
  • Date: Sun, 07 Feb 2010 20:45:47 +0100

On 2010-02-07 at 16:27:53 [+0100], PulkoMandy <pulkomandy@xxxxxxxxx> wrote:
> > But I thought it already does. Didn't Oliver mention this a while back?
> > So
> > it's really easy to rewrite those string composition pieces of code. If
> > you
> > see a piece of code like
> >
> > BString copyStatus;
> > copyStatus << "File ";
> > copyStatus << current;
> > copyStatus << " of ";
> > copyStatus << total;
> > copyStatus << " copied.";
> >
> > Simply replace that with:
> >
> > char buffer[256];
> > snprintf(buffer, sizeof(buffer), TR("File %1d of %2d copied."), current,
> > total);
> >
> > The only drawback here is the fixed buffer size. But can some one say if
> > this works? Will snprintf insert current and total at the correct
> > locations?
> 
> Yes it does. But that is not a nice way of showing our beautiful C++ API.
> HAving a BString::SPrintf() method or something similar would make it
> easier and avoid buffer problems.

How about:

BStringBuilder builder = _TR("File %1s of %2s copied");
BString result = builder << BIntFormatter(current) << BIntFormatter(total);

or

BString result = BStringBuilder(_TR("File %s of %s copied"))
        << BIntFormatter(current) << BIntFormatter(total);

(the latter leaving out the positonal arguments as they match the default 
order).

Please note: using %d or %f will only work locale-wise when the POSIX locale 
stuff has been implemented. Furthmore, the POSIX API is somewhat limited with 
respect to getting more information about the resulting string (where is the 
total number in that string?), so I'd recommend to provide a string-only 
builder class (or whatever we name it) and then use type specific formatting 
objects to convert numbers/dates/times into strings.
Only the converters would live in liblocale, the string builder could be 
built into libbe.

AFAICS, the above can be implemented without need for temporary strings, the 
resulting buffer will be filled all in one go.

Opinions?

cheers,
        Oliver

Other related posts: