A var type mimics perl's variables: it is simultaneously a string and a floating point number, and can be accessed or modified as a string or a floating point number: the operators & and &= modify it as a string, the usual arithmetic operators act on them as a floating-point number.
var v = 1.23; v += 1; // increase by 1 v &= 3; // append the string representation of 3 // the value of 'v' is now: 2.233 var x = "hello" & v; // the value of 'x' is: hello2.233 double d = v.dbl(); // explicit conversions string s = v.str(); int i = v.integer(); x(1,3) = "--"; // substrings are accessed with the () operator // the value of 'x' is now: "h--o2.233"
++,-- | Increment (decrement) the value by 1. These operators keep zero-paddedness, i.e. if a var is an integer (contains only digits after an optional leading -), and has a value '001', and one calls the ++ operator on this, it will be '002' |
+=,-= |
Increment (decrement) by a given value, which can be a string
(it will be converted to a numerical value in this case), a
var, a double or an int. In the last case (and if the var is an
integer also) these operations keep zero-paddedness. That is, if '08'
is incremented by 1, it will be '09'. Keep in mind, that += and -=
called sequentially with the same value do not necessarily have zero
effect: if '09' is incremented by 2 and then decremented by 2, it will
be '9', instead of '09'
var a = "001"; a += 12; // a has the value '013' a += 1000; // a has the value '1013' now |
*=,/= | Multiply or divide by a given value, which can be a string, var, double or int. |
*,/,+,- | These operators can be called with a var on the left or right (or both) side. The other operand can be a var, string, double or int. The result is a var |
var a = "apple"; if(a.str() < "orange") { ... }
The behaviour of the ==, != operators depends on their arguments: if a var is compared to another var, a string or a char*, it carries out string-comparison. Otherwise a floating point comparison is carried out.
var a = "001"; var b = "1"; if(a == b) cerr<<"This will not be printed"<<endl; if(a.dbl() == b.dbl()) cerr<<"This will be printed"<<endl;(This is not necessarily a good approach, it might change in the future. To be sure what is happening, you can always convert it to string or double explicitely, using the dbl() or str() member functions) The & operator (if at least one of its arguments is a var) concatenates its two arguments, returning a var. Similarly, the &= operator appends its right-hand argument to its right-hand argument (this must be a var):
var a = "hello "; a &= "world";
Vars automatically convert between string and numerical representations. When they are set to a numerical value, the string representation is automatically updated using a sprintf(...) conversion. To control the format of this conversion, one can use the
format(const var &format);member function. Any later assignment from a numerical value will use this format. One can find-and-replace substrings in a var, using its member function. It modifies the original var:
var a = "Hello boy"; a.replace("o","a"); // replace all occurences of "o" to "a"; Result: Hella bay a.replace1("l","d"); // replace first occurence of "l" to "d"; Result: Hedla bayThe following nom-member functions replace all occurences of from to to in the string s, and return the modified string (the original is left intact):
var replace(const var &from, const var &to, const var &in); var replace1(const var &from, const var &to, const var &in); var replace_re(const var &pattern, const var &replacement, const var &in );A portion of a var can be accessed via the ( ) operator (parenthesis operator). This operator accepts 2 arguments, the index of the first and the last character of the substring. For example if s = "apple", then s(1,3) evaluates to "ppl". The returned substring behaves exactly like a normal var, but it is more: it contains a reference to the original var, of which it is a substring. Any modifications to this substring will effect the original var:
var s = "apple"; s(1,3) = "sk m";Now 's' has the value "ask me"
There are two member functions to match regular expressions against this variable:
bool var::matches(const var ®exp);
int var::matches(const var ®exp, array &results);
The following member-function searches a match of the regular expression 'pattern', and if this is found, replaces it to the text 'replacement'.
var::replace(const var &pattern, const var &replacement);This replacement string may contain the strings \0, \1, \2, etc. \0 refers to the whole matching expression, \1, \2, etc refer to the 1st, 2nd etc sub-expressions. Do not forget that you need to escape the backslash, for example: s.replace_re("_([0-9])_","\\1");
... more coming soon
Source files: var.h var.cc