ubit
Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
ubit::UStr Class Reference

Ubit String. More...

#include <ustr.hpp>

Inheritance diagram for ubit::UStr:
ubit::UData ubit::UNode ubit::UObject

Public Member Functions

virtual int getNodeType () const
 returns the XML node type.
 
virtual const UStrgetNodeName () const
 return the XML node name.
 
virtual UStr getNodeValue () const
 return the XML node value.
 
virtual const UStrgetData () const
 
virtual UStrtoStr ()
 dynamic cast: returns this object if it derives from UStr and null otherwise.
 
virtual const UStrtoStr () const
 dynamic cast: returns this object if it derives from UStr and null otherwise.
 
virtual UStronChange (UCall &c)
 adds a callback that is fired when the string is modified. More...
 
 UStr ()
 creates a new empty string; More...
 
 UStr (const char *)
 creates a new string; More...
 
 UStr (const UStr &)
 creates a new string; More...
 
 UStr (const std::string &)
 creates a new string; More...
 
int length () const
 returns the char count of the string.
 
bool empty () const
 returns true if the string is empty.
 
bool isEmpty () const
 returns true if the string is empty.
 
virtual UStrclear ()
 clears the string's content.
 
virtual UStroperator= (const char *)
 
virtual UStroperator= (const UStr &)
 
virtual UStroperator= (const std::string &)
 
UStrsetInt (int)
 
UStrsetLong (long)
 
UStrsetFloat (float)
 
UStrsetDouble (double)
 
int toInt () const
 
long toLong () const
 
float toFloat () const
 
double toDouble () const
 
int scan (const char *format,...) const
 
bool scanValue (float &val, UStr &unit) const
 
bool scanValue (float &val, const char *&unit) const
 
const char * c_str () const
 returns an unmutable C string.
 
char at (int pos) const
 returns the character at this position. More...
 
char operator[] (int pos) const
 same as at(int).
 
char setCharAt (int pos, char c, bool update=true)
 changes the character at offset 'pos'. More...
 
bool operator== (const char *s2) const
 
bool operator== (const UStr &s2) const
 
bool operator== (const std::string &s2) const
 
bool operator!= (const char *s2) const
 
bool operator!= (const UStr &s2) const
 
bool operator!= (const std::string &s2) const
 
virtual bool equals (const char *, bool ignore_case=false) const
 checks if string contents are the same, ignores case if last argument is true.
 
virtual bool equals (const UStr &, bool ignore_case=false) const
 checks if string contents are the same, ignores case if last argument is true.
 
virtual bool equals (const std::string &, bool ignore_case=false) const
 checks if string contents are the same, ignores case if last argument is true.
 
virtual int compare (const char *, bool ignore_case=false) const
 compare strings lexicographically, ignores case if last argument is true.
 
virtual int compare (const UStr &, bool ignore_case=false) const
 compare strings lexicographically, ignores case if last argument is true.
 
virtual int compare (const std::string &, bool ignore_case=false) const
 compare strings lexicographically, ignores case if last argument is true.
 
virtual int compareN (const char *, unsigned int n, bool ignore_case=false) const
 compare the N first chars of strings, ignores case if last argument is true.
 
virtual int compareN (const UStr &, unsigned int n, bool ignore_case=false) const
 compare the N first chars of strings, ignores case if last argument is true.
 
virtual int compareN (const std::string &, unsigned int n, bool ignore_case=false) const
 compare the N first chars of strings, ignores case if last argument is true.
 
virtual int find (char s, int pos=0) const
 search for the first occurrence of 's' starting at 'pos', return -1 if not found.
 
virtual int find (const char *s, int pos=0) const
 search for the first occurrence of 's' starting at 'pos', return -1 if not found.
 
virtual int find (const UStr &s, int pos=0) const
 search for the first occurrence of 's' starting at 'pos', return -1 if not found.
 
virtual int find (const std::string &s, int pos=0) const
 search for the first occurrence of 's' starting at 'pos', return -1 if not found.
 
virtual int rfind (char s) const
 search for the last occurrence of 's', return -1 if not found.
 
UStr substring (int from_pos=0, unsigned int nbchars=npos) const
 returns a substring.
 
std::string toString (int pos=0, unsigned int nbchars=npos) const
 UStr to std::string conversion. More...
 
bool toString (std::string &to, int pos=0, unsigned int nbchars=npos) const
 UStr to std::string conversion. More...
 
UStr toUpper () const
 returns a upercase copy.
 
UStr toLower () const
 returns a lowercase copy.
 
void upper ()
 converts to uppercase characters.
 
void lower ()
 converts to lowercase characters.
 
void capitalize ()
 capitalizes this string.
 
UStr split (int pos, bool delete_char_at_pos=false)
 splits this UStr removes chars after 'pos' in this UStr and returns the remaining chars in another UStr
 
void trim (bool trim_beginning=true, bool trim_end=true)
 removes white spaces, tabs and newlines at the beginning and/or the end of this UStr.
 
int tokenize (std::vector< UStr *> &tokens, const UStr &separators, bool trim_begin=true, bool trim_end=true) const
 parses this UStr and stores the tokens separated by a separator in the vector. More...
 
virtual void append (char)
 
virtual void append (const char *)
 
virtual void append (const char *, unsigned int nbchars)
 
virtual void append (const UStr &)
 
virtual void append (const UStr &, unsigned int nbchars)
 
virtual void append (const std::string &)
 
virtual void append (const std::string &, unsigned int nbchars)
 
virtual void append (int)
 
virtual void append (double)
 
UStroperator &= (const char c)
 
UStroperator &= (const char *s2)
 
UStroperator &= (const UStr &s2)
 
UStroperator &= (const std::string &s2)
 
UStroperator &= (int i)
 
UStroperator &= (double d)
 appends the argument to the end of this UStr. More...
 
virtual bool insert (int to_pos, char)
 
virtual bool insert (int to_pos, const char *from, int from_pos=0, unsigned int from_nbchars=npos)
 
virtual bool insert (int to_pos, const UStr &from, int from_pos=0, unsigned int from_nbchars=npos)
 
virtual bool insert (int to_pos, const std::string &from, int from_pos=0, unsigned int from_nbchars=npos)
 inserts 'from' string at offset 'to_pos' in *this string. More...
 
virtual void remove (int pos, unsigned int nbchars)
 removes 'nbchars' characters from offset 'pos' details: More...
 
virtual void replace (int pos, unsigned int nbchars, const UStr &)
 
virtual void replace (int pos, unsigned int nbchars, const char *)
 
virtual void replace (int pos, unsigned int nbchars, const std::string &)
 replaces 'nbchars' characters from offset 'pos' in this string by the argument string.
 
virtual UStr basename (bool with_suffix=true) const
 returns the base name of a pathname (omitting the directory). More...
 
virtual UStr dirname (bool with_final_slash=true) const
 returns the directory of a pathname (omitting the file name). More...
 
virtual UStr suffix () const
 returns the suffix of a pathname.
 
virtual UStr expand () const
 expands a pathname that contains a ~ into an absolute path.
 
virtual int read (const UStr &pathname)
 reads this file and loads its content into this UStr. More...
 
virtual int write (const UStr &pathname)
 writes the content of this UStr in this file. More...
 
virtual void changed (bool update=true)
 [impl] called when object's content is changed. More...
 
virtual void update ()
 updates grahics.
 
 UStr (const char *, UConst)
 creates a new CONSTANT string; More...
 
virtual void syncVals (char *, int)
 
virtual void addingTo (UChild &, UElem &parent)
 called when this object is added to a parent.
 
virtual void removingFrom (UChild &, UElem &parent)
 NOTE that this function require a specific destructor.
 
virtual bool checkFormat (int pos, int newchar)
 
virtual bool checkFormat (int pos, const char *)
 checks input conformance to format model (if any).
 
virtual void initImpl (const char *, int len)
 
virtual void setImpl (const char *, int len)
 
virtual void setImplNoCopy (char *_s, int _len)
 
virtual bool insertImpl (int to_pos, char newchar, bool upd)
 
virtual bool insertImpl (int to_pos, const char *s2, int from_pos, unsigned int from_len, bool upd)
 
virtual bool replaceImpl (int pos, unsigned int nbchars, const char *s2, bool upd)
 
virtual void getSize (UUpdateContext &, UDimension &) const
 
virtual void getSize (UUpdateContext &, UDimension &, float available_width, int offset, int &str_sublen, int &change_line) const
 
virtual void paint (UGraph &, UUpdateContext &, const URect &) const
 
virtual void paint (UGraph &, UUpdateContext &, const URect &, int offset, int cellen) const
 
- Public Member Functions inherited from ubit::UData
 UABSTRACT_CLASS (UData)
 
 UData (UConst)
 
virtual int getDisplayType () const
 
virtual UDatatoData ()
 dynamic cast: returns this object if it derives from UData and null otherwise.
 
virtual const UDatatoData () const
 dynamic cast: returns this object if it derives from UData and null otherwise.
 
- Public Member Functions inherited from ubit::UNode
virtual int isDisplayable () const
 
virtual UNodetoNode ()
 dynamic cast: returns this object if it derives from UNode and null otherwise.
 
virtual const UNodetoNode () const
 dynamic cast: returns this object if it derives from UNode and null otherwise.
 
virtual bool isChildOf (const UElem &parent, bool indirect=true) const
 returns true if this object a child of 'parent'. More...
 
UParentIter pbegin () const
 returns an iterator to the beginning of the list of direct parents. More...
 
UParentIter pend () const
 returns an iterator to the end of the parent list ( More...
 
virtual UParentsparents () const
 returns the list of direct parents ( More...
 
virtual UElemgetParent (int pos=0) const
 returns the Nth direct parent. More...
 
virtual bool hasSceneGraphParent () const
 
template<class CC >
CC * findParent () const
 < this object has at least one parent in the scene graph. More...
 
virtual int getBoxParents (std::vector< UBox *> &parvect) const
 retrieves direct and indirect parents that are boxes.
 
virtual UBoxgetParent (const UView *) const
 returns the direct or indirect parent that contains this view. More...
 
virtual UViewgetParentView (const UView *) const
 returns the direct or indirect parent's view that contains this view. More...
 
virtual UBoxgetParent (const UInputEvent &) const
 
virtual UViewgetParentView (const UInputEvent &) const
 
virtual void removeFromParents (bool update_parents=true)
 [impl] removes this object from all direct parents. More...
 
virtual bool fire (UEvent &) const
 fires callback functions that match this event.
 
virtual UNoderemoveAllAttrs (bool autodel=true)
 removes all attributes (if any, redefined by subclasses).
 
bool isAutoUpdate () const
 return current update policy: see setAutoUpdate().
 
UNodesetAutoUpdate (bool state=true)
 changes the update policy of this object when its is modified. More...
 
void fireParents (const UCond &c, UNode *n) const
 
void updateAutoParents (const UUpdate &m)
 
virtual void addChangeCall (UCall &)
 
virtual UElemgetSubGroup () const
 
void _addAttr (const UChild &attribute)
 [impl] to be removed @.
 
UChildIter _abegin () const
 [impl] to be removed @.
 
UChildIter _aend () const
 [impl] to be removed @.
 
- Public Member Functions inherited from ubit::UObject
 UObject (const UObject &)
 
UObjectoperator= (const UObject &)
 
virtual const UClassgetClass () const
 instance method that returns the metaclass of this object. More...
 
const UStrgetClassName () const
 returns getClass().getName().
 
virtual void error (const char *function_name, const char *format,...) const
 prints out an error message. More...
 
virtual void warning (const char *function_name, const char *format,...) const
 prints out a warning message: More...
 
bool isDeletable () const
 checks if this object can be destroyed by 'delete'. More...
 
bool isConst () const
 checks whether the content of this object can be modified ( More...
 
UObjectsetConst ()
 specifies that the content of this object cannot be modified. More...
 
bool isAutoUpdate () const
 return current update policy: More...
 
UObjectsetAutoUpdate (bool state=true)
 changes the update policy of this object when its is modified. More...
 
bool isIgnoringChangeCallbacks () const
 return true if change callbacks are fired.
 
UObjectignoreChangeCallbacks (bool state)
 change callbacks are not fired if argument is true.
 
virtual UAttrtoAttr ()
 dynamic cast: returns this object if it derives from UAttr and null otherwise.
 
virtual const UAttrtoAttr () const
 dynamic cast: returns this object if it derives from UAttr and null otherwise.
 
virtual UElemtoElem ()
 dynamic cast: returns this object if it derives from UElem and null otherwise.
 
virtual const UElemtoElem () const
 dynamic cast: returns this object if it derives from UElem and null otherwise.
 
virtual UCalltoCall ()
 dynamic cast: returns this object if it derives from UCall and null otherwise.
 
virtual const UCalltoCall () const
 dynamic cast: returns this object if it derives from UCall and null otherwise.
 
virtual UBoxtoBox ()
 dynamic cast: returns this object if it derives from UBox and null otherwise.
 
virtual const UBoxtoBox () const
 dynamic cast: returns this object if it derives from UBox and null otherwise.
 
virtual UWintoWin ()
 dynamic cast: returns this object if it derives from UWin and null otherwise.
 
virtual const UWintoWin () const
 dynamic cast: returns this object if it derives from UWin and null otherwise.
 
virtual UMenutoMenu ()
 dynamic cast: returns this object if it derives from UMenu and null otherwise.
 
virtual const UMenutoMenu () const
 dynamic cast: returns this object if it derives from UMenu and null otherwise.
 
State getState () const
 returns the current object state.
 
void setState (State s)
 changes the object state.
 
PtrCount getPtrCount () const
 [impl] returns the number of uptr<> smart pointers that are pointing to this object.
 
bool checkConst () const
 [impl] produces an error if this object is logically constant ( More...
 
bool isDestructed () const
 [impl] this object has been destructed (but memory has not been freed yet).
 
bool isDestructing () const
 [impl] this object is being destructed (one of its destructors has been called).
 
void * operator new (size_t)
 < [impl] returns true if there is at least one scene graph parent (redefined by UNode). More...
 
void operator delete (void *)
 delete operator is forbidden on instances that derive from UObject.
 
void addPtr () const
 [Impl] a uptr is added to this object.
 
void removePtr () const
 [Impl] a uptr is removed from this object.
 

Static Public Member Functions

static int readLines (const UStr &filename, std::vector< UStr *> &lines, bool trim_beginning=true, bool trim_end=true)
 reads this file and store its lines as a UStr vector. More...
 
- Static Public Member Functions inherited from ubit::UObject
static const char * getVersion ()
 returns the version of the Ubit package.
 
static const UClassClass ()
 class method that returns the metaclass of this class. More...
 
static UStylecreateStyle ()
 

Static Public Attributes

static const unsigned int npos = static_cast<unsigned int>(-1)
 
- Static Public Attributes inherited from ubit::UObject
static UConst UCONST
 UCONST can be given as an argument to some constructors to make the object constant. More...
 

Friends

std::ostream & operator<< (std::ostream &, const UStr &)
 prints the string on an output stream (e.g. cout << str).
 
std::istream & operator>> (std::istream &, UStr &)
 reads the string from an input stream (eg. cin >> str).
 
UStr operator & (const UStr &s1, const UStr &s2)
 
UStr operator & (const UStr &s1, const char *s2)
 
UStr operator & (const char *s1, const UStr &s2)
 
UStr operator & (const UStr &s1, const std::string &s2)
 
UStr operator & (const std::string &s1, const UStr &s2)
 
UStr operator & (const UStr &, int)
 
UStr operator & (int, const UStr &)
 
UStr operator & (const UStr &, double)
 
UStr operator & (double, const UStr &)
 creates a new string that collates the 2 arguments. More...
 

Additional Inherited Members

- Public Types inherited from ubit::UNode
enum  NodeType {
  ELEMENT_NODE = 1, ATTRIBUTE_NODE = 2, TEXT_NODE = 3, CDATA_SECTION_NODE = 4,
  ENTITY_REFERENCE_NODE = 5, ENTITY_NODE = 6, PROCESSING_INSTRUCTION_NODE = 7, COMMENT_NODE = 8,
  DOCUMENT_NODE = 9, DOCUMENT_TYPE_NODE = 10, DOCUMENT_FRAGMENT_NODE = 11, NOTATION_NODE = 12
}
 redefines Class() and getClass() methods ( More...
 
enum  DisplayType {
  WINLIST =-1, UNDISPLAYBLE =0, ATTRIBUTE, DATA,
  INLINE, BLOCK, BORDER, HARDWIN,
  SOFTWIN
}
 Ubit display types.
 
- Public Types inherited from ubit::UObject
typedef unsigned char State
 
typedef short PtrCount
 
- Protected Member Functions inherited from ubit::UNode
 UNode ()
 constructor, can anly be called by subclasses because UNode is abstract.
 
virtual ~UNode ()
 destructor, note that children are recursively destroyed except if pointed elsewhere ( More...
 
virtual void destructs ()
 unlinks the object from its parents and destroys its children.
 
- Protected Attributes inherited from ubit::UNode
UParents _parents
 
UChildren _attributes
 
- Protected Attributes inherited from ubit::UObject
PtrCount ptr_count
 
Modes omodes
 
State ostate
 

Detailed Description

Ubit String.

Strings can have callback functions that are fired when their value is changed (see an example below). String parents can also be notified when string are modified by using the UOn::strChange condition:

Example:

 
   class Demo {
   public:
      void strCB(UStr* s);   // this method can have an optional UEvent& parameter
      void textCB(UEvent&);  // the UEvent& parameter is optional
      ....
   };
   Demo* d = new Demo();
   UStr* s = new UStr("Hello");
   // d->strCB(s) will be called when the content of 's' is changed
   s->onChange( ucall(d, s, &Demo::strCB) );
   // 's' is displayed in (and edited by) the textfield
   // (as expected, the strCB() callback will be called when 's' is modified)
   UBox* tf = new UTextfield(*s);
   ....

Alternatively:

   // utextfield(...) is a shortcut for *new UTextfield(...)
   UBox& tf = utextfield(s + UOn::strChange / ucall(d, &Demo::textCB));

d->textCB(UEvent&) will be called when the content of 's' is changed. The UEvent& parameter is optional. It makes it possible to retrieve the string as follows:

   void Demo::textCB(UEvent& e) {
     UStr* s = 0;
     if (e.getTarget()) s = e.getTarget()->toStr();
     ...etc...
     // NB: one could also write:
     // if (e.getTarget()) s = dynamic_cast<UStr*>(e.getTarget());
  }

Note strCB() could also have a UEvent& parameter that would make it possible to retrieve the string in the same way.

See also
: UCall for more info on callback methods/functions and their parameters

Constructor & Destructor Documentation

§ UStr() [1/5]

UStr::UStr ( )

creates a new empty string;

See also
also shortcut ustr().

§ UStr() [2/5]

ubit::UStr::UStr ( const char *  )

creates a new string;

See also
also shortcut ustr(const char*).

§ UStr() [3/5]

ubit::UStr::UStr ( const UStr )

creates a new string;

See also
also shortcut ustr(const UStr&).

§ UStr() [4/5]

ubit::UStr::UStr ( const std::string &  )

creates a new string;

See also
also shortcut ustr(const std::string&).

§ UStr() [5/5]

ubit::UStr::UStr ( const char *  ,
UConst   
)

creates a new CONSTANT string;

See also
UObject::UCONST.

Member Function Documentation

§ at()

char UStr::at ( int  pos) const

returns the character at this position.

  • the last char is returned if pos = -1
  • 0 is returned if the position is invalid (no exception thrown)

§ basename()

UStr UStr::basename ( bool  with_suffix = true) const
virtual

returns the base name of a pathname (omitting the directory).

this string is supposed to contain the pathname. the suffix (= the pathname extension) is removed if 'with_suffix' is false.

§ changed()

void UStr::changed ( bool  update = true)
virtual

[impl] called when object's content is changed.

This function updates graphics (if 'update' is true) then fires parents' UOn::strChange callbacks

Reimplemented from ubit::UData.

§ dirname()

UStr UStr::dirname ( bool  with_final_slash = true) const
virtual

returns the directory of a pathname (omitting the file name).

this string is supposed to contain the pathname. the returned dirname is ended by a / if 'with_final_slash' is true.

§ insert()

virtual bool ubit::UStr::insert ( int  to_pos,
const std::string &  from,
int  from_pos = 0,
unsigned int  from_nbchars = npos 
)
virtual

inserts 'from' string at offset 'to_pos' in *this string.

details:

  • pos = 0 specifies the beginning of the string (BEFORE the first char)
  • pos = -1 specifies the end of the string (AFTER the last char)
  • from_nbchars = npos means "to the end of the string"

§ onChange()

UStr & UStr::onChange ( UCall c)
virtual

adds a callback that is fired when the string is modified.

See also
an example in the UStr doc.

Reimplemented from ubit::UData.

§ operator &=()

UStr& ubit::UStr::operator&= ( double  d)
inline

appends the argument to the end of this UStr.

usage: str &= arg; see also: append() and operator &

§ read()

int UStr::read ( const UStr pathname)
virtual

reads this file and loads its content into this UStr.

pathname can start with a ~ returns the reading status (see UFilestat)

§ readLines()

int UStr::readLines ( const UStr filename,
std::vector< UStr *> &  lines,
bool  trim_beginning = true,
bool  trim_end = true 
)
static

reads this file and store its lines as a UStr vector.

filename can start with a ~ returns the reading status (see UFilestat). apply trim() to each line with arguments trim_beginning and trim_end

§ remove()

void UStr::remove ( int  pos,
unsigned int  nbchars 
)
virtual

removes 'nbchars' characters from offset 'pos' details:

  • pos = 0 specifies the first char of the string
  • pos = -1 specifies the last char of the string

§ replaceImpl()

bool UStr::replaceImpl ( int  pos,
unsigned int  nbchars,
const char *  s2,
bool  upd 
)
virtual

!

§ setCharAt()

char UStr::setCharAt ( int  pos,
char  c,
bool  update = true 
)

changes the character at offset 'pos'.

  • pos = -1 refers to the last char of the string
  • returns the character or 0 if the position is invalid
  • does not throw exception (check the returned value)

§ tokenize()

int UStr::tokenize ( std::vector< UStr *> &  tokens,
const UStr separators,
bool  trim_begin = true,
bool  trim_end = true 
) const

parses this UStr and stores the tokens separated by a separator in the vector.

  • return the number of valid tokens, which may differ from tokens.size() as explained below.
  • the separator is one character from 'separators'
  • this function augments the 'tokens' vector and creates its UStr elements. NOTE THAT if the vector is not empty the existing UStr elements are REUSED or unchanged if tokens.size() > the number of valid tokens. (this may be potentially dangerous but CPU efficient for tokenizing a large number of strings)
  • trim() is called for each token with arguments trim_begin and trim_end.
  • the content of this string is unchanged

§ toString() [1/2]

std::string ubit::UStr::toString ( int  pos = 0,
unsigned int  nbchars = npos 
) const

UStr to std::string conversion.

nbchars = npos means "copy to the end of *this string". the second variant appends the substring to the 'to' argument.

§ toString() [2/2]

bool ubit::UStr::toString ( std::string &  to,
int  pos = 0,
unsigned int  nbchars = npos 
) const

UStr to std::string conversion.

nbchars = npos means "copy to the end of *this string". the second variant appends the substring to the 'to' argument.

§ write()

int UStr::write ( const UStr pathname)
virtual

writes the content of this UStr in this file.

pathname can start with a ~ returns the writing status (see UFilestat)

Friends And Related Function Documentation

§ operator &

UStr operator& ( double  ,
const UStr  
)
friend

creates a new string that collates the 2 arguments.

usage: str = arg1 & arg2; NB: the + operator has another meaning: it is used to create widget child lists


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