1 mike 1.1.2.1 //%2005////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 // of this software and associated documentation files (the "Software"), to
14 // deal in the Software without restriction, including without limitation the
15 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 // sell copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
18 //
19 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
20 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
21 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
22 mike 1.1.2.1 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
23 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 //==============================================================================
29 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef _Pegasus_StringRep_h
33 #define _Pegasus_StringRep_h
34
|
35 mike 1.1.2.6 #include <Pegasus/Common/AtomicInt.h>
36 #include <new>
|
37 mike 1.1.2.1
38 PEGASUS_NAMESPACE_BEGIN
39
40 struct StringRep
41 {
42 StringRep();
43
44 ~StringRep();
45
46 static StringRep* alloc(size_t cap);
47
48 static void free(StringRep* rep);
49
50 static StringRep* create(const Uint16* data, size_t size);
51
52 static StringRep* create(const char* data, size_t size);
53
54 static StringRep* createASCII7(const char* data, size_t size);
55
|
56 mike 1.1.2.3 static StringRep* copyOnWrite(StringRep* rep);
|
57 mike 1.1.2.1
58 static Uint32 length(const Uint16* str);
59
60 static void ref(const StringRep* rep);
61
62 static void unref(const StringRep* rep);
63
|
64 mike 1.1.2.3 static StringRep _emptyRep;
|
65 mike 1.1.2.1
|
66 mike 1.1.2.2 // Number of characters in this string, excluding the null terminator.
|
67 mike 1.1.2.1 size_t size;
|
68 mike 1.1.2.2
69 // Number of characters this representation has room for. This is
70 // greater or equal to size.
|
71 mike 1.1.2.1 size_t cap;
|
72 mike 1.1.2.2
73 // Number of string refering to this StringRep (1, 2, etc).
|
74 mike 1.1.2.6 NewAtomicInt refs;
|
75 mike 1.1.2.2
76 // The first character in the string. Extra space is allocated off the
77 // end of this structure for additional characters.
|
78 mike 1.1.2.1 Uint16 data[1];
79 };
80
81 inline void StringRep::free(StringRep* rep)
82 {
|
83 mike 1.1.2.6 rep->refs.~NewAtomicInt();
|
84 mike 1.1.2.1 ::operator delete(rep);
85 }
86
87 inline StringRep::StringRep() : size(0), cap(0)
88 {
|
89 mike 1.1.2.3 // Only called on _emptyRep. We set the reference count to two to
|
90 mike 1.1.2.2 // keep a String from modifying it (if the reference count were one,
91 // a string would think it was the sole owner of the StringRep object).
|
92 mike 1.1.2.6 new(&refs) NewAtomicInt();
|
93 mike 1.1.2.1 data[0] = 0;
94 }
95
96 inline StringRep::~StringRep()
97 {
|
98 mike 1.1.2.3 // Only called on _emptyRep.
|
99 mike 1.1.2.6 refs.~NewAtomicInt();
|
100 mike 1.1.2.1 }
101
102 inline void StringRep::ref(const StringRep* rep)
103 {
|
104 mike 1.1.2.3 if (rep != &StringRep::_emptyRep)
|
105 mike 1.1.2.6 ((StringRep*)rep)->refs.inc();
|
106 mike 1.1.2.1 }
107
108 inline void StringRep::unref(const StringRep* rep)
109 {
|
110 mike 1.1.2.6 if (rep != &StringRep::_emptyRep && ((StringRep*)rep)->refs.dec_and_test())
|
111 mike 1.1.2.4 StringRep::free((StringRep*)rep);
|
112 mike 1.1.2.1 }
113
|
114 mike 1.1.2.5 PEGASUS_COMMON_LINKAGE void StringThrowOutOfBounds();
|
115 mike 1.1.2.1
|
116 mike 1.1.2.3 PEGASUS_COMMON_LINKAGE void StringAppendCharAux(StringRep*& _rep);
|
117 mike 1.1.2.2
|
118 mike 1.1.2.3 PEGASUS_COMMON_LINKAGE Boolean StringEqualNoCase(
|
119 mike 1.1.2.2 const String& s1, const String& s2);
120
|
121 mike 1.1.2.3 PEGASUS_COMMON_LINKAGE Uint32 StringFindAux(
|
122 mike 1.1.2.2 const StringRep* _rep, const Char16* s, Uint32 n);
123
|
124 mike 1.1.2.3 inline void _checkBounds(size_t index, size_t size)
|
125 mike 1.1.2.1 {
|
126 mike 1.1.2.4 #ifndef PEGASUS_STRING_NO_THROW
|
127 mike 1.1.2.1 if (index > size)
|
128 mike 1.1.2.5 StringThrowOutOfBounds();
|
129 mike 1.1.2.1 #endif
|
130 mike 1.1.2.4 }
|
131 mike 1.1.2.1
132 PEGASUS_NAMESPACE_END
133
134 #endif /* _Pegasus_StringRep_h */
|