]>
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> | |
22 | #include "sys/types.h" | |
23 | ||
24 | class io_stream; | |
25 | class String { | |
26 | class _data; | |
27 | public: | |
28 | // Static members first | |
3c054baf RC |
29 | inline String(); |
30 | inline String (String const &); | |
31 | // We're notperformance bottlenecked. | |
32 | String (const char *); | |
33 | inline String & operator = (String const &); | |
34 | ~String(); | |
35 | // Up to the user to delete[] these. | |
36 | char * cstr(); | |
37 | char * cstr() const; // may be less optimal | |
38 | char const * cstr_oneuse() const; // only valid until the next mutator call | |
39 | // pretends to be const !! | |
40 | inline size_t size() const; // number of characters (!= storage size). | |
41 | size_t find (char) const; | |
42 | // operator == and != can be done if/when we have a 'casesensitive' flag to | |
43 | // the constructors | |
44 | // - means this sorts to the left of the parameter | |
45 | int compare (String const &, size_t const = 0) const; | |
46 | static int compare (String const &, String const &, size_t const = 0); | |
47 | int casecompare (String const &, size_t const = 0) const; | |
48 | static int casecompare (String const &, String const &, size_t const = 0); | |
49 | static int casecompare (String const, String const); | |
50 | String &append (String const &); | |
51 | String &operator += (String const &); | |
52 | String operator + (String const &) const; | |
53 | String operator + (char const *) const; | |
54 | bool operator == (String const &) const; | |
55 | bool operator == (char const *) const; | |
56 | ||
57 | private: | |
58 | class _data { | |
59 | public: | |
60 | _data (); | |
61 | _data (size_t); | |
62 | _data (_data const &); | |
63 | ~_data (); | |
64 | unsigned count; //Invariant: all constructors set to 1; | |
65 | // For now, char *, but can be TCHAR, or even UNICODE | |
66 | // when time permits. | |
67 | unsigned char *theString; | |
68 | char *cstr; // cached/oneuse Cstr encoded version | |
69 | size_t length; | |
70 | } *theData; // Invariant, there is always an | |
71 | static String absorb (unsigned char *, size_t); | |
72 | }; | |
73 | ||
74 | String::String() : theData (new _data) {} | |
75 | String::String(String const &aString) : theData (aString.theData) | |
76 | { | |
77 | ++theData->count; | |
78 | } | |
79 | ||
80 | String & | |
81 | String::operator= (String const &aString) | |
82 | { | |
83 | // Don't touch the order | |
84 | ++aString.theData->count; | |
85 | if (--theData->count == 0) | |
86 | delete theData; | |
87 | theData = aString.theData; | |
88 | return *this; | |
89 | } | |
90 | ||
91 | size_t | |
92 | String::size() const | |
93 | { | |
94 | return theData->length; | |
95 | } | |
96 | ||
97 | #endif // CINSTALL_STRING_H |