![]()
|
Top: Basic types: string: Manipulation #include <ptypes.h> // get/set length and misc. int length(const string& s); int setlength(string&, int); char* unique(string&); string dup(const string& s); void clear(string& s); bool isempty(const string& s); // concatenate void concat(string& s, const char* sc, int catlen); // copy (get substring by position and length) string copy(const string& s, int from, int cnt); // insert string or character void ins(const char* s1, string& s, int at); void ins(char s1, string& s, int at); void ins(const string& s1, string& s, int at); void ins(const char* s1, int s1len, string& s, int at); // delete substring void del(string& s, int at, int cnt); // find substring or character int pos(const char* s1, const string& s); int pos(char s1, const string& s); int pos(const string& s1, const string& s); int rpos(char s1, const string& s); // compare substring bool contains(const char* s1, const string& s, int at); bool contains(char s1, const string& s, int at); bool contains(const string& s1, const string& s, int at); bool contains(const char* s1, int s1len, const string& s, int at); // conversion string itobase(large value, int base, int width = 0, char pad = ' '); string itostring(<ordinal> v); large stringtoi(const string& s); string lowercase(const string& s); int length(const string& s) returns the actual length of the string, not counting the terminating null-symbol. setlength(string&, int) changes the actual length of the string. The content of the original string is preserved, however the content of extra characters added during reallocation is undefined. char* unique(string&) makes the string buffer unique, i.e. a new buffer is allocated and data is copied if necessary, so that the reference count after calling this function is guaranteed to be 1. All string manipulation functions call unique() whenever a modification is made on the string buffer. You may need to call this function explicitly to obtain a character pointer to the buffer; in all other cases reference counting mechanism works transparently. string dup(const string& s) creates a unique copy of the string s. bool isempty(string&) returns true if the given string is empty. Using this function is preferable to comparing the string with empty string literal "". clear(string&) makes the given string empty. Using this function is preferable to assigning an empty string literal "". concat(string& s, const char* sc, int catlen) adds the given buffer sc of length catlen to the string object s. Use operators + and += instead to concatenate characters, null-terminated strings and string objects. ins(..., string& s, int at) inserts a character, a null-terminated string, a string object or a buffer with specified length into string object s at the given position at. If the position is out of bounds, ins() does nothing. del(string& s, int at, int cnt) deletes cnt characters starting from position at of the string s. int pos(..., const string& s) returns the position of the first occurrence of a character, a null-terminated string or a string object (first parameter) in the source string s, or returns -1 if the substring is not found. Function rpos() performs reverse-search. bool contains(..., const string& s, int at) returns true if the given character, null-terminated string or string object (first parameter) equals the substring of s at the given position at. string itobase(large value, int base, int width = 0, char pad = ' ') converts an integer value to a string with the numeration base specified by base, which can be in the range 2 - 64. Additionally, to right-justify the resulting string you can specify width and pad parameters. string itostring(<ordinal> v) converts the given ordinal value v to a string. Various overloaded versions of this function accept ordinal values of different sizes. large stringtoi(const string& s) converts a string to a 64-bit integer. This function accepts only positive numbers and 0. It returns -1 if the string does not represent a valid positive number. Stringtoi() replaces atoll(), which is not present on some systems (e.g. on BSD clone and Darwin). Note an important difference between these two functions: on error atoll() returns 0, whereas PTypes' stringtoi() returns -1. string lowercase(const string& s) converts all characters of the given string s to lower case. The current version of the library "understands" only lower ASCII characters; all other characters remain unchanged. See also: Constructors/destructors, Operators, Typecasts PTypes home |