ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GStr.h
Revision: 2
Committed: Mon Mar 22 22:03:27 2010 UTC (10 years ago) by gpertea
File size: 8517 byte(s)
Log Message:
added my gclib source files

Line User Rev File contents
1 gpertea 2 //---------------------------------------------------------------------------
2     #ifndef GSTR_H
3     #define GSTR_H
4     //---------------------------------------------------------------------------
5     #include <string.h>
6     #include <stdio.h>
7     #include <stdlib.h>
8     #include "GBase.h"
9    
10     // This class uses reference counting and copy-on-write semantics
11    
12     // All indexes are zero-based. For all functions that accept an index, a
13     // negative index specifies an index from the right of the string. Also,
14     // for all functions that accept a length, a length of -1 specifies the rest
15     // of the string.
16     enum enTokenizeMode {
17     tkFullString,
18     tkCharSet
19     };
20    
21     class GStr {
22     friend GStr operator+(const char* s1, const GStr& s2);
23     friend bool operator==(const char* s1, const GStr& s2);
24     friend bool operator<(const char* s1, const GStr& s2);
25     friend bool operator<=(const char* s1, const GStr& s2);
26     friend bool operator>(const char* s1, const GStr& s2);
27     friend bool operator>=(const char* s1, const GStr& s2);
28     friend bool operator!=(const char* s1, const GStr& s2);
29     friend void swap(GStr& s1, GStr& s2);
30     public:
31     GStr();
32     GStr(const GStr& s);
33     GStr(const char* s);
34     GStr(const int i);
35     GStr(const double f);
36     GStr(char c, int n = 1);
37     ~GStr();
38     operator const char* () const { return my_data->chars;} //inline here
39     char& operator[](int index);
40     char operator[](int index) const;
41     GStr& operator=(const GStr& s);
42     GStr& operator=(const char* s);
43     GStr& operator=(const int i);
44     GStr& operator=(const double f);
45     GStr operator+(const GStr& s) const;
46     GStr operator+(const char* s) const;
47     GStr operator+(const char c) const;
48     GStr operator+(const int i) const;
49     GStr operator+(const double f) const;
50     bool operator==(const GStr& s) const;
51     bool operator==(const char* s) const;
52     bool operator<(const GStr& s) const;
53     bool operator<(const char* s) const;
54     bool operator<=(const GStr& s) const;
55     bool operator<=(const char* s) const;
56     bool operator>(const GStr& s) const;
57     bool operator>(const char* s) const;
58     bool operator>=(const GStr& s) const;
59     bool operator>=(const char* s) const;
60     bool operator!=(const GStr& s) const;
61     bool operator!=(const char* s) const;
62     GStr& operator+=(const GStr& s);
63     GStr& operator+=(const char* s);
64     GStr& operator+=(const char c);
65     GStr& operator+=(const int i);
66     GStr& operator+=(const double f);
67     //interface:
68     public:
69     int length() const;
70     bool is_empty() const;
71     bool is_space() const;
72     GStr substr(int index = 0, int len = -1) const;
73     GStr to(char c); //return the first part up to first occurence of c
74     //or whole string if c not found
75     GStr from(char c); //same as to, but starting from the right side
76     GStr copy() const;
77     GStr& format(const char *fmt,...);
78     GStr& appendfmt(const char *fmt,...);
79     GStr& cut(int index = 0, int len = -1); //delete a specified length
80     GStr& remove(int from, int to) {
81     return cut(from, to-from+1);
82     }
83    
84     //paste a string at the specified position
85     GStr& paste(const GStr& s, int index = 0, int len=-1);
86     GStr& paste(const char* s, int index = 0, int len = -1);
87     GStr& replace(const char* from, const char* to=NULL);
88     GStr& insert(const GStr& s, int index = 0);
89     GStr& insert(const char* s, int index = 0);
90     GStr& append(const char* s);
91     GStr& append(const GStr& s);
92     GStr& upper();
93     GStr& lower();
94     GStr& clear();//make empty
95     //character translation or removal:
96     GStr& tr(const char* from, const char* to=NULL);
97     //number of occurences of a char in the string:
98     int count(char c);
99     void startTokenize(const char* delimiter, enTokenizeMode tokenizemode=tkCharSet);
100     bool nextToken(GStr& token);
101     int asInt(int base=10);
102     double asReal();
103     double asDouble() { return asReal(); }
104     bool asReal(double& r);
105     bool asDouble(double& r) { return asReal(r); }
106     bool asInt(int& r, int base=10);
107     int index(const GStr& s, int start_index = 0) const;
108     int index(const char* s, int start_index = 0) const;
109     int index(char c, int start_index = 0) const;
110     int rindex(char c) const;
111     int rindex(const char* str) const;
112     bool contains(const GStr& s) const;
113     bool contains(const char* s) const;
114     bool contains(char c) const;
115     bool startsWith(const char* s) const;
116     GStr split(const char* delim);
117     GStr split(char c);
118     /* splits "this" in two parts, at the first (leftmost)
119     encounter of delim:
120     1st would stay in "this"
121     (which this way is truncated)
122     2nd will go to the returned string
123     */
124     GStr splitr(const char* delim);
125     GStr splitr(char c);
126     /* splits "this" in two parts, at the last (rightmost)
127     encounter of delim:
128     1st would stay in "this"
129     2nd will be returned
130     */
131    
132     int peelInt() const; //extract an integer, (left to right), from a
133     //mixed alphanumeric string, e.g. 'T24HC1234b'=> 2
134     int peelIntR() const; //same as above, but starts from the right side
135     //e.g. 'T2HC1234b'=> 1234
136     GStr& trim(char c);
137     GStr& trim(const char* c=" \t\n\r"); //trim both ends of characters in given set
138     GStr& trimR(const char* c=" \t\n\r"); //trim only right end
139     GStr& trimR(char c=' ');
140     GStr& chomp(char c='\n') { return trimR(c); }
141     GStr& chomp(const char* cstr); //like trimR, but given string is taken as a whole
142     GStr& trimL(const char* c=" \t\n\r"); //trim only left end
143     GStr& trimL(char c=' ');
144     GStr& padR(int len, char c=' '); //align it in len spaces to the right
145     GStr& padL(int len, char c=' '); //align it in len spaces to the left
146     GStr& padC(int len, char c=' '); //center it
147     size_t read(FILE* stream, const char* delimiter="\n", size_t bufsize=4096);
148     //read next token from stream, using the given string as
149     //a marker where the block should stop
150    
151     static const int max_token_size = 200;
152     static const int max_line_size = 600;
153     const char* chars() const;
154     const char* text() const;
155     protected:
156     char* fTokenDelimiter;
157     int fLastTokenStart;
158     enTokenizeMode fTokenizeMode;
159     void* readbuf; //file read buffer for the read() function
160     size_t readbufsize; //last setting for the readbuf
161     static void invalid_args_error(const char* fname);
162     static void invalid_index_error(const char* fname);
163     struct Data {//structure holding actual
164     //string data and reference count information
165     Data() { ref_count=0; length=0; chars[0] = '\0'; }
166     unsigned int ref_count;
167     int length;
168     char chars[1];
169     };
170     static Data* new_data(int length); //alloc a specified length string's Data
171     static Data* new_data(const char* str); //alloc a copy of a specified string
172     void replace_data(int length);
173     void replace_data(Data* data);
174     void make_unique();
175     char* chrs(); // this is dangerous, length should not be affected
176     static Data null_data; //a null (empty) string Data is available here
177     Data* my_data; //pointer to a Data object holding actual string data
178     };
179    
180     /***************************************************************************/
181    
182     inline int GStr::length() const {
183     return my_data->length;
184     }
185    
186    
187     inline const char *GStr::chars() const {
188     return my_data->chars;
189     }
190    
191     inline char *GStr::chrs() { //protected version, allows modification of the chars
192     return my_data->chars;
193     }
194    
195     inline const char *GStr::text() const {
196     return my_data->chars;
197     }
198    
199    
200     inline bool operator>=(const char *s1, const GStr& s2) {
201     return (strcmp(s1, s2.chars()) >= 0);
202     }
203    
204     inline bool operator!=(const char *s1, const GStr& s2) {
205     return (strcmp(s1, s2.chars()) != 0);
206     }
207    
208     inline void swap(GStr& s1, GStr& s2) {
209     GStr::Data *tmp = s1.my_data; s1.my_data = s2.my_data;
210     s2.my_data = tmp;
211     }
212    
213    
214     #endif