]>
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 | ||
c93bc6d0 MB |
16 | #ifndef SETUP_STRING___H |
17 | #define SETUP_STRING___H | |
3c054baf | 18 | |
c93bc6d0 | 19 | // A String class to replace all the char * manipulation. |
3c054baf RC |
20 | |
21 | #include <stdarg.h> | |
45e01f23 | 22 | #include <sys/types.h> |
6391823e | 23 | #include <iosfwd> |
b401ef47 | 24 | #include <string> |
3c054baf | 25 | |
3c054baf RC |
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 *); | |
318af134 | 34 | String (std::string const &); |
3c054baf RC |
35 | inline String & operator = (String const &); |
36 | ~String(); | |
d2a3615c | 37 | char const * c_str() const; // only valid until the next mutator call |
3c054baf RC |
38 | // pretends to be const !! |
39 | inline size_t size() const; // number of characters (!= storage size). | |
ad646f43 | 40 | String substr (size_t start = 0, int len = -1) const; |
3c054baf RC |
41 | // operator == and != can be done if/when we have a 'casesensitive' flag to |
42 | // the constructors | |
43 | // - means this sorts to the left of the parameter | |
44 | int compare (String const &, size_t const = 0) const; | |
afa76033 | 45 | private: |
3c054baf | 46 | int casecompare (String const &, size_t const = 0) const; |
afa76033 | 47 | public: |
3c054baf RC |
48 | String &append (String const &); |
49 | String &operator += (String const &); | |
50 | String operator + (String const &) const; | |
51 | String operator + (char const *) const; | |
52 | bool operator == (String const &) const; | |
53 | bool operator == (char const *) const; | |
69711722 RC |
54 | bool operator != (String const &) const; |
55 | bool operator != (char const *) const; | |
405d7186 RC |
56 | struct caseless { bool operator () (String const &s1, String const &s2) const |
57 | { | |
58 | return s1.casecompare (s2) < 0; | |
59 | }}; | |
2b734ec7 | 60 | |
4abaa9f9 | 61 | operator std::string() const { |
d2a3615c | 62 | return std::string( size() ? c_str() : "" ); |
4abaa9f9 | 63 | }; |
3c054baf RC |
64 | |
65 | private: | |
66 | class _data { | |
67 | public: | |
68 | _data (); | |
69 | _data (size_t); | |
70 | _data (_data const &); | |
71 | ~_data (); | |
72 | unsigned count; //Invariant: all constructors set to 1; | |
73 | // For now, char *, but can be TCHAR, or even UNICODE | |
74 | // when time permits. | |
75 | unsigned char *theString; | |
76 | char *cstr; // cached/oneuse Cstr encoded version | |
77 | size_t length; | |
78 | } *theData; // Invariant, there is always an | |
79 | static String absorb (unsigned char *, size_t); | |
80 | }; | |
81 | ||
318af134 RC |
82 | std::ostream & |
83 | operator << (std::ostream &os, String const &theString); | |
6391823e | 84 | |
3c054baf RC |
85 | String::String() : theData (new _data) {} |
86 | String::String(String const &aString) : theData (aString.theData) | |
87 | { | |
88 | ++theData->count; | |
89 | } | |
90 | ||
91 | String & | |
92 | String::operator= (String const &aString) | |
93 | { | |
94 | // Don't touch the order | |
95 | ++aString.theData->count; | |
96 | if (--theData->count == 0) | |
97 | delete theData; | |
98 | theData = aString.theData; | |
99 | return *this; | |
100 | } | |
101 | ||
102 | size_t | |
103 | String::size() const | |
104 | { | |
105 | return theData->length; | |
106 | } | |
107 | ||
cb026227 | 108 | char *new_cstr_char_array (const std::string& s); |
3a8630fd | 109 | |
346627e7 IP |
110 | #define __TOSTRING__(X) #X |
111 | /* Note the layer of indirection here is needed to allow | |
112 | stringification of the expansion of macros, i.e. "#define foo | |
113 | bar", "TOSTRING(foo)", to yield "bar". */ | |
114 | #define TOSTRING(X) __TOSTRING__(X) | |
115 | ||
cb026227 | 116 | std::string format_1000s (int num, char sep = ','); |
0ac305ec | 117 | |
8300be9c MB |
118 | std::string stringify(int num); |
119 | ||
afa76033 MB |
120 | int casecompare (const std::string& a, const std::string& b, size_t limit = 0); |
121 | ||
2b791966 MB |
122 | std::string replace(const std::string& haystack, const std::string& needle, |
123 | const std::string& replacement); | |
124 | ||
cb026227 MB |
125 | class casecompare_lt_op |
126 | { | |
127 | public: | |
128 | bool operator () (const std::string& a, const std::string& b) const | |
129 | { return casecompare(a, b) < 0; } | |
130 | }; | |
131 | ||
132 | inline std::string operator+ (const char *a, const std::string& b) | |
133 | { return std::string(a) + b; } | |
134 | ||
c93bc6d0 | 135 | #endif /* SETUP_STRING___H */ |