9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Russ Cox" <rsc@swtch.com>
To: "Fans of the OS Plan 9 from Bell Labs" <9fans@cse.psu.edu>
Subject: Re: [9fans] s_putc and Runes
Date: Sun,  4 Feb 2007 22:25:15 -0500	[thread overview]
Message-ID: <ee9e417a0702041925l48432744kf91c8813a86c8bf5@mail.gmail.com> (raw)
In-Reply-To: <fc7d8c1bc0687a5996c00867bfbda23b@plan9.jp>

> void
> s_putrune(String *s, Rune r)
> {
>         char rbuf[UTFmax];
>         s_nappend(s, rbuf, runetochar(rbuf, &r));
> }

This is fine.

> or as part of the library (where I'm a bit shakier on the semantics):
>
> void
> s_putrune(String *s, Rune r)
> {
>         char rbuf[UTFmax];
>         int n;
>         if(s->ref > 1)
>                 sysfatal("can't s_putc a shared string");
>         if (s->ptr >= s->end - n=runelen(r))
>                 s_grow(s, n+1);
>         (s->ptr) += runetochar(s->ptr, &r);
> }

Why bother with these details?  The above is fine.  If it ever
became too slow, the right thing to do would be to fix s_nappend,
not code around everything here.  Further, this code is *very*
unlikely to be the bottleneck -- you're calling it once per at most
3 bytes, so the extra little bit you might or might not be saving
this way pales in comparison to the added complexity.

The main reason there is no Rune support in the String
library is that it is not widely used.  It was written for upas
and later extracted, but the interface is a bit clunky.

I confess that most of the time, when I need to write to a
growable string buffer, I just use fmtstrinit, fmtprint, and fmtstrflush.
It's just easier, and you get all the print verbs!

Russ


  reply	other threads:[~2007-02-05  3:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-05  2:41 Joel Salomon
2007-02-05  3:25 ` Russ Cox [this message]
2007-02-05  9:41   ` Bruce Ellis
2007-02-05 12:25   ` erik quanstrom
2007-02-05 19:52   ` Joel Salomon
2007-02-05 21:42     ` Paul Lalonde
2007-02-05 23:57       ` Joel Salomon
2007-02-06  6:23         ` Bruce Ellis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ee9e417a0702041925l48432744kf91c8813a86c8bf5@mail.gmail.com \
    --to=rsc@swtch.com \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).