WvStreams
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends
WvFastString Class Reference

A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to allocate any memory, thus making it faster. More...

#include <wvstring.h>

Inheritance diagram for WvFastString:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 WvFastString ()
 Create an empty, NULL string.
void setsize (size_t i)
WvFastString offset (size_t i) const
 Returns a copy of string pointed i bytes into this.
 WvFastString (const WvFastString &s)
 Copy constructor.
 WvFastString (const WvString &s)
 WvFastString (const char *_str)
 Create a string out of a (char *)-style string _without_ copying any memory.
 WvFastString (const QString &s)
 Create a string out of a Qt library QString.
 WvFastString (const QCString &s)
 WvFastString (const std::string &s)
 Create a string out of a stdc++ string.
 WvFastString (short i)
 NOTE: make sure that 32 bytes is big enough for your longest int.
 WvFastString (unsigned short i)
 WvFastString (int i)
 WvFastString (unsigned int i)
 WvFastString (long i)
 WvFastString (unsigned long i)
 WvFastString (long long i)
 WvFastString (unsigned long long i)
 WvFastString (double i)
 WvFastString (WVSTRING_FORMAT_DECL)
 Now, you're probably thinking to yourself: Boy, does this ever look ridiculous.
size_t len () const
bool operator== (WvStringParm s2) const
bool operator!= (WvStringParm s2) const
bool operator< (WvStringParm s2) const
bool operator== (const char *s2) const
bool operator!= (const char *s2) const
bool operator< (const char *s2) const
bool operator! () const
 the not operator is 'true' if string is empty
const char * operator+ (int i) const
const char * operator- (int i) const
 operator const char * () const
 auto-convert WvString to (const char *), when needed.
const char * cstr () const
 return a (const char *) for this string.
 operator QString () const
 return a Qt library QString containing the contents of this string.
int num () const
 Return a stdc++ string with the contents of this string.
bool isnull () const
 returns true if this string is null
const WvFastStringifnull (WvStringParm defval) const
 returns either this string, or, if isnull(), the given string.

Static Public Member Functions

static void do_format (WvFastString &output, const char *format, const WvFastString *const *a)
 when this is called, we assume output.str == NULL; it will be filled.

Static Public Attributes

static const WvFastString null

Protected Member Functions

void construct (const char *_str)
WvFastStringoperator= (const WvFastString &s2)
void link (WvStringBuf *_buf, const char *_str)
void unlink ()
WvStringBufalloc (size_t size)
void newbuf (size_t size)

Protected Attributes

WvStringBufbuf
char * str

Static Protected Attributes

static WvStringBuf nullbuf = { 0, 1 }

Friends

class WvString

Detailed Description

A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to allocate any memory, thus making it faster.

When we copy to a normal WvString object, _then_ we allocate the memory. If that never happens, we never need to allocate.

DON'T CREATE INSTANCES OF THIS! It's mostly useful for parameter passing, and for that you should use WvStringParm. You can get yourself into _big_ trouble if you have an instance of a WvFastString created from a (char *) object and then you modify the original (char *).

For almost all purposes, use WvString instead. At worst, it's a bit slower.

Definition at line 93 of file wvstring.h.


Constructor & Destructor Documentation

Create an empty, NULL string.

In the past, these were dangerous and could only be filled with operator= or setsize(); nowadays, NULL strings are explicitly allowed, since it's useful to express the difference between a zero-length string and a NULL result.

Definition at line 33 of file wvstring.cc.

Copy constructor.

We can safely copy from a normal WvString like this too, since no special behaviour is required in this direction. (Note that copying from a WvFastString to a WvString _does_ require special care!)

Definition at line 39 of file wvstring.cc.

WvFastString::WvFastString ( const char *  _str)

Create a string out of a (char *)-style string _without_ copying any memory.

It's fast, but we have to trust that the _str won't change for the lifetime of our WvFastString. That's usually safe, if you didn't use WvFastString where you should have used a WvString.

Definition at line 59 of file wvstring.cc.

WvFastString::WvFastString ( const QString &  s)

Create a string out of a Qt library QString.

If you use this, you need to link with libwvqt.so.

Definition at line 12 of file wvqtstring.cc.

WvFastString::WvFastString ( const std::string &  s) [inline]

Create a string out of a stdc++ string.

To use this, #include wvstdstring.h.

Definition at line 15 of file wvstdstring.h.

NOTE: make sure that 32 bytes is big enough for your longest int.

This is true up to at least 64 bits.

Definition at line 178 of file wvstring.cc.

WvFastString::WvFastString ( WVSTRING_FORMAT_DECL  ) [inline]

Now, you're probably thinking to yourself: Boy, does this ever look ridiculous.

And indeed it does. However, it is completely type-safe and when functions are enabled, it reduces automatically to its minimum possible implementation. (ie. all extra comparisons with wv_null go away if the parameter really _is_ wv_null, and that is the default!)

I failed to find a way to optimize out the comparisons for parameters that _are_ provided, however.

There is a small problem, which is that only up to 20 (numbers 0 to 19) additional arguments are allowed. Luckily, no one has ever used that many on one "printf"-type line in the history of the world.

Definition at line 188 of file wvstring.h.

References do_format().


Member Function Documentation

WvFastString WvFastString::offset ( size_t  i) const

Returns a copy of string pointed i bytes into this.

Will not make it point past the trailing null byte.

Definition at line 79 of file wvstring.cc.

void WvFastString::do_format ( WvFastString output,
const char *  format,
const WvFastString *const *  argv 
) [static]

when this is called, we assume output.str == NULL; it will be filled.

Accept a printf-like format specifier (but more limited) and an array of WvStrings, and render them into another WvString.

For example: WvString x[] = {"foo", "blue", 1234}; WvString ret = WvString::do_format("%s%10.2s%-10s", x);

The 'ret' string will be: "foo bl1234 " Note that only 's' is supported, though integers can be rendered automatically into WvStrings. d, f, etc are not allowed!

This function is usually called from some other function which allocates the array automatically.

$ns (n > 0) is also supported for internationalization purposes. e.g. ("%$2s is arg2, and %$1s ia arg1", arg1, arg2)

Definition at line 497 of file wvstring.cc.

References cstr().

Referenced by WvFastString().

WvFastString::operator const char * ( ) const [inline]

auto-convert WvString to (const char *), when needed.

Definition at line 259 of file wvstring.h.

const char* WvFastString::cstr ( ) const [inline]

return a (const char *) for this string.

The typecast operator does this automatically when needed, but sometimes (especially with varargs like in printf()) that isn't convenient enough.

Definition at line 267 of file wvstring.h.

Referenced by WvBackslashEncoder::_encode(), WvCRL::decode(), WvX509::decode(), do_format(), UniRetryGen::exists(), UniRegistryGen::get(), UniPStoreGen::get(), UniRetryGen::get(), operator QString(), WvBufBase< unsigned char >::putstr(), WvConstStringBuffer::reset(), UniRegistryGen::set(), WvStringMask::set(), UniPStoreGen::set(), UniReplicateGen::set(), spacecat(), UniConfGen::str2int(), substr(), WvLog::uwrite(), and WvStream::write().

WvFastString::operator QString ( ) const

return a Qt library QString containing the contents of this string.

You need to link to libwvqt.so if you use this.

Definition at line 42 of file wvqtstring.cc.

References cstr().

int WvFastString::num ( ) const [inline]

Return a stdc++ string with the contents of this string.

To use this, #include wvstdstring.h. used to convert WvString to int, when needed. we no longer provide a typecast, because it causes annoyance.

Definition at line 286 of file wvstring.h.

Referenced by WvDBusServerAuth::authorize(), UniConfDaemonConn::execute(), WvDBusMsg::Iter::get_int(), WvIPRouteList::get_kernel(), and WvDBusMsg::Iter::get_uint().

const WvFastString& WvFastString::ifnull ( WvStringParm  defval) const [inline]

returns either this string, or, if isnull(), the given string.

Definition at line 294 of file wvstring.h.

References isnull().


The documentation for this class was generated from the following files: