]>
Commit | Line | Data |
---|---|---|
3c054baf RC |
1 | /* |
2 | * Copyright (c) 2002, Robert Collins. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by Robert Collins. | |
13 | * | |
14 | */ | |
15 | ||
16 | // A String class to replace all the char * manipulation. | |
17 | ||
18 | #ifndef CINSTALL_STRING_H | |
19 | #define CINSTALL_STRING_H | |
20 | ||
21 | #include <stdarg.h> | |
45e01f23 | 22 | #include <sys/types.h> |
6391823e | 23 | #include <iosfwd> |
3c054baf RC |
24 | |
25 | class io_stream; | |
26 | class String { | |
27 | class _data; | |
28 | public: | |
29 | // Static members first | |
3c054baf RC |
30 | inline String(); |
31 | inline String (String const &); | |
32 | // We're notperformance bottlenecked. | |
33 | String (const char *); | |
34 | inline String & operator = (String const &); | |
35 | ~String(); | |
36 | // Up to the user to delete[] these. | |
37 | char * cstr(); | |
38 | char * cstr() const; // may be less optimal | |
39 | char const * cstr_oneuse() const; // only valid until the next mutator call | |
40 | // pretends to be const !! | |
41 | inline size_t size() const; // number of characters (!= storage size). | |
42 | size_t find (char) const; | |
43 | // operator == and != can be done if/when we have a 'casesensitive' flag to | |
44 | // the constructors | |
45 | // - means this sorts to the left of the parameter | |
46 | int compare (String const &, size_t const = 0) const; | |
47 | static int compare (String const &, String const &, size_t const = 0); | |
48 | int casecompare (String const &, size_t const = 0) const; | |
49 | static int casecompare (String const &, String const &, size_t const = 0); | |
50 | static int casecompare (String const, String const); | |
51 | String &append (String const &); | |
52 | String &operator += (String const &); | |
53 | String operator + (String const &) const; | |
54 | String operator + (char const *) const; | |
55 | bool operator == (String const &) const; | |
56 | bool operator == (char const *) const; | |
57 | ||
58 | private: | |
59 | class _data { | |
60 | public: | |
61 | _data (); | |
62 | _data (size_t); | |
63 | _data (_data const &); | |
64 | ~_data (); | |
65 | unsigned count; //Invariant: all constructors set to 1; | |
66 | // For now, char *, but can be TCHAR, or even UNICODE | |
67 | // when time permits. | |
68 | unsigned char *theString; | |
69 | char *cstr; // cached/oneuse Cstr encoded version | |
70 | size_t length; | |
71 | } *theData; // Invariant, there is always an | |
72 | static String absorb (unsigned char *, size_t); | |
73 | }; | |
74 | ||
6391823e RC |
75 | ostream & |
76 | operator << (ostream &os, String const &theString); | |
77 | ||
3c054baf RC |
78 | String::String() : theData (new _data) {} |
79 | String::String(String const &aString) : theData (aString.theData) | |
80 | { | |
81 | ++theData->count; | |
82 | } | |
83 | ||
84 | String & | |
85 | String::operator= (String const &aString) | |
86 | { | |
87 | // Don't touch the order | |
88 | ++aString.theData->count; | |
89 | if (--theData->count == 0) | |
90 | delete theData; | |
91 | theData = aString.theData; | |
92 | return *this; | |
93 | } | |
94 | ||
95 | size_t | |
96 | String::size() const | |
97 | { | |
98 | return theData->length; | |
99 | } | |
100 | ||
101 | #endif // CINSTALL_STRING_H |