Re: pseudo-specs for a String class: char *buf

From: Kinkie <gkinkie_at_gmail.com>
Date: Wed, 3 Sep 2008 16:53:39 +0200

On Wed, Sep 3, 2008 at 3:59 PM, Alex Rousskov
<rousskov_at_measurement-factory.com> wrote:
> Hi Kinkie,
>
> I looked at your StringNg wiki page and noticed that your string has
> a "char *buf" pointer into the memory buffer (in addition to the buffer
> pointer itself). I think it would be better to use an offset instead of
> the pointer into internal buffer area:

Yes, I had a discussion with Adrian about the same issue earlier on on IRC.
You make excellent points - as Adrian did :)
Here's my take

> - cleaner design: no peeking into other object's privates
Yes. At the same time the KBuf::Buf class (the "other object") is a
private member class of the KBuf class;
it's actually little more than a glorified struct, and shouldn't be
thought as a first-level citizen on its own.

> - easier to change memory buffer internals
If you mean "change the buffer contents", that's an operation which
should be quite rare.
If you mean "change the code" that should be even rarer.

> - easier to support several buffer types with different internals
I didn't really think of different buffer types. Do you have in mind
any scenario where it would be useful?

> - easier to support re-allocation of buffer memory
> - easier to provide a thread-safe implementation.

On the other hand, char* are significantly more efficient for common
operations, consistently with the design goals..

I'm not saying that I won't change them, I'd just like to be shown
scenarios where it makes a difference.

On an unrelated issue, since it was of interest to some of us, here's
a sample of the caller code for tokenization functions (actual live
code):

    KBuf s1;
    cout << "tokenization: \n";
    {
        s1="The quick brown fox jumped over the lazy dog";
        char *needle=" ";
        KBuf cs1(needle);
        while (!s1.isNull()) {
            cout << "token: " << s1.nextToken(cs1) << endl;
        }
    }
    cout << endl;

And here's the output:
tokenization:
token: The
token: quick
token: brown
token: fox
token: jumped
token: over
token: the
token: lazy
token: dog

-- 
 /kinkie
Received on Wed Sep 03 2008 - 14:53:46 MDT

This archive was generated by hypermail 2.2.0 : Sat Sep 06 2008 - 12:00:04 MDT