WvStreams
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends | List of all members
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]

Public Member Functions

 WvFastString ()
 Create an empty, NULL string. More...
 
void setsize (size_t i)
 
WvFastString offset (size_t i) const
 Returns a copy of string pointed i bytes into this. More...
 
 WvFastString (const WvFastString &s)
 Copy constructor. More...
 
 WvFastString (const WvString &s)
 
 WvFastString (const char *_str)
 Create a string out of a (char *)-style string without copying any memory. More...
 
 WvFastString (const QString &s)
 Create a string out of a Qt library QString. More...
 
 WvFastString (const QCString &s)
 
 WvFastString (const std::string &s)
 Create a string out of a stdc++ string. More...
 
 WvFastString (short i)
 NOTE: make sure that 32 bytes is big enough for your longest int. More...
 
 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. More...
 
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. More...
 
 operator QString () const
 return a Qt library QString containing the contents of this string. More...
 
int num () const
 Return a stdc++ string with the contents of this string. More...
 
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. More...
 

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

◆ WvFastString() [1/7]

WvFastString::WvFastString ( )

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.

◆ WvFastString() [2/7]

WvFastString::WvFastString ( const WvFastString s)

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() [3/7]

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() [4/7]

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() [5/7]

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.

◆ WvFastString() [6/7]

WvFastString::WvFastString ( short  i)

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() [7/7]

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

◆ offset()

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.

◆ do_format()

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().

◆ cstr()

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(), do_format(), UniRetryGen::exists(), UniRegistryGen::get(), UniPStoreGen::get(), UniRetryGen::get(), WvConstStringBuffer::reset(), UniRegistryGen::set(), UniPStoreGen::set(), UniReplicateGen::set(), substr(), and WvLog::uwrite().

◆ operator QString()

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.

◆ num()

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().


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