Page Speed Optimization Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | List of all members
net_instaweb::SharedString Class Reference

#include "shared_string.h"

Public Member Functions

 SharedString (const StringPiece &str)
 SharedString (const GoogleString &str)
 SharedString (const char *str)
 SharedString (const SharedString &src)
SharedStringoperator= (const SharedString &src)
StringPiece Value () const
void Assign (StringPiece str)
void Assign (const char *data, int size)
void Append (StringPiece str)
void Append (const char *data, size_t size)
void Extend (int new_size)
void SwapWithString (GoogleString *str)
void DetachAndClear ()
void RemovePrefix (int n)
void RemoveSuffix (int n)
int size () const
 Computes the size, taking into account any removed prefix or suffix.
bool empty () const
const char * data () const
void WriteAt (int dest_offset, const char *source, int count)
void DetachRetainingContent ()
bool unique () const
bool trimmed () const
const GoogleStringStringValue () const
bool SharesStorage (const SharedString &that) const
 Determines whether this and that share the same storage.

Detailed Description

Reference-counted string. This class allows for shared underlying storage with other SharedString instances, but for trimming a SharedString instance's view of it via RemoveSuffix() and RemovePrefix().

Constructor & Destructor Documentation

net_instaweb::SharedString::SharedString ( const GoogleString str)

When constructing with a GoogleString, going through the StringPiece ctor above causes an extra copy compared with string implementations that use copy-on-write. So we make an explicit GoogleString constructor.

net_instaweb::SharedString::SharedString ( const char *  str)

Given the two constructors above, it is ambiguous which one gets called when passed a string-literal, so making an explicit const char* constructor eliminates the ambiguity. This is likely beneficial mostly for tests.

net_instaweb::SharedString::SharedString ( const SharedString src)

Storage-sharing occurs as a result of the copy-constructor and assignment operator.

Member Function Documentation

void net_instaweb::SharedString::Append ( StringPiece  str)

Appends a new string to the underlying storage. Other SharedStrings will not be affected by this mutation.

This function tries to avoid detaching from other SharedStrings, and only needs to do so if this has been truncated.

Unlike Assign, it is invalid to append characters managed by this SharedString. In other words, shared_string.Append(shared_string.Value()) will fail.

Note: Append() is not thread-safe. Concurrent accesses to any SharedStrings with the same storage will fail.

void net_instaweb::SharedString::Assign ( StringPiece  str)

Resets SharedString to be a copy of str, erasing any previous prefix/suffix. Calling this function detaches any connected SharedStrings.

It is valid to assign from a value inside the SharedString. In other words, shared_string.Assign(shared_string.Value().substr(...)) will work.

void net_instaweb::SharedString::DetachAndClear ( )

Clears the contents of the string, and erases any removed prefix or suffix, detaching from any other previously-linked SharedStrings.

void net_instaweb::SharedString::DetachRetainingContent ( )

Disassociates this SharedString with any others that have linked the same storage. Retains the same string value.

void net_instaweb::SharedString::Extend ( int  new_size)

Makes the string representation at least 'new_size' large, without specifying how new bytes should be filled in. Typically this will be followed by a call to WriteAt().

This function does not detach other SharedStrings – the underlying storage will still be shared. Consequently this function does not shrink strings, as that could invalidate trimmed SharedStrings sharing the storage.

If this method is called on a truncated SharedString, then it will be detached prior to extending it.

void net_instaweb::SharedString::RemovePrefix ( int  n)

Removes the first n characters from the string. Other linked SharedStrings remain linked, but are unaffected by this removal because each has its own skip_ and size_.

void net_instaweb::SharedString::RemoveSuffix ( int  n)

Removes the last n characters from the string. Other linked SharedStrings remain linked, but are unaffected by this removal because each has its own skip_ and size_.

const GoogleString* net_instaweb::SharedString::StringValue ( ) const

Returns back a GoogleString* representation for the contained value.

This is makes sense to call only if the string is not trimmed. If RemovePrefix or RemoveSuffix has been called on this SharedString, the returned string may have extra characters in it.

Note: we suggest against using this routine. It is better to consume the data via the StringPiece returned from Value().

This routine is, however, useful to call from tests to determine storage uniqueness.

void net_instaweb::SharedString::SwapWithString ( GoogleString str)

Swaps storage with the the passed-in string, detaching from any other previously-linked SharedStrings.

bool net_instaweb::SharedString::trimmed ( ) const

Determines whether RemovePrefix or RemoveSuffix has every been called on this SharedString. Note that other SharedStrings sharing the same storage as this may be trimmed differently.

bool net_instaweb::SharedString::unique ( ) const

Determines whether this SharedString shares storage from other SharedStrings.

StringPiece net_instaweb::SharedString::Value ( ) const

Returns the value as a StringPiece, taking into account any calls to RemovePrefix, RemoveSuffix, and any string-mutations due to Append or WriteAt on this or any other SharedStrings sharing storage due to the assignment operator or operator=.

void net_instaweb::SharedString::WriteAt ( int  dest_offset,
const char *  source,
int  count 

WriteAt allows mutation of the underlying string data. The string must already be sized as needed via previous Append() or Extend() calls. Mutations done via this method will affect all references to the underlying storage.

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