xref: /haiku/src/add-ons/kernel/file_systems/packagefs/util/String.h (revision d07c930c1eb710289922edd3abf5244c259a86c6)
1*d07c930cSIngo Weinhold /*
2*d07c930cSIngo Weinhold  * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3*d07c930cSIngo Weinhold  * Distributed under the terms of the MIT License.
4*d07c930cSIngo Weinhold  */
5*d07c930cSIngo Weinhold #ifndef STRING_H
6*d07c930cSIngo Weinhold #define STRING_H
7*d07c930cSIngo Weinhold 
8*d07c930cSIngo Weinhold 
9*d07c930cSIngo Weinhold #include "StringPool.h"
10*d07c930cSIngo Weinhold 
11*d07c930cSIngo Weinhold 
12*d07c930cSIngo Weinhold class String {
13*d07c930cSIngo Weinhold public:
14*d07c930cSIngo Weinhold 								String();
15*d07c930cSIngo Weinhold 								String(const String& other);
16*d07c930cSIngo Weinhold 								~String();
17*d07c930cSIngo Weinhold 
18*d07c930cSIngo Weinhold 			bool				SetTo(const char* string);
19*d07c930cSIngo Weinhold 			bool				SetTo(const char* string, size_t maxLength);
20*d07c930cSIngo Weinhold 			bool				SetToExactLength(const char* string,
21*d07c930cSIngo Weinhold 									size_t length);
22*d07c930cSIngo Weinhold 
23*d07c930cSIngo Weinhold 			const char*			Data() const;
24*d07c930cSIngo Weinhold 			uint32				Hash() const;
25*d07c930cSIngo Weinhold 
26*d07c930cSIngo Weinhold 			bool				IsEmpty() const;
27*d07c930cSIngo Weinhold 
28*d07c930cSIngo Weinhold 			String&				operator=(const String& other);
29*d07c930cSIngo Weinhold 
30*d07c930cSIngo Weinhold 			bool				operator==(const String& other) const;
31*d07c930cSIngo Weinhold 			bool				operator!=(const String& other) const;
32*d07c930cSIngo Weinhold 
33*d07c930cSIngo Weinhold 								operator const char*() const;
34*d07c930cSIngo Weinhold 
35*d07c930cSIngo Weinhold private:
36*d07c930cSIngo Weinhold 			StringData*			fData;
37*d07c930cSIngo Weinhold };
38*d07c930cSIngo Weinhold 
39*d07c930cSIngo Weinhold 
40*d07c930cSIngo Weinhold inline
41*d07c930cSIngo Weinhold String::String()
42*d07c930cSIngo Weinhold 	:
43*d07c930cSIngo Weinhold 	fData(StringData::GetEmpty())
44*d07c930cSIngo Weinhold {
45*d07c930cSIngo Weinhold }
46*d07c930cSIngo Weinhold 
47*d07c930cSIngo Weinhold 
48*d07c930cSIngo Weinhold inline
49*d07c930cSIngo Weinhold String::String(const String& other)
50*d07c930cSIngo Weinhold 	:
51*d07c930cSIngo Weinhold 	fData(other.fData)
52*d07c930cSIngo Weinhold {
53*d07c930cSIngo Weinhold 	fData->AcquireReference();
54*d07c930cSIngo Weinhold }
55*d07c930cSIngo Weinhold 
56*d07c930cSIngo Weinhold 
57*d07c930cSIngo Weinhold inline
58*d07c930cSIngo Weinhold String::~String()
59*d07c930cSIngo Weinhold {
60*d07c930cSIngo Weinhold 	fData->ReleaseReference();
61*d07c930cSIngo Weinhold }
62*d07c930cSIngo Weinhold 
63*d07c930cSIngo Weinhold 
64*d07c930cSIngo Weinhold inline bool
65*d07c930cSIngo Weinhold String::SetTo(const char* string)
66*d07c930cSIngo Weinhold {
67*d07c930cSIngo Weinhold 	return SetToExactLength(string, strlen(string));
68*d07c930cSIngo Weinhold }
69*d07c930cSIngo Weinhold 
70*d07c930cSIngo Weinhold 
71*d07c930cSIngo Weinhold inline bool
72*d07c930cSIngo Weinhold String::SetTo(const char* string, size_t maxLength)
73*d07c930cSIngo Weinhold {
74*d07c930cSIngo Weinhold 	return SetToExactLength(string, strnlen(string, maxLength));
75*d07c930cSIngo Weinhold }
76*d07c930cSIngo Weinhold 
77*d07c930cSIngo Weinhold 
78*d07c930cSIngo Weinhold inline const char*
79*d07c930cSIngo Weinhold String::Data() const
80*d07c930cSIngo Weinhold {
81*d07c930cSIngo Weinhold 	return fData->String();
82*d07c930cSIngo Weinhold }
83*d07c930cSIngo Weinhold 
84*d07c930cSIngo Weinhold 
85*d07c930cSIngo Weinhold inline uint32
86*d07c930cSIngo Weinhold String::Hash() const
87*d07c930cSIngo Weinhold {
88*d07c930cSIngo Weinhold 	return fData->Hash();
89*d07c930cSIngo Weinhold }
90*d07c930cSIngo Weinhold 
91*d07c930cSIngo Weinhold 
92*d07c930cSIngo Weinhold inline bool
93*d07c930cSIngo Weinhold String::IsEmpty() const
94*d07c930cSIngo Weinhold {
95*d07c930cSIngo Weinhold 	return fData == StringData::Empty();
96*d07c930cSIngo Weinhold }
97*d07c930cSIngo Weinhold 
98*d07c930cSIngo Weinhold 
99*d07c930cSIngo Weinhold inline bool
100*d07c930cSIngo Weinhold String::operator==(const String& other) const
101*d07c930cSIngo Weinhold {
102*d07c930cSIngo Weinhold 	return fData == other.fData;
103*d07c930cSIngo Weinhold }
104*d07c930cSIngo Weinhold 
105*d07c930cSIngo Weinhold 
106*d07c930cSIngo Weinhold inline bool
107*d07c930cSIngo Weinhold String::operator!=(const String& other) const
108*d07c930cSIngo Weinhold {
109*d07c930cSIngo Weinhold 	return !(*this == other);
110*d07c930cSIngo Weinhold }
111*d07c930cSIngo Weinhold 
112*d07c930cSIngo Weinhold 
113*d07c930cSIngo Weinhold inline
114*d07c930cSIngo Weinhold String::operator const char*() const
115*d07c930cSIngo Weinhold {
116*d07c930cSIngo Weinhold 	return fData->String();
117*d07c930cSIngo Weinhold }
118*d07c930cSIngo Weinhold 
119*d07c930cSIngo Weinhold 
120*d07c930cSIngo Weinhold #endif	// STRING_H
121